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 / composer / editors / ScriptUnitEditor.java @ 1066
History | View | Annotate | Download (5.24 KB)
1 |
package org.gvsig.scripting.swing.impl.composer.editors; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.beans.PropertyChangeEvent; |
6 |
import java.beans.PropertyChangeListener; |
7 |
import javax.swing.JComponent; |
8 |
import javax.swing.JPanel; |
9 |
import javax.swing.JTabbedPane; |
10 |
import javax.swing.text.JTextComponent; |
11 |
|
12 |
import org.gvsig.scripting.ScriptingScript; |
13 |
import org.gvsig.scripting.ScriptingUnit; |
14 |
import org.gvsig.scripting.swing.api.JCodeEditor; |
15 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
16 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
17 |
import org.gvsig.scripting.swing.impl.composer.ChangeListenerSupport; |
18 |
import org.gvsig.scripting.swing.impl.composer.JPropertiesScript; |
19 |
import org.gvsig.tools.script.Script; |
20 |
|
21 |
public class ScriptUnitEditor extends JPanel implements JCodeEditor { |
22 |
|
23 |
/**
|
24 |
*
|
25 |
*/
|
26 |
private static final long serialVersionUID = 5191252525229037754L; |
27 |
|
28 |
private JTabbedPane tabs; |
29 |
|
30 |
protected ScriptingScript script;
|
31 |
protected final ScriptingUIManager uimanager; |
32 |
protected final ChangeListenerSupport changeListeners; |
33 |
protected BaseTextEditor textEditor;
|
34 |
protected JPropertiesScript propertiesPanel;
|
35 |
protected boolean modified; |
36 |
|
37 |
public ScriptUnitEditor(ScriptingUIManager uimanager) {
|
38 |
this(uimanager, null); |
39 |
} |
40 |
|
41 |
public ScriptUnitEditor(ScriptingUIManager uimanager, Script script) {
|
42 |
this.script = (ScriptingScript) script;
|
43 |
this.uimanager = uimanager;
|
44 |
this.modified = false; |
45 |
this.changeListeners = new ChangeListenerSupport(); |
46 |
initComponents(); |
47 |
if( this.script!=null ) { |
48 |
this.set(this.script); |
49 |
} |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public ScriptingScript getScript() {
|
54 |
return (ScriptingScript) this.script; |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public ScriptingUnit getUnit() {
|
59 |
return this.script; |
60 |
} |
61 |
|
62 |
private void initComponents() { |
63 |
this.textEditor = new BaseTextEditor(); |
64 |
this.textEditor.addChangeListener(new PropertyChangeListener() { |
65 |
@Override
|
66 |
public void propertyChange(PropertyChangeEvent evt) { |
67 |
modified = true;
|
68 |
fireChangeEvent(evt); |
69 |
} |
70 |
}); |
71 |
|
72 |
this.propertiesPanel = new JPropertiesScript(this.script); |
73 |
this.propertiesPanel.addChangeListener(new PropertyChangeListener() { |
74 |
@Override
|
75 |
public void propertyChange(PropertyChangeEvent evt) { |
76 |
modified = true;
|
77 |
fireChangeEvent(evt); |
78 |
} |
79 |
}); |
80 |
|
81 |
this.tabs = new JTabbedPane(); |
82 |
this.tabs.setTabPlacement(JTabbedPane.BOTTOM); |
83 |
this.tabs.addTab(this.uimanager.getTranslation("Code"), this.textEditor); |
84 |
this.tabs.addTab(this.uimanager.getTranslation("Properties"), this.propertiesPanel); |
85 |
this.setLayout(new BorderLayout()); |
86 |
this.add(BorderLayout.CENTER, tabs); |
87 |
|
88 |
} |
89 |
|
90 |
protected JTabbedPane getTabs() { |
91 |
return this.tabs; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public boolean isModified() { |
96 |
return this.modified; |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void save() { |
101 |
if (this.propertiesPanel != null) { |
102 |
this.propertiesPanel.fectch(script);
|
103 |
} |
104 |
if (this.textEditor != null) { |
105 |
script.setCode(this.textEditor.getText());
|
106 |
} |
107 |
script.save(); |
108 |
this.modified = false; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void reload() { |
113 |
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void addChangeListener(PropertyChangeListener listener) { |
118 |
this.changeListeners.addChangeListener(listener);
|
119 |
} |
120 |
|
121 |
public void fireChangeEvent(String name, Object oldValue) { |
122 |
this.changeListeners.fireChange(name, oldValue);
|
123 |
} |
124 |
|
125 |
public void fireChangeEvent(PropertyChangeEvent evt) { |
126 |
this.changeListeners.fireChange(evt);
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void set(ScriptingUnit unit) { |
131 |
this.script = (ScriptingScript) unit;
|
132 |
this.propertiesPanel.set(unit);
|
133 |
this.textEditor.setText(this.script.getMimeType(), this.script.getCode()); |
134 |
if( unit.isASystemUnit() ) {
|
135 |
this.textEditor.getJTextComponent().setEditable(false); |
136 |
} else {
|
137 |
this.textEditor.getJTextComponent().setEditable(true); |
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public void fetch(ScriptingUnit unit) { |
143 |
ScriptingScript script = (ScriptingScript) unit; |
144 |
this.propertiesPanel.fectch(unit);
|
145 |
script.setCode(this.textEditor.getText());
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public JTextComponent getJTextComponent() { |
150 |
return this.textEditor.getJTextComponent(); |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public SyntaxtHighlightTextComponent getSyntaxtHighlightTextComponent() {
|
155 |
return this.textEditor.getSyntaxtHighlightTextComponent(); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void selectLine(int line) { |
160 |
this.textEditor.selectLine(line);
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public int getLineCount() { |
165 |
return this.textEditor.getLineCount(); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public JComponent asJComponent() { |
170 |
return this; |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void gotoline(int line) { |
175 |
this.textEditor.gotoline(line);
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void addUpdateCaretPositionActionListener(ActionListener actionlistener) { |
180 |
this.textEditor.addUpdateCaretPositionActionListener(actionlistener);
|
181 |
} |
182 |
|
183 |
} |