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 | 165 | jobacas | 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 | 412 | jjdelcerro | import javax.swing.JTextArea; |
11 | 165 | jobacas | import javax.swing.JViewport; |
12 | 461 | jjdelcerro | import javax.swing.text.BadLocationException; |
13 | 368 | jjdelcerro | import javax.swing.text.JTextComponent; |
14 | 165 | jobacas | import org.gvsig.scripting.ScriptingManager; |
15 | import org.gvsig.scripting.ScriptingScript; |
||
16 | 200 | jbadia | import org.gvsig.scripting.swing.api.JEditor; |
17 | 165 | jobacas | import org.gvsig.scripting.swing.api.ScriptingUIManager; |
18 | 368 | jjdelcerro | import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
19 | 180 | jobacas | import org.gvsig.scripting.swing.impl.JDialogContent; |
20 | 461 | jjdelcerro | import org.slf4j.Logger; |
21 | import org.slf4j.LoggerFactory; |
||
22 | 165 | jobacas | |
23 | public class EditorHelper { |
||
24 | 461 | jjdelcerro | private static Logger logger = LoggerFactory.getLogger(EditorHelper.class); |
25 | |||
26 | SyntaxtHighlightTextComponent editorPanel; |
||
27 | private final ScriptingUIManager uimanager; |
||
28 | private final ScriptingScript script; |
||
29 | 165 | jobacas | |
30 | 461 | jjdelcerro | EditorHelper(ScriptingUIManager uimanager, ScriptingScript script) { |
31 | this.uimanager = uimanager;
|
||
32 | this.script = script;
|
||
33 | } |
||
34 | 165 | jobacas | |
35 | 461 | jjdelcerro | public String getText() { |
36 | return editorPanel.getText();
|
||
37 | } |
||
38 | 165 | jobacas | |
39 | 461 | jjdelcerro | public JTextComponent getJEditorPanel() { |
40 | return this.editorPanel.getJTextComponent(); |
||
41 | } |
||
42 | 165 | jobacas | |
43 | 461 | jjdelcerro | public void addDefaultActionListener(ActionListener actionlistener) { |
44 | editorPanel.addUpdateCaretPositionActionListener(actionlistener); |
||
45 | } |
||
46 | 165 | jobacas | |
47 | 461 | jjdelcerro | public JPanel getPropertiesPanel() { |
48 | return new JPropertiesScript(this.script); |
||
49 | } |
||
50 | 165 | jobacas | |
51 | 461 | jjdelcerro | protected JPanel getCodePanel() { |
52 | JPanel panel = new JPanel(); |
||
53 | panel.setLayout(new BorderLayout()); |
||
54 | 165 | jobacas | |
55 | 461 | jjdelcerro | editorPanel = (SyntaxtHighlightTextComponent) uimanager.createSyntaxHighlightingPanel(); |
56 | 468 | jjdelcerro | editorPanel.setContentType(script.getMimeType()); |
57 | 461 | jjdelcerro | editorPanel.setText(script.getCode()); |
58 | |||
59 | editorPanel.addKeyListener(new MyKeyListener(this)); |
||
60 | |||
61 | 468 | jjdelcerro | panel.add(editorPanel.asJComponent(), BorderLayout.CENTER);
|
62 | 461 | jjdelcerro | 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 | 165 | jobacas | } |
115 | 461 | jjdelcerro | return editor;
|
116 | } |
||
117 | 165 | jobacas | |
118 | 461 | jjdelcerro | 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 | 165 | jobacas | } |
135 | 461 | jjdelcerro | } |
136 | 165 | jobacas | |
137 | 461 | jjdelcerro | private String getWordToSearch(JTextArea textArea) throws BadLocationException { |
138 | 165 | jobacas | |
139 | 461 | jjdelcerro | // Si hay un texto seleccionado devolbemos ese
|
140 | String selected = textArea.getSelectedText();
|
||
141 | if (selected != null) { |
||
142 | return selected;
|
||
143 | } |
||
144 | 165 | jobacas | |
145 | 461 | jjdelcerro | // 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 | 165 | jobacas | |
153 | 461 | jjdelcerro | // 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 | 165 | jobacas | |
179 | 461 | jjdelcerro | } |
180 | 165 | jobacas | |
181 | 461 | jjdelcerro | private void doSave(JEditor editor) { |
182 | editor.save(); |
||
183 | setUnsavedMark(editor, false);
|
||
184 | } |
||
185 | 165 | jobacas | |
186 | 461 | jjdelcerro | 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 | 165 | jobacas | |
195 | 461 | jjdelcerro | } |
196 | 165 | jobacas | |
197 | 461 | jjdelcerro | 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 | 165 | jobacas | |
206 | 461 | jjdelcerro | private void doScriptChanged(JEditor editor) { |
207 | editor.getScript().setSaved(false);
|
||
208 | setUnsavedMark(editor, true);
|
||
209 | } |
||
210 | 165 | jobacas | |
211 | 461 | jjdelcerro | private void doRunScript(JEditor editor) { |
212 | editor.getScript().runAsTask(null);
|
||
213 | } |
||
214 | 165 | jobacas | |
215 | 461 | jjdelcerro | public void keyReleased(KeyEvent arg0) { |
216 | int keyPressed = arg0.getKeyCode();
|
||
217 | int modifier = arg0.getModifiers();
|
||
218 | 165 | jobacas | |
219 | 461 | jjdelcerro | // System.out.println("keyReleased:: modifier:"+Integer.toHexString(modifier)+", keyPressed:"+Integer.toHexString(keyPressed));
|
220 | JTextArea textArea = (JTextArea) arg0.getSource(); |
||
221 | JEditor editor = getJEditor(arg0); |
||
222 | 200 | jbadia | |
223 | 461 | jjdelcerro | if (editor == null) { |
224 | return;
|
||
225 | } |
||
226 | 412 | jjdelcerro | |
227 | 461 | jjdelcerro | 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 | 412 | jjdelcerro | |
259 | 461 | jjdelcerro | public void keyPressed(KeyEvent arg0) { |
260 | int keyPressed = arg0.getKeyCode();
|
||
261 | int modifier = arg0.getModifiers();
|
||
262 | 412 | jjdelcerro | |
263 | 461 | jjdelcerro | // System.out.println("keyPressed:: modifier:"+Integer.toHexString(modifier)+", keyPressed:"+Integer.toHexString(keyPressed));
|
264 | } |
||
265 | 412 | jjdelcerro | |
266 | 461 | jjdelcerro | 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 | 468 | jjdelcerro | if( editor != null ) { |
272 | doScriptChanged(editor); |
||
273 | } |
||
274 | 461 | jjdelcerro | } |
275 | 412 | jjdelcerro | |
276 | 461 | jjdelcerro | } |
277 | } |
||
278 | 165 | jobacas | |
279 | 461 | jjdelcerro | public ScriptingManager getManager() {
|
280 | return this.uimanager.getManager(); |
||
281 | } |
||
282 | 165 | jobacas | |
283 | 461 | jjdelcerro | public ScriptingUIManager getUIManager() {
|
284 | return this.uimanager; |
||
285 | } |
||
286 | |||
287 | 165 | jobacas | } |