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