gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / tools / ScriptListPickerController.java @ 1066
History | View | Annotate | Download (10.1 KB)
1 |
package org.gvsig.scripting.swing.impl.tools; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.awt.event.ItemEvent; |
6 |
import java.awt.event.ItemListener; |
7 |
import java.awt.event.KeyEvent; |
8 |
import java.awt.event.KeyListener; |
9 |
import java.util.ArrayList; |
10 |
import java.util.List; |
11 |
import javax.swing.DefaultComboBoxModel; |
12 |
import javax.swing.DefaultListModel; |
13 |
import javax.swing.JButton; |
14 |
import javax.swing.JComboBox; |
15 |
import javax.swing.JList; |
16 |
import javax.swing.SwingUtilities; |
17 |
import javax.swing.event.ListSelectionEvent; |
18 |
import javax.swing.event.ListSelectionListener; |
19 |
import org.gvsig.scripting.ScriptingUnit; |
20 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
21 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
22 |
import org.gvsig.tools.script.Script; |
23 |
import org.gvsig.tools.swing.api.ListElement; |
24 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
25 |
import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
26 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
public class ScriptListPickerController extends AbstractPickerController<List<Script>> { |
33 |
|
34 |
private class ListComponent { |
35 |
|
36 |
private final JComboBox combo; |
37 |
private final JList list; |
38 |
|
39 |
public ListComponent(JComboBox combo) { |
40 |
this.list = null; |
41 |
this.combo = combo;
|
42 |
} |
43 |
|
44 |
public ListComponent(JList list) { |
45 |
this.list = list;
|
46 |
this.combo = null; |
47 |
} |
48 |
|
49 |
public void setEnabled(boolean b) { |
50 |
if (this.list == null) { |
51 |
this.combo.setEnabled(b);
|
52 |
} else {
|
53 |
this.list.setEnabled(b);
|
54 |
} |
55 |
} |
56 |
|
57 |
public int getSelectedIndex() { |
58 |
if (this.list == null) { |
59 |
return this.combo.getSelectedIndex(); |
60 |
} else {
|
61 |
return this.list.getSelectedIndex(); |
62 |
} |
63 |
} |
64 |
|
65 |
public void clearElements() { |
66 |
if (this.list == null) { |
67 |
this.combo.setModel(new DefaultComboBoxModel()); |
68 |
} else {
|
69 |
this.list.setModel(new DefaultListModel()); |
70 |
} |
71 |
} |
72 |
|
73 |
public void addElement(ListElement element) { |
74 |
if (this.list == null) { |
75 |
this.combo.addItem(element);
|
76 |
} else {
|
77 |
((DefaultListModel) (this.list.getModel())).addElement(element); |
78 |
} |
79 |
} |
80 |
|
81 |
public void addSelectionListener(final ActionListener listener) { |
82 |
if (this.combo != null) { |
83 |
this.combo.addItemListener(new ItemListener() { |
84 |
@Override
|
85 |
public void itemStateChanged(ItemEvent e) { |
86 |
ActionEvent event = new ActionEvent(this, 1, "ItemChanged"); |
87 |
listener.actionPerformed(event); |
88 |
} |
89 |
}); |
90 |
} |
91 |
if (this.list != null) { |
92 |
this.list.addListSelectionListener(new ListSelectionListener() { |
93 |
@Override
|
94 |
public void valueChanged(ListSelectionEvent e) { |
95 |
ActionEvent event = new ActionEvent(this, 1, "ItemChanged"); |
96 |
listener.actionPerformed(event); |
97 |
} |
98 |
}); |
99 |
} |
100 |
} |
101 |
|
102 |
public void addKeyListener(KeyListener listener) { |
103 |
if( this.list!=null ) { |
104 |
this.list.addKeyListener(listener);
|
105 |
} |
106 |
} |
107 |
|
108 |
private void setSelectedIndex(int n) { |
109 |
if (this.list == null) { |
110 |
this.combo.setSelectedIndex(n);
|
111 |
} else {
|
112 |
this.list.setSelectedIndex(n);
|
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
private List<Script> scripts; |
118 |
private final ListComponent list; |
119 |
private final JButton btnRemove; |
120 |
private final JButton btnChooser; |
121 |
|
122 |
public ScriptListPickerController(JComboBox combo, JButton remove, JButton chooser) { |
123 |
this.scripts = new ArrayList<>(); |
124 |
this.list = new ListComponent(combo); |
125 |
this.btnChooser = chooser;
|
126 |
this.btnRemove = remove;
|
127 |
|
128 |
this.initComponents();
|
129 |
} |
130 |
|
131 |
public ScriptListPickerController(JList list, JButton remove, JButton chooser) { |
132 |
this.scripts = new ArrayList<>(); |
133 |
this.list = new ListComponent(list); |
134 |
this.btnChooser = chooser;
|
135 |
this.btnRemove = remove;
|
136 |
|
137 |
this.initComponents();
|
138 |
} |
139 |
|
140 |
private void initComponents() { |
141 |
this.list.setEnabled(true); |
142 |
|
143 |
this.btnRemove.setEnabled(false); |
144 |
this.btnRemove.setIcon(this.getIcon("picker-script-remove")); |
145 |
this.btnRemove.setText(""); |
146 |
this.btnChooser.setEnabled(true); |
147 |
this.btnChooser.setIcon(this.getIcon("picker-script")); |
148 |
this.btnChooser.setText(""); |
149 |
this.btnChooser.addActionListener(new ActionListener() { |
150 |
@Override
|
151 |
public void actionPerformed(ActionEvent e) { |
152 |
doChooseScript(); |
153 |
} |
154 |
}); |
155 |
this.btnRemove.addActionListener(new ActionListener() { |
156 |
@Override
|
157 |
public void actionPerformed(ActionEvent e) { |
158 |
doRemoveScript(); |
159 |
} |
160 |
}); |
161 |
|
162 |
this.list.addSelectionListener(new ActionListener() { |
163 |
@Override
|
164 |
public void actionPerformed(ActionEvent e) { |
165 |
doSelectionChanged(); |
166 |
} |
167 |
}); |
168 |
this.list.addKeyListener(new KeyListener() { |
169 |
@Override
|
170 |
public void keyTyped(KeyEvent e) { |
171 |
if( (e.getModifiers() & KeyEvent.CTRL_MASK) == KeyEvent.CTRL_MASK) { |
172 |
doMoveCurrentScript(e.getKeyCode()); |
173 |
} |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void keyPressed(KeyEvent e) { |
178 |
if( (e.getModifiers() & KeyEvent.CTRL_MASK) == KeyEvent.CTRL_MASK) { |
179 |
doMoveCurrentScript(e.getKeyCode()); |
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void keyReleased(KeyEvent e) { |
185 |
} |
186 |
}); |
187 |
} |
188 |
|
189 |
private void doMoveCurrentScript(int keyCode) { |
190 |
int n = this.list.getSelectedIndex(); |
191 |
if( n<0 ) { |
192 |
return;
|
193 |
} |
194 |
if( keyCode ==KeyEvent.VK_UP) { |
195 |
if( n<1 ) { |
196 |
return;
|
197 |
} |
198 |
Script sc = this.scripts.get(n);
|
199 |
this.scripts.remove(n);
|
200 |
this.scripts.add(n-1, sc); |
201 |
this.list.setSelectedIndex(n-1); |
202 |
this.rebuildList();
|
203 |
return;
|
204 |
} |
205 |
if( keyCode ==KeyEvent.VK_DOWN) { |
206 |
if( n>=this.scripts.size()-1 ) { |
207 |
return;
|
208 |
} |
209 |
Script sc = this.scripts.get(n);
|
210 |
this.scripts.remove(n);
|
211 |
this.scripts.add(n+1, sc); |
212 |
this.list.setSelectedIndex(n+1); |
213 |
this.rebuildList();
|
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
private void doSelectionChanged() { |
219 |
if (this.list.getSelectedIndex() >= 0) { |
220 |
this.btnRemove.setEnabled(true); |
221 |
} else {
|
222 |
this.btnRemove.setEnabled(false); |
223 |
} |
224 |
} |
225 |
|
226 |
private void doChooseScript() { |
227 |
ScriptingUIManager manager = ScriptingSwingLocator.getUIManager(); |
228 |
ScriptSwingManager.ScriptChooserPanel chooser = manager.createScriptChooserPanel(); |
229 |
chooser.showDialog(); |
230 |
if (chooser.getAction() == WindowManager_v2.BUTTON_OK) {
|
231 |
Script theScript = chooser.getScript(); |
232 |
if (theScript != null) { |
233 |
this.add(theScript);
|
234 |
this.fireChangeEvent();
|
235 |
} |
236 |
} |
237 |
} |
238 |
|
239 |
private void doRemoveScript() { |
240 |
int index = this.list.getSelectedIndex(); |
241 |
if (index < 0) { |
242 |
return;
|
243 |
} |
244 |
this.scripts.remove(index);
|
245 |
rebuildList(); |
246 |
} |
247 |
|
248 |
private void add(Script script) { |
249 |
this.scripts.add(script);
|
250 |
this.rebuildList();
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public List<Script> get() { |
255 |
return this.scripts; |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void set(List<Script> value) { |
260 |
this.scripts = value;
|
261 |
this.rebuildList();
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public void coerceAndSet(Object value) { |
266 |
if (value instanceof List) { |
267 |
this.set((List<Script>) value); |
268 |
return;
|
269 |
} |
270 |
String s = "null"; |
271 |
if (value != null) { |
272 |
s = value.getClass().getName(); |
273 |
} |
274 |
throw new IllegalArgumentException("Can't coerce '" + s + "' to List of scripts."); |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public void setEnabled(boolean enabled) { |
279 |
this.btnChooser.setEnabled(enabled?this.isEditable():false); |
280 |
this.list.setEnabled(enabled);
|
281 |
if (enabled) {
|
282 |
if (this.list.getSelectedIndex() >= 0) { |
283 |
this.btnRemove.setEnabled(this.isEditable()); |
284 |
} else {
|
285 |
this.btnRemove.setEnabled(false); |
286 |
} |
287 |
} else {
|
288 |
this.btnRemove.setEnabled(false); |
289 |
} |
290 |
} |
291 |
|
292 |
@Override
|
293 |
public void setEditable(boolean editable) { |
294 |
super.setEditable(editable);
|
295 |
this.btnChooser.setEnabled(editable);
|
296 |
this.btnRemove.setEnabled(editable);
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public boolean isEnabled() { |
301 |
return this.btnChooser.isEnabled(); |
302 |
} |
303 |
|
304 |
private void rebuildList() { |
305 |
SwingUtilities.invokeLater(new Runnable() { |
306 |
@Override
|
307 |
public void run() { |
308 |
int n = list.getSelectedIndex();
|
309 |
list.clearElements(); |
310 |
if (scripts != null) { |
311 |
String label;
|
312 |
for (Script script : scripts) {
|
313 |
if (script instanceof ScriptingUnit) { |
314 |
label = ((ScriptingUnit) script).getUserPath(); |
315 |
} else {
|
316 |
label = script.getName(); |
317 |
} |
318 |
list.addElement(new ListElement(label, script));
|
319 |
} |
320 |
} |
321 |
if( n>=0 && n<scripts.size() ) { |
322 |
btnRemove.setEnabled(true);
|
323 |
list.setSelectedIndex(n); |
324 |
} else {
|
325 |
btnRemove.setEnabled(false);
|
326 |
} |
327 |
} |
328 |
}); |
329 |
} |
330 |
|
331 |
} |