1+ /*
2+ * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3+ *
4+ * Redistribution and use in source and binary forms, with or without
5+ * modification, are permitted provided that the following conditions
6+ * are met:
7+ *
8+ * - Redistributions of source code must retain the above copyright
9+ * notice, this list of conditions and the following disclaimer.
10+ *
11+ * - Redistributions in binary form must reproduce the above copyright
12+ * notice, this list of conditions and the following disclaimer in the
13+ * documentation and/or other materials provided with the distribution.
14+ *
15+ * - Neither the name of Oracle or the names of its
16+ * contributors may be used to endorse or promote products derived
17+ * from this software without specific prior written permission.
18+ *
19+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+ */
31+ package scala .swing .examples .tutorials .events
32+
33+ import scala .swing ._
34+ import scala .swing .event .{ButtonClicked , SelectionChanged }
35+ import java .awt .Dimension
36+
37+ /**
38+ * Tutorial: How to Write a List Selection Listener
39+ * [[http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html ]]
40+ *
41+ * Source code reference:
42+ * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/events/ListSelectionDemoProject/src/events/ListSelectionDemo.java ]]
43+ *
44+ * ListSelectionDemo.scala requires no other files.
45+ */
46+ class ListSelectionDemo extends BorderPanel {
47+ val listData = Array ( " one" , " two" , " three" , " four" ,
48+ " five" , " six" , " seven" )
49+ val columnNames = Array ( " French" , " Spanish" , " Italian" )
50+ val list = new ListView (listData)
51+ val listPane = new ScrollPane (list)
52+
53+ val modes = Array ( " SINGLE_SELECTION" , " SINGLE_INTERVAL_SELECTION" ,
54+ " MULTIPLE_INTERVAL_SELECTION" )
55+ val comboBox = new ComboBox (modes) {
56+ selection.index = 2
57+ }
58+ val controlPane = new FlowPanel () {
59+ contents += new Label (" Selection mode:" )
60+ contents += comboBox
61+ }
62+
63+ // Build output area.
64+ val output = new TextArea (1 , 10 ) {
65+ editable = false
66+ }
67+ val outputPane = new ScrollPane (output) {
68+ verticalScrollBarPolicy = ScrollPane .BarPolicy .Always
69+ horizontalScrollBarPolicy = ScrollPane .BarPolicy .AsNeeded
70+ }
71+ val listContainer = new GridPanel (1 , 1 ) {
72+ contents += listPane
73+ border = Swing .TitledBorder (Swing .EmptyBorder , " List" )
74+ }
75+ val topHalf = new BoxPanel (Orientation .Horizontal ) {
76+ contents += listContainer
77+ border = Swing .EmptyBorder (5 , 5 , 0 , 5 )
78+ minimumSize = new Dimension (100 , 50 )
79+ preferredSize = new Dimension (100 , 110 )
80+ }
81+
82+ val bottomHalf = new BorderPanel () {
83+ layout(controlPane) = BorderPanel .Position .North
84+ layout(outputPane) = BorderPanel .Position .Center
85+ preferredSize = new Dimension (450 , 135 )
86+ }
87+
88+ // Do the layout.
89+ val splitPane = new SplitPane (Orientation .Horizontal , topHalf, bottomHalf)
90+
91+ layout(splitPane) = BorderPanel .Position .Center
92+
93+ listenTo(comboBox.selection)
94+ listenTo(list.selection)
95+
96+ reactions += {
97+ case SelectionChanged (`comboBox`) =>
98+ val newMode = comboBox.selection.item
99+ newMode match {
100+ case " SINGLE_SELECTION" =>
101+ list.selection.intervalMode = ListView .IntervalMode .Single
102+ case " SINGLE_INTERVAL_SELECTION" =>
103+ list.selection.intervalMode = ListView .IntervalMode .SingleInterval
104+ case " MULTIPLE_INTERVAL_SELECTION" =>
105+ list.selection.intervalMode = ListView .IntervalMode .MultiInterval
106+ }
107+ output.append(" ----------" + " Mode: " + newMode + " ----------" + " \n " )
108+ case SelectionChanged (`list`) =>
109+ val firstIndex = list.selection.leadIndex
110+ val lastIndex = list.selection.anchorIndex // anchor = last?
111+ val isAdjusting = list.selection.adjusting
112+ output.append(" Event for indexes " + firstIndex + " - " + lastIndex +
113+ " ; isAdjusting is " + isAdjusting + " ; selected indexes:" )
114+ if (list.selection.indices.isEmpty) {
115+ output.append(" <none>" )
116+ }
117+ else {
118+ // Find out which indexes are selected.
119+ for (i <- list.selection.indices) {
120+ output.append(" " + i)
121+ }
122+ }
123+ output.append(" \n " )
124+ output.caret.position = output.peer.getDocument().getLength()
125+ }
126+ }
127+
128+ object ListSelection extends SimpleSwingApplication {
129+ // Create and set up the window.
130+ lazy val top = new MainFrame {
131+ title = " ListSelectionDemo"
132+ contents = new ListSelectionDemo ()
133+ }
134+ }
0 commit comments