Lines 24-44
Link Here
|
24 |
val MultiInterval = Value(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) |
24 |
val MultiInterval = Value(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) |
25 |
} |
25 |
} |
26 |
|
26 |
|
27 |
def wrap[A](c: JList) = new ListView[A] { |
27 |
def wrap[A](c: JList[A]) = new ListView[A] { |
28 |
override lazy val peer = c |
28 |
override lazy val peer = c |
29 |
} |
29 |
} |
30 |
|
30 |
|
31 |
object Renderer { |
31 |
object Renderer { |
32 |
def wrap[A](r: ListCellRenderer): Renderer[A] = new Wrapped[A](r) |
32 |
def wrap[A](r: ListCellRenderer[A]): Renderer[A] = new Wrapped[A](r) |
33 |
|
33 |
|
34 |
/** |
34 |
/** |
35 |
* Wrapper for <code>javax.swing.ListCellRenderer<code>s |
35 |
* Wrapper for <code>javax.swing.ListCellRenderer<code>s |
36 |
*/ |
36 |
*/ |
37 |
class Wrapped[A](override val peer: ListCellRenderer) extends Renderer[A] { |
37 |
class Wrapped[A](override val peer: ListCellRenderer[A]) extends Renderer[A] { |
38 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean, a: A, index: Int) = { |
38 |
def componentFor(list: ListView[_ <: A], isSelected: Boolean, focused: Boolean, a: A, index: Int) = { |
39 |
Component.wrap(peer.getListCellRendererComponent(list.peer, a, index, isSelected, focused).asInstanceOf[JComponent]) |
39 |
Component.wrap(peer.getListCellRendererComponent(list.peer, a, index, isSelected, focused).asInstanceOf[JComponent]) |
|
|
40 |
} |
40 |
} |
41 |
} |
41 |
} |
|
|
42 |
|
42 |
|
43 |
/** |
43 |
/** |
44 |
* Returns a renderer for items of type <code>A</code>. The given function |
44 |
* Returns a renderer for items of type <code>A</code>. The given function |
Lines 55-62
Link Here
|
55 |
* </code> |
55 |
* </code> |
56 |
*/ |
56 |
*/ |
57 |
def apply[A,B](f: A => B)(implicit renderer: Renderer[B]): Renderer[A] = new Renderer[A] { |
57 |
def apply[A,B](f: A => B)(implicit renderer: Renderer[B]): Renderer[A] = new Renderer[A] { |
58 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component = |
58 |
def componentFor(list: ListView[_ <: A], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component = |
59 |
renderer.componentFor(list, isSelected, focused, f(a), index) |
59 |
renderer.componentFor(list.asInstanceOf[ListView[_ <: B]], isSelected, focused, f(a), index) |
60 |
} |
60 |
} |
61 |
} |
61 |
} |
62 |
|
62 |
|
Lines 69-79
Link Here
|
69 |
* @see javax.swing.ListCellRenderer |
69 |
* @see javax.swing.ListCellRenderer |
70 |
*/ |
70 |
*/ |
71 |
abstract class Renderer[-A] { |
71 |
abstract class Renderer[-A] { |
72 |
def peer: ListCellRenderer = new ListCellRenderer { |
72 |
def peer: ListCellRenderer[_ >: A] = new ListCellRenderer[A] { |
73 |
def getListCellRendererComponent(list: JList, a: Any, index: Int, isSelected: Boolean, focused: Boolean) = |
73 |
def getListCellRendererComponent(list: JList[_ <: A], a: A, index: Int, isSelected: Boolean, focused: Boolean) = |
74 |
componentFor(ListView.wrap[A](list), isSelected, focused, a.asInstanceOf[A], index).peer |
74 |
componentFor(ListView.wrap[A](list.asInstanceOf[JList[A]]), isSelected, focused, a, index).peer |
75 |
} |
75 |
} |
76 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component |
76 |
def componentFor(list: ListView[_ <: A], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
/** |
79 |
/** |
Lines 110-116
Link Here
|
110 |
/** |
110 |
/** |
111 |
* Configures the component before returning it. |
111 |
* Configures the component before returning it. |
112 |
*/ |
112 |
*/ |
113 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component = { |
113 |
def componentFor(list: ListView[_ <: A], isSelected: Boolean, focused: Boolean, a: A, index: Int): Component = { |
114 |
preConfigure(list, isSelected, focused, a, index) |
114 |
preConfigure(list, isSelected, focused, a, index) |
115 |
configure(list, isSelected, focused, a, index) |
115 |
configure(list, isSelected, focused, a, index) |
116 |
component |
116 |
component |
Lines 123-132
Link Here
|
123 |
* that renders the string returned from an item's <code>toString</code>. |
123 |
* that renders the string returned from an item's <code>toString</code>. |
124 |
*/ |
124 |
*/ |
125 |
implicit object GenericRenderer extends Renderer[Any] { |
125 |
implicit object GenericRenderer extends Renderer[Any] { |
126 |
override lazy val peer: ListCellRenderer = new DefaultListCellRenderer |
126 |
override lazy val peer: ListCellRenderer[Any] = (new DefaultListCellRenderer).asInstanceOf[ListCellRenderer[Any]] |
127 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean, a: Any, index: Int): Component = { |
127 |
def componentFor(list: ListView[_ <: Any], isSelected: Boolean, focused: Boolean, a: Any, index: Int): Component = { |
128 |
val c = peer.getListCellRendererComponent(list.peer, a, index, isSelected, focused).asInstanceOf[JComponent] |
128 |
val c = peer.getListCellRendererComponent(list.peer, a, index, isSelected, focused) |
129 |
Component.wrap(c) |
129 |
Component.wrap(c.asInstanceOf[JComponent]) |
130 |
} |
130 |
} |
131 |
} |
131 |
} |
132 |
} |
132 |
} |
Lines 142-175
Link Here
|
142 |
*/ |
142 |
*/ |
143 |
class ListView[A] extends Component { |
143 |
class ListView[A] extends Component { |
144 |
import ListView._ |
144 |
import ListView._ |
145 |
override lazy val peer: JList = new JList with SuperMixin |
145 |
override lazy val peer: JList[A] = new JList[A] with SuperMixin |
146 |
|
146 |
|
147 |
def this(items: Seq[A]) = { |
147 |
def this(items: Seq[A]) = { |
148 |
this() |
148 |
this() |
149 |
listData = items |
149 |
listData = items |
150 |
} |
150 |
} |
151 |
|
151 |
|
152 |
protected class ModelWrapper(val items: Seq[A]) extends AbstractListModel { |
152 |
protected class ModelWrapper[A](val items: Seq[A]) extends AbstractListModel[A] { |
153 |
def getElementAt(n: Int) = items(n).asInstanceOf[AnyRef] |
153 |
def getElementAt(n: Int) = items(n) |
154 |
def getSize = items.size |
154 |
def getSize = items.size |
155 |
} |
155 |
} |
156 |
|
156 |
|
157 |
def listData: Seq[A] = peer.getModel match { |
157 |
def listData: Seq[A] = peer.getModel match { |
158 |
case model: ModelWrapper => model.items |
158 |
case model: ModelWrapper[a] => model.items |
159 |
case model @ _ => new Seq[A] { selfSeq => |
159 |
case model => new Seq[A] { selfSeq => |
160 |
def length = model.getSize |
160 |
def length = model.getSize |
161 |
def iterator = new Iterator[A] { |
161 |
def iterator = new Iterator[A] { |
162 |
var idx = 0 |
162 |
var idx = 0 |
163 |
def next = { idx += 1; apply(idx-1) } |
163 |
def next = { idx += 1; apply(idx-1) } |
164 |
def hasNext = idx < selfSeq.length |
164 |
def hasNext = idx < selfSeq.length |
165 |
} |
165 |
} |
166 |
def apply(n: Int) = model.getElementAt(n).asInstanceOf[A] |
166 |
def apply(n: Int): A = model.getElementAt(n) |
167 |
} |
167 |
} |
168 |
} |
168 |
} |
169 |
|
169 |
|
170 |
def listData_=(items: Seq[A]) { |
170 |
def listData_=(items: Seq[A]) { |
171 |
peer.setModel(new AbstractListModel { |
171 |
peer.setModel(new AbstractListModel[A] { |
172 |
def getElementAt(n: Int) = items(n).asInstanceOf[AnyRef] |
172 |
def getElementAt(n: Int) = items(n) |
173 |
def getSize = items.size |
173 |
def getSize = items.size |
174 |
}) |
174 |
}) |
175 |
} |
175 |
} |
Lines 216-222
Link Here
|
216 |
def adjusting = peer.getSelectionModel.getValueIsAdjusting |
216 |
def adjusting = peer.getSelectionModel.getValueIsAdjusting |
217 |
} |
217 |
} |
218 |
|
218 |
|
219 |
def renderer: ListView.Renderer[A] = ListView.Renderer.wrap(peer.getCellRenderer) |
219 |
def renderer: ListView.Renderer[A] = ListView.Renderer.wrap[A](peer.getCellRenderer.asInstanceOf[ListCellRenderer[A]]) |
220 |
def renderer_=(r: ListView.Renderer[A]) { peer.setCellRenderer(r.peer) } |
220 |
def renderer_=(r: ListView.Renderer[A]) { peer.setCellRenderer(r.peer) } |
221 |
|
221 |
|
222 |
def fixedCellWidth = peer.getFixedCellWidth |
222 |
def fixedCellWidth = peer.getFixedCellWidth |