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 @ 301
History | View | Annotate | Download (10.6 KB)
1 |
package org.gvsig.scripting.swing.impl.composer; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Label; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.KeyEvent; |
8 |
import java.awt.event.KeyListener; |
9 |
import java.awt.event.WindowEvent; |
10 |
import java.awt.event.WindowFocusListener; |
11 |
import java.util.Iterator; |
12 |
import java.util.Map; |
13 |
import java.util.Set; |
14 |
|
15 |
import javax.swing.DefaultListModel; |
16 |
import javax.swing.JEditorPane; |
17 |
import javax.swing.JFrame; |
18 |
import javax.swing.JList; |
19 |
import javax.swing.JPanel; |
20 |
import javax.swing.JScrollPane; |
21 |
import javax.swing.JTabbedPane; |
22 |
import javax.swing.JViewport; |
23 |
import javax.swing.ListSelectionModel; |
24 |
import javax.swing.event.CaretEvent; |
25 |
import javax.swing.event.CaretListener; |
26 |
import javax.swing.event.ListSelectionEvent; |
27 |
import javax.swing.event.ListSelectionListener; |
28 |
import javax.swing.text.BadLocationException; |
29 |
import javax.swing.text.Document; |
30 |
|
31 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpClass; |
32 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpMethod; |
33 |
import org.gvsig.scripting.ScriptingManager; |
34 |
import org.gvsig.scripting.ScriptingScript; |
35 |
import org.gvsig.scripting.swing.api.JEditor; |
36 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
37 |
import org.gvsig.scripting.swing.impl.JDialogContent; |
38 |
import org.gvsig.scripting.swing.impl.composer.DefaultJCodeEditor.EditorActionEvent; |
39 |
import org.gvsig.scripting.swing.impl.syntaxhighlight.styles.PythonStyledDocument; |
40 |
|
41 |
|
42 |
public class EditorHelper { |
43 |
JEditorPane editorPanel;
|
44 |
private final ScriptingUIManager uimanager; |
45 |
protected ActionListener defaultActionlistener = null; |
46 |
private final ScriptingScript script; |
47 |
private PythonStyledDocument pydoc = null; |
48 |
|
49 |
EditorHelper(ScriptingUIManager uimanager, ScriptingScript script){ |
50 |
this.uimanager=uimanager;
|
51 |
this.script=script;
|
52 |
} |
53 |
|
54 |
public String getText(){ |
55 |
return editorPanel.getText();
|
56 |
} |
57 |
|
58 |
public JEditorPane getJEditorPanel(){ |
59 |
return this.editorPanel; |
60 |
} |
61 |
|
62 |
public void addDefaultActionListener(ActionListener actionlistener) { |
63 |
this.defaultActionlistener = actionlistener;
|
64 |
} |
65 |
|
66 |
public JPanel getPropertiesPanel(){ |
67 |
return new JPropertiesScript(this.script); |
68 |
} |
69 |
|
70 |
|
71 |
protected JPanel getCodePanel(){ |
72 |
JPanel panel = new JPanel(); |
73 |
panel.setLayout( new BorderLayout() ); |
74 |
|
75 |
editorPanel = uimanager.createSyntaxHighlightingPanel(); |
76 |
editorPanel.setContentType("text/"+ script.getLangName());
|
77 |
editorPanel.setText(script.getCode()); |
78 |
Document doc = editorPanel.getDocument();
|
79 |
if( doc instanceof PythonStyledDocument ) { |
80 |
pydoc = (PythonStyledDocument) doc; |
81 |
} |
82 |
|
83 |
editorPanel.addCaretListener(new CaretListener() |
84 |
{ |
85 |
public void caretUpdate(CaretEvent e) |
86 |
{ |
87 |
int lineNumber = 1; |
88 |
int columnNumber = 0; |
89 |
int currentPosition = editorPanel.getCaretPosition();
|
90 |
try
|
91 |
{ |
92 |
String strText = editorPanel.getDocument().getText(0, currentPosition); |
93 |
char arrText[] = strText.toCharArray(); |
94 |
for(int i=0; i<strText.length(); i++) |
95 |
{ |
96 |
if(arrText[i] == '\n') lineNumber++; |
97 |
} |
98 |
columnNumber = currentPosition - strText.lastIndexOf('\n');
|
99 |
} |
100 |
catch(BadLocationException ble) |
101 |
{ |
102 |
System.err.println(ble);
|
103 |
} |
104 |
if( pydoc != null ) { |
105 |
pydoc.setPosition(lineNumber, columnNumber); |
106 |
} |
107 |
launchException(this,1,"position",lineNumber,columnNumber); |
108 |
} |
109 |
|
110 |
}); |
111 |
|
112 |
editorPanel.addKeyListener(new MyKeyListener(this)); |
113 |
|
114 |
panel.add(new JScrollPane(editorPanel),BorderLayout.CENTER); |
115 |
panel.setVisible(true);
|
116 |
|
117 |
return panel;
|
118 |
} |
119 |
|
120 |
public void launchException(CaretListener listener, int excId, String command, int lineNumber, int columnNumber){ |
121 |
this.defaultActionlistener.actionPerformed(
|
122 |
new EditorActionEvent(listener, excId, command, lineNumber, columnNumber));
|
123 |
} |
124 |
|
125 |
public void getSuggestions(final JEditorPane editor,final String text) { |
126 |
Map<String, ScriptingHelpMethod> methods = |
127 |
uimanager.getManager().getHelpManager().findMethods(text); |
128 |
|
129 |
final JFrame popupWindow = new JFrame("Suggestions Window"); |
130 |
|
131 |
popupWindow.addWindowFocusListener(new WindowFocusListener() { |
132 |
public void windowGainedFocus(WindowEvent e) { |
133 |
|
134 |
} |
135 |
|
136 |
public void windowLostFocus(WindowEvent e) { |
137 |
popupWindow.setVisible(false);
|
138 |
} |
139 |
}); |
140 |
|
141 |
ListSelectionListener menuListener = new ListSelectionListener() { |
142 |
public void valueChanged(ListSelectionEvent arg0) { |
143 |
if(arg0.getValueIsAdjusting()){
|
144 |
String[] s = ((JList)arg0.getSource()).getSelectedValue().toString().split(" - "); |
145 |
String selection = ""; |
146 |
if(s.length==1){ |
147 |
selection = s[0];
|
148 |
}else{
|
149 |
for(int i=0;i<s.length-1;i++){ |
150 |
selection=selection+s[i]; |
151 |
} |
152 |
} |
153 |
editor.select(editor.getCaretPosition()-text.length(), editor.getCaretPosition()); |
154 |
editor.replaceSelection(selection); |
155 |
popupWindow.setVisible(false);
|
156 |
} |
157 |
} |
158 |
|
159 |
}; |
160 |
DefaultListModel listModel = new DefaultListModel(); |
161 |
|
162 |
for(int x=0;x<methods.size();x++){ |
163 |
Set<String> keys = methods.keySet(); |
164 |
Object[] l = keys.toArray(); |
165 |
|
166 |
ScriptingHelpMethod helpMethod = methods.get(l[x]); |
167 |
Iterator<ScriptingHelpClass> it = helpMethod.iterator();
|
168 |
while(it.hasNext()){
|
169 |
ScriptingHelpClass cn = it.next(); |
170 |
listModel.addElement(helpMethod.getName()+" - "+cn.getName());
|
171 |
|
172 |
} |
173 |
} |
174 |
|
175 |
JList list = new JList(listModel); |
176 |
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
177 |
list.setLayoutOrientation(JList.VERTICAL);
|
178 |
list.setVisibleRowCount(0);
|
179 |
list.addListSelectionListener(menuListener); |
180 |
JScrollPane scroll = new JScrollPane(list); |
181 |
|
182 |
popupWindow.setUndecorated(true);
|
183 |
|
184 |
popupWindow.setLayout(new BorderLayout()); |
185 |
if(listModel.getSize()==0){ |
186 |
popupWindow.setSize(new Dimension(300, 50)); |
187 |
JScrollPane s = new JScrollPane(new Label("No suggestions availables to the text '"+text+"'.")); |
188 |
popupWindow.add(s, BorderLayout.CENTER);
|
189 |
}else{
|
190 |
popupWindow.setSize(new Dimension(300, 200)); |
191 |
popupWindow.add(scroll, BorderLayout.CENTER);
|
192 |
} |
193 |
popupWindow.setLocation((editor.getLocationOnScreen().x+editor.getCaret().getMagicCaretPosition().x+3), (editor.getLocationOnScreen().y+editor.getCaret().getMagicCaretPosition().y+5)); |
194 |
popupWindow.setVisible(true);
|
195 |
} |
196 |
|
197 |
public void getHelpPopup(final JEditorPane editor,final String text){ |
198 |
new JDialogContent(uimanager, "help-browser","Select JavaDoc","Select the JavaDoc that you want to see",new JHelpSelection(uimanager,text)); |
199 |
} |
200 |
|
201 |
public void saveScript(JPropertiesScript properties){ |
202 |
|
203 |
if(properties!=null){ |
204 |
script.setName(properties.name.getText()); |
205 |
script.setDescription(properties.description.getText()); |
206 |
script.setCreatedBy(properties.createdBy.getText()); |
207 |
script.setVersion(properties.version.getText()); |
208 |
script.setCode(this.getText());
|
209 |
|
210 |
script.save(); |
211 |
} |
212 |
} |
213 |
|
214 |
public static class MyKeyListener implements KeyListener{ |
215 |
|
216 |
private final EditorHelper editorHelper; |
217 |
|
218 |
MyKeyListener(EditorHelper editorHelper){ |
219 |
this.editorHelper = editorHelper;
|
220 |
} |
221 |
|
222 |
public void keyPressed(KeyEvent arg0) { |
223 |
// TODO Auto-generated method stub
|
224 |
JEditorPane jeditorpane = ((JEditorPane)arg0.getSource()); |
225 |
|
226 |
//Obtenemos la selección con jeditorpane.getSelectedText()
|
227 |
//Mostramos el PopUp con las opciones que deseemos
|
228 |
int keyPressed = arg0.getKeyCode();
|
229 |
int modifier = arg0.getModifiers();
|
230 |
|
231 |
if(keyPressed==KeyEvent.VK_SPACE && modifier==KeyEvent.CTRL_MASK){ |
232 |
String code = jeditorpane.getText();
|
233 |
code = code.substring(0, jeditorpane.getCaretPosition());
|
234 |
String[] textSelected = code.split("[^0-9a-zA-Z]"); |
235 |
editorHelper.getSuggestions(jeditorpane, textSelected[textSelected.length-1]);
|
236 |
}else{
|
237 |
if(keyPressed==KeyEvent.VK_F1){ |
238 |
String code = jeditorpane.getText();
|
239 |
code = code.substring(0, jeditorpane.getCaretPosition());
|
240 |
String[] textSelected = code.split("[^0-9a-zA-Z]"); |
241 |
editorHelper.getHelpPopup(jeditorpane, textSelected[textSelected.length-1]);
|
242 |
}else{
|
243 |
JEditorPane jEditor = (JEditorPane)arg0.getSource(); |
244 |
JViewport jView = (JViewport)jEditor.getParent(); |
245 |
JScrollPane jScroll = (JScrollPane)jView.getParent(); |
246 |
JPanel panel = (JPanel)jScroll.getParent(); |
247 |
JTabbedPane tabsCode = (JTabbedPane)panel.getParent(); |
248 |
JEditor jCode = null;
|
249 |
if(tabsCode.getParent() instanceof DefaultJCodeEditor){ |
250 |
jCode = (DefaultJCodeEditor)tabsCode.getParent(); |
251 |
}else {
|
252 |
jCode = (DefaultJDialogEditor)tabsCode.getParent(); |
253 |
} |
254 |
if(keyPressed==KeyEvent.VK_F5){ |
255 |
jCode.getScript().run(); |
256 |
}else{
|
257 |
if(keyPressed==KeyEvent.VK_S && modifier==KeyEvent.CTRL_MASK){ |
258 |
JTabbedPane tabs = (JTabbedPane)jCode.getParent(); |
259 |
|
260 |
int pestana = tabs.getSelectedIndex();
|
261 |
|
262 |
String title = tabs.getTitleAt(pestana);
|
263 |
if (!jCode.getScript().isSaved()){
|
264 |
if(title.charAt(0)=='*' && title.length()>1){ |
265 |
tabs.setTitleAt(pestana, title.substring(1));
|
266 |
} |
267 |
jCode.save(); |
268 |
} |
269 |
} |
270 |
} |
271 |
} |
272 |
} |
273 |
} |
274 |
|
275 |
public void keyReleased(KeyEvent arg0) { |
276 |
// TODO Auto-generated method stub
|
277 |
|
278 |
} |
279 |
|
280 |
public void keyTyped(KeyEvent arg0) { |
281 |
int keyPressed = arg0.getKeyChar();
|
282 |
int modifier = arg0.getModifiers();
|
283 |
|
284 |
if(modifier==0){ |
285 |
JEditorPane jEditor = (JEditorPane)arg0.getSource(); |
286 |
JViewport jView = (JViewport)jEditor.getParent(); |
287 |
JScrollPane jScroll = (JScrollPane)jView.getParent(); |
288 |
JPanel panel = (JPanel)jScroll.getParent(); |
289 |
JTabbedPane tabsCode = (JTabbedPane)panel.getParent(); |
290 |
JTabbedPane tabs = null; |
291 |
JEditor jCode = null;
|
292 |
if (tabsCode.getParent() instanceof DefaultJCodeEditor){ |
293 |
jCode = (DefaultJCodeEditor)tabsCode.getParent(); |
294 |
|
295 |
} else {
|
296 |
if(tabsCode.getParent() instanceof DefaultJDialogEditor){ |
297 |
jCode = (DefaultJDialogEditor)tabsCode.getParent(); |
298 |
} |
299 |
} |
300 |
|
301 |
tabs = (JTabbedPane)jCode.getParent();
|
302 |
int pestana = tabs.getSelectedIndex();
|
303 |
|
304 |
String title = tabs.getTitleAt(pestana);
|
305 |
if (jCode.getScript().isSaved()){
|
306 |
tabs.setTitleAt(pestana, "*"+title);
|
307 |
jCode.getScript().setSaved(false);
|
308 |
} |
309 |
|
310 |
if(keyPressed==KeyEvent.VK_ENTER){ |
311 |
String text = jEditor.getText();
|
312 |
text = text.substring(0, jEditor.getCaretPosition());
|
313 |
|
314 |
String[] lines = text.split("\n"); |
315 |
String lastLine=""; |
316 |
if(lines.length>1){ |
317 |
lastLine=lines[lines.length-1];
|
318 |
}else{
|
319 |
lastLine = text; |
320 |
} |
321 |
int i=0; |
322 |
String s = ""; |
323 |
while(i<lastLine.length() && lastLine.charAt(i)==KeyEvent.VK_SPACE){ |
324 |
i++; |
325 |
s=s+" ";
|
326 |
} |
327 |
jEditor.select(jEditor.getCaretPosition(),jEditor.getCaretPosition()); |
328 |
jEditor.replaceSelection(s); |
329 |
} |
330 |
} |
331 |
} |
332 |
} |
333 |
|
334 |
public ScriptingManager getManager() {
|
335 |
return this.uimanager.getManager(); |
336 |
} |
337 |
|
338 |
public ScriptingUIManager getUIManager() {
|
339 |
return this.uimanager; |
340 |
} |
341 |
|
342 |
} |