Skip to content

Commit acf7c70

Browse files
committed
clean up readme; remove commented stale code
- remove the commented former and unused class `RefBuffer` - add more information about architecture (events) to read-me; update information about versions and branches
1 parent c228e6f commit acf7c70

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,35 @@ The widget class hierarchy loosely resembles that of Java Swing. The main differ
1414
a number of components, like TextField, CheckBox, RadioButton, and so on. Our guess is that
1515
this architecture was chosen because Java lacks multiple inheritance.
1616
In scala-swing, components that can have child components extend the Container trait.
17-
- Layout managers and panels are coupled. There is no way to exchange the layout manager
17+
- Layout managers and panels are coupled. There is no way to exchange the layout manager
1818
of a panel. As a result, the layout constraints for widgets can be typed.
1919
(Note that you gain more type-safety and don't loose much flexibility here. Besides
2020
being not a common operation, exchanging the layout manager of a panel in Java
2121
Swing almost always leads to exchanging the layout constraints for every of the panel's
2222
child component. In the end, it is not more work to move all children to a newly created
2323
panel.)
24-
25-
The event system. TODO.
26-
24+
- Widget hierarchies are built by adding children to their parent container's `contents`
25+
collection. The typical usage style is to create anonymous subclasses of the widgets to
26+
customize their properties, and nest children and event reactions.
27+
- The scala-swing event system follows a different approach than the underlying Java system.
28+
Instead of add event listeners with a particular interface (such as `java.awt.ActionListener`),
29+
a `Reactor` instances announces the interest in receiving events by calling `listenTo` for
30+
a `Publisher`. Publishers are also reactors and listen to themselves per default as a convenience.
31+
A reactor contains an object `reactions` which serves as a convenient place to register observers
32+
by adding partial functions that pattern match for any event that the observer is interested in.
33+
This is shown in the examples section below.
2734
- For more details see [SIP-8](docs/SIP-8.md)
2835

2936
The library comprises two main packages:
3037

3138
- `scala.swing`: All widget classes and traits.
3239
- `scala.swing.event`: The event hierarchy.
3340

34-
3541
## Examples
3642

3743
A number of examples can be found in the `examples` project.
38-
A good place to start is `[12] scala.swing.examples.UIDemo` (_index number may be different for you_). This pulls in the all the other examples into a tabbed window.
44+
A good place to start is `[16] scala.swing.examples.UIDemo`.
45+
This pulls in the all the other examples into a tabbed window.
3946

4047
```
4148
$ sbt examples/run
@@ -62,22 +69,51 @@ Multiple main classes detected, select one to run:
6269
Enter number:
6370
```
6471

72+
### Frame with a Button
73+
74+
The following example shows how to plug components and containers together and react to a
75+
mouse click on a button:
76+
77+
```scala
78+
import scala.swing._
79+
80+
new Frame {
81+
title = "Hello world"
82+
83+
contents = new FlowPanel {
84+
contents += new Label("Launch rainbows:")
85+
contents += new Button("Click me") {
86+
reactions += {
87+
case event.ButtonClicked(_) =>
88+
println("All the colours!")
89+
}
90+
}
91+
}
92+
93+
pack()
94+
centerOnScreen()
95+
open()
96+
}
97+
```
6598

6699
## Versions
67100

68101
- The `1.0.x` branch is compiled with JDK 6 and released for Scala 2.10, 2.11. The 1.0.x releases can be used with both Scala versions on JDK 6 or newer.
69102
- The `2.0.x` branch is compiled with JDK 8 and released for Scala 2.11 and 2.12.
70103
- When using Scala 2.11, you can use the Scala swing 2.0.x releases on JDK 6 or newer.
71104
- Scala 2.12 requires you to use JDK 8 (that has nothing to do with scala-swing).
105+
- Version `2.1.0` adds support for Scala 2.13, while dropping Scala 2.10.
72106

73-
The reason to have two versions is to allow for binary incompatible changes. Also, some java-swing classes were generified in JDK 7 (see [SI-3634](https://issues.scala-lang.org/browse/SI-3634)) and require the scala-swing sources to be adjusted.
107+
The reason to have different major versions is to allow for binary incompatible changes. Also, some java-swing classes were
108+
generified in JDK 7 (see [SI-3634](https://issues.scala-lang.org/browse/SI-3634)) and require the scala-swing sources to be adjusted.
74109

75110

76111
## API documentation (Scaladoc)
77112

78-
The API documentation for scala-swing can be found at [http://www.scala-lang.org/documentation/api.html](http://www.scala-lang.org/documentation/api.html).
113+
The API documentation for scala-swing can be found
114+
at [http://www.scala-lang.org/documentation/api.html](http://www.scala-lang.org/documentation/api.html).
79115

80116

81117
## Current Work
82118

83-
Current changes are being made on the `2.0.x` branch.
119+
Current changes are being made on the `work` branch.

src/main/scala/scala/swing/Publisher.scala

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -127,48 +127,15 @@ private[swing] trait SingleRefCollection[+A <: AnyRef] extends Iterable[A] { sel
127127
}
128128

129129
private[swing] class StrongReference[+T <: AnyRef](value: T) extends Reference[T] {
130-
private[this] var ref: Option[T] = Some(value)
131-
def isValid: Boolean = ref.isDefined
132-
def apply(): T = ref.get
133-
def get : Option[T] = ref
134-
override def toString: String = get.map(_.toString).getOrElse("<deleted>")
135-
def clear(): Unit = { ref = None }
136-
def enqueue(): Boolean = false
137-
def isEnqueued: Boolean = false
138-
}
139-
140-
//abstract class RefBuffer[A <: AnyRef] extends BufferWrapper[A] with SingleRefCollection[A] { self =>
141-
// protected val underlying: mutable.Buffer[Reference[A]]
142-
//
143-
// def addOne (el: A): this.type = { purgeReferences(); underlying += Ref(el) ; this }
144-
// def prependOne(el: A): this.type = { purgeReferences(); Ref(el) +=: underlying; this }
145-
//
146-
// def remove(el: A): Unit = { underlying -= Ref(el); purgeReferences(); }
147-
//
148-
// def remove(n: Int): A = { val el = apply(n); remove(el); el }
149-
//
150-
// def insertAll(n: Int, iter: Iterable[A]): Unit = {
151-
// purgeReferences()
152-
// underlying.insertAll(n, iter.view.map(Ref))
153-
// }
154-
//
155-
// def update(n: Int, el: A): Unit = { purgeReferences(); underlying(n) = Ref(el) }
156-
//
157-
// def apply(n: Int): A = {
158-
// purgeReferences()
159-
// var el = underlying(n).get
160-
// while (el.isEmpty) {
161-
// purgeReferences(); el = underlying(n).get
162-
// }
163-
// el.get
164-
// }
165-
//
166-
// def length: Int = { purgeReferences(); underlying.length }
167-
//
168-
// def clear(): Unit = { underlying.clear(); purgeReferences() }
169-
//
170-
// protected[this] def removeReference(ref: Reference[A]): Unit = { underlying -= ref }
171-
//}
130+
private[this] var ref: Option[T] = Some(value)
131+
def isValid: Boolean = ref.isDefined
132+
def apply(): T = ref.get
133+
def get : Option[T] = ref
134+
override def toString: String = get.map(_.toString).getOrElse("<deleted>")
135+
def clear(): Unit = { ref = None }
136+
def enqueue(): Boolean = false
137+
def isEnqueued: Boolean = false
138+
}
172139

173140
private[swing] abstract class RefSet[A <: AnyRef] extends SetWrapper[A] with SingleRefCollection[A] { self =>
174141
protected val underlying: mutable.Set[Reference[A]]

0 commit comments

Comments
 (0)