1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2007-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.swing
import event._
import javax.swing.{JList, JComponent, JComboBox, JTextField, ComboBoxModel, AbstractListModel, ListCellRenderer}
import java.awt.event.ActionListener
object ComboBox {
/**
* An editor for a combo box. Let's you edit the currently selected item.
* It is highly recommended to use the BuiltInEditor class. For anything
* else, one cannot guarantee that it integrates nicely with the current
* LookAndFeel.
*
* Publishes action events.
*/
trait Editor[A] extends Publisher {
lazy val comboBoxPeer: javax.swing.ComboBoxEditor = new javax.swing.ComboBoxEditor with Publisher {
def addActionListener(l: ActionListener) {
this match {
// TODO case w: Action.Trigger.Wrapper =>
// w.peer.addActionListener(l)
case _ =>
this.subscribe(new Reactions.Wrapper(l) ({
case ActionEvent(c) => l.actionPerformed(new java.awt.event.ActionEvent(c.peer, 0, ""))
}))
}
}
def removeActionListener(l: ActionListener) {
this match {
// TODO case w: Action.Trigger.Wrapper =>
// w.peer.removeActionListener(l)
case _ =>
this.unsubscribe(new Reactions.Wrapper(l)({ case _ => }))
}
}
def getEditorComponent: JComponent = Editor.this.component.peer
def getItem(): AnyRef = item.asInstanceOf[AnyRef]
def selectAll() { startEditing() }
def setItem(a: Any) { item = a.asInstanceOf[A] }
}
def component: Component
def item: A
def item_=(a: A)
def startEditing()
}
/**
* Use this editor, if you want to reuse the builtin editor supplied by the current
* Look and Feel. This is restricted to a text field as the editor widget. The
* conversion from and to a string is done by the supplied functions.
*
* It's okay if string2A throws exceptions. They are caught by an input verifier.
*/
class BuiltInEditor[A](comboBox: ComboBox[A])(string2A: String => A,
a2String: A => String) extends ComboBox.Editor[A] {
protected[swing] class DelegatedEditor(editor: javax.swing.ComboBoxEditor) extends javax.swing.ComboBoxEditor {
var value: A = {
val v = comboBox.peer.getSelectedItem
try {
v match {
case s: String => string2A(s)
case _ => v.asInstanceOf[A]
}
} catch {
case _: Exception =>
throw new IllegalArgumentException("ComboBox not initialized with a proper value, was '" + v + "'.")
}
}
def addActionListener(l: ActionListener) {
editor.addActionListener(l)
}
def removeActionListener(l: ActionListener) {
editor.removeActionListener(l)
}
def getEditorComponent: JComponent = editor.getEditorComponent.asInstanceOf[JComponent]
def selectAll() { editor.selectAll() }
def getItem(): AnyRef = { verifier.verify(getEditorComponent); value.asInstanceOf[AnyRef] }
def setItem(a: Any) { editor.setItem(a) }
val verifier = new javax.swing.InputVerifier {
// TODO: should chain with potentially existing verifier in editor
def verify(c: JComponent) = try {
value = string2A(c.asInstanceOf[JTextField].getText)
true
}
catch {
case e: Exception => false
}
}
def textEditor = getEditorComponent.asInstanceOf[JTextField]
textEditor.setInputVerifier(verifier)
textEditor.addActionListener(Swing.ActionListener{ a =>
getItem() // make sure our value is updated
textEditor.setText(a2String(value))
})
}
override lazy val comboBoxPeer: javax.swing.ComboBoxEditor = new DelegatedEditor(comboBox.peer.getEditor)
def component = Component.wrap(comboBoxPeer.getEditorComponent.asInstanceOf[JComponent])
def item: A = { comboBoxPeer.asInstanceOf[DelegatedEditor].value }
def item_=(a: A) { comboBoxPeer.setItem(a2String(a)) }
def startEditing() { comboBoxPeer.selectAll() }
}
implicit def stringEditor(c: ComboBox[String]): Editor[String] = new BuiltInEditor(c)(s => s, s => s)
implicit def intEditor(c: ComboBox[Int]): Editor[Int] = new BuiltInEditor(c)(s => s.toInt, s => s.toString)
implicit def floatEditor(c: ComboBox[Float]): Editor[Float] = new BuiltInEditor(c)(s => s.toFloat, s => s.toString)
implicit def doubleEditor(c: ComboBox[Double]): Editor[Double] = new BuiltInEditor(c)(s => s.toDouble, s => s.toString)
def newConstantModel[A](items: Seq[A]): ComboBoxModel = {
new AbstractListModel with ComboBoxModel {
private var selected = items(0)
def getSelectedItem: AnyRef = selected.asInstanceOf[AnyRef]
def setSelectedItem(a: Any) {
if ((selected != null && selected != a) ||
selected == null && a != null) {
selected = a.asInstanceOf[A]
fireContentsChanged(this, -1, -1)
}
}
def getElementAt(n: Int) = items(n).asInstanceOf[AnyRef]
def getSize = items.size
}
}
/*def newMutableModel[A, Self](items: Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self]): ComboBoxModel = {
new AbstractListModel with ComboBoxModel {
private var selected = items(0)
def getSelectedItem: AnyRef = selected.asInstanceOf[AnyRef]
def setSelectedItem(a: Any) { selected = a.asInstanceOf[A] }
def getElementAt(n: Int) = items(n).asInstanceOf[AnyRef]
def getSize = items.size
}
}
def newConstantModel[A](items: Seq[A]): ComboBoxModel = items match {
case items: Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self] => newMutableModel
case _ => newConstantModel(items)
}*/
}
/**
* Let's the user make a selection from a list of predefined items. Visually,
* this is implemented as a button-like component with a pull-down menu.
*
* @see javax.swing.JComboBox
*/
class ComboBox[A](items: Seq[A]) extends Component with Publisher {
override lazy val peer: JComboBox = new JComboBox(ComboBox.newConstantModel(items)) with SuperMixin
object selection extends Publisher {
def index: Int = peer.getSelectedIndex
def index_=(n: Int) { peer.setSelectedIndex(n) }
def item: A = peer.getSelectedItem.asInstanceOf[A]
def item_=(a: A) { peer.setSelectedItem(a) }
peer.addActionListener(Swing.ActionListener { e =>
publish(event.SelectionChanged(ComboBox.this))
})
}
/**
* Sets the renderer for this combo box's items. Index -1 is
* passed to the renderer for the selected item (not in the pull-down menu).
*
* The underlying combo box renders all items in a <code>ListView</code>
* (both, in the pull-down menu as well as in the box itself), hence the
* <code>ListView.Renderer</code>.
*
* Note that the UI peer of a combo box usually changes the colors
* of the component to its own defaults _after_ the renderer has been
* configured. That's Swing's principle of most suprise.
*/
def renderer: ListView.Renderer[A] = ListView.Renderer.wrap(peer.getRenderer)
def renderer_=(r: ListView.Renderer[A]) { peer.setRenderer(r.peer) }
/* XXX: currently not safe to expose:
def editor: ComboBox.Editor[A] =
def editor_=(r: ComboBox.Editor[A]) { peer.setEditor(r.comboBoxPeer) }
*/
def editable: Boolean = peer.isEditable
/**
* Makes this combo box editable. In order to do so, this combo needs an
* editor which is supplied by the implicit argument. For default
* editors, see ComboBox companion object.
*/
def makeEditable()(implicit editor: ComboBox[A] => ComboBox.Editor[A]) {
peer.setEditable(true)
peer.setEditor(editor(this).comboBoxPeer)
}
def prototypeDisplayValue: Option[A] = toOption[A](peer.getPrototypeDisplayValue)
def prototypeDisplayValue_=(v: Option[A]) {
peer.setPrototypeDisplayValue(v map toAnyRef orNull)
}
}
|