Skip to content

Commit 72fd1d6

Browse files
committed
remove swing.Seq alias; use immutable.Seq where appropriate
- we leave scala.collection.Seq where the API would use Java Arrays, so we do not get sub-par performance in 2.13 - we use the more strict type immutable.Seq where its effortless and actually ensures correct behaviour. Since we are in a binary incompatible new scala-swing version, in the worst case user code that used Array in Scala <2.13 would have to enforce toList, toVector or similar
1 parent acf7c70 commit 72fd1d6

File tree

12 files changed

+41
-32
lines changed

12 files changed

+41
-32
lines changed

examples/src/main/scala/scala/swing/examples/tutorials/components/ListDemo.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ListDemo extends BorderPanel {
7676
val nameSelected: String = listModel(initiallySelected)
7777

7878
//Create a panel that uses BoxLayout.
79-
val buttonPane = new BoxPanel(Orientation.Horizontal) {
79+
val buttonPane: BoxPanel = new BoxPanel(Orientation.Horizontal) {
8080
border = Swing.EmptyBorder(5, 5, 5, 5)
8181
contents += fireButton
8282
contents += Swing.HStrut(5)
@@ -157,7 +157,7 @@ class ListDemo extends BorderPanel {
157157
}
158158

159159
object ListDemo extends SimpleSwingApplication {
160-
lazy val top = new MainFrame() {
160+
lazy val top: Frame = new MainFrame() {
161161
title = "ListDemo"
162162
//Create and set up the content pane.
163163
contents = new ListDemo()

src/main/scala-2.13.0-M5/scala/swing/BufferWrapper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class BufferWrapper[A] extends mutable.Buffer[A] {
4444
}
4545
}
4646

47-
override def patchInPlace(from: Int, patch: Seq[A], replaced: Int): this.type = {
47+
override def patchInPlace(from: Int, patch: scala.collection.Seq[A], replaced: Int): this.type = {
4848
if (replaced > 0) {
4949
remove(from, replaced)
5050
}

src/main/scala/scala/swing/ComboBox.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ object ComboBox {
121121
implicit def floatEditor (c: ComboBox[Float ]): Editor[Float ] = new BuiltInEditor(c)(s => s.toFloat , s => s.toString)
122122
implicit def doubleEditor (c: ComboBox[Double ]): Editor[Double ] = new BuiltInEditor(c)(s => s.toDouble, s => s.toString)
123123

124-
def newConstantModel[A](items: Seq[A]): ComboBoxModel[A] = {
124+
def newConstantModel[A](items: scala.collection.Seq[A]): ComboBoxModel[A] = {
125125
new AbstractListModel[A] with ComboBoxModel[A] {
126126
private var selected: A = if (items.isEmpty) null.asInstanceOf[A] else items(0)
127127
def getSelectedItem: AnyRef = selected.asInstanceOf[AnyRef]
@@ -137,7 +137,7 @@ object ComboBox {
137137
}
138138
}
139139

140-
/*def newMutableModel[A, Self](items: Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self]): ComboBoxModel = {
140+
/*def newMutableModel[A, Self](items: scala.collection.Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self]): ComboBoxModel = {
141141
new AbstractListModel with ComboBoxModel {
142142
private var selected = items(0)
143143
def getSelectedItem: AnyRef = selected.asInstanceOf[AnyRef]
@@ -147,8 +147,8 @@ object ComboBox {
147147
}
148148
}
149149
150-
def newConstantModel[A](items: Seq[A]): ComboBoxModel = items match {
151-
case items: Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self] => newMutableModel
150+
def newConstantModel[A](items: scala.collection.Seq[A]): ComboBoxModel = items match {
151+
case items: scala.collection.Seq[A] with scala.collection.mutable.Publisher[scala.collection.mutable.Message[A], Self] => newMutableModel
152152
case _ => newConstantModel(items)
153153
}*/
154154
}
@@ -159,7 +159,7 @@ object ComboBox {
159159
*
160160
* @see javax.swing.JComboBox
161161
*/
162-
class ComboBox[A](items: Seq[A]) extends Component with Publisher {
162+
class ComboBox[A](items: scala.collection.Seq[A]) extends Component with Publisher {
163163
override lazy val peer: JComboBox[A] = new JComboBox(ComboBox.newConstantModel(items)) with SuperMixin
164164

165165
object selection extends Publisher {

src/main/scala/scala/swing/Container.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Container {
1919
override def peer: javax.swing.JComponent
2020

2121
protected val _contents = new Content
22-
def contents: Seq[Component] = _contents
22+
def contents: scala.collection.Seq[Component] = _contents
2323

2424
protected class Content extends BufferWrapper[Component] {
2525
override def clear(): Unit = peer.removeAll()
@@ -61,5 +61,5 @@ trait Container extends UIElement {
6161
/**
6262
* The child components of this container.
6363
*/
64-
def contents: Seq[Component]
64+
def contents: scala.collection.Seq[Component]
6565
}

src/main/scala/scala/swing/FileChooser.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import java.io.File
1313
import javax.swing.filechooser.FileFilter
1414
import javax.swing.{Icon, JFileChooser}
1515

16+
import scala.collection.immutable
17+
1618
object FileChooser {
1719
/**
1820
* The result of a file dialog. The precise meaning of the `Approve`
@@ -90,7 +92,7 @@ class FileChooser(dir: File) {
9092

9193
def selectedFile: File = peer.getSelectedFile
9294
def selectedFile_=(file: File): Unit = peer.setSelectedFile(file)
93-
def selectedFiles: Seq[File] = peer.getSelectedFiles.toSeq
95+
def selectedFiles: immutable.Seq[File] = peer.getSelectedFiles.toIndexedSeq
9496
def selectedFiles_=(files: File*): Unit = peer.setSelectedFiles(files.toArray)
9597

9698
def multiSelectionEnabled: Boolean = peer.isMultiSelectionEnabled

src/main/scala/scala/swing/ListView.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import event._
1212
import javax.swing._
1313
import javax.swing.event._
1414

15+
import scala.collection.immutable
16+
1517
object ListView {
1618
/**
1719
* The supported modes of user selections.
@@ -143,19 +145,19 @@ class ListView[A] extends Component {
143145
import ListView._
144146
override lazy val peer: JList[A] = new JList[A] with SuperMixin
145147

146-
def this(items: Seq[A]) = {
148+
def this(items: scala.collection.Seq[A]) = {
147149
this()
148150
listData = items
149151
}
150152

151-
protected class ModelWrapper[B](val items: Seq[B]) extends AbstractListModel[B] {
153+
protected class ModelWrapper[B](val items: scala.collection.Seq[B]) extends AbstractListModel[B] {
152154
def getElementAt(n: Int): B = items(n)
153155
def getSize: Int = items.size
154156
}
155157

156-
def listData: Seq[A] = peer.getModel match {
158+
def listData: scala.collection.Seq[A] = peer.getModel match {
157159
case model: ModelWrapper[a] => model.items
158-
case model => new Seq[A] { selfSeq =>
160+
case model => new immutable.Seq[A] { selfSeq =>
159161
def length: Int = model.getSize
160162
def iterator: Iterator[A] = new Iterator[A] {
161163
var idx = 0
@@ -166,7 +168,7 @@ class ListView[A] extends Component {
166168
}
167169
}
168170

169-
def listData_=(items: Seq[A]): Unit = {
171+
def listData_=(items: scala.collection.Seq[A]): Unit = {
170172
peer.setModel(new AbstractListModel[A] {
171173
def getElementAt(n: Int): A = items(n)
172174
def getSize: Int = items.size
@@ -177,7 +179,7 @@ class ListView[A] extends Component {
177179
* The current item selection.
178180
*/
179181
object selection extends Publisher {
180-
protected abstract class Indices[B](a: => Seq[B]) extends SetWrapper[B] {
182+
protected abstract class Indices[B](a: => scala.collection.Seq[B]) extends SetWrapper[B] {
181183
def contains(n: B): Boolean = a.contains(n)
182184

183185
override def size: Int = a.length
@@ -201,10 +203,11 @@ class ListView[A] extends Component {
201203
/**
202204
* The currently selected items.
203205
*/
204-
def items: Seq[A] = {
206+
def items: immutable.Seq[A] = {
205207
// note: we should be using `getSelectedValuesList`, but it would break the Scala 2.11
206-
// promise of working with Java 6 (requires Java 7)
207-
peer.getSelectedValues.iterator.map(_.asInstanceOf[A]).toSeq
208+
// promise of working with Java 6 (requires Java 7).
209+
// Note: in Scala <= 2.12, toSeq might produce a Stream.
210+
peer.getSelectedValues.iterator.map(_.asInstanceOf[A]).toIndexedSeq
208211
}
209212

210213
def intervalMode: IntervalMode.Value = IntervalMode(peer.getSelectionModel.getSelectionMode)

src/main/scala/scala/swing/RichWindow.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ object Dialog {
146146
optionType: Options.Value = Options.YesNo,
147147
messageType: Message.Value = Message.Question,
148148
icon: Icon = EmptyIcon,
149-
entries: Seq[Any],
149+
entries: scala.collection.Seq[Any],
150150
initial: Int): Result.Value = {
151151
val r = JOptionPane.showOptionDialog(nullPeer(parent), message, title,
152152
optionType.id, messageType.id, Swing.wrapIcon(icon),
@@ -159,10 +159,10 @@ object Dialog {
159159
title: String = uiString("OptionPane.inputDialogTitle"),
160160
messageType: Message.Value = Message.Question,
161161
icon: Icon = EmptyIcon,
162-
entries: Seq[A] = Nil,
162+
entries: scala.collection.Seq[A] = Nil,
163163
initial: A): Option[A] = {
164164
val e = if (entries.isEmpty) null
165-
else (entries map toAnyRef).toArray
165+
else entries.iterator.map(toAnyRef).toArray
166166
val r = JOptionPane.showInputDialog(nullPeer(parent), message, title,
167167
messageType.id, Swing.wrapIcon(icon),
168168
e, initial)

src/main/scala/scala/swing/RootPanel.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package scala.swing
1010

11+
import scala.collection.immutable
12+
1113
/**
1214
* The root of a component hierarchy. Contains at most one component.
1315
*
@@ -19,7 +21,7 @@ trait RootPanel extends Container {
1921
/**
2022
* At most one component.
2123
*/
22-
def contents: Seq[Component] =
24+
def contents: immutable.Seq[Component] =
2325
if (peer.getContentPane.getComponentCount == 0) Nil
2426
else {
2527
val c = peer.getContentPane.getComponent(0).asInstanceOf[javax.swing.JComponent]

src/main/scala/scala/swing/ScrollPane.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package scala.swing
1010

1111
import javax.swing.{JScrollPane, ScrollPaneConstants}
1212

13+
import scala.collection.immutable
14+
1315
object ScrollPane {
1416
object BarPolicy extends Enumeration {
1517
import ScrollPaneConstants._
@@ -46,7 +48,7 @@ class ScrollPane extends Component with Container {
4648
this()
4749
contents = c
4850
}
49-
def contents: Seq[Component] =
51+
def contents: immutable.Seq[Component] =
5052
List(UIElement.cachedWrapper[Component](peer.getViewport.getView.asInstanceOf[javax.swing.JComponent]))
5153

5254
/**

src/main/scala/scala/swing/SplitPane.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package scala.swing
1010

1111
import javax.swing.JSplitPane
1212

13+
import scala.collection.immutable
1314
import scala.swing.Swing.nullPeer
1415

1516
/**
@@ -25,7 +26,7 @@ class SplitPane(o: Orientation.Value, left: Component, right: Component) extends
2526
def this(o: Orientation.Value) = this(o, new Component {}, new Component {})
2627
def this() = this(Orientation.Horizontal)
2728

28-
def contents: Seq[Component] = List(leftComponent, rightComponent)
29+
def contents: immutable.Seq[Component] = List(leftComponent, rightComponent)
2930
def contents_=(left: Component, right: Component): Unit = {
3031
peer.setLeftComponent(nullPeer(left))
3132
peer.setRightComponent(nullPeer(right))

src/main/scala/scala/swing/Table.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Table extends Component with Scrollable.Wrapper {
133133
* by calling `toString` on the elements. The size of this sequence
134134
* must correspond with the inner dimension of `rowData`.
135135
*/
136-
def this(rowData: Array[Array[Any]], columnNames: Seq[_]) = {
136+
def this(rowData: Array[Array[Any]], columnNames: scala.collection.Seq[_]) = {
137137
this()
138138
model = new AbstractTableModel {
139139
override def getColumnName(column: Int): String = columnNames(column).toString
@@ -196,7 +196,7 @@ class Table extends Component with Scrollable.Wrapper {
196196

197197
object selection extends Publisher {
198198
// TODO: could be a sorted set
199-
protected abstract class SelectionSet[A](a: => Seq[A]) extends SetWrapper[A] {
199+
protected abstract class SelectionSet[A](a: => scala.collection.Seq[A]) extends SetWrapper[A] {
200200
// def -=(n: A): this.type
201201
// def +=(n: A): this.type
202202

src/main/scala/scala/swing/package.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ package object swing {
2222
type Image = java.awt.Image
2323
type Font = java.awt.Font
2424

25-
type Seq[+A] = scala.collection.Seq[A] // because scala.Seq differs between Scala 2.12 and 2.13
26-
2725
implicit lazy val reflectiveCalls = scala.language.reflectiveCalls
2826
implicit lazy val implicitConversions = scala.language.implicitConversions
2927

30-
private[swing] def ifNull[A](o: Object, a: A): A = if(o eq null) a else o.asInstanceOf[A]
31-
private[swing] def toOption[A](o: Object): Option[A] = if(o eq null) None else Some(o.asInstanceOf[A])
28+
private[swing] def ifNull [A](o: Object, a: A): A = if(o eq null) a else o.asInstanceOf[A]
29+
private[swing] def toOption [A](o: Object): Option[A] = if(o eq null) None else Some(o.asInstanceOf[A])
30+
3231
private[swing] def toAnyRef(x: Any): AnyRef = x.asInstanceOf[AnyRef]
3332
}

0 commit comments

Comments
 (0)