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