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