Skip to content

Commit 943ef11

Browse files
author
Allan Jacobs
committed
Merged changes from Andy Hicks, mostly having to do with createImageIcon.
1 parent 8c0754a commit 943ef11

File tree

13 files changed

+403
-552
lines changed

13 files changed

+403
-552
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ import javax.swing.UIManager
4747
* /scala/swing/examples/tutorials/images/left.gif
4848
*/
4949
class ButtonDemo extends FlowPanel {
50-
val leftButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/right.gif").get
51-
val middleButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/middle.gif").get
52-
val rightButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/left.gif").get
53-
//
50+
val leftButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/right.gif")
51+
val middleButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/middle.gif")
52+
val rightButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/left.gif")
53+
5454
val disable: Button = new Button("Disable middle button") {
55-
icon = leftButtonIcon
55+
leftButtonIcon.foreach( icon = _)
5656
verticalTextPosition = Alignment.Center
5757
horizontalTextPosition = Alignment.Leading
5858
mnemonic = event.Key.D
5959
tooltip = "Click this button to disable the middle button."
6060
}
61-
//
61+
6262
val middle: Button = new Button("Middle button") {
63-
icon = middleButtonIcon
63+
middleButtonIcon.foreach( icon = _ )
6464
verticalTextPosition = Alignment.Bottom
6565
horizontalTextPosition = Alignment.Center
6666
mnemonic = event.Key.M
6767
tooltip = "This middle button does nothing when you click it."
6868
}
69-
//
69+
7070
val enable: Button = new Button("Enable middle button") {
71-
icon = rightButtonIcon
71+
rightButtonIcon.foreach( icon = _ )
7272
enabled = false
7373
mnemonic = event.Key.E
7474
tooltip = "Click this button to enable the middle button."
@@ -82,16 +82,17 @@ class ButtonDemo extends FlowPanel {
8282
listenTo(enable)
8383

8484
reactions += {
85-
case ButtonClicked(`enable`) => enableMiddle
86-
case ButtonClicked(`disable`) => disableMiddle
85+
case ButtonClicked(`enable`) => enableMiddle()
86+
case ButtonClicked(`disable`) => disableMiddle()
8787
}
8888

8989
def enableMiddle(): Unit = {
9090
enable.enabled = false
9191
middle.enabled = true
9292
disable.enabled = true
9393
}
94-
def disableMiddle(): Unit = {
94+
95+
def disableMiddle(): Unit = {
9596
enable.enabled = true
9697
middle.enabled = false
9798
disable.enabled = false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ object ButtonHtmlDemo extends SimpleSwingApplication {
111111
lazy val top = new MainFrame() {
112112
title = "ButtonHtmlDemo"
113113
//Create and set up the content pane.
114-
contents = new ButtonHtmlDemo();
114+
contents = new ButtonHtmlDemo()
115115
}
116116
}

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

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import scala.swing._
3434
import scala.swing.event.ButtonClicked
3535
import scala.swing.event.Key
3636
import java.awt.Font
37-
import javax.swing.ImageIcon
38-
import java.net.URL
37+
38+
import scala.swing.examples.tutorials.components.ButtonDemo._
3939

4040
/**
4141
* Tutorial: How to Use Buttons, Check Boxes, and Radio Buttons
@@ -83,21 +83,25 @@ class CheckboxDemo extends BorderPanel {
8383
*/
8484

8585
//Create the check boxes.
86-
val chinButton = new CheckBox("Chin")
87-
chinButton.mnemonic = Key.C // .VK_C
88-
chinButton.selected = true
86+
val chinButton = new CheckBox("Chin") {
87+
mnemonic = Key.C // .VK_C
88+
selected = true
89+
}
8990

90-
val glassesButton = new CheckBox("Glasses")
91-
glassesButton.mnemonic = Key.G;
92-
glassesButton.selected = true
91+
val glassesButton = new CheckBox("Glasses") {
92+
mnemonic = Key.G
93+
selected = true
94+
}
9395

94-
val hairButton = new CheckBox("Hair")
95-
hairButton.mnemonic = Key.H
96-
hairButton.selected = true
96+
val hairButton = new CheckBox("Hair") {
97+
mnemonic = Key.H
98+
selected = true
99+
}
97100

98-
val teethButton = new CheckBox("Teeth")
99-
teethButton.mnemonic = Key.T
100-
teethButton.selected = true
101+
val teethButton = new CheckBox("Teeth") {
102+
mnemonic = Key.T
103+
selected = true
104+
}
101105

102106
//Indicates what's on the geek.
103107
val choices: StringBuffer = new StringBuffer("cght")
@@ -125,34 +129,31 @@ class CheckboxDemo extends BorderPanel {
125129
listenTo(hairButton)
126130
listenTo(teethButton)
127131

132+
133+
def changeState( idx:Int, checkBox: CheckBox, label:Char ): Unit = {
134+
choices.setCharAt(idx, if (checkBox.selected) label else '-' )
135+
updatePicture()
136+
}
137+
128138
reactions += {
129-
case ButtonClicked(`chinButton`) =>
130-
choices.setCharAt(0, if (!chinButton.selected) '-' else 'c')
131-
updatePicture()
132-
case ButtonClicked(`glassesButton`) =>
133-
choices.setCharAt(1, if (!glassesButton.selected) '-' else 'g')
134-
updatePicture()
135-
case ButtonClicked(`hairButton`) =>
136-
choices.setCharAt(2, if (!hairButton.selected) '-' else 'h')
137-
updatePicture()
138-
case ButtonClicked(`teethButton`) =>
139-
choices.setCharAt(3, if (!teethButton.selected) '-' else 't')
140-
updatePicture()
139+
case ButtonClicked(`chinButton`) => changeState(0, chinButton, 'c')
140+
case ButtonClicked(`glassesButton`) => changeState(1, glassesButton, 'g')
141+
case ButtonClicked(`hairButton`) => changeState(2, hairButton, 'h')
142+
case ButtonClicked(`teethButton`) => changeState(3, teethButton, 't')
141143
}
142144

143145
def updatePicture(): Unit = {
146+
pictureLabel.tooltip = choices.toString
147+
144148
//Get the icon corresponding to the image.
145-
val s = choices.toString()
146-
val icon: Option[ImageIcon] = CheckboxDemo.createImageIcon(
147-
"/scala/swing/examples/tutorials/images/geek/geek-"
148-
+ choices.toString()
149-
+ ".gif")
150-
pictureLabel.icon = if (icon.isDefined) icon.get else Swing.EmptyIcon
151-
pictureLabel.tooltip = choices.toString()
152-
if (icon == null) {
153-
pictureLabel.text = "Missing Image"
154-
} else {
155-
pictureLabel.text = null
149+
createImageIcon( s"/scala/swing/examples/tutorials/images/geek/geek-$choices.gif") match {
150+
case Some( icon ) =>
151+
pictureLabel.icon = icon
152+
pictureLabel.text = null
153+
case None =>
154+
pictureLabel.icon = Swing.EmptyIcon
155+
pictureLabel.text = "Missing Image"
156+
156157
}
157158
}
158159
}
@@ -165,6 +166,6 @@ object CheckboxDemo extends SimpleSwingApplication {
165166
lazy val top = new MainFrame() {
166167
title = "CheckboxDemo"
167168
//Create and set up the content pane.
168-
contents = new CheckboxDemo();
169+
contents = new CheckboxDemo()
169170
}
170171
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ import java.awt.{ Color, Dimension, Font }
4141
*
4242
* Source code reference:
4343
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ColorChooserDemoProject/src/components/ColorChooserDemo.java]]
44-
*
45-
* ColorChooserDemo.scala requires no other files.
4644
*/
4745
class ColorChooserDemo extends BorderPanel {
4846
//Set up the banner at the top of the window
49-
val banner = new Label("Welcome to the Tutorial Zone!", EmptyIcon, Alignment.Center) {
47+
val banner = new Label("Welcome to the Scala-Swing Zone!", EmptyIcon, Alignment.Center) {
5048
foreground = Color.yellow
5149
background = Color.blue
5250
opaque = true
@@ -75,6 +73,6 @@ object ColorChooserDemo extends SimpleSwingApplication {
7573
lazy val top = new MainFrame() {
7674
title = "ColorChooserDemo"
7775
//Create and set up the content pane.
78-
contents = new ColorChooserDemo();
76+
contents = new ColorChooserDemo()
7977
}
8078
}

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
package scala.swing.examples.tutorials.components
3333

3434
import scala.swing._
35-
import scala.swing.event.{ SelectionChanged }
35+
import scala.swing.event.SelectionChanged
3636
import java.awt.{ Dimension, Font }
37-
import javax.swing.{ BorderFactory, ImageIcon }
38-
import java.net.URL
37+
import javax.swing.BorderFactory
38+
3939

4040
/**
4141
* Tutorial: How to Use Combo Boxes
@@ -55,10 +55,10 @@ class ComboBoxDemo extends BorderPanel {
5555
}
5656

5757
//Set up the picture label.
58-
val imgIcon = ComboBoxDemo.createImageIcon("/scala/swing/examples/tutorials/images/"
59-
+ petList.selection.item + ".gif")
58+
val imgIcon = ComboBoxDemo.createImageIcon(s"/scala/swing/examples/tutorials/images/${petList.selection.item}.gif")
59+
6060
val picture = new Label() {
61-
icon = if (imgIcon.isDefined) imgIcon.get else Swing.EmptyIcon
61+
icon = imgIcon.getOrElse( Swing.EmptyIcon )
6262
font = font.deriveFont(Font.ITALIC)
6363
horizontalAlignment = Alignment.Center
6464
//The preferred size is hard-coded to be the width of the
@@ -78,25 +78,20 @@ class ComboBoxDemo extends BorderPanel {
7878
}
7979

8080
def updateLabel(s: String): Unit = {
81-
val ic = ComboBoxDemo.createImageIcon("/scala/swing/examples/tutorials/images/" + s + ".gif")
82-
picture.icon = if (ic.isDefined) imgIcon.get else Swing.EmptyIcon
81+
picture.icon =
82+
ComboBoxDemo.createImageIcon("/scala/swing/examples/tutorials/images/" + s + ".gif").getOrElse( Swing.EmptyIcon )
8383
}
8484
}
8585

8686
object ComboBoxDemo extends SimpleSwingApplication {
87-
val birdString = "Bird"
88-
val catString = "Cat"
89-
val dogString = "Dog"
90-
val rabbitString = "Rabbit"
91-
val pigString = "Pig"
9287

93-
/** Returns an ImageIcon, or null if the path was invalid. */
94-
def createImageIcon(path: String): Option[javax.swing.ImageIcon] = {
88+
/** Returns an ImageIcon option, or None if the path was invalid. */
89+
def createImageIcon(path: String ): Option[javax.swing.ImageIcon] =
9590
Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL))
96-
}
91+
9792
lazy val top = new MainFrame() {
9893
title = "ComboBoxDemo"
9994
//Create and set up the content pane.
100-
contents = new ComboBoxDemo();
95+
contents = new ComboBoxDemo()
10196
}
10297
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ import java.awt.Color
3737
import java.util.Date
3838
import java.text.SimpleDateFormat
3939

40+
41+
import scala.util.{Try, Failure, Success}
42+
4043
/**
4144
* Tutorial: How to Use Combo Boxes
4245
* [[http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html]]
4346
*
4447
* Source code reference:
4548
* [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemo2Project/src/components/ComboBoxDemo2.java]]
46-
*
47-
* ComboBoxDemo2.scala requires no other files.
4849
*/
4950
class ComboBoxDemo2 extends BoxPanel(Orientation.Vertical) {
5051
val patternExamples = Array[String](
@@ -85,14 +86,14 @@ class ComboBoxDemo2 extends BoxPanel(Orientation.Vertical) {
8586
val patternPanel = new BoxPanel(Orientation.Vertical) {
8687
contents += patternLabel1
8788
contents += patternLabel2
88-
xLayoutAlignment = 0.0 // Left
89+
xLayoutAlignment = java.awt.Component.LEFT_ALIGNMENT
8990
contents += patternList
9091
}
9192

9293
val resultPanel = new GridPanel(0,1) {
9394
contents += resultLabel
9495
contents += result
95-
xLayoutAlignment = 0.0 // Left
96+
xLayoutAlignment = java.awt.Component.LEFT_ALIGNMENT
9697
}
9798

9899
contents += patternPanel
@@ -102,21 +103,20 @@ class ComboBoxDemo2 extends BoxPanel(Orientation.Vertical) {
102103

103104
listenTo(patternList.selection)
104105
reactions += {
105-
case SelectionChanged(`patternList`) => // currentPattern = patternList.selection.item
106-
reformat()
106+
case SelectionChanged(`patternList`) => reformat()
107107
}
108108

109109
def reformat(): Unit = {
110-
val today = new Date()
111-
val formatter = new SimpleDateFormat(patternList.selection.item)
112-
try {
113-
val dateString = formatter.format(today)
114-
result.foreground = Color.black
115-
result.text = dateString
116-
}
117-
catch {
118-
case iae: IllegalArgumentException => result.foreground = Color.red
119-
result.text = "Error: " + iae.getMessage()
110+
Try {
111+
val today = new Date()
112+
val formatter = new SimpleDateFormat(patternList.selection.item)
113+
formatter.format(today)
114+
} match {
115+
case Success( dateString) =>
116+
result.foreground = Color.black
117+
result.text = dateString
118+
case Failure( err ) =>
119+
result.text = s"Error: ${err.getMessage}"
120120
}
121121
}
122122
}
@@ -125,6 +125,6 @@ object ComboBoxDemo2 extends SimpleSwingApplication {
125125
lazy val top = new MainFrame() {
126126
title = "ComboBoxDemo2"
127127
//Create and set up the content pane.
128-
contents = new ComboBoxDemo2();
128+
contents = new ComboBoxDemo2()
129129
}
130130
}

0 commit comments

Comments
 (0)