1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.swing.control; |
|
27 |
|
28 import java.awt.EventQueue; |
|
29 import java.util.*; |
|
30 import javax.swing.*; |
|
31 import javax.swing.event.*; |
|
32 import com.oracle.solaris.vp.panel.common.control.*; |
|
33 import com.oracle.solaris.vp.panel.common.model.PanelDescriptor; |
|
34 import com.oracle.solaris.vp.panel.swing.view.ListSelectorPanel; |
|
35 |
|
36 @SuppressWarnings({"serial"}) |
|
37 public abstract class ListSelectorControl<P extends PanelDescriptor, |
|
38 C extends ListSelectorPanel, S> extends SelectorControl<P, C, S> { |
|
39 |
|
40 // |
|
41 // Inner classes |
|
42 // |
|
43 |
|
44 protected class NavListSelectionModel extends DefaultListSelectionModel { |
|
45 // |
|
46 // ListSelectionModel methods |
|
47 // |
|
48 |
|
49 @Override |
|
50 public void setSelectionInterval(int index0, int index1) { |
|
51 super.setSelectionInterval(index0, index1); |
|
52 if (!getValueIsAdjusting()) { |
|
53 goToSelected(); |
|
54 } |
|
55 } |
|
56 |
|
57 @Override |
|
58 public void setValueIsAdjusting(boolean adjusting) { |
|
59 boolean oldVal = getValueIsAdjusting(); |
|
60 if (oldVal != adjusting) { |
|
61 super.setValueIsAdjusting(adjusting); |
|
62 if (!adjusting && oldVal) { |
|
63 goToSelected(); |
|
64 } |
|
65 } |
|
66 } |
|
67 |
|
68 // |
|
69 // NavListSelectionModel methods |
|
70 // |
|
71 |
|
72 public void setSelectionIntervalDirectly(int index0, int index1) { |
|
73 super.setSelectionInterval(index0, index1); |
|
74 } |
|
75 } |
|
76 |
|
77 // |
|
78 // Instance data |
|
79 // |
|
80 |
|
81 private NavListSelectionModel listSelModel; |
|
82 |
|
83 // |
|
84 // Constructors |
|
85 // |
|
86 |
|
87 public ListSelectorControl(String id, String name, P descriptor) { |
|
88 super(id, name, descriptor); |
|
89 } |
|
90 |
|
91 // |
|
92 // Control methods |
|
93 // |
|
94 |
|
95 @Override |
|
96 public void start(Navigator navigator, Map<String, String> parameters) |
|
97 throws NavigationAbortedException, InvalidParameterException, |
|
98 NavigationFailedException { |
|
99 |
|
100 super.start(navigator, parameters); |
|
101 |
|
102 // Re-retrieve/create the model now that Control is started |
|
103 getComponent().getSelectionComponent().setModel(getListModel()); |
|
104 } |
|
105 |
|
106 @Override |
|
107 public void stop(boolean isCancel) throws NavigationAbortedException { |
|
108 super.stop(isCancel); |
|
109 |
|
110 // Reset model so that GC-able data within can be reclaimed |
|
111 getComponent().getSelectionComponent().setModel(new DefaultListModel()); |
|
112 } |
|
113 |
|
114 // |
|
115 // SwingControl methods |
|
116 // |
|
117 |
|
118 @Override |
|
119 protected void configComponent(C panel) { |
|
120 super.configComponent(panel); |
|
121 |
|
122 listSelModel = new NavListSelectionModel(); |
|
123 panel.getSelectionComponent().setSelectionModel(listSelModel); |
|
124 |
|
125 listSelModel.addListSelectionListener( |
|
126 new ListSelectionListener() { |
|
127 @Override |
|
128 public void valueChanged(ListSelectionEvent e) { |
|
129 if (!e.getValueIsAdjusting()) { |
|
130 updateActions(); |
|
131 } |
|
132 } |
|
133 }); |
|
134 } |
|
135 |
|
136 // |
|
137 // SelectorControl methods |
|
138 // |
|
139 |
|
140 @Override |
|
141 protected List<S> getSelectableValues() { |
|
142 ListModel model = getListModel(); |
|
143 int n = model.getSize(); |
|
144 List<S> values = new ArrayList<S>(n); |
|
145 |
|
146 for (int i = 0; i < n; i++) { |
|
147 @SuppressWarnings({"unchecked"}) |
|
148 S object = (S)model.getElementAt(i); |
|
149 values.add(object); |
|
150 } |
|
151 |
|
152 return values; |
|
153 } |
|
154 |
|
155 @Override |
|
156 protected List<S> getSelectedValues() { |
|
157 Object[] selection = |
|
158 getComponent().getSelectionComponent().getSelectedValues(); |
|
159 |
|
160 List<S> objects = new ArrayList<S>(selection.length); |
|
161 |
|
162 for (Object item : selection) { |
|
163 @SuppressWarnings({"unchecked"}) |
|
164 S object = (S)item; |
|
165 objects.add(object); |
|
166 } |
|
167 |
|
168 return objects; |
|
169 } |
|
170 |
|
171 @Override |
|
172 protected void selectRunningChild() { |
|
173 Control child = getRunningChild(); |
|
174 if (child != null) { |
|
175 final int index = getListIndexOf(child); |
|
176 if (index != -1) { |
|
177 EventQueue.invokeLater( |
|
178 new Runnable() { |
|
179 @Override |
|
180 public void run() { |
|
181 // Set list selection directly without going through |
|
182 // the Navigator |
|
183 listSelModel.setSelectionIntervalDirectly(index, |
|
184 index); |
|
185 } |
|
186 }); |
|
187 } |
|
188 } |
|
189 } |
|
190 |
|
191 // |
|
192 // ListSelectorControl methods |
|
193 // |
|
194 |
|
195 /** |
|
196 * Gets the index of the list item that corresponds to the given child |
|
197 * {@code Control}. |
|
198 * |
|
199 * @return an index, or -1 if no index is appropriate |
|
200 */ |
|
201 protected abstract int getListIndexOf(Control child); |
|
202 |
|
203 /** |
|
204 * Gets the model to use in the {@link ListSelectorPanel}'s list. |
|
205 */ |
|
206 protected abstract ListModel getListModel(); |
|
207 } |
|