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 @ 1272
History | View | Annotate | Download (7.13 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.JOptionPane; |
9 |
import javax.swing.JPanel; |
10 |
import javax.swing.JTabbedPane; |
11 |
import javax.swing.SwingUtilities; |
12 |
import javax.swing.text.JTextComponent; |
13 |
|
14 |
import org.gvsig.scripting.ScriptingScript; |
15 |
import org.gvsig.scripting.ScriptingUnit; |
16 |
import org.gvsig.scripting.swing.api.JCodeEditor; |
17 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
18 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
19 |
import org.gvsig.scripting.swing.impl.FileChangesObserver; |
20 |
import org.gvsig.scripting.swing.impl.composer.ChangeListenerSupport; |
21 |
import org.gvsig.scripting.swing.impl.composer.JPropertiesScript; |
22 |
import org.gvsig.tools.observer.Observable; |
23 |
import org.gvsig.tools.observer.Observer; |
24 |
import org.gvsig.tools.script.Script; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
public class ScriptUnitEditor extends JPanel implements JCodeEditor { |
29 |
|
30 |
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptUnitEditor.class); |
31 |
/**
|
32 |
*
|
33 |
*/
|
34 |
private static final long serialVersionUID = 5191252525229037754L; |
35 |
|
36 |
private JTabbedPane tabs; |
37 |
|
38 |
protected ScriptingScript script;
|
39 |
protected final ScriptingUIManager uimanager; |
40 |
protected final ChangeListenerSupport changeListeners; |
41 |
protected BaseTextEditor textEditor;
|
42 |
protected JPropertiesScript propertiesPanel;
|
43 |
protected boolean modified; |
44 |
private final Observer fileObserver; |
45 |
private boolean saving; |
46 |
|
47 |
public ScriptUnitEditor(ScriptingUIManager uimanager) {
|
48 |
this(uimanager, null); |
49 |
} |
50 |
|
51 |
public ScriptUnitEditor(ScriptingUIManager uimanager, Script script) {
|
52 |
this.script = (ScriptingScript) script;
|
53 |
this.uimanager = uimanager;
|
54 |
this.modified = false; |
55 |
this.saving = false; |
56 |
this.changeListeners = new ChangeListenerSupport(); |
57 |
initComponents(); |
58 |
if (this.script != null) { |
59 |
this.set(this.script); |
60 |
} |
61 |
this.fileObserver = (Observable o, Object o1) -> { |
62 |
doFileModified(); |
63 |
}; |
64 |
} |
65 |
|
66 |
private synchronized void doFileModified() { |
67 |
if (saving) {
|
68 |
return;
|
69 |
} |
70 |
if (!SwingUtilities.isEventDispatchThread()) { |
71 |
SwingUtilities.invokeLater(() -> {doFileModified();});
|
72 |
return;
|
73 |
} |
74 |
if (this.isModified()) { |
75 |
JOptionPane.showMessageDialog(
|
76 |
this,
|
77 |
"The file '" + this.getUnit().getUserPath() + "' has externaly modified.", |
78 |
"File modification",
|
79 |
JOptionPane.INFORMATION_MESSAGE
|
80 |
); |
81 |
} else {
|
82 |
this.reload();
|
83 |
} |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public ScriptingScript getScript() {
|
88 |
return (ScriptingScript) this.script; |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public ScriptingUnit getUnit() {
|
93 |
return this.script; |
94 |
} |
95 |
|
96 |
private void initComponents() { |
97 |
this.textEditor = new BaseTextEditor(); |
98 |
this.textEditor.addChangeListener((PropertyChangeEvent evt) -> { |
99 |
modified = true;
|
100 |
fireChangeEvent(evt); |
101 |
}); |
102 |
|
103 |
this.propertiesPanel = new JPropertiesScript(this.script); |
104 |
this.propertiesPanel.addChangeListener((PropertyChangeEvent evt) -> { |
105 |
modified = true;
|
106 |
fireChangeEvent(evt); |
107 |
}); |
108 |
|
109 |
this.tabs = new JTabbedPane(); |
110 |
this.tabs.setTabPlacement(JTabbedPane.BOTTOM); |
111 |
this.tabs.addTab(this.uimanager.getTranslation("Code"), this.textEditor); |
112 |
this.tabs.addTab(this.uimanager.getTranslation("Properties"), this.propertiesPanel); |
113 |
this.setLayout(new BorderLayout()); |
114 |
this.add(BorderLayout.CENTER, tabs); |
115 |
|
116 |
} |
117 |
|
118 |
protected JTabbedPane getTabbedPane() { |
119 |
return this.tabs; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public boolean isModified() { |
124 |
return this.modified; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public synchronized void save() { |
129 |
try {
|
130 |
this.saving = true; |
131 |
this.fetch(this.getUnit()); |
132 |
script.save(); |
133 |
this.modified = false; |
134 |
} finally {
|
135 |
this.saving = false; |
136 |
} |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void reload() { |
141 |
// LOGGER.info("reload() enter, "+threadId());
|
142 |
JTextComponent text = this.getJTextComponent(); |
143 |
int pos = text.getCaretPosition();
|
144 |
this.set(this.getUnit()); |
145 |
int len = text.getDocument().getLength();
|
146 |
if( pos > len ) {
|
147 |
pos = len; |
148 |
} |
149 |
if( pos < 0 ) { |
150 |
pos = 0;
|
151 |
} |
152 |
// LOGGER.info("reload() "+threadId()+", pos="+pos+", len="+len);
|
153 |
try {
|
154 |
text.setCaretPosition(pos); |
155 |
} catch(Exception ex) { |
156 |
LOGGER.trace("Can't restore caret position after reload", ex);
|
157 |
// LOGGER.info("reload():"+ ex.getMessage());
|
158 |
} |
159 |
// LOGGER.info("reload() exit, "+threadId());
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void addChangeListener(PropertyChangeListener listener) { |
164 |
this.changeListeners.addChangeListener(listener);
|
165 |
} |
166 |
|
167 |
protected void fireChangeEvent(String name, Object oldValue) { |
168 |
this.changeListeners.fireChange(name, oldValue);
|
169 |
} |
170 |
|
171 |
protected void fireChangeEvent(PropertyChangeEvent evt) { |
172 |
this.changeListeners.fireChange(evt);
|
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void set(ScriptingUnit unit) { |
177 |
this.script = (ScriptingScript) unit;
|
178 |
this.propertiesPanel.set(unit);
|
179 |
this.textEditor.setText(this.script.getMimeType(), this.script.getCode()); |
180 |
if (unit.isASystemUnit()) {
|
181 |
this.textEditor.getJTextComponent().setEditable(false); |
182 |
} else {
|
183 |
this.textEditor.getJTextComponent().setEditable(true); |
184 |
} |
185 |
if (this.script != null) { |
186 |
FileChangesObserver.getInstance().addObserver( |
187 |
this.fileObserver,
|
188 |
this.script.getScriptFile()
|
189 |
); |
190 |
} |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void fetch(ScriptingUnit unit) { |
195 |
ScriptingScript theScript = (ScriptingScript) unit; |
196 |
if (this.propertiesPanel != null) { |
197 |
this.propertiesPanel.fectch(theScript);
|
198 |
} |
199 |
if (this.textEditor != null) { |
200 |
theScript.setCode(this.textEditor.getText());
|
201 |
} |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public JTextComponent getJTextComponent() { |
206 |
return this.textEditor.getJTextComponent(); |
207 |
} |
208 |
|
209 |
@Override
|
210 |
public SyntaxtHighlightTextComponent getSyntaxtHighlightTextComponent() {
|
211 |
return this.textEditor.getSyntaxtHighlightTextComponent(); |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public void selectLine(int line) { |
216 |
this.textEditor.selectLine(line);
|
217 |
} |
218 |
|
219 |
@Override
|
220 |
public int getLineCount() { |
221 |
return this.textEditor.getLineCount(); |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public JComponent asJComponent() { |
226 |
return this; |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public void gotoline(int line) { |
231 |
this.textEditor.gotoline(line);
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void addUpdateCaretPositionActionListener(ActionListener actionlistener) { |
236 |
this.textEditor.addUpdateCaretPositionActionListener(actionlistener);
|
237 |
} |
238 |
|
239 |
} |