Statistics
| Revision:

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 1063 jjdelcerro
package org.gvsig.scripting.swing.impl.tools;
2
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5 1064 jjdelcerro
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 1063 jjdelcerro
import java.util.List;
11
import javax.swing.DefaultComboBoxModel;
12 1064 jjdelcerro
import javax.swing.DefaultListModel;
13 1063 jjdelcerro
import javax.swing.JButton;
14
import javax.swing.JComboBox;
15 1064 jjdelcerro
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 1063 jjdelcerro
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 1064 jjdelcerro
import org.gvsig.tools.swing.api.script.ScriptSwingManager;
26 1063 jjdelcerro
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 1064 jjdelcerro
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 1063 jjdelcerro
    private List<Script> scripts;
118 1064 jjdelcerro
    private final ListComponent list;
119 1063 jjdelcerro
    private final JButton btnRemove;
120
    private final JButton btnChooser;
121 1064 jjdelcerro
122 1063 jjdelcerro
    public ScriptListPickerController(JComboBox combo, JButton remove, JButton chooser) {
123 1064 jjdelcerro
        this.scripts = new ArrayList<>();
124
        this.list = new ListComponent(combo);
125 1063 jjdelcerro
        this.btnChooser = chooser;
126
        this.btnRemove = remove;
127 1064 jjdelcerro
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 1063 jjdelcerro
        this.btnRemove.setEnabled(false);
144
        this.btnRemove.setIcon(this.getIcon("picker-script-remove"));
145
        this.btnRemove.setText("");
146 1064 jjdelcerro
        this.btnChooser.setEnabled(true);
147 1063 jjdelcerro
        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 1064 jjdelcerro
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 1063 jjdelcerro
    }
188 1064 jjdelcerro
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 1065 jjdelcerro
            this.scripts.remove(n);
200 1064 jjdelcerro
            this.scripts.add(n-1, sc);
201 1065 jjdelcerro
            this.list.setSelectedIndex(n-1);
202 1064 jjdelcerro
            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 1065 jjdelcerro
            this.scripts.remove(n);
211 1064 jjdelcerro
            this.scripts.add(n+1, sc);
212 1065 jjdelcerro
            this.list.setSelectedIndex(n+1);
213 1064 jjdelcerro
            this.rebuildList();
214
        }
215
216
    }
217 1063 jjdelcerro
218 1064 jjdelcerro
    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 1063 jjdelcerro
    private void doChooseScript() {
227
        ScriptingUIManager manager = ScriptingSwingLocator.getUIManager();
228
        ScriptSwingManager.ScriptChooserPanel chooser = manager.createScriptChooserPanel();
229
        chooser.showDialog();
230 1064 jjdelcerro
        if (chooser.getAction() == WindowManager_v2.BUTTON_OK) {
231 1063 jjdelcerro
            Script theScript = chooser.getScript();
232 1064 jjdelcerro
            if (theScript != null) {
233 1063 jjdelcerro
                this.add(theScript);
234
                this.fireChangeEvent();
235
            }
236
        }
237
    }
238 1064 jjdelcerro
239
    private void doRemoveScript() {
240
        int index = this.list.getSelectedIndex();
241
        if (index < 0) {
242 1063 jjdelcerro
            return;
243
        }
244
        this.scripts.remove(index);
245 1064 jjdelcerro
        rebuildList();
246 1063 jjdelcerro
    }
247 1064 jjdelcerro
248 1063 jjdelcerro
    private void add(Script script) {
249
        this.scripts.add(script);
250 1064 jjdelcerro
        this.rebuildList();
251 1063 jjdelcerro
    }
252 1064 jjdelcerro
253 1063 jjdelcerro
    @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 1064 jjdelcerro
        this.rebuildList();
262 1063 jjdelcerro
    }
263
264
    @Override
265
    public void coerceAndSet(Object value) {
266 1064 jjdelcerro
        if (value instanceof List) {
267 1063 jjdelcerro
            this.set((List<Script>) value);
268
            return;
269
        }
270
        String s = "null";
271 1064 jjdelcerro
        if (value != null) {
272 1063 jjdelcerro
            s = value.getClass().getName();
273
        }
274 1064 jjdelcerro
        throw new IllegalArgumentException("Can't coerce '" + s + "' to List of scripts.");
275 1063 jjdelcerro
    }
276
277
    @Override
278
    public void setEnabled(boolean enabled) {
279 1066 jjdelcerro
        this.btnChooser.setEnabled(enabled?this.isEditable():false);
280 1064 jjdelcerro
        this.list.setEnabled(enabled);
281
        if (enabled) {
282
            if (this.list.getSelectedIndex() >= 0) {
283 1066 jjdelcerro
                this.btnRemove.setEnabled(this.isEditable());
284 1064 jjdelcerro
            } else {
285
                this.btnRemove.setEnabled(false);
286
            }
287
        } else {
288
            this.btnRemove.setEnabled(false);
289
        }
290 1063 jjdelcerro
    }
291
292
    @Override
293 1066 jjdelcerro
    public void setEditable(boolean editable) {
294
        super.setEditable(editable);
295
        this.btnChooser.setEnabled(editable);
296
        this.btnRemove.setEnabled(editable);
297
    }
298
299
    @Override
300 1063 jjdelcerro
    public boolean isEnabled() {
301
        return this.btnChooser.isEnabled();
302
    }
303
304 1064 jjdelcerro
    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 1063 jjdelcerro
            }
328 1064 jjdelcerro
        });
329 1063 jjdelcerro
    }
330 1064 jjdelcerro
331 1063 jjdelcerro
}