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