diff --git a/.gitignore b/.gitignore index f236bdfa..fa010609 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ build.properties /.cache /.idea /.settings +/examples/.classpath +/examples/.project +scala-swing.iml # bak files produced by ./cleanup-commit *.bak @@ -42,4 +45,4 @@ qbin # Mac specific, but that is common enough a dev platform to warrant inclusion. .DS_Store -target/ \ No newline at end of file +target/ diff --git a/.travis.yml b/.travis.yml index c7ab0d72..0511f96e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: scala script: - - sbt ++$TRAVIS_SCALA_VERSION clean update compile test + - sbt ++$TRAVIS_SCALA_VERSION clean update compile test examples/compile scala: - 2.11.1 diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/TextSamplerDemoHelp.html b/examples/src/main/resources/scala/swing/examples/tutorials/TextSamplerDemoHelp.html new file mode 100644 index 00000000..8f2e779f --- /dev/null +++ b/examples/src/main/resources/scala/swing/examples/tutorials/TextSamplerDemoHelp.html @@ -0,0 +1,32 @@ + + + +Styled Text Sample + +Waving Duke icon +

This is an uneditable EditorPane, +which was initialized +with HTML text from a +URL. +

+An editor pane uses specialized editor kits +to read, write, display, and edit text of +different formats. +The Swing text package includes editor kits +for plain text, HTML, and RTF. +You can also develop +custom editor kits for other formats. + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Back24.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Back24.gif new file mode 100644 index 00000000..787518c8 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Back24.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Bird.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Bird.gif new file mode 100644 index 00000000..b8a05771 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Bird.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Cat.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Cat.gif new file mode 100644 index 00000000..efaa2a37 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Cat.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Dog.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Dog.gif new file mode 100644 index 00000000..e7f030dd Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Dog.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/FD.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/FD.jpg new file mode 100644 index 00000000..0354eb8f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/FD.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Forward24.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Forward24.gif new file mode 100644 index 00000000..1936fd4b Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Forward24.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Open16.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Open16.gif new file mode 100644 index 00000000..fabd5676 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Open16.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Pig.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Pig.gif new file mode 100644 index 00000000..d70eb563 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Pig.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Rabbit.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Rabbit.gif new file mode 100644 index 00000000..99ab7e0c Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Rabbit.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Save16.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Save16.gif new file mode 100644 index 00000000..954f1acc Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Save16.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/Up24.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/Up24.gif new file mode 100644 index 00000000..3db88736 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/Up24.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/blue.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/blue.gif new file mode 100644 index 00000000..baf9248a Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/blue.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T0.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T0.gif new file mode 100644 index 00000000..e916a218 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T0.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T1.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T1.gif new file mode 100644 index 00000000..00a19637 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T1.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T10.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T10.gif new file mode 100644 index 00000000..7d9a840f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T10.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T11.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T11.gif new file mode 100644 index 00000000..37f546b0 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T11.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T12.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T12.gif new file mode 100644 index 00000000..ee03aa57 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T12.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T13.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T13.gif new file mode 100644 index 00000000..860586d1 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T13.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T2.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T2.gif new file mode 100644 index 00000000..8a9d1139 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T2.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T3.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T3.gif new file mode 100644 index 00000000..d0f820ee Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T3.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T4.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T4.gif new file mode 100644 index 00000000..b325b569 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T4.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T5.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T5.gif new file mode 100644 index 00000000..a2a8276d Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T5.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T6.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T6.gif new file mode 100644 index 00000000..6e74a893 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T6.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T7.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T7.gif new file mode 100644 index 00000000..845cf036 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T7.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T8.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T8.gif new file mode 100644 index 00000000..25d28ca5 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T8.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T9.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T9.gif new file mode 100644 index 00000000..d1dd8668 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/doggy/T9.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/dukeWaveRed.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/dukeWaveRed.gif new file mode 100644 index 00000000..283ef53c Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/dukeWaveRed.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/flyingBee.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/flyingBee.jpg new file mode 100644 index 00000000..7a06978e Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/flyingBee.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek-cght.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek-cght.gif new file mode 100644 index 00000000..4e129925 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek-cght.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-----.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-----.gif new file mode 100644 index 00000000..d992b176 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-----.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek----t.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek----t.gif new file mode 100644 index 00000000..72d0b8e4 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek----t.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---h-.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---h-.gif new file mode 100644 index 00000000..49a6381d Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---h-.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---ht.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---ht.gif new file mode 100644 index 00000000..b86abad1 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek---ht.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g--.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g--.gif new file mode 100644 index 00000000..53b26992 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g--.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g-t.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g-t.gif new file mode 100644 index 00000000..cf03dc61 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--g-t.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--gh-.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--gh-.gif new file mode 100644 index 00000000..643de8f0 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--gh-.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--ght.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--ght.gif new file mode 100644 index 00000000..c5074392 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek--ght.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c---.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c---.gif new file mode 100644 index 00000000..3a79a296 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c---.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c--t.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c--t.gif new file mode 100644 index 00000000..81ec13d3 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c--t.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-h-.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-h-.gif new file mode 100644 index 00000000..e5aca555 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-h-.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-ht.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-ht.gif new file mode 100644 index 00000000..deb2222f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-c-ht.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg--.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg--.gif new file mode 100644 index 00000000..ff352f62 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg--.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg-t.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg-t.gif new file mode 100644 index 00000000..91f74243 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cg-t.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cgh-.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cgh-.gif new file mode 100644 index 00000000..5b07badb Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cgh-.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cght.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cght.gif new file mode 100644 index 00000000..4e129925 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/geek/geek-cght.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/gifIcon.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/gifIcon.gif new file mode 100644 index 00000000..915e184a Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/gifIcon.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/green.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/green.gif new file mode 100644 index 00000000..4fc0ba2f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/green.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/jpgIcon.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/jpgIcon.gif new file mode 100644 index 00000000..7294f360 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/jpgIcon.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/kathyCosmo.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/kathyCosmo.gif new file mode 100644 index 00000000..104e9e8f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/kathyCosmo.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/lainesTongue.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/lainesTongue.gif new file mode 100644 index 00000000..d3984a3b Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/lainesTongue.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/left.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/left.gif new file mode 100644 index 00000000..279e6710 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/left.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/middle.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/middle.gif new file mode 100644 index 00000000..fbdcf575 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/middle.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/pngIcon.png b/examples/src/main/resources/scala/swing/examples/tutorials/images/pngIcon.png new file mode 100644 index 00000000..bd2fd547 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/pngIcon.png differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/red.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/red.gif new file mode 100644 index 00000000..8f4dda4a Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/red.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/right.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/right.gif new file mode 100644 index 00000000..b256e5f7 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/right.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/scala-stairs.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/scala-stairs.jpg new file mode 100644 index 00000000..5d6e3f78 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/scala-stairs.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sound.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/sound.gif new file mode 100644 index 00000000..8efb49f5 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sound.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/stickerface.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/stickerface.gif new file mode 100644 index 00000000..b3d29955 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/stickerface.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw01.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw01.jpg new file mode 100644 index 00000000..c41c2cb2 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw01.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw02.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw02.jpg new file mode 100644 index 00000000..2addc1ad Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw02.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw03.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw03.jpg new file mode 100644 index 00000000..d93e11d2 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw03.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw04.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw04.jpg new file mode 100644 index 00000000..5a7b4797 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw04.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw05.jpg b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw05.jpg new file mode 100644 index 00000000..56633634 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/sunw05.jpg differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tiffIcon.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tiffIcon.gif new file mode 100644 index 00000000..84b41328 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tiffIcon.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T1.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T1.gif new file mode 100644 index 00000000..efa6741f Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T1.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T10.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T10.gif new file mode 100644 index 00000000..ce92fe75 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T10.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T11.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T11.gif new file mode 100644 index 00000000..d4aed158 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T11.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T12.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T12.gif new file mode 100644 index 00000000..a7e9b9cb Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T12.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T13.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T13.gif new file mode 100644 index 00000000..d574e7aa Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T13.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T14.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T14.gif new file mode 100644 index 00000000..6b0e4c7b Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T14.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T15.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T15.gif new file mode 100644 index 00000000..57f5a5d5 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T15.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T16.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T16.gif new file mode 100644 index 00000000..d7ea7ecc Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T16.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T17.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T17.gif new file mode 100644 index 00000000..926668e7 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T17.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T2.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T2.gif new file mode 100644 index 00000000..0e2a5ed7 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T2.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T3.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T3.gif new file mode 100644 index 00000000..4252148c Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T3.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T4.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T4.gif new file mode 100644 index 00000000..f7ebea23 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T4.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T5.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T5.gif new file mode 100644 index 00000000..eb50274c Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T5.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T6.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T6.gif new file mode 100644 index 00000000..0bb3e64d Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T6.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T7.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T7.gif new file mode 100644 index 00000000..ef7b9552 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T7.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T8.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T8.gif new file mode 100644 index 00000000..8e518ad1 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T8.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T9.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T9.gif new file mode 100644 index 00000000..7a104bfd Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/tumble/T9.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/wavy.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/wavy.gif new file mode 100644 index 00000000..c9be4659 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/wavy.gif differ diff --git a/examples/src/main/resources/scala/swing/examples/tutorials/images/yellow.gif b/examples/src/main/resources/scala/swing/examples/tutorials/images/yellow.gif new file mode 100644 index 00000000..3b0b1fa0 Binary files /dev/null and b/examples/src/main/resources/scala/swing/examples/tutorials/images/yellow.gif differ diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/BorderDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/BorderDemo.scala new file mode 100644 index 00000000..47fbae37 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/BorderDemo.scala @@ -0,0 +1,160 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import java.awt.Color + +/** + * Tutorial: How to Use Borders + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/border.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/BorderDemoProject/src/components/BorderDemo.java]] + * + * BorderDemo.scala requires the following file: + * scala/swing/examples/tutorials/images/wavy.gif + */ +class BorderDemo extends GridPanel(1, 0) { + + val paneEdge = Swing.EmptyBorder(0, 10, 10, 10) + val blackline = Swing.LineBorder(Color.black) + val raisedetched = Swing.EtchedBorder(Swing.Raised) + val loweredetched = Swing.EtchedBorder(Swing.Lowered) + val raisedbevel = Swing.BeveledBorder(Swing.Raised) + val loweredbevel = Swing.BeveledBorder(Swing.Lowered) + val empty = Swing.EmptyBorder + + + def addCompForBorder(bord: javax.swing.border.Border, description: String): Seq[Component] = + List(Swing.RigidBox(new Dimension(0, 10)), new GridPanel(1, 1) { + contents += new Label(description, Swing.EmptyIcon, Alignment.Center) + border = bord + }) + + //First pane: simple borders + val simpleBorders = new BoxPanel(Orientation.Vertical) { + contents ++= + addCompForBorder(blackline, "line border") ++ + addCompForBorder(raisedetched, "raised etched border") ++ + addCompForBorder(loweredetched, "lowered etched border") ++ + addCompForBorder(raisedbevel, "raised bevel border") ++ + addCompForBorder(loweredbevel, "lowered bevel border") ++ + addCompForBorder(empty, "empty border") + } + + //Second pane: matte borders + val matteBorders = new BoxPanel(Orientation.Vertical) { + border = paneEdge + + val icon = BorderDemo.createImageIcon("/scala/swing/examples/tutorials/images/wavy.gif", "wavy-line border icon") + + contents ++= + addCompForBorder( + Swing.MatteBorder(-1, -1, -1, -1, icon.getOrElse(null.asInstanceOf[javax.swing.Icon])), + "matte border (-1,-1,-1,-1,icon)") ++ + addCompForBorder( + Swing.MatteBorder(1, 5, 1, 1, Color.red), + "matte border (1,5,1,1,Color.red)") ++ + addCompForBorder( + Swing.MatteBorder(0, 20, 0, 0, icon.getOrElse(null.asInstanceOf[javax.swing.Icon])), + "matte border (0,20,0,0,icon)") + } + + //Third pane: titled borders + val titledBorders = new BoxPanel(Orientation.Vertical) { + border = paneEdge + + def titleBorder(border: javax.swing.border.Border, justification: Int, position: Int): javax.swing.border.Border = { + val titled = Swing.TitledBorder(border, "title") + titled.setTitleJustification(justification) + titled.setTitlePosition(position) + titled + } + + contents ++= + addCompForBorder( + titleBorder(blackline, javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION), + "titled line border (centered, default pos.)") ++ + addCompForBorder( + titleBorder(loweredetched, javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION), + "titled lowered etched border (right just., default pos.)") ++ + addCompForBorder( + titleBorder(loweredbevel, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP), + "titled lowered bevel border (default just., above top)") ++ + addCompForBorder( + titleBorder(empty, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.BOTTOM), + "titled empty border (default just., bottom)") + } + + //Fourth pane: compound borders + val compoundBorders = new BoxPanel(Orientation.Vertical) { + border = paneEdge + + val titledCompound = Swing.TitledBorder(Swing.CompoundBorder(Swing.LineBorder(Color.red), Swing.CompoundBorder(Swing.BeveledBorder(Swing.Raised), Swing.BeveledBorder(Swing.Lowered))), "title") + titledCompound.setTitleJustification(javax.swing.border.TitledBorder.CENTER) + titledCompound.setTitlePosition(javax.swing.border.TitledBorder.BELOW_BOTTOM) + + contents ++= + addCompForBorder( + Swing.CompoundBorder(raisedbevel, loweredbevel), + "compound border (two bevels)") ++ + addCompForBorder( + Swing.CompoundBorder(Swing.LineBorder(Color.red), Swing.CompoundBorder(raisedbevel, loweredbevel)), + "compound border (add a red outline)") ++ + addCompForBorder( + titledCompound, + "titled compound border (centered, below bottom)") + } + + + contents += new TabbedPane() { + tooltip = "Blue Wavy Line border art crew:
   Bill Pauley
   Cris St. Aubyn
   Ben Wronsky
   Nathan Walrath
   Tommy Adams, special consultant" + + pages += new TabbedPane.Page("Simple", simpleBorders) + pages += new TabbedPane.Page("Matte", matteBorders) + pages += new TabbedPane.Page("Titled", titledBorders) + pages += new TabbedPane.Page("Compound", compoundBorders) + + selection.index = 0 + } +} + +object BorderDemo extends SimpleSwingApplication { + def createImageIcon(path: String, description: String): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new MainFrame() { + title = "BorderDemo" + //Create and set up the content pane. + contents = new BorderDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonDemo.scala new file mode 100644 index 00000000..a480b761 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonDemo.scala @@ -0,0 +1,111 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + *   - Redistributions of source code must retain the above copyright + *     notice, this list of conditions and the following disclaimer. + * + *   - Redistributions in binary form must reproduce the above copyright + *     notice, this list of conditions and the following disclaimer in the + *     documentation and/or other materials provided with the distribution. + * + *   - Neither the name of Oracle or the names of its + *     contributors may be used to endorse or promote products derived + *     from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.ButtonClicked + +/** + * Tutorial: How to Use Buttons, Check Boxes, and Radio Buttons + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/button.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ButtonDemoProject/src/components/ButtonDemo.java]] + * + * ButtonDemo.scala requires the following files: + * /scala/swing/examples/tutorials/images/right.gif + * /scala/swing/examples/tutorials/images/middle.gif + * /scala/swing/examples/tutorials/images/left.gif + */ +class ButtonDemo extends FlowPanel { + val leftButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/right.gif") + val middleButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/middle.gif") + val rightButtonIcon = ButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/left.gif") + + val disable: Button = new Button("Disable middle button") { + leftButtonIcon.foreach( icon = _) + verticalTextPosition = Alignment.Center + horizontalTextPosition = Alignment.Leading + mnemonic = event.Key.D + tooltip = "Click this button to disable the middle button." + } + + val middle: Button = new Button("Middle button") { + middleButtonIcon.foreach( icon = _ ) + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + mnemonic = event.Key.M + tooltip = "This middle button does nothing when you click it." + } + + val enable: Button = new Button("Enable middle button") { + rightButtonIcon.foreach( icon = _ ) + enabled = false + mnemonic = event.Key.E + tooltip = "Click this button to enable the middle button." + } + + contents += disable + contents += middle + contents += enable + + listenTo(disable) + listenTo(enable) + + reactions += { + case ButtonClicked(`enable`) => enableMiddle() + case ButtonClicked(`disable`) => disableMiddle() + } + + def enableMiddle(): Unit = { + enable.enabled = false + middle.enabled = true + disable.enabled = true + } + + def disableMiddle(): Unit = { + enable.enabled = true + middle.enabled = false + disable.enabled = false + } +} + +object ButtonDemo extends SimpleSwingApplication { + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new MainFrame() { + title = "ButtonDemo" + javax.swing.UIManager.put("swing.boldMetal", false) + + contents = new ButtonDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonHtmlDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonHtmlDemo.scala new file mode 100644 index 00000000..cfe31595 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ButtonHtmlDemo.scala @@ -0,0 +1,123 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + *   - Redistributions of source code must retain the above copyright + *     notice, this list of conditions and the following disclaimer. + * + *   - Redistributions in binary form must reproduce the above copyright + *     notice, this list of conditions and the following disclaimer in the + *     documentation and/or other materials provided with the distribution. + * + *   - Neither the name of Oracle or the names of its + *     contributors may be used to endorse or promote products derived + *     from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.ButtonClicked +import java.awt.Font + +/* + * Tutorial: How to Use Buttons, Check Boxes, and Radio Buttons + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/button.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ButtonHtmlDemoProject/src/components/ButtonHtmlDemo.java]] + * + * ButtonHtmlDemo.scala requires the following files: + * /scala/swing/examples/tutorials/images/right.gif + * /scala/swing/examples/tutorials/images/middle.gif + * /scala/swing/examples/tutorials/images/left.gif + */ +class ButtonHtmlDemo extends FlowPanel { + val leftButtonIcon = createImageIcon("/scala/swing/examples/tutorials/images/right.gif").get + val middleButtonIcon = createImageIcon("/scala/swing/examples/tutorials/images/middle.gif").get + val rightButtonIcon = createImageIcon("/scala/swing/examples/tutorials/images/left.gif").get + // + val disable: Button = new Button("
Disable
" + + "middle button") { + icon = leftButtonIcon + verticalTextPosition = Alignment.Center + horizontalTextPosition = Alignment.Leading + mnemonic = event.Key.D + tooltip = "Click this button to disable the middle button." + font = font.deriveFont(Font.PLAIN) + } + disable.font = font + // + val middle: Button = new Button("Middle button") { + icon = middleButtonIcon + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + mnemonic = event.Key.M + tooltip = "This middle button does nothing when you click it." + font = disable.font + } + // + val enable: Button = new Button("
Enable
" + + "middle button") { + icon = rightButtonIcon + enabled = false + mnemonic = event.Key.E + tooltip = "Click this button to enable the middle button." + font = disable.font + } + + contents += disable + contents += middle + contents += enable + + listenTo(disable) + listenTo(enable) + + reactions += { + case ButtonClicked(`enable`) => enableMiddle + case ButtonClicked(`disable`) => disableMiddle + } + + def enableMiddle(): Unit = { + enable.enabled = false + middle.enabled = true + disable.enabled = true + } + def disableMiddle(): Unit = { + enable.enabled = true + middle.enabled = false + disable.enabled = false + } + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = { + val imgURL: java.net.URL = getClass().getResource(path) + val img: Option[javax.swing.ImageIcon] = if (imgURL != null) { + Some(Swing.Icon(imgURL)) + } else { + None + } + img + } + +} + +object ButtonHtmlDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "ButtonHtmlDemo" + //Create and set up the content pane. + contents = new ButtonHtmlDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/CheckboxDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/CheckboxDemo.scala new file mode 100644 index 00000000..cae5f182 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/CheckboxDemo.scala @@ -0,0 +1,170 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.ButtonClicked +import scala.swing.event.Key +import java.awt.Font + + +/** + * Tutorial: How to Use Buttons, Check Boxes, and Radio Buttons + * http://docs.oracle.com/javase/tutorial/uiswing/components/button.html + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/CheckBoxDemoProject/src/components/CheckBoxDemo.java]] + * + * CheckBoxDemo.scala requires 16 image files in the /scala/swing/examples/tutorials/images/geek + * directory: + * geek-----.gif, geek-c---.gif, geek--g--.gif, geek---h-.gif, geek----t.gif, + * geek-cg--.gif, ..., geek-cght.gif. + */ +class CheckboxDemo extends BorderPanel { + /** + * Four accessory choices provide for 16 different + * combinations. The image for each combination is + * contained in a separate image file whose name indicates + * the accessories. The filenames are "geek-XXXX.gif" + * where XXXX can be one of the following 16 choices. + * The "choices" StringBuffer contains the string that + * indicates the current selection and is used to generate + * the file name of the image to display. + + ---- //zero accessories + + c--- //one accessory + -g-- + --h- + ---t + + cg-- //two accessories + c-h- + c--t + -gh- + -g-t + --ht + + -ght //three accessories + c-ht + cg-t + cgh- + + cght //all accessories + */ + + //Create the check boxes. + val chinButton = new CheckBox("Chin") { + mnemonic = Key.C // .VK_C + selected = true + } + + val glassesButton = new CheckBox("Glasses") { + mnemonic = Key.G + selected = true + } + + val hairButton = new CheckBox("Hair") { + mnemonic = Key.H + selected = true + } + + val teethButton = new CheckBox("Teeth") { + mnemonic = Key.T + selected = true + } + + //Indicates what's on the geek. + val choices: StringBuffer = new StringBuffer("cght") + + //Set up the picture label + val pictureLabel = new Label() + pictureLabel.font = pictureLabel.font.deriveFont(Font.ITALIC) + updatePicture() + + //Put the check boxes in a column in a panel + val checkPanel: GridPanel = new GridPanel(0, 1) { + contents += chinButton + contents += glassesButton + contents += hairButton + contents += teethButton + } + + layout(checkPanel) = BorderPanel.Position.East + layout(pictureLabel) = BorderPanel.Position.Center + border = Swing.EmptyBorder(20, 20, 20, 20) + + //Register a listener for the check boxes. + listenTo(chinButton) + listenTo(glassesButton) + listenTo(hairButton) + listenTo(teethButton) + + + def changeState( idx:Int, checkBox: CheckBox, label:Char ): Unit = { + choices.setCharAt(idx, if (checkBox.selected) label else '-' ) + updatePicture() + } + + reactions += { + case ButtonClicked(`chinButton`) => changeState(0, chinButton, 'c') + case ButtonClicked(`glassesButton`) => changeState(1, glassesButton, 'g') + case ButtonClicked(`hairButton`) => changeState(2, hairButton, 'h') + case ButtonClicked(`teethButton`) => changeState(3, teethButton, 't') + } + + def updatePicture(): Unit = { + pictureLabel.tooltip = choices.toString + + //Get the icon corresponding to the image. + CheckboxDemo.createImageIcon( s"/scala/swing/examples/tutorials/images/geek/geek-$choices.gif") match { + case Some( icon ) => + pictureLabel.icon = icon + pictureLabel.text = null + case None => + pictureLabel.icon = Swing.EmptyIcon + pictureLabel.text = "Missing Image" + + } + } +} + +object CheckboxDemo extends SimpleSwingApplication { + /** Returns an ImageIcon, or None if the path was invalid. */ + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = { + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + } + lazy val top = new MainFrame() { + title = "CheckboxDemo" + //Create and set up the content pane. + contents = new CheckboxDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ColorChooserDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ColorChooserDemo.scala new file mode 100644 index 00000000..ca1b9091 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ColorChooserDemo.scala @@ -0,0 +1,78 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.Swing.EmptyIcon +import scala.swing.event.ColorChanged +import java.awt.{ Color, Dimension, Font } + +/** + * Tutorial: How to Use Color Choosers + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ColorChooserDemoProject/src/components/ColorChooserDemo.java]] + */ +class ColorChooserDemo extends BorderPanel { + //Set up the banner at the top of the window + val banner = new Label("Welcome to the Scala-Swing Zone!", EmptyIcon, Alignment.Center) { + foreground = Color.yellow + background = Color.blue + opaque = true + font = new Font("SansSerif", Font.BOLD, 24) + preferredSize = new Dimension(100, 65) + } + + val bannerPanel = new BorderPanel() { + layout(banner) = BorderPanel.Position.Center + border = Swing.TitledBorder(Swing.EmptyBorder, "Banner") + } + + //Set up color chooser for setting text color + val tcc: ColorChooser = new ColorChooser(banner.foreground) { + border = Swing.TitledBorder(Swing.EmptyBorder, "Choose Text Color") + reactions += { + case ColorChanged(_, c) => banner.foreground = c + } + } + + layout(bannerPanel) = BorderPanel.Position.Center + layout(tcc) = BorderPanel.Position.South +} + +object ColorChooserDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "ColorChooserDemo" + //Create and set up the content pane. + contents = new ColorChooserDemo() + } +} diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo.scala new file mode 100644 index 00000000..c24e24bb --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo.scala @@ -0,0 +1,96 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.SelectionChanged +import java.awt.{ Dimension, Font } +import javax.swing.BorderFactory + +/* + * Tutorial: How to Use Combo Boxes + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html]] + * + * Source code reference: + * http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java + */ +class ComboBoxDemo extends BorderPanel { + + val petStrings: Array[String] = Array("Bird", "Cat", "Dog", "Rabbit", "Pig") + + //Create the combo box, select the item at index 4. + //Indices start at 0, so 4 specifies the pig. + val petList = new ComboBox[String](petStrings) { + selection.item = petStrings(4) + } + + //Set up the picture label. + val imgIcon = ComboBoxDemo.createImageIcon(s"/scala/swing/examples/tutorials/images/${petList.selection.item}.gif") + + val picture = new Label() { + icon = imgIcon.getOrElse( Swing.EmptyIcon ) + font = font.deriveFont(Font.ITALIC) + horizontalAlignment = Alignment.Center + //The preferred size is hard-coded to be the width of the + //widest image and the height of the tallest image + the border. + //A real program would compute this. + preferredSize = new Dimension(177, 122 + 10) + } + updateLabel(petList.selection.item) + + layout(petList) = BorderPanel.Position.East + layout(picture) = BorderPanel.Position.Center + border = BorderFactory.createEmptyBorder(20, 20, 20, 20) + + listenTo(petList.selection) + reactions += { + case SelectionChanged(`petList`) => updateLabel(petList.selection.item) + } + + def updateLabel(s: String): Unit = { + val ic = ComboBoxDemo.createImageIcon(s"/scala/swing/examples/tutorials/images/$s.gif") + picture.icon = imgIcon.getOrElse( Swing.EmptyIcon ) + } +} + +object ComboBoxDemo extends SimpleSwingApplication { + + /** Returns an ImageIcon, or null if the path was invalid. */ + def createImageIcon(path: String ): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new MainFrame() { + title = "ComboBoxDemo" + //Create and set up the content pane. + contents = new ComboBoxDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo2.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo2.scala new file mode 100644 index 00000000..da38684b --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ComboBoxDemo2.scala @@ -0,0 +1,129 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.SelectionChanged +import java.awt.Color +import java.util.Date +import java.text.SimpleDateFormat + + +import scala.util.{Try, Failure, Success} + +/** + * Tutorial: How to Use Combo Boxes + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemo2Project/src/components/ComboBoxDemo2.java]] + */ +class ComboBoxDemo2 extends BoxPanel(Orientation.Vertical) { + val patternExamples = Array[String]( + "dd MMMMM yyyy", + "dd.MM.yy", + "MM/dd/yy", + "yyyy.MM.dd G 'at' hh:mm:ss z", + "EEE, MMM d, ''yy", + "h:mm a", + "H:mm:ss:SSS", + "K:mm a,z", + "yyyy.MMMMM.dd GGG hh:mm aaa") + + + //Set up the UI for selecting a pattern. + val patternLabel1 = new Label("Enter the pattern string or") + val patternLabel2 = new Label("select one from the list:") + val patternList = new ComboBox[String](patternExamples) { + makeEditable() + selection.item = patternExamples(0) + } + + //Create the UI for displaying result. + val resultLabel = new Label("Current Date/Time", Swing.EmptyIcon, Alignment.Left ) { + foreground = Color.black + border = Swing.EmptyBorder(5, 5, 5, 5) + } + + val result = new Label(" ") { + foreground = Color.black + border = Swing.CompoundBorder( + Swing.LineBorder(Color.black), + Swing.EmptyBorder(5,5,5,5) + ) + } + + val patternPanel = new BoxPanel(Orientation.Vertical) { + contents += patternLabel1 + contents += patternLabel2 + xLayoutAlignment = java.awt.Component.LEFT_ALIGNMENT + contents += patternList + } + + val resultPanel = new GridPanel(0,1) { + contents += resultLabel + contents += result + xLayoutAlignment = java.awt.Component.LEFT_ALIGNMENT + } + + contents += patternPanel + contents += resultPanel + border = Swing.EmptyBorder(10,10,10,10) + reformat() + + listenTo(patternList.selection) + reactions += { + case SelectionChanged(`patternList`) => reformat() + } + + def reformat(): Unit = { + Try { + val today = new Date() + val formatter = new SimpleDateFormat(patternList.selection.item) + formatter.format(today) + } match { + case Success( dateString) => + result.foreground = Color.black + result.text = dateString + case Failure( err ) => + result.text = s"Error: ${err.getMessage}" + } + } +} + +object ComboBoxDemo2 extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "ComboBoxDemo2" + //Create and set up the content pane. + contents = new ComboBoxDemo2() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/DocumentSizeFilter.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/DocumentSizeFilter.scala new file mode 100644 index 00000000..54d5a54e --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/DocumentSizeFilter.scala @@ -0,0 +1,68 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import java.awt.Toolkit +import javax.swing.text.{ AttributeSet, DocumentFilter } + +/* A 1.4 class used by TextComponentDemo.scala. */ + +class DocumentSizeFilter(val maxCharacters: Int) extends DocumentFilter { + val Debug: Boolean = false + + override def insertString(fb: DocumentFilter.FilterBypass, offs: Int, str: String, a: AttributeSet): Unit = { + //This rejects the entire insertion if it would make + //the contents too long. Another option would be + //to truncate the inserted string so the contents + //would be exactly maxCharacters in length. + if ((fb.getDocument.getLength + str.length) <= maxCharacters) { + super.insertString(fb, offs, str, a) + } else { + Toolkit.getDefaultToolkit.beep() + } + } + + def replaceString(fb: DocumentFilter.FilterBypass, offs: Int, length: Int, str: String, a: AttributeSet): Unit = { + if (Debug) { + println("in DocumentSizeFilter's replace method") + } + //This rejects the entire replacement if it would make + //the contents too long. Another option would be + //to truncate the replacement string so the contents + //would be exactly maxCharacters in length. + if ((fb.getDocument.getLength + str.length - length) <= maxCharacters) { + super.replace(fb, offs, length, str, a) + } else { + Toolkit.getDefaultToolkit.beep() + } + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/FrameDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/FrameDemo.scala new file mode 100644 index 00000000..b0d5ff89 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/FrameDemo.scala @@ -0,0 +1,59 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import java.awt.Dimension + +/** + * Tutorial: How to Make Frames (Main Windows) + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java]] + * + * FrameDemo.scala requires no other files. + */ +object FrameDemo extends SimpleSwingApplication { + /** + * Create the GUI and show it. + */ + lazy val top = new MainFrame() { + title = "FrameDemo" + val emptylabel = new Label("") { + preferredSize = new Dimension(175, 100) + } + //Create and set up the content pane. + contents = new BorderPanel() { + layout(emptylabel) = BorderPanel.Position.Center + } + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/HtmlDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/HtmlDemo.scala new file mode 100644 index 00000000..def31c4e --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/HtmlDemo.scala @@ -0,0 +1,105 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.ButtonClicked +import java.awt.{ Component, Dimension } +import scala.swing.event.Key + +/** + * Tutorial: How to Use HTML in Swing Components + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/html.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/HtmlDemoProject/src/components/HtmlDemo.java]] + * + * HtmlDemo.scala needs no other files. + */ +class HtmlDemo extends BoxPanel(Orientation.Horizontal) { + val initialText = "\n" + + "Color and font test:\n" + + "\n" + val htmlTextArea = new TextArea(10, 20) { + text = initialText + } + val scrollPane = new ScrollPane(htmlTextArea) + val changeTheLabel = new Button("Change the label") { + mnemonic = Key.C + xLayoutAlignment = Component.CENTER_ALIGNMENT + } + val theLabel = new Label(initialText) { + minimumSize = new Dimension(200, 200) + preferredSize = new Dimension(200, 200) + maximumSize = new Dimension(200, 200) + verticalAlignment = Alignment.Center + horizontalAlignment = Alignment.Center + } + + val leftPanel = new BoxPanel(Orientation.Vertical) { + border = Swing.CompoundBorder( + Swing.TitledBorder(Swing.EmptyBorder, "Edit the HTML, then click the button"), + Swing.EmptyBorder(10, 10, 10, 10)) + contents ++= Array( scrollPane, Swing.RigidBox(new Dimension(0, 10)), changeTheLabel ) + } + + val rightPanel = new BoxPanel(Orientation.Vertical) { + border = Swing.CompoundBorder( + Swing.TitledBorder(Swing.EmptyBorder, "A label with HTML"), + Swing.EmptyBorder(10, 10, 10, 10)) + contents += theLabel + } + + border = Swing.EmptyBorder(10, 10, 10, 10) + contents += leftPanel + contents += rightPanel + + listenTo(changeTheLabel) + reactions += { + case ButtonClicked(`changeTheLabel`) => theLabel.text = htmlTextArea.text + } +} + +object HtmlDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "HtmlDemo" + //Create and set up the content pane. + contents = new HtmlDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/IconDemoApp.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/IconDemoApp.scala new file mode 100644 index 00000000..47ca7803 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/IconDemoApp.scala @@ -0,0 +1,183 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import java.awt.RenderingHints +import java.awt.image.BufferedImage +import javax.swing.{ Icon, ImageIcon } + +/** + * Tutorial: How to Use Icons + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html]] + * + * Source code references: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java]] + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/MissingIcon.java]] + * + * IconDemoApp.scala + * MissingIcon.scala + */ +/** + * This application is intended to demonstrate the loading of image files into icons + * for use in a Swing user interface. It creates a toolbar with a thumbnail preview + * of each image. Clicking on the thumbnail will show the full image + * in the main display area. + * + * IconDemoApp.scala requires the following files:
+ * The following files are copyright 2006 spriggs.net and licensed under a + * Creative Commons License (http://creativecommons.org/licenses/by-sa/3.0/) + *
+ * /scala/swing/examples/tutorials/images/sunw01.jpg
+ * /scala/swing/examples/tutorials/images/sunw02.jpg
+ * images/sunw03.jpg
+ * /scala/swing/examples/tutorials/images/sunw04.jpg
+ * /scala/swing/examples/tutorials/images/sunw05.jpg
+ * + * @author Collin Fagan + * @version 2.0 + */ +class IconDemoApp extends MainFrame { + + private val imagedir = "/scala/swing/examples/tutorials/images/" + private val placeholderIcon = new MissingIcon() + + /** + * List of all the descriptions of the image files. These correspond one to + * one with the image file names + */ + private val imageFileNames = Array[String]( "sunw01.jpg", "sunw02.jpg", + "sunw03.jpg", "sunw04.jpg", "sunw05.jpg") + + private val imageCaptions = Array[String]("Original SUNW Logo", "The Clocktower", + "Clocktower from the West", "The Mansion", "Sun Auditorium") + + // A label for displaying the pictures + private val photographLabel = new Label() { + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + horizontalAlignment = Alignment.Center + border = Swing.EmptyBorder(5, 5, 5, 5) + } + + // We add two glue components. Later in process() we will add thumbnail buttons + // to the toolbar in between these glue components. This will center the + // buttons in the toolbar. + private val buttonBar = new ToolBar() { + contents += Swing.Glue + contents += Swing.Glue + } + contents = new BorderPanel() { + layout(buttonBar) = BorderPanel.Position.South + layout(photographLabel) = BorderPanel.Position.Center + } + size = new Dimension(400, 300) + + // this centers the frame on the screen + // setLocationRelativeTo(null) + + def loadImage( fileName:String, caption: String ): Option[ThumbnailAction] = { + IconDemoApp.createImageIcon(imagedir + fileName) match { + case Some(img) => + val thumbnailIcon = new ImageIcon(getScaledImage(img.getImage, 32, 32)) + Some(new ThumbnailAction(img, thumbnailIcon, caption, photographLabel)) + case None => + Some(new ThumbnailAction(placeholderIcon, placeholderIcon, caption, photographLabel)) + } + } + + import scala.concurrent._ + import scala.concurrent.ExecutionContext.Implicits.global + + val f:Future[List[Option[ThumbnailAction]]] = Future { + imageCaptions.zip(imageFileNames).map { + case (cap, file) => loadImage(file, cap) + }.toList + } + + f.onSuccess{ + case thumbs:List[Option[ThumbnailAction]] => + buttonBar.contents.dropRight(1) + thumbs.foreach{ thumbAction => { + thumbAction.foreach { ta => + buttonBar.contents += new Button(ta) + } + }} + buttonBar.contents += Swing.Glue + + } + + + /** + * Resizes an image using a Graphics2D object backed by a BufferedImage. + * @param srcImg - source image to scale + * @param w - desired width + * @param h - desired height + * @return - the new resized image + */ + private def getScaledImage(srcImg: Image, w: Int, h: Int): Image = { + val resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB) + val g2: Graphics2D = resizedImg.createGraphics() + g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) + g2.drawImage(srcImg, 0, 0, w, h, null) + g2.dispose() + resizedImg + } +} + +object IconDemoApp extends SimpleSwingApplication { + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new IconDemoApp() { + title = "Icon Demo: Please Select an Image" + } +} + +/** + * @param photo - The full size photo to show in the button. + * @param thumb - The thumbnail to show in the button. + * @param desc - The description of the icon. + */ +class ThumbnailAction(photo: Icon, thumb: Icon, desc: String, photographLabel:Label) extends Action("") { + // icon when an Action is applied to a butt + toolTip = desc + + // The short description becomes the tooltip of a button. + icon = thumb + + def apply(): Unit = { + photographLabel.icon = photo + IconDemoApp.top.title = s"Icon Demo: $desc" + } +} + + diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/IntegerEditor.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/IntegerEditor.scala new file mode 100644 index 00000000..d6859044 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/IntegerEditor.scala @@ -0,0 +1,158 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + *   - Redistributions of source code must retain the above copyright + *     notice, this list of conditions and the following disclaimer. + * + *   - Redistributions in binary form must reproduce the above copyright + *     notice, this list of conditions and the following disclaimer in the + *     documentation and/or other materials provided with the distribution. + * + *   - Neither the name of Oracle or the names of its + *     contributors may be used to endorse or promote products derived + *     from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import javax.swing.AbstractAction +import javax.swing.DefaultCellEditor +import javax.swing.JFormattedTextField +import javax.swing.JOptionPane +import javax.swing.JTable +import javax.swing.KeyStroke +import javax.swing.SwingConstants +import javax.swing.SwingUtilities +import java.awt.event.ActionEvent +import java.awt.event.KeyEvent +import java.awt.Component +import java.awt.Toolkit +import java.text.NumberFormat +import java.text.ParseException +import javax.swing.text.DefaultFormatterFactory +import javax.swing.text.NumberFormatter + +@SerialVersionUID(1L) +class IntegerEditor(min: Int, max: Int) extends DefaultCellEditor(new JFormattedTextField()) { + + var ftf: JFormattedTextField = getComponent.asInstanceOf[JFormattedTextField] + + var integerFormat: NumberFormat = NumberFormat.getIntegerInstance + + private var minimum: java.lang.Integer = new java.lang.Integer(min) + + private var maximum: java.lang.Integer = new java.lang.Integer(max) + + private var DEBUG: Boolean = false + + val intFormatter = new NumberFormatter(integerFormat) + + intFormatter.setFormat(integerFormat) + + intFormatter.setMinimum(minimum) + + intFormatter.setMaximum(maximum) + + ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter)) + + ftf.setValue(minimum) + + ftf.setHorizontalAlignment(SwingConstants.TRAILING) + + ftf.setFocusLostBehavior(JFormattedTextField.PERSIST) + + ftf.getInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check") + + ftf.getActionMap.put("check", new AbstractAction() { + + def actionPerformed(e: ActionEvent) { + if (!ftf.isEditValid) { + if (userSaysRevert()) { + ftf.postActionEvent() + } + } else try { + ftf.commitEdit() + ftf.postActionEvent() + } catch { + case exc: java.text.ParseException => + } + } + }) + + override def getTableCellEditorComponent(table: JTable, value: AnyRef, isSelected: Boolean, row: Int, column: Int): Component = { + val ftf = super.getTableCellEditorComponent(table, value, isSelected, row, column).asInstanceOf[JFormattedTextField] + ftf.setValue(value) + ftf + } + + override def getCellEditorValue(): AnyRef = { + val ftf = getComponent.asInstanceOf[JFormattedTextField] + val o = ftf.getValue + if (o.isInstanceOf[java.lang.Integer]) { + o + } else if (o.isInstanceOf[Number]) { + new java.lang.Integer(o.asInstanceOf[Number].intValue()) + } else { + if (DEBUG) { + println("getCellEditorValue: o isn't a Number") + } + try { + integerFormat.parseObject(o.toString) + } catch { + case exc: ParseException => { + System.err.println("getCellEditorValue: can't parse o: " + o) + null + } + } + } + } + + override def stopCellEditing(): Boolean = { + val ftf = getComponent.asInstanceOf[JFormattedTextField] + if (ftf.isEditValid) { + try { + ftf.commitEdit() + } catch { + case exc: java.text.ParseException => + } + } else { + if (!userSaysRevert()) { + return false + } + } + super.stopCellEditing() + } + + protected def userSaysRevert(): Boolean = { + Toolkit.getDefaultToolkit.beep() + ftf.selectAll() + val options = Array[Object]("Edit", "Revert") + val answer = JOptionPane.showOptionDialog(SwingUtilities.getWindowAncestor(ftf), + "The value must be an integer between " + minimum + " and " + maximum + ".\n" + + "You can either continue editing " + "or revert to the last valid value.", + "Invalid Text Entered", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, + null, + options, + options(1)) + if (answer == 1) { + ftf.setValue(ftf.getValue) + return true + } + false + } +} diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/LabelDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/LabelDemo.scala new file mode 100644 index 00000000..2b8f0d72 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/LabelDemo.scala @@ -0,0 +1,83 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ +import javax.swing.ImageIcon +import javax.swing.UIManager + +/** + * Tutorial: How to Use Labels + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/label.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/LabelDemoProject/src/components/LabelDemo.java]] + * + * LabelDemo.scala needs one other file: + * /scala/swing/examples/tutorials/images/middle.gif + */ +class LabelDemo extends GridPanel(3, 1) { + val icon: Option[ImageIcon] = LabelDemo.createImageIcon("/scala/swing/examples/tutorials/images/middle.gif", + "a pretty but meaningless splat") + //Create the first label. + val label1: Label = new Label("Image and Text", icon.get, Alignment.Center) { + //Set the position of its text, relative to its icon: + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + } + + //Create the other labels. + val label2 = new Label("Text-Only Label") + val label3 = new Label("", icon.get, Alignment.Center) + + //Create tool tips, for the heck of it. + label1.tooltip = "A label containing both image and text" + label2.tooltip = "A label containing only text" + label3.tooltip = "A label containing only an image" + + contents += label1 + contents += label2 + contents += label3 +} + +object LabelDemo extends SimpleSwingApplication { + UIManager.put("swing.boldMetal", false) + /** Returns an ImageIcon option, or None if the path was invalid. */ + def createImageIcon(path: String, desc:String ): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new MainFrame() { + title = "LabelDemo" + //Create and set up the content pane. + javax.swing.UIManager.put("swing.boldMetal", false) + contents = new LabelDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/ListDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/ListDemo.scala new file mode 100644 index 00000000..9f054015 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/ListDemo.scala @@ -0,0 +1,165 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.{ ButtonClicked, EditDone, SelectionChanged, ValueChanged } +import java.awt.Toolkit + +/** + * Tutorial: How to Use Lists + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/list.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ListDemoProject/src/components/ListDemo.java]] + * + * ListDemo.scala requires no other files. + */ +class ListDemo extends BorderPanel { + + private val listModel: Array[String] = Array("Jane Doe", "John Smith", "Kathy Green") + + //Create the list and put it in a scroll pane. + private val initiallySelected = 0 + private val listMe: ListView[String] = new ListView[String](listModel) { + selection.intervalMode = ListView.IntervalMode.Single + selectIndices(initiallySelected) + visibleRowCount = 5 + } + + val listScrollPane: ScrollPane = new ScrollPane(listMe) + + val hireButton: Button = new Button("Hire") { + enabled = false + reactions += { + case ButtonClicked(_) => hireListenerMethod() + } + } + + val fireButton: Button = new Button("Fire") { + reactions += { + case ButtonClicked(_) => fireListenerMethod() + } + } + + val employeeName: TextField = new TextField(10) + // employeeName.peer.getDocument().addDocumentListener(hireListener); + val nameSelected: String = listModel(initiallySelected) + + //Create a panel that uses BoxLayout. + val buttonPane = new BoxPanel(Orientation.Horizontal) { + border = Swing.EmptyBorder(5, 5, 5, 5) + contents += fireButton + contents += Swing.HStrut(5) + contents += fireButton + contents += Swing.HStrut(5) + contents += new Separator() + contents += Swing.HStrut(5) + contents += employeeName + contents += hireButton + } + + layout(listScrollPane) = BorderPanel.Position.Center + layout(buttonPane) = BorderPanel.Position.South + + listenTo(listMe.selection) + listenTo(employeeName) + + reactions += { + case EditDone(`employeeName`) => + hireListenerMethod() + case SelectionChanged(`listMe`) => + fireButton.enabled = listMe.selection.leadIndex >= 0 + case ValueChanged(`employeeName`) => + hireButton.enabled = employeeName.text.trim().length > 0 + } + + //This method tests for string equality. You could certainly + //get more sophisticated about the algorithm. For example, + //you might want to ignore white space and capitalization. + def alreadyInList(name: String): Boolean = listMe.listData.contains(name) + + def fireListenerMethod(): Unit = { + //This method can be called only if + //there's a valid selection + //so go ahead and remove whatever's selected. + var index: Int = listMe.selection.leadIndex + val (x, y) = listMe.listData.splitAt(index) + listMe.listData = x ++ y.tail + + val size: Int = listMe.listData.size + + if (size == 0) { //Nobody's left, disable firing. + fireButton.enabled = false + } else { //Select an index. + if (index == size) { + //removed item in last position + index -= 1 + } + listMe.selectIndices(index) + listMe.ensureIndexIsVisible(index) + } + } + def hireListenerMethod(): Unit = { + val nameSelected: String = employeeName.text + //User didn't type in a unique name... + if (nameSelected.equals("") || alreadyInList(nameSelected)) { + Toolkit.getDefaultToolkit.beep() + employeeName.requestFocusInWindow() + employeeName.selectAll() + } else { + val index: Int = listMe.selection.leadIndex + 1 //get selected index + + val (x: Seq[String], y: Seq[String]) = listMe.listData.splitAt(index) + val z = employeeName.text +: y + listMe.listData = x ++ z + //If we just wanted to add to the end, we'd do this: + //listModel.listData += employeeName.text; + + //Reset the text field. + employeeName.requestFocusInWindow() + employeeName.text = "" + + //Select the new item and make it visible. + listMe.selectIndices(index) + listMe.ensureIndexIsVisible(index) + } + } +} + +object ListDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "ListDemo" + //Create and set up the content pane. + contents = new ListDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/MenuGlueDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/MenuGlueDemo.scala new file mode 100644 index 00000000..65fc0c9c --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/MenuGlueDemo.scala @@ -0,0 +1,75 @@ + +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ + +/** + * Tutorial: How to Use Menus + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/MenuGlueDemoProject/src/components/MenuGlueDemo.java]] + * + * MenuGlueDemo.scala requires no other files. + * + * @author ges + * @author kwalrath + */ +class MenuGlueDemo extends MainFrame { + def createMenuBar(): MenuBar = { + new MenuBar() { + contents += createMenu("Menu 1") + contents += createMenu("Menu 2") + contents += Swing.HGlue + contents += createMenu("Menu 3") + } + } + + def createMenu(title: String): Menu = { + new Menu(title) { + contents += new MenuItem(s"Menu item #1 in $title") + contents += new MenuItem(s"Menu item #2 in $title") + contents += new MenuItem(s"Menu item #3 in $title") + } + } +} + +object MenuGlueDemo extends SimpleSwingApplication { + lazy val top = new MenuGlueDemo() { + title = "MenuGlueDemo" + contents = new FlowPanel() { + contents += createMenuBar() + } + size = new Dimension(300, 100) + } +} diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/MissingIcon.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/MissingIcon.scala new file mode 100644 index 00000000..65e7c454 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/MissingIcon.scala @@ -0,0 +1,77 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR + * PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import java.awt.BasicStroke +import java.awt.Color +import java.awt.Component +import java.awt.Graphics +import java.awt.Graphics2D +import javax.swing.Icon + +/** + * The "missing icon" is a white box with a black border and a red x. + * It's used to display something when there are issues loading an + * icon from an external location. + * + * @author Collin Fagan + */ +class MissingIcon extends Icon { + + private val width = 32 + private val height = 32 + + private val stroke = new BasicStroke(4) + + def paintIcon(c: Component, g: Graphics, x: Int, y: Int): Unit = { + val g2d: Graphics2D = g.create().asInstanceOf[Graphics2D] + + g2d.setColor(Color.WHITE) + g2d.fillRect(x + 1, y + 1, width - 2, height - 2) + + g2d.setColor(Color.BLACK) + g2d.drawRect(x + 1, y + 1, width - 2, height - 2) + + g2d.setColor(Color.RED) + + g2d.setStroke(stroke) + g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10) + g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10) + + g2d.dispose() + } + + override def getIconWidth: Int = width + + override def getIconHeight: Int = height + +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/PasswordDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/PasswordDemo.scala new file mode 100644 index 00000000..326b98a1 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/PasswordDemo.scala @@ -0,0 +1,86 @@ +package scala.swing.examples.tutorials.components + +import scala.swing._ + +/** + * Tutorial: How to Use Password Fields + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/PasswordDemoProject/src/components/PasswordDemo.java]] + * + * PasswordDemo.scala requires no other files. + */ +class PasswordDemo(val controllingFrame: Frame) extends FlowPanel { + val passwordField = new PasswordField(10) + val label = new Label("Enter the password: ") +// label.peer.setLabelFor(passwordField.peer) + + val buttonPane = createButtonPanel() + val textPane = new FlowPanel() { + contents += label + contents += passwordField + } + + def createButtonPanel(): GridPanel = { + new GridPanel(0, 1) { + val okButton = new Button(Action("OK") { + val input: Array[Char] = passwordField.password + if (PasswordDemo.isPasswordCorrect(input)) { + Dialog.showMessage(this, + "Success! You typed the right password.", + "Passord Success", Dialog.Message.Info, Swing.EmptyIcon) + } else { + Dialog.showMessage(this, + "Invalid password. Try again.", + "Error Message", + Dialog.Message.Error, Swing.EmptyIcon) + } + //Zero out the possible password, for security. + for (i <- 0 until input.length) { + input(i) = '0' + } + passwordField.selectAll() + passwordField.requestFocusInWindow() + }) + + val helpButton = new Button(Action("Help") { + Dialog.showMessage(this, + "You can get the password by searching this example's\n" + + "source code for the string \"correctPassword\".\n" + + "Or look at the section How to Use Password Fields in\n" + + "the components section of The Java Tutorial.", + "Passord Help", Dialog.Message.Info, Swing.EmptyIcon) + }) + + contents += okButton + contents += helpButton + + listenTo(okButton) + listenTo(helpButton) + } + } +} + +object PasswordDemo extends SimpleSwingApplication { + /** + * Checks the passed-in array against the correct password. + * After this method returns, you should invoke eraseArray + * on the passed-in array. + */ + def isPasswordCorrect(input: Array[Char]): Boolean = { + val correctPassword = Array[Char]('b', 'u', 'g', 'a', 'b', 'o', 'o') + val isCorrect = input.deep == correctPassword.deep + + //Zero out the password. + // Arrays.fill(correctPassword,'0'); + for (i <- 0 until correctPassword.length) + correctPassword.update(i, '0') + isCorrect + } + lazy val top = new MainFrame() { + title = "PasswordDemo" + //Create and set up the content pane. + contents = new PasswordDemo(this) + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/RadioButtonDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/RadioButtonDemo.scala new file mode 100644 index 00000000..28b30166 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/RadioButtonDemo.scala @@ -0,0 +1,130 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import java.awt.Dimension +import scala.swing._ +import scala.swing.event.{ ButtonClicked, Key } +import javax.swing.ImageIcon + +/** + * Tutorial: How to Use Buttons, Check Boxes, and Radio Buttons + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/button.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java]] + * + * RadioButtonDemo.scala requires these files: + * /scala/swing/examples/tutorials/images/Bird.gif + * /scala/swing/examples/tutorials/images/Cat.gif + * /scala/swing/examples/tutorials/images/Dog.gif + * /scala/swing/examples/tutorials/images/Rabbit.gif + * /scala/swing/examples/tutorials/images/Pig.gif + */ +class RadioButtonDemo extends BorderPanel { + val birdString = "Bird" + val catString = "Cat" + val dogString = "Dog" + val rabbitString = "Rabbit" + val pigString = "Pig" + + //Create the radio buttons. + val birdButton = new RadioButton(birdString) { + mnemonic = Key.B + selected = true + } + val catButton = new RadioButton(catString) { + mnemonic = Key.C + selected = false + } + val dogButton = new RadioButton(dogString) { + mnemonic = Key.D + selected = false + } + val rabbitButton = new RadioButton(rabbitString) { + mnemonic = Key.R + selected = false + } + val pigButton = new RadioButton(pigString) { + mnemonic = Key.P + selected = false + } + + //Group the radio buttons. + val group = new ButtonGroup() { + buttons ++= Seq(birdButton, catButton, dogButton, rabbitButton, pigButton) + } + + //Set up the picture label. + val imgIcon = RadioButtonDemo.createImageIcon(s"/scala/swing/examples/tutorials/images/$birdString.gif") + val picture = new Label() { + icon = imgIcon.get + //The preferred size is hard-coded to be the width of the + //widest image and the height of the tallest image. + //A real program would compute this. + preferredSize = new Dimension(177, 122) + } + + //Put the radio buttons in a column in a panel. + val radioPanel = new GridPanel(0, 1) { + contents ++= Seq(birdButton, catButton, dogButton, rabbitButton, pigButton) + } + + layout(radioPanel) = BorderPanel.Position.East + layout(picture) = BorderPanel.Position.Center + border = Swing.EmptyBorder(20, 20, 20, 20) + + listenTo(birdButton) + listenTo(catButton) + listenTo(dogButton) + listenTo(rabbitButton) + listenTo(pigButton) + + reactions += { + case ButtonClicked(button) => picture.icon = getPictureIcon(button.text) + } + + def getPictureIcon(gifName: String): ImageIcon = { + RadioButtonDemo.createImageIcon("/scala/swing/examples/tutorials/images/" + gifName + ".gif").get + } +} + +object RadioButtonDemo extends SimpleSwingApplication { + + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new MainFrame() { + title = "RadioButtonDemo" + contents = new RadioButtonDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/SliderDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/SliderDemo.scala new file mode 100644 index 00000000..9f7847df --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/SliderDemo.scala @@ -0,0 +1,176 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import scala.swing._ +import scala.swing.event.{ ValueChanged, WindowDeiconified, WindowIconified } +import java.awt.Font +import java.awt.event.{ ActionEvent, ActionListener } +import javax.swing.Timer +import javax.swing.{ImageIcon, UIManager} + +/** + * Tutorial: How to Use Sliders + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SliderDemoProject/src/components/SliderDemo.java]] + * + * SliderDemo.scala requires all the files in the /scala/swing/examples/tutorials/images/doggy + * directory. + */ +class SliderDemo(window: Window) extends BoxPanel(Orientation.Vertical) with ActionListener { + //Set up animation parameters. + val FpsMin = 0 + val FpsMax = 30 + val FpsInit = 15 //initial frames per second + var frameNumber = 0 + val NumFrames = 14 + val images = new Array[Option[ImageIcon]](NumFrames) + var frozen = false + var delay = 1000 / FpsInit + + val sliderLabel = new Label("Frames Per Second") { + horizontalAlignment = Alignment.Center + xLayoutAlignment = java.awt.Component.CENTER_ALIGNMENT + } + + val framesPerSecond = new Slider { + orientation = Orientation.Horizontal + min = FpsMin + max = FpsMax + value = FpsInit + //Turn on labels at major tick marks. + majorTickSpacing = 10 + minorTickSpacing = 1 + paintTicks = true + paintLabels = true + border = Swing.EmptyBorder(0, 0, 10, 0) + font = new Font("Serif", Font.ITALIC, 15) + } + + val picture = new Label() { + horizontalAlignment = Alignment.Center + xLayoutAlignment = java.awt.Component.CENTER_ALIGNMENT + Swing.Lowered + border = Swing.CompoundBorder( + Swing.BeveledBorder(Swing.Lowered), Swing.EmptyBorder(10, 10, 10, 10)) + } + + updatePicture(0); //display first frame + contents += sliderLabel + contents += framesPerSecond + contents += picture + border = Swing.EmptyBorder(10, 10, 10, 10) + + //Set up a timer that calls this object's action handler. + val timer = new Timer(delay, this) + timer.setInitialDelay(delay * 7) //We pause animation twice per cycle + //by restarting the timer + timer.setCoalesce(true) + + listenTo(framesPerSecond) + listenTo(window) + reactions += { + case ValueChanged(`framesPerSecond`) => + if (!framesPerSecond.adjusting) { + val fps: Int = framesPerSecond.value + if (fps == 0) { + if (!frozen) stopAnimation() + } else { + delay = 1000 / fps + timer.setDelay(delay) + timer.setInitialDelay(delay * 10) + if (frozen) startAnimation() + } + } + case WindowIconified(`window`) => + stopAnimation() + case WindowDeiconified(`window`) => + startAnimation() + } + + startAnimation() + + def startAnimation(): Unit = { + //Start (or restart) animating! + timer.start() + frozen = false + } + + def stopAnimation(): Unit = { + //Stop the animating thread. + timer.stop() + frozen = true + } + + //Called when the Timer fires. + def actionPerformed(e: ActionEvent): Unit = { + //Advance the animation frame. + frameNumber = if (frameNumber == (NumFrames - 1)) 0 else frameNumber + 1 + + updatePicture(frameNumber); //display the next picture + + if (frameNumber == (NumFrames - 1) || frameNumber == (NumFrames / 2 - 1)) { + timer.restart() + } + } + + /** Update the label to display the image for the current frame. */ + def updatePicture(frameNum: Int): Unit = { + //Get the image if we haven't already. + if (images(frameNumber) == null) { + images(frameNumber) = SliderDemo.createImageIcon(s"/scala/swing/examples/tutorials/images/doggy/T$frameNumber.gif") + } + + //Set the image. + images(frameNumber) match { + case Some( frm ) => picture.icon = frm + case None => picture.text = s"image #$frameNumber not found" + } + } +} + +object SliderDemo extends SimpleSwingApplication { + UIManager.put("swing.boldMetal", false) + /** Returns an ImageIcon, or null if the path was invalid. */ + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = { + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + } + + lazy val top = new MainFrame() { + title = "SliderDemo" + //Create and set up the content pane. + contents = new SliderDemo(this) + } +} + diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo.scala new file mode 100644 index 00000000..3efd6018 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo.scala @@ -0,0 +1,107 @@ +///* +// * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. +// * +// * Redistribution and use in source and binary forms, with or without +// * modification, are permitted provided that the following conditions +// * are met: +// * +// * - Redistributions of source code must retain the above copyright +// * notice, this list of conditions and the following disclaimer. +// * +// * - Redistributions in binary form must reproduce the above copyright +// * notice, this list of conditions and the following disclaimer in the +// * documentation and/or other materials provided with the distribution. +// * +// * - Neither the name of Oracle or the names of its +// * contributors may be used to endorse or promote products derived +// * from this software without specific prior written permission. +// * +// * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +// * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +// * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +// * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// */ +// +//package scala.swing.examples.tutorials.components +// +//import scala.swing._ +//import scala.swing.event.SelectionChanged +//import java.awt.Font +//import java.net.URL +//import javax.swing.ImageIcon +// +////SplitPaneDemo itself is not a visible component. It is used by SplitPaneDemo2.scala. +//class SplitPaneDemo extends FlowPanel { +// val imageNames: Array[String] = Array("Bird", "Cat", "Dog", "Rabbit", "Pig", +// "dukeWaveRed", "kathyCosmo", "left", "middle", "right", "stickerface") +// //Create the list of images and put it in a scroll pane. +// val list = new ListView[String](imageNames) +// list.selection.intervalMode = ListView.IntervalMode.Single +// list.selectIndices(0) +// // +// val listScrollPane = new ScrollPane(list) +// val picture = new Label() { +// horizontalAlignment = Alignment.Center +// } +// picture.font = font.deriveFont(Font.ITALIC) +// val pictureScrollPane = new ScrollPane(picture) +// +// //Create a split pane with the two scroll panes in it. +// //Use Orientation.Vertical to get a left/right split pane +// val splitPane = new SplitPane(Orientation.Vertical, listScrollPane, pictureScrollPane) { +// oneTouchExpandable = true +// dividerLocation = 150 +// } +// +// // Provide minimum sizes for the two components in the split pane. +// listScrollPane.minimumSize = new Dimension(100, 50) +// pictureScrollPane.minimumSize = new Dimension(400, 200) +// updateLabel(imageNames(list.selection.leadIndex)) +// listenTo(list.selection) +// +// reactions += { +// case SelectionChanged(`list`) => +// updateLabel(imageNames(list.selection.leadIndex)) +// } +// +// //Renders the selected image +// def updateLabel (name: String): Unit = { +// val icon: Option[ImageIcon] = SplitPaneDemo.createImageIcon("/scala/swing/examples/tutorials/images/" + name + ".gif") +// if (icon.isDefined) { +// picture.text = null +// picture.icon = icon.get +// } else { +// picture.text = "Image not found" +// picture.icon = null +// } +// } +// +// //Used by SplitPaneDemo2 +// def getImageList(): ListView[String] = { +// list; +// } +// +// def getSplitPane(): SplitPane = { +// splitPane; +// } +//} +// +//object SplitPaneDemo extends SimpleSwingApplication { +// /** Returns an ImageIcon option, or NOne if the path was invalid. */ +// def createImageIcon(path: String): Option[javax.swing.ImageIcon] = { +// Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) +// } +// +// lazy val top = new MainFrame() { +// title = "SplitPaneDemo" +// contents = new SplitPaneDemo() +// } +//} +// diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo2.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo2.scala new file mode 100644 index 00000000..b83cfb62 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/SplitPaneDemo2.scala @@ -0,0 +1,159 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package scala.swing.examples.tutorials.components + +import java.awt.Font + +import scala.swing._ +import scala.swing.event.SelectionChanged +import javax.swing.ImageIcon + +/** + * How to Use Split Panes + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SplitPaneDemo2Project/src/components/SplitPaneDemo2.java]] + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SplitPaneDemoProject/src/components/SplitPaneDemo.java]] + * + * SplitPaneDemo2.scala uses + * SplitPaneDemo.scala + */ +class SplitPaneDemo2 extends MainFrame { + title = "SplitPaneDemo2" + + //Create an instance of SplitPaneDemo. + val splitPaneDemo = new SplitPaneDemo() + val top: SplitPane = splitPaneDemo.splitPane + val listMe: ListView[String] = splitPaneDemo.imageList + + //XXXX: Bug #4131528, borders on nested split panes accumulate. + //Workaround: Set the border on any split pane within + //another split pane to null. Components within nested split + //panes need to have their own border for this to work well. + top.border = null + + //Create a regular old label + val label = new Label("Click on an image name in the list.") { + horizontalAlignment = Alignment.Center + } + + //Create a split pane and put "top" (a split pane) + //and JLabel instance in it. + //Use Orientation.Horizontal to get a top/bottom split pane. + val splitPane = new SplitPane(Orientation.Horizontal, top, label) { + oneTouchExpandable = true + dividerLocation = 180 + } + + //Provide minimum sizes for the two components in the split pane + top.minimumSize = new Dimension(100, 50) + label.minimumSize = new Dimension(100, 30) + + //Add the split pane to this frame + contents = splitPane + + listenTo(splitPaneDemo.imageList.selection) + + reactions += { + case (e: SelectionChanged) => + val theList: ListView[String] = e.source.asInstanceOf[ListView[String]] + if (!theList.selection.adjusting) { + if (theList.selection.leadIndex < 0) + label.text = "Nothing selected." + } else { + val index = theList.selection.leadIndex + label.text = s"Selected image number $index" + } + } + +} + +object SplitPaneDemo2 extends SimpleSwingApplication { + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + + lazy val top = new SplitPaneDemo2() +} + + + +class SplitPaneDemo extends FlowPanel { + val imageNames: Array[String] = Array("Bird", "Cat", "Dog", "Rabbit", "Pig", + "dukeWaveRed", "kathyCosmo", "left", "middle", "right", "stickerface") + + //Create the list of images and put it in a scroll pane. + val imageList = new ListView[String](imageNames) + imageList.selection.intervalMode = ListView.IntervalMode.Single + imageList.selectIndices(0) + + val listScrollPane = new ScrollPane(imageList) { + minimumSize = new Dimension(100, 50) + } + + val picture = new Label() { + horizontalAlignment = Alignment.Center + } + picture.font = font.deriveFont(Font.ITALIC) + val pictureScrollPane = new ScrollPane(picture) { + minimumSize = new Dimension(400, 200) + } + + //Create a split pane with the two scroll panes in it. + //Use Orientation.Vertical to get a left/right split pane + val splitPane = new SplitPane(Orientation.Vertical, listScrollPane, pictureScrollPane) { + oneTouchExpandable = true + dividerLocation = 150 + } + + updateLabel(imageNames(imageList.selection.leadIndex)) + + listenTo(imageList.selection) + + reactions += { + case SelectionChanged(`imageList`) => + updateLabel(imageNames(imageList.selection.leadIndex)) + } + + //Renders the selected image + def updateLabel (name: String): Unit = { + val icon: Option[ImageIcon] = SplitPaneDemo2.createImageIcon("/scala/swing/examples/tutorials/images/" + name + ".gif") + if (icon.isDefined) { + picture.text = null + picture.icon = icon.get + } else { + picture.text = "Image not found" + picture.icon = null + } + } + +} diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/components/TopLevelDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/components/TopLevelDemo.scala new file mode 100644 index 00000000..010588a0 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/components/TopLevelDemo.scala @@ -0,0 +1,68 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.components + +import scala.swing._ + +/** + * Tutorial: Using Top-Level Containers + * [[http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TopLevelDemoProject/src/components/TopLevelDemo.java]] + * + * TopLevelDemo.scala requires no other files. + */ +object TopLevelDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "TopLevelDemo" + + //Create the menu bar. Make it have a green background. + val greenMenuBar: MenuBar = new MenuBar() { + opaque = true + background = new Color(154, 165, 127) + preferredSize = new Dimension(200, 20) + } + + //Create a yellow label to put in the content pane. + val yellowlabel = new Label("Hello World") { + opaque = true + background = new Color(248, 213, 131) + preferredSize = new Dimension(200, 180) + } + + //Set the menu bar and add the label to the content pane. + menuBar = greenMenuBar + contents = new BorderPanel { + layout(yellowlabel) = BorderPanel.Position.Center + } + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/concurrency/Flipper.scala b/examples/src/main/scala/scala/swing/examples/tutorials/concurrency/Flipper.scala new file mode 100644 index 00000000..3d3418f6 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/concurrency/Flipper.scala @@ -0,0 +1,123 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.concurrency + +import scala.swing._ +import scala.swing.event.ButtonClicked +import javax.swing.SwingWorker +import java.util.Random + +/** + * Tutorial: Tasks that Have Interim Results + * [[http://http://docs.oracle.com/javase/tutorial/uiswing/concurrency/interim.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/concurrency/FlipperProject/src/concurrency/Flipper.java]] + */ +class Flipper extends MainFrame { + var flipTask: FlipTask = null + title = "Flipper" + + val headsText = new TextField(20) { + editable = false + horizontalAlignment = Alignment.Right + border = Swing.BeveledBorder(Swing.Lowered) + } + val totalText = new TextField(20) { + editable = false + horizontalAlignment = Alignment.Right + border = Swing.BeveledBorder(Swing.Lowered) + } + val devText = new TextField(20) { + editable = false + horizontalAlignment = Alignment.Right + border = Swing.BeveledBorder(Swing.Lowered) + } + val startButton = new Button("Start") + val stopButton = new Button("Stop") { + enabled = false + } + val panel = new GridBagPanel() { + val c = new Constraints() + c.insets = new Insets(3, 10, 3, 10) + layout(headsText) = c + layout(totalText) = c + layout(devText) = c + layout(startButton) = c + layout(stopButton) = c + } + + contents = panel + + class FlipPair(val heads: Long, val total: Long) {} + + class FlipTask extends SwingWorker[Unit, FlipPair] { + override def doInBackground(): Unit = { + var heads: Long = 0 + var total: Long = 0 + val random: Random = new Random() + while (!isCancelled()) { + total += 1 + if (random.nextBoolean()) { + heads += 1 + } + publish(new FlipPair(heads, total)) + } + } + + override def process(pairs: java.util.List[FlipPair]): Unit = { + val pair: FlipPair = pairs.get(pairs.size - 1); + headsText.text = pair.heads.toInt.toString + totalText.text = pair.total.toInt.toString + val dt = (pair.heads.toDouble) / (pair.total.toDouble) - 0.5 + devText.text = f"$dt%.10g" + } + } + + listenTo(startButton) + listenTo(stopButton) + reactions += { + case ButtonClicked(`startButton`) => + startButton.enabled = false + stopButton.enabled = true + flipTask = new FlipTask() + flipTask.execute() + case ButtonClicked(`stopButton`) => + startButton.enabled = true + stopButton.enabled = false + flipTask.cancel(true) + flipTask = null + } +} + +object Flipper extends SimpleSwingApplication { + lazy val top = new Flipper() +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/events/Beeper.scala b/examples/src/main/scala/scala/swing/examples/tutorials/events/Beeper.scala new file mode 100644 index 00000000..3d44e0a5 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/events/Beeper.scala @@ -0,0 +1,65 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.events + +import scala.swing._ +import scala.swing.event.ButtonClicked +import java.awt.{Dimension,Toolkit } + +/** + * Tutorial: Introduction to Event Listeners + * [[http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/events/BeeperProject/src/events/Beeper.java]] + * + * Beeper.scala requires no other files. + */ +class Beeper extends BorderPanel { + val button = new Button("Click Me") { + preferredSize = new Dimension(200, 80) + } + layout(button) = BorderPanel.Position.Center + listenTo(button) + reactions += { + case ButtonClicked(`button`) => + Toolkit.getDefaultToolkit().beep() + } + +} + +object Beeper extends SimpleSwingApplication { + //Create and set up the window. + lazy val top = new MainFrame { + title = "Beeper" + contents = new Beeper() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/BLDComponent.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BLDComponent.scala new file mode 100644 index 00000000..d7720d33 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BLDComponent.scala @@ -0,0 +1,96 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +import scala.swing._ +import scala.swing.event.MousePressed +import java.awt.{ Color, Dimension, Graphics2D } + +/** + * A rectangle that has a fixed maximum size. + * + * Used by BoxLayoutDemo.scala. + */ +class BLDComponent(alignmentX: Float, hue: Float, shortSideSize: Int, + var restrictMaximumSize: Boolean, val printSize: Boolean, nameArg: String) extends Panel { + name = nameArg + xLayoutAlignment = alignmentX + opaque = true + val normalHue = Color.getHSBColor(hue, 0.4f, 0.85f) + preferredSize = new Dimension(shortSideSize * 2, shortSideSize) + listenTo(mouse.clicks) + + reactions += { + case MousePressed(_, point: Point, _, _, _) => + val alignment = point.getX().toFloat / size.getWidth().toFloat + // Round to the nearest 1/10th. + val tmp = Math.round(alignment * 10.0f) + xLayoutAlignment = tmp.toFloat / 10.0f + revalidate() /// this GUI needs relayout + repaint() + } + + override def paintComponent(g: Graphics2D): Unit = { + super.paintComponent(g) + val width: Int = size.getWidth().toInt + val height: Int = size.getHeight().toInt + val alignmentX: Float = xLayoutAlignment.toFloat + + g.setColor(normalHue); + g.fill3DRect(0, 0, width, height, true); + + /* Draw a vertical white line at the alignment point.*/ + // XXX: This code is probably not the best. + g.setColor(Color.white); + val x: Int = (alignmentX * width.toFloat).toInt - 1; + g.drawLine(x, 0, x, height - 1); + + /* Say what the alignment point is. */ + g.setColor(Color.black); + g.drawString(alignmentX.toString, 3, height - 3); + + if (printSize) { + System.out.println("BLDComponent " + name + ": size is " + + width + "x" + height + + "; preferred size is " + + preferredSize.getWidth() + "x" + + preferredSize.getHeight()); + } + } + + def getMaximumSize(): Dimension = { + if (restrictMaximumSize) preferredSize else super.maximumSize + } + + def setSizeRestriction(restrictSize: Boolean): Unit = { + restrictMaximumSize = restrictSize + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxAlignmentDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxAlignmentDemo.scala new file mode 100644 index 00000000..8eab1ee5 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxAlignmentDemo.scala @@ -0,0 +1,164 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +import scala.swing._ +import javax.swing.{ ImageIcon } +import java.awt.{ Color, Dimension } +import java.net.URL + +/** + * Tutorials: How to Use BoxAlignment + * [[http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxAlignmentDemoProject/src/layout/BoxAlignmentDemo.java]] + * + * BoxAlignmentDemo.scala requires the following files: + * scala/swing/examples/tutorials/images/middle.gif + * scala/swing/examples/tutorials/images/geek-cght.gif + * + * This demo shows how to specify alignments when you're using + * a BoxLayout for components with maximum sizes and different + * default alignments. + */ +class BoxAlignmentDemo extends BorderPanel { + + val buttonRow = new FlowPanel() { + contents += createButtonRow(false) + contents += createButtonRow(true) + } + val labelAndComponent = new FlowPanel() { + contents += createLabelAndComponent(false) + contents += createLabelAndComponent(true) + } + val buttonAndComponent = new FlowPanel() { + contents += createYAlignmentExample(false) + contents += createYAlignmentExample(true) + } + val tabbedPane = new TabbedPane() + tabbedPane.pages += new TabbedPane.Page("Altering alignments", buttonRow) + tabbedPane.pages += new TabbedPane.Page("X alignment mismatch", labelAndComponent) + tabbedPane.pages += new TabbedPane.Page("Y alignment mismatch", buttonAndComponent) + //Add tabbedPane to this panel + layout(tabbedPane) = BorderPanel.Position.Center + + def createButtonRow(changeAlignment: Boolean): BoxPanel = { + val button1 = new Button("A Button") { + icon = BoxAlignmentDemo.createImageIcon("/scala/swing/examples/tutorials/images/middle.gif").get + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + } + val button2 = new Button("Another Button") { + icon = BoxAlignmentDemo.createImageIcon("/scala/swing/examples/tutorials/images/geek-cght.gif").get + verticalTextPosition = Alignment.Bottom + horizontalTextPosition = Alignment.Center + } + val title = if (changeAlignment) "Desired" else "Default" + if (changeAlignment) { + button1.yLayoutAlignment = 1.0f // Bottom + button2.yLayoutAlignment = 1.0f // Bottom + } + val panel = new BoxPanel(Orientation.Horizontal) { + border = Swing.TitledBorder(Swing.EmptyBorder, title) + contents += button1 + contents += button2 + } + panel + } + + def createLabelAndComponent(doItRight: Boolean): BoxPanel = { + val dSize = new Dimension(150, 100) + val component = new FlowPanel() { + maximumSize = dSize + preferredSize = dSize + minimumSize = dSize + border = Swing.TitledBorder(Swing.LineBorder(Color.black), "A Panel") + } + val title = if (doItRight) "Matched" else "Mismatched" + val paneBorder = Swing.TitledBorder(Swing.EmptyBorder, title) + paneBorder.setTitleJustification(javax.swing.border.TitledBorder.CENTER) + paneBorder.setTitlePosition(javax.swing.border.TitledBorder.BELOW_TOP) + paneBorder.setTitleColor(Color.black) + val label = new Label("This is a Label") + if (doItRight) label.xLayoutAlignment = 0.5f // Center + val pane = new BoxPanel(Orientation.Vertical) { + border = paneBorder + contents += label + contents += component + } + pane + } + + def createYAlignmentExample(doItRight: Boolean): BoxPanel = { + val dSize1 = new Dimension(150, 100) + val title = if (doItRight) "Matched" else "Mismatched" + val componentBorder = Swing.TitledBorder(Swing.LineBorder(Color.black), "A Panel") + componentBorder.setTitleJustification(javax.swing.border.TitledBorder.CENTER) + componentBorder.setTitlePosition(javax.swing.border.TitledBorder.BELOW_TOP) + componentBorder.setTitleColor(Color.black) + val component1 = new FlowPanel() { + maximumSize = dSize1 + preferredSize = dSize1 + minimumSize = dSize1 + border = componentBorder + } + if (!doItRight) component1.yLayoutAlignment = 0.0f // TOP_ALIGNMENT + + val dSize2 = new Dimension(100, 50) + val component2 = new FlowPanel() { + maximumSize = dSize2 + preferredSize = dSize2 + minimumSize = dSize2 + border = componentBorder + } + val paneBorder = Swing.TitledBorder(Swing.EmptyBorder, title) + val pane = new BoxPanel(Orientation.Horizontal) { + border = paneBorder + contents += component1 + contents += component2 + } + pane + } +} + +object BoxAlignmentDemo extends SimpleSwingApplication { + /** Returns an ImageIcon option, or None if the path was invalid. */ + def createImageIcon(path: String): Option[javax.swing.ImageIcon] = { + Option(resourceFromClassloader(path)).map(imgURL => Swing.Icon(imgURL)) + } + + //Create and set up the window. + lazy val top = new MainFrame { + title = "BoxAlignmentDemo" + contents = new BoxAlignmentDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo.scala new file mode 100644 index 00000000..083f7077 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo.scala @@ -0,0 +1,73 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +import scala.swing._ + +/** + * Tutorials: How to Use BoxLayout + * [[http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java]] + * + * BoxLayoutDemo.scala requires no other files. + */ +class BoxLayoutDemo extends BoxPanel(Orientation.Vertical) { + val button1 = new Button("Button 1") { + xLayoutAlignment = 0.5 + } + val button2 = new Button("Button 2") { + xLayoutAlignment = 0.5 + } + val button3 = new Button("Button 3") { + xLayoutAlignment = 0.5 + } + val button4 = new Button("Long-Named Button 4") { + xLayoutAlignment = 0.5 + } + val button5 = new Button("5") { + xLayoutAlignment = 0.5 + } + contents += button1 + contents += button2 + contents += button3 + contents += button4 + contents += button5 +} + +object BoxLayoutDemo extends SimpleSwingApplication { + //Create and set up the window. + lazy val top = new MainFrame { + title = "BoxLayoutDemo" + contents = new BoxLayoutDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo2.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo2.scala new file mode 100644 index 00000000..d5e64fd4 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/BoxLayoutDemo2.scala @@ -0,0 +1,107 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +import scala.swing._ +import scala.swing.event.ButtonClicked +import java.awt.Color + +/** + * Tutorials: How to Use BoxLayout + * [[http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html]] + * + * Source code references: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemo2Project/src/layout/BoxLayoutDemo2.java]] + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemo2Project/src/layout/BLDComponent.java]] + * + * BoxLayoutDemo2.scala requires BLDComponent.scala. + */ +class BoxLayoutDemo2 extends BorderPanel { + private val NumComponents = 3 + // Component.LEFT_ALIGNMENT, Component.CENTER_ALIGNMENT, Component.RIGHT_ALIGNMENT + private val xAlignment = Array(0.0f, 0.5f, 1.0f) + private val hue = Array(0.0f, 0.33f, 0.67f) + private var restrictSize = true + private val sizeIsRandom = false + private val bldComponent = new Array[BLDComponent](NumComponents) + val panel = new BoxPanel(Orientation.Vertical) + //Create the rectangles. + var shortSideSize = 15 + for (i <- 0 until NumComponents) { + if (sizeIsRandom) { + shortSideSize = (30.0 * Math.random()).toInt + 30 + } + else { + shortSideSize += 10 + } + bldComponent(i) = new BLDComponent(xAlignment(i), hue(i), + shortSideSize, restrictSize, sizeIsRandom, i.toString) + panel.contents += bldComponent(i).asInstanceOf[Component] + } + + //Create the instructions. + val label = new Label("Click a rectangle to change its X alignment.") + val cb = new CheckBox("Restrict maximum rectangle size.") { + selected = restrictSize + } + + border = Swing.LineBorder(Color.red) + + // Use a BoxPanel instead of javax.swing.Box + val box = new BoxPanel(Orientation.Vertical) { + contents += label + contents += cb + } + + layout(panel) = BorderPanel.Position.Center + layout(box) = BorderPanel.Position.South + + listenTo(cb) + + reactions += { + case ButtonClicked(`cb`) => + restrictSize = cb.selected + notifyBldComponents() + } + + def notifyBldComponents(): Unit = { + for (i <- 0 until NumComponents) + bldComponent(i).setSizeRestriction(restrictSize) + bldComponent(0).revalidate + } +} + +object BoxLayoutDemo2 extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "BoxLayoutDemo2" + contents = new BoxLayoutDemo2() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/DiagonalLayout.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/DiagonalLayout.scala new file mode 100644 index 00000000..06e350a3 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/DiagonalLayout.scala @@ -0,0 +1,181 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +/* + * 1.2+ version. Used by CustomLayoutDemo.scala. + */ + +import java.awt._ +//remove if not needed +import scala.collection.JavaConversions._ + +class DiagonalLayout(private var vgap: Int) extends LayoutManager { + + private var minWidth: Int = 0 + + private var minHeight: Int = 0 + + private var preferredWidth: Int = 0 + + private var preferredHeight: Int = 0 + + private var sizeUnknown: Boolean = true + + def this() { + this(5) + } + + /* Required by LayoutManager. */ + def addLayoutComponent(name: String, comp: Component) { + } + + def removeLayoutComponent(comp: Component) { + } + + private def setSizes(parent: Container) { + val nComps = parent.getComponentCount + var d: Dimension = null + //Reset preferred/minimum width and height. + preferredWidth = 0 + preferredHeight = 0 + minWidth = 0 + minHeight = 0 + for (i <- 0 until nComps) { + val c = parent.getComponent(i) + if (c.isVisible) { + d = c.getPreferredSize + if (i > 0) { + preferredWidth += d.width / 2 + preferredHeight += vgap + } else { + preferredWidth = d.width + } + preferredHeight += d.height + minWidth = Math.max(c.getMinimumSize.width, minWidth) + minHeight = preferredHeight + } + } + } + + /* Required by LayoutManager. */ + def preferredLayoutSize(parent: Container): Dimension = { + val dim = new Dimension(0, 0) + val nComps = parent.getComponentCount + setSizes(parent) + val insets = parent.getInsets + dim.width = preferredWidth + insets.left + insets.right + dim.height = preferredHeight + insets.top + insets.bottom + sizeUnknown = false + dim + } + + /* Required by LayoutManager. */ + def minimumLayoutSize(parent: Container): Dimension = { + val dim = new Dimension(0, 0) + val nComps = parent.getComponentCount + val insets = parent.getInsets + dim.width = minWidth + insets.left + insets.right + dim.height = minHeight + insets.top + insets.bottom + sizeUnknown = false + dim + } + + /* Required by LayoutManager. */ + /* + * This is called when the panel is first displayed, + * and every time its size changes. + * Note: You CAN'T assume preferredLayoutSize or + * minimumLayoutSize will be called -- in the case + * of applets, at least, they probably won't be. + */ + def layoutContainer(parent: Container) { + val insets = parent.getInsets + val maxWidth = parent.getWidth - (insets.left + insets.right) + val maxHeight = parent.getHeight - (insets.top + insets.bottom) + val nComps = parent.getComponentCount + var previousWidth = 0 + var previousHeight = 0 + var x = 0 + var y = insets.top + val rowh = 0 + val start = 0 + var xFudge = 0 + var yFudge = 0 + var oneColumn = false + // Go through the components' sizes, if neither + // preferredLayoutSize nor minimumLayoutSize has + // been called. + if (sizeUnknown) { + setSizes(parent) + } + if (maxWidth <= minWidth) { + oneColumn = true + } + if (maxWidth != preferredWidth) { + xFudge = (maxWidth - preferredWidth) / (nComps - 1) + } + if (maxHeight > preferredHeight) { + yFudge = (maxHeight - preferredHeight) / (nComps - 1) + } + for (i <- 0 until nComps) { + val c = parent.getComponent(i) + if (c.isVisible) { + val d = c.getPreferredSize + // increase x and y, if appropriate + if (i > 0) { + if (!oneColumn) { + x += previousWidth / 2 + xFudge + } + y += previousHeight + vgap + yFudge + } + // If x is too large, + if ((!oneColumn) && (x + d.width) > (parent.getWidth - insets.right)) { + x = parent.getWidth - insets.bottom - d.width + } + // If y is too large, + if ((y + d.height) > (parent.getHeight - insets.bottom)) { + // do nothing. + // Another choice would be to do what we do to x. + } + // Set the component's size and position. + c.setBounds(x, y, d.width, d.height) + previousWidth = d.width + previousHeight = d.height + } + } + } + + override def toString(): String = { + val str = "" + getClass.getName + "[vgap=" + vgap + str + "]" + } +} diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/layout/GridBagLayoutDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/layout/GridBagLayoutDemo.scala new file mode 100644 index 00000000..473136d4 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/layout/GridBagLayoutDemo.scala @@ -0,0 +1,92 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.layout + +import scala.swing._ + +/** + * Tutorials: How to Use GridBagLayout + * [[http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridBagLayoutDemoProject/src/layout/GridBagLayoutDemo.java]] + * + * GridBagLayoutDemo.scala requires no other files. + */ +class GridBagLayoutDemo extends GridBagPanel { + val shouldFill = true + val shouldWeightX = true + val RightToLeft = false + val c: Constraints = new Constraints() + //natural height, maximum width + if (shouldFill) c.fill = GridBagPanel.Fill.Horizontal + if (shouldWeightX) c.weightx = 0.5 + c.gridx = 0 + c.gridy = 0 + layout(new Button("Button 1")) = c + + c.fill = GridBagPanel.Fill.Horizontal + c.weightx = 0.5 + c.gridx = 1 + c.gridy = 0 + layout(new Button("Button 2")) = c + + c.fill = GridBagPanel.Fill.Horizontal + c.weightx = 0.5 + c.gridx = 2 + c.gridy = 0 + layout(new Button("Button 3")) = c + + c.fill = GridBagPanel.Fill.Horizontal + c.ipady = 40 // make this component tall + c.weightx = 0.5 + c.gridwidth = 3 + c.gridx = 0 + c.gridy = 1 + layout(new Button("Long-Named Button 4")) = c + + c.fill = GridBagPanel.Fill.Horizontal + c.ipady = 0 //reset to default + c.weightx = 1.0 //request any extra vertical space + c.anchor = GridBagPanel.Anchor.PageEnd //bottom of space + c.insets = new Insets(10, 0, 0, 0) // top padding + c.gridx = 1 + c.gridwidth = 2 + c.gridy = 22 + layout(new Button("5")) = c +} + +object GridBagLayoutDemo extends SimpleSwingApplication { + lazy val top = new MainFrame() { + title = "GridBagLayoutDemo" + contents = new GridBagLayoutDemo() + } +} \ No newline at end of file diff --git a/examples/src/main/scala/scala/swing/examples/tutorials/misc/FocusConceptsDemo.scala b/examples/src/main/scala/scala/swing/examples/tutorials/misc/FocusConceptsDemo.scala new file mode 100644 index 00000000..2d878e02 --- /dev/null +++ b/examples/src/main/scala/scala/swing/examples/tutorials/misc/FocusConceptsDemo.scala @@ -0,0 +1,106 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package scala.swing.examples.tutorials.misc + +import scala.swing._ +import scala.util.control.NonFatal +import javax.swing.UIManager + +/** + * Tutorial: How to Use the Focus System + * [[http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html]] + * + * Source code reference: + * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/FocusConceptsDemoProject/src/misc/FocusConceptsDemo.java]] + * + * FocusConceptsDemo.scala requires no other files. + * + * When running the demo, try the following: + * 1. If necessary, click the window to give it the focus. + * 2. Move the focus from component to component using the Tab key. + * 3. You will notice that when the focus moves into the text area, it stays in the text area. + * 4. Move the focus out of the text area using Control-Tab. + * 5. Move the focus in the opposite direction using Shift-Tab. + * 6. Move the focus out of the text area in the opposite direction using Control-Shift-Tab. + */ +class FocusConceptsDemo extends BorderPanel { + val b1 = new Button("Button") + val b2 = new Button("Button") + val b3 = new Button("Button") + val b4 = new Button("Button") + + val buttonPanel = new GridPanel(1, 1) { + contents += b1 + contents += b2 + contents += b3 + contents += b4 + } + + val text1 = new TextArea("TextArea", 15, 40) + val textAreaPanel = new BorderPanel() { + layout(text1) = BorderPanel.Position.Center + } + + val t1 = new TextField("TextField") + val t2 = new TextField("TextField") + val t3 = new TextField("TextField") + val t4 = new TextField("TextField") + val textFieldPanel = new GridPanel(1, 1) { + contents += t1 + contents += t2 + contents += t3 + contents += t4 + } + + layout(buttonPanel) = BorderPanel.Position.North + layout(textAreaPanel) = BorderPanel.Position.Center + layout(textFieldPanel) = BorderPanel.Position.South + border = Swing.EmptyBorder(20, 20, 20, 20) +} + +object FocusConceptsDemo extends SimpleSwingApplication { + + /* Use an appropriate Look and Feel */ + try { + //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); + } catch { + case NonFatal(ex) => ex.printStackTrace() + } + /* Turn off metal's use of bold fonts */ + UIManager.put("swing.boldMetal", false); + //Create and set up the window. + lazy val top = new MainFrame() { + title = "FocusConceptsDemo" + contents = new FocusConceptsDemo() + } +} \ No newline at end of file diff --git a/src/main/scala/scala/swing/TextPane.scala b/src/main/scala/scala/swing/TextPane.scala new file mode 100644 index 00000000..3ffd1a89 --- /dev/null +++ b/src/main/scala/scala/swing/TextPane.scala @@ -0,0 +1,18 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ +package scala.swing + +/** + * A text component used to model styled paragraphs of text. + * + * @see javax.swing.JTextPane + */ +class TextPane() extends TextComponent { + override lazy val peer: javax.swing.JTextPane = new javax.swing.JTextPane() with SuperMixin + def styledDocument: javax.swing.text.StyledDocument = peer.getStyledDocument() +} \ No newline at end of file diff --git a/src/main/scala/scala/swing/ToolBar.scala b/src/main/scala/scala/swing/ToolBar.scala new file mode 100644 index 00000000..7de5d2db --- /dev/null +++ b/src/main/scala/scala/swing/ToolBar.scala @@ -0,0 +1,16 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2007-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.swing + +/** + * Like a menu, a way to map text or icons to actions; but detachable from the rest of the user interface. + */ +class ToolBar extends scala.swing.Component with SequentialContainer.Wrapper { + override lazy val peer: javax.swing.JToolBar = new javax.swing.JToolBar with SuperMixin +} \ No newline at end of file