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 / EditorHelper.java @ 591
History | View | Annotate | Download (10.5 KB)
1 |
package org.gvsig.scripting.swing.impl.composer; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.KeyEvent; |
7 |
import java.awt.event.KeyListener; |
8 |
import javax.swing.JOptionPane; |
9 |
import javax.swing.JPanel; |
10 |
import javax.swing.JTextArea; |
11 |
import javax.swing.text.BadLocationException; |
12 |
import javax.swing.text.JTextComponent; |
13 |
import org.gvsig.scripting.ScriptingManager; |
14 |
import org.gvsig.scripting.ScriptingScript; |
15 |
import org.gvsig.scripting.swing.api.JEditor; |
16 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
17 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
18 |
import org.gvsig.scripting.swing.impl.JDialogContent; |
19 |
import org.gvsig.scripting.swing.impl.composer.DefaultJScriptingComposer.JMyTabbedPane; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.i18n.I18nManager; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
public class EditorHelper { |
26 |
private static Logger logger = LoggerFactory.getLogger(EditorHelper.class); |
27 |
|
28 |
SyntaxtHighlightTextComponent editorPanel; |
29 |
private final ScriptingUIManager uimanager; |
30 |
private final ScriptingScript script; |
31 |
|
32 |
EditorHelper(ScriptingUIManager uimanager, ScriptingScript script) { |
33 |
this.uimanager = uimanager;
|
34 |
this.script = script;
|
35 |
} |
36 |
|
37 |
public String getText() { |
38 |
return editorPanel.getText();
|
39 |
} |
40 |
|
41 |
public JTextComponent getJEditorPanel() { |
42 |
return this.editorPanel.getJTextComponent(); |
43 |
} |
44 |
|
45 |
public void addDefaultActionListener(ActionListener actionlistener) { |
46 |
editorPanel.addUpdateCaretPositionActionListener(actionlistener); |
47 |
} |
48 |
|
49 |
public JPanel getPropertiesPanel() { |
50 |
return new JPropertiesScript(this.script); |
51 |
} |
52 |
|
53 |
protected JPanel getCodePanel() { |
54 |
JPanel panel = new JPanel(); |
55 |
panel.setLayout(new BorderLayout()); |
56 |
|
57 |
editorPanel = (SyntaxtHighlightTextComponent) uimanager.createSyntaxHighlightingPanel(); |
58 |
editorPanel.setContentType(script.getMimeType()); |
59 |
editorPanel.setText(script.getCode()); |
60 |
|
61 |
editorPanel.addKeyListener(new MyKeyListener(this)); |
62 |
|
63 |
panel.add(editorPanel.asJComponent(), BorderLayout.CENTER);
|
64 |
panel.setVisible(true);
|
65 |
|
66 |
return panel;
|
67 |
} |
68 |
|
69 |
public void getSuggestions(final JTextArea editor, final String text) { |
70 |
SuggestionController suggestion = new SuggestionController(uimanager.getManager().getHelpManager(), editor, text);
|
71 |
if( suggestion.isEmpty() ) {
|
72 |
I18nManager I18nManager = ToolsLocator.getI18nManager(); |
73 |
JOptionPane.showMessageDialog(
|
74 |
editor, |
75 |
I18nManager.getTranslation("_cant_show_suggestions")
|
76 |
+"\n"
|
77 |
+ I18nManager.getTranslation("_No_suggestions_found_installed_in_the_system")
|
78 |
+"\n"
|
79 |
+"\n"
|
80 |
+ I18nManager.getTranslation("_Try_installing_a_package_of_documentation_for_scripting_with_javadocs"),
|
81 |
I18nManager.getTranslation("_information"),
|
82 |
JOptionPane.INFORMATION_MESSAGE
|
83 |
); |
84 |
} else {
|
85 |
suggestion.showSuggestion(); |
86 |
} |
87 |
} |
88 |
|
89 |
public JDialogContent getHelpPopup(final JTextArea editor, final String text) { |
90 |
return new JDialogContent(uimanager, "help-browser", "Select JavaDoc", "Select the JavaDoc that you want to see", new JHelpSelection(uimanager, text, editor)); |
91 |
} |
92 |
|
93 |
public void saveScript(JPropertiesScript properties) { |
94 |
|
95 |
if (properties != null) { |
96 |
script.setName(properties.name.getText()); |
97 |
script.setDescription(properties.description.getText()); |
98 |
script.setCreatedBy(properties.createdBy.getText()); |
99 |
script.setVersion(properties.version.getText()); |
100 |
script.setCode(this.getText());
|
101 |
|
102 |
script.save(); |
103 |
} |
104 |
} |
105 |
|
106 |
public static class MyKeyListener implements KeyListener { |
107 |
|
108 |
private final EditorHelper editorHelper; |
109 |
|
110 |
MyKeyListener(EditorHelper editorHelper) { |
111 |
this.editorHelper = editorHelper;
|
112 |
} |
113 |
|
114 |
private Component findParentComponent(Component comp, Class[] parentClasses) { |
115 |
while( comp != null ) { |
116 |
for (Class parentClass : parentClasses) { |
117 |
if( parentClass.isInstance(comp) ) {
|
118 |
return comp;
|
119 |
} |
120 |
} |
121 |
comp = comp.getParent(); |
122 |
} |
123 |
return null; |
124 |
} |
125 |
|
126 |
private JEditor getJEditor(KeyEvent event) { |
127 |
JEditor editor = null;
|
128 |
|
129 |
try {
|
130 |
JTextArea textArea = (JTextArea) event.getSource(); |
131 |
Component component = this.findParentComponent( |
132 |
textArea, |
133 |
new Class[] { JEditor.class } |
134 |
); |
135 |
editor = (JEditor) component; |
136 |
} catch (Throwable t) { |
137 |
logger.debug("Can't find editor component.",t);
|
138 |
} |
139 |
return editor;
|
140 |
} |
141 |
|
142 |
private void setUnsavedMark(JEditor editor, boolean mark) { |
143 |
try {
|
144 |
JMyTabbedPane tabs = (JMyTabbedPane) editor.getParent(); |
145 |
int index = tabs.getSelectedIndex();
|
146 |
tabs.setTabChanged(index, mark); |
147 |
} catch (Throwable t) { |
148 |
// Ignore
|
149 |
} |
150 |
} |
151 |
|
152 |
private String getWordToSearch(JTextArea textArea) throws BadLocationException { |
153 |
|
154 |
// Si hay un texto seleccionado devolbemos ese
|
155 |
String selected = textArea.getSelectedText();
|
156 |
if (selected != null) { |
157 |
return selected;
|
158 |
} |
159 |
|
160 |
// Si no hay texto seleccionado, buscamos la palabra bajo el cursor,
|
161 |
// la seleccionamos y devolbemos.
|
162 |
int curpos = textArea.getSelectionStart();
|
163 |
int curline = textArea.getLineOfOffset(curpos);
|
164 |
int beginline = textArea.getLineStartOffset(curline);
|
165 |
int endline = textArea.getLineEndOffset(curline);
|
166 |
String line = textArea.getText(beginline, endline - beginline);
|
167 |
|
168 |
// Buscamos el principio de palabra
|
169 |
int beginword = curpos - beginline - 1; |
170 |
while (beginword > 0) { |
171 |
char c = line.charAt(beginword);
|
172 |
if (Character.isLetterOrDigit(c)) { |
173 |
beginword--; |
174 |
} else {
|
175 |
break;
|
176 |
} |
177 |
} |
178 |
// Buscamos el final de la palabra
|
179 |
int maxlen = line.length();
|
180 |
int endword = curpos - beginline;
|
181 |
while (endword < maxlen) {
|
182 |
char c = line.charAt(endword);
|
183 |
if (Character.isLetterOrDigit(c)) { |
184 |
endword++; |
185 |
} else {
|
186 |
break;
|
187 |
} |
188 |
} |
189 |
String word = line.substring(beginword + 1, endword); |
190 |
textArea.setSelectionStart(beginline + beginword + 1);
|
191 |
textArea.setSelectionEnd(beginline + endword); |
192 |
return word;
|
193 |
|
194 |
} |
195 |
|
196 |
private void doSave(JEditor editor) { |
197 |
editor.save(); |
198 |
setUnsavedMark(editor, false);
|
199 |
} |
200 |
|
201 |
private void doShowHelp(JEditor editor, JTextArea textArea) { |
202 |
try {
|
203 |
String word = getWordToSearch(textArea);
|
204 |
JDialogContent dialog = editorHelper.getHelpPopup(textArea, word); |
205 |
dialog.showDialog(); |
206 |
} catch (Throwable t) { |
207 |
// Ignore
|
208 |
} |
209 |
|
210 |
} |
211 |
|
212 |
private void doSuggestions(JEditor editor, JTextArea textArea) { |
213 |
try {
|
214 |
String word = getWordToSearch(textArea);
|
215 |
editorHelper.getSuggestions(textArea, word); |
216 |
} catch (Throwable t) { |
217 |
logger.info("Can't show suggestions.",t);
|
218 |
} |
219 |
} |
220 |
|
221 |
private void doScriptChanged(JEditor editor) { |
222 |
editor.getScript().setSaved(false);
|
223 |
setUnsavedMark(editor, true);
|
224 |
} |
225 |
|
226 |
private void doRunScript(JEditor editor) { |
227 |
editor.getScript().runAsTask(null);
|
228 |
} |
229 |
|
230 |
public void keyReleased(KeyEvent arg0) { |
231 |
int keyPressed = arg0.getKeyCode();
|
232 |
int modifier = arg0.getModifiers();
|
233 |
|
234 |
// System.out.println("keyReleased:: modifier:"+Integer.toHexString(modifier)+", keyPressed:"+Integer.toHexString(keyPressed));
|
235 |
JTextArea textArea = (JTextArea) arg0.getSource(); |
236 |
JEditor editor = getJEditor(arg0); |
237 |
|
238 |
if (editor == null) { |
239 |
return;
|
240 |
} |
241 |
|
242 |
switch (keyPressed) {
|
243 |
case KeyEvent.VK_F1: |
244 |
if (modifier == 0) { |
245 |
doShowHelp(editor, textArea); |
246 |
} |
247 |
break;
|
248 |
case KeyEvent.VK_F5: |
249 |
if (modifier == 0) { |
250 |
doRunScript(editor); |
251 |
} |
252 |
break;
|
253 |
case KeyEvent.VK_S: |
254 |
if (modifier == KeyEvent.CTRL_MASK) { |
255 |
doSave(editor); |
256 |
} |
257 |
break;
|
258 |
case KeyEvent.VK_SPACE: |
259 |
if (modifier == KeyEvent.CTRL_MASK |
260 |
|| modifier == KeyEvent.ALT_MASK) {
|
261 |
doSuggestions(editor, textArea); |
262 |
} |
263 |
break;
|
264 |
case KeyEvent.VK_ENTER: |
265 |
if (modifier == KeyEvent.CTRL_MASK) { |
266 |
doSuggestions(editor, textArea); |
267 |
} |
268 |
break;
|
269 |
default:
|
270 |
break;
|
271 |
} |
272 |
} |
273 |
|
274 |
public void keyPressed(KeyEvent arg0) { |
275 |
int keyPressed = arg0.getKeyCode();
|
276 |
int modifier = arg0.getModifiers();
|
277 |
|
278 |
// System.out.println("keyPressed:: modifier:"+Integer.toHexString(modifier)+", keyPressed:"+Integer.toHexString(keyPressed));
|
279 |
} |
280 |
|
281 |
public void keyTyped(KeyEvent arg0) { |
282 |
char keyChar = arg0.getKeyChar();
|
283 |
if (keyChar != KeyEvent.CHAR_UNDEFINED) { |
284 |
JTextArea textArea = (JTextArea) arg0.getSource(); |
285 |
JEditor editor = getJEditor(arg0); |
286 |
if( editor != null ) { |
287 |
doScriptChanged(editor); |
288 |
} |
289 |
} |
290 |
|
291 |
} |
292 |
} |
293 |
|
294 |
public ScriptingManager getManager() {
|
295 |
return this.uimanager.getManager(); |
296 |
} |
297 |
|
298 |
public ScriptingUIManager getUIManager() {
|
299 |
return this.uimanager; |
300 |
} |
301 |
|
302 |
} |