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 @ 1063
History | View | Annotate | Download (3.74 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.util.List; |
6 |
import javax.swing.DefaultComboBoxModel; |
7 |
import javax.swing.JButton; |
8 |
import javax.swing.JComboBox; |
9 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
10 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
11 |
import org.gvsig.tools.script.Script; |
12 |
import org.gvsig.tools.swing.api.ListElement; |
13 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
14 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* @author jjdelcerro
|
19 |
*/
|
20 |
public class ScriptListPickerController extends AbstractPickerController<List<Script>> { |
21 |
private List<Script> scripts; |
22 |
private final JComboBox combo; |
23 |
private final JButton btnRemove; |
24 |
private final JButton btnChooser; |
25 |
|
26 |
public ScriptListPickerController(JComboBox combo, JButton remove, JButton chooser) { |
27 |
this.combo = combo;
|
28 |
this.btnChooser = chooser;
|
29 |
this.btnRemove = remove;
|
30 |
|
31 |
this.combo.setEnabled(false); |
32 |
this.btnRemove.setEnabled(false); |
33 |
this.btnRemove.setIcon(this.getIcon("picker-script-remove")); |
34 |
this.btnRemove.setText(""); |
35 |
this.btnChooser.setEnabled(false); |
36 |
this.btnChooser.setIcon(this.getIcon("picker-script")); |
37 |
this.btnChooser.setText(""); |
38 |
this.btnChooser.addActionListener(new ActionListener() { |
39 |
@Override
|
40 |
public void actionPerformed(ActionEvent e) { |
41 |
doChooseScript(); |
42 |
} |
43 |
}); |
44 |
this.btnRemove.addActionListener(new ActionListener() { |
45 |
@Override
|
46 |
public void actionPerformed(ActionEvent e) { |
47 |
doRemoveScript(); |
48 |
} |
49 |
}); |
50 |
} |
51 |
|
52 |
private void doChooseScript() { |
53 |
ScriptingUIManager manager = ScriptingSwingLocator.getUIManager(); |
54 |
ScriptSwingManager.ScriptChooserPanel chooser = manager.createScriptChooserPanel(); |
55 |
chooser.showDialog(); |
56 |
if( chooser.getAction()==WindowManager_v2.BUTTON_OK ) {
|
57 |
Script theScript = chooser.getScript(); |
58 |
if( theScript!=null ) { |
59 |
this.add(theScript);
|
60 |
this.fireChangeEvent();
|
61 |
} |
62 |
} |
63 |
} |
64 |
|
65 |
private void doRemoveScript() { |
66 |
int index = this.combo.getSelectedIndex(); |
67 |
if( index < 0 ) { |
68 |
return;
|
69 |
} |
70 |
this.scripts.remove(index);
|
71 |
rebuildCombo(); |
72 |
} |
73 |
|
74 |
private void add(Script script) { |
75 |
this.scripts.add(script);
|
76 |
this.rebuildCombo();
|
77 |
} |
78 |
|
79 |
@Override
|
80 |
public List<Script> get() { |
81 |
return this.scripts; |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void set(List<Script> value) { |
86 |
this.scripts = value;
|
87 |
this.rebuildCombo();
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public void coerceAndSet(Object value) { |
92 |
if( value instanceof List ) { |
93 |
this.set((List<Script>) value); |
94 |
return;
|
95 |
} |
96 |
String s = "null"; |
97 |
if( value != null ) { |
98 |
s = value.getClass().getName(); |
99 |
} |
100 |
throw new IllegalArgumentException("Can't coerce '"+s+"' to List of scripts."); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void setEnabled(boolean enabled) { |
105 |
this.btnChooser.setEnabled(enabled);
|
106 |
this.btnRemove.setEnabled(enabled);
|
107 |
this.combo.setEnabled(enabled);
|
108 |
} |
109 |
|
110 |
@Override
|
111 |
public boolean isEnabled() { |
112 |
return this.btnChooser.isEnabled(); |
113 |
} |
114 |
|
115 |
private void rebuildCombo() { |
116 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
117 |
if( this.scripts!=null ) { |
118 |
for (Script script : scripts) {
|
119 |
model.addElement(new ListElement(script.getName(), script));
|
120 |
} |
121 |
} |
122 |
this.combo.setModel(model);
|
123 |
} |
124 |
|
125 |
} |