Statistics
| Revision:

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 @ 212

History | View | Annotate | Download (10.2 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

    
30
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpClass;
31
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpMethod;
32
import org.gvsig.scripting.ScriptingManager;
33
import org.gvsig.scripting.ScriptingScript;
34
import org.gvsig.scripting.swing.api.JEditor;
35
import org.gvsig.scripting.swing.api.ScriptingUIManager;
36
import org.gvsig.scripting.swing.impl.JDialogContent;
37
import org.gvsig.scripting.swing.impl.composer.DefaultJCodeEditor.EditorActionEvent;
38

    
39

    
40
public class EditorHelper {
41
        JEditorPane editorPanel;
42
        private final ScriptingUIManager uimanager;
43
        protected ActionListener defaultActionlistener = null;
44
        private final ScriptingScript script;
45
        
46
        EditorHelper(ScriptingUIManager uimanager, ScriptingScript script){
47
                this.uimanager=uimanager;
48
                this.script=script;
49
        }
50
        
51
        public String getText(){
52
                return editorPanel.getText();
53
        }
54
        
55
        public JEditorPane getJEditorPanel(){
56
                return this.editorPanel;
57
        }
58
        
59
        public void addDefaultActionListener(ActionListener actionlistener) {
60
                this.defaultActionlistener = actionlistener;  
61
        }
62

    
63
        public JPanel getPropertiesPanel(){
64
                return new JPropertiesScript(this.script);
65
        }        
66
        
67
        
68
        protected JPanel getCodePanel(){
69
                JPanel panel = new JPanel();
70
                panel.setLayout( new BorderLayout() );
71
                
72
                editorPanel = uimanager.createSyntaxHighlightingPanel();
73
                editorPanel.setContentType("text/"+ script.getLangName());
74
                editorPanel.setText(script.getCode());
75

    
76
                editorPanel.addCaretListener(new CaretListener()
77
                {
78
                        public void caretUpdate(CaretEvent e)
79
                        {
80
                                int lineNumber = 1;
81
                                int columnNumber = 0;
82
                                int currentPosition = editorPanel.getCaretPosition();
83
                                try
84
                                {
85
                                        String strText = editorPanel.getDocument().getText(0, currentPosition);
86
                                        char arrText[] = strText.toCharArray();
87
                                        for(int i=0; i<strText.length(); i++)
88
                                        {
89
                                                if(arrText[i] == '\n') lineNumber++;
90
                                        }
91
                                        columnNumber = currentPosition - strText.lastIndexOf('\n');
92
                                }
93
                                catch(BadLocationException ble)
94
                                {
95
                                        System.err.println(ble);
96
                                }
97
                                launchException(this,1,"position",lineNumber,columnNumber);
98
                        }
99
                        
100
                });
101
                
102
                editorPanel.addKeyListener(new MyKeyListener(this));
103
                
104
                panel.add(new JScrollPane(editorPanel),BorderLayout.CENTER);
105
                panel.setVisible(true);
106

    
107
                return panel;
108
        }
109

    
110
        public void launchException(CaretListener listener, int excId, String command, int lineNumber, int columnNumber){
111
                this.defaultActionlistener.actionPerformed(
112
                                new EditorActionEvent(listener, excId, command, lineNumber, columnNumber));
113
        }
114
        
115
        public void getSuggestions(final JEditorPane editor,final String text) {
116
        Map<String, ScriptingHelpMethod> methods =
117
            uimanager.getManager().getHelpManager().findMethods(text);
118

    
119
                final JFrame popupWindow = new JFrame("Suggestions Window");
120

    
121
        popupWindow.addWindowFocusListener(new WindowFocusListener() {
122
            public void windowGainedFocus(WindowEvent e) {
123

    
124
            }
125

    
126
            public void windowLostFocus(WindowEvent e) {
127
                     popupWindow.setVisible(false);
128
            }
129
        });
130

    
131
                ListSelectionListener menuListener = new ListSelectionListener() {
132
                        public void valueChanged(ListSelectionEvent arg0) {
133
                                if(arg0.getValueIsAdjusting()){
134
                                        String[] s = ((JList)arg0.getSource()).getSelectedValue().toString().split(" - ");
135
                                        String selection = "";
136
                                        if(s.length==1){
137
                                                selection = s[0];
138
                                        }else{
139
                                                for(int i=0;i<s.length-1;i++){
140
                                                        selection=selection+s[i];
141
                                                }
142
                                        }
143
                                        editor.select(editor.getCaretPosition()-text.length(), editor.getCaretPosition());
144
                                        editor.replaceSelection(selection);
145
                                        popupWindow.setVisible(false);
146
                                }
147
                        }
148

    
149
                };
150
                DefaultListModel listModel = new DefaultListModel();
151
        
152
                for(int x=0;x<methods.size();x++){
153
            Set<String> keys = methods.keySet();
154
                        Object[] l = keys.toArray();
155

    
156
                        ScriptingHelpMethod helpMethod = methods.get(l[x]);
157
            Iterator<ScriptingHelpClass> it = helpMethod.iterator();
158
                        while(it.hasNext()){
159
                                ScriptingHelpClass cn = it.next();
160
                                listModel.addElement(helpMethod.getName()+" - "+cn.getName());                                
161
                        
162
                        }
163
                }
164
                
165
                JList list = new JList(listModel);
166
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
167
                list.setLayoutOrientation(JList.VERTICAL);
168
                list.setVisibleRowCount(0);
169
                list.addListSelectionListener(menuListener);
170
                JScrollPane scroll = new JScrollPane(list);
171
                
172
                popupWindow.setUndecorated(true);
173

    
174
                popupWindow.setLayout(new BorderLayout());
175
                if(listModel.getSize()==0){
176
                        popupWindow.setSize(new Dimension(300, 50));
177
                        JScrollPane s = new JScrollPane(new Label("No suggestions availables to the text '"+text+"'."));
178
                        popupWindow.add(s, BorderLayout.CENTER);
179
                }else{
180
                        popupWindow.setSize(new Dimension(300, 200));
181
                        popupWindow.add(scroll, BorderLayout.CENTER);
182
                }
183
                popupWindow.setLocation((editor.getLocationOnScreen().x+editor.getCaret().getMagicCaretPosition().x+3), (editor.getLocationOnScreen().y+editor.getCaret().getMagicCaretPosition().y+5));
184
                popupWindow.setVisible(true);
185
        }
186

    
187
        public void getHelpPopup(final JEditorPane editor,final String text){
188
                new JDialogContent(uimanager, "help-browser","Select JavaDoc","Select the JavaDoc that you want to see",new JHelpSelection(uimanager,text));
189
        }
190
        
191
        public void saveScript(JPropertiesScript properties){
192

    
193
                if(properties!=null){
194
                        script.setName(properties.name.getText());
195
                        script.setDescription(properties.description.getText());
196
                        script.setCreatedBy(properties.createdBy.getText());
197
                        script.setVersion(properties.version.getText());
198
                        script.setCode(this.getText());
199
                        
200
                        script.save();
201
                }
202
        }
203
        
204
        public static class MyKeyListener implements KeyListener{
205

    
206
                private final EditorHelper editorHelper;
207

    
208
                MyKeyListener(EditorHelper editorHelper){
209
                        this.editorHelper = editorHelper;
210
                }
211
                
212
                public void keyPressed(KeyEvent arg0) {
213
                        // TODO Auto-generated method stub
214
                        JEditorPane jeditorpane = ((JEditorPane)arg0.getSource());
215

    
216
                        //Obtenemos la selección con jeditorpane.getSelectedText()
217
                        //Mostramos el PopUp con las opciones que deseemos
218
                        int keyPressed = arg0.getKeyCode();
219
                        int modifier = arg0.getModifiers();
220

    
221
                        if(keyPressed==KeyEvent.VK_SPACE && modifier==KeyEvent.CTRL_MASK){
222
                                String code = jeditorpane.getText();
223
                                code = code.substring(0, jeditorpane.getCaretPosition());
224
                                String[] textSelected = code.split("[^0-9a-zA-Z]");
225
                                editorHelper.getSuggestions(jeditorpane, textSelected[textSelected.length-1]);
226
                        }else{
227
                                if(keyPressed==KeyEvent.VK_F1){
228
                                        String code = jeditorpane.getText();
229
                                        code = code.substring(0, jeditorpane.getCaretPosition());
230
                                        String[] textSelected = code.split("[^0-9a-zA-Z]");
231
                                        editorHelper.getHelpPopup(jeditorpane, textSelected[textSelected.length-1]);
232
                                }else{
233
                                        JEditorPane jEditor = (JEditorPane)arg0.getSource();
234
                                        JViewport jView = (JViewport)jEditor.getParent();
235
                                        JScrollPane jScroll = (JScrollPane)jView.getParent();
236
                                        JPanel panel = (JPanel)jScroll.getParent();
237
                                        JTabbedPane tabsCode = (JTabbedPane)panel.getParent();
238
                                        JEditor jCode = null;
239
                                        if(tabsCode.getParent() instanceof DefaultJCodeEditor){
240
                                                jCode = (DefaultJCodeEditor)tabsCode.getParent();
241
                                        }else {
242
                                                jCode = (DefaultJDialogEditor)tabsCode.getParent();
243
                                        }
244
                                        if(keyPressed==KeyEvent.VK_F5){
245
                                                jCode.getScript().run();
246
                                        }else{
247
                                                if(keyPressed==KeyEvent.VK_S && modifier==KeyEvent.CTRL_MASK){
248
                                                        JTabbedPane tabs = (JTabbedPane)jCode.getParent();
249
                                                                                
250
                                                        int pestana = tabs.getSelectedIndex();
251
                                                        
252
                                                        String title = tabs.getTitleAt(pestana);
253
                                                        if (!jCode.getScript().isSaved()){
254
                                                                if(title.charAt(0)=='*' && title.length()>1){
255
                                                                        tabs.setTitleAt(pestana, title.substring(1));
256
                                                                }
257
                                                                jCode.save();
258
                                                        }
259
                                                }
260
                                        }
261
                                }
262
                        }
263
                }
264

    
265
                public void keyReleased(KeyEvent arg0) {
266
                        // TODO Auto-generated method stub
267

    
268
                }
269

    
270
                public void keyTyped(KeyEvent arg0) {
271
                        int keyPressed = arg0.getKeyChar();
272
                        int modifier = arg0.getModifiers();
273
                        
274
                        if(modifier==0){
275
                                JEditorPane jEditor = (JEditorPane)arg0.getSource();
276
                                JViewport jView = (JViewport)jEditor.getParent();
277
                                JScrollPane jScroll = (JScrollPane)jView.getParent();
278
                                JPanel panel = (JPanel)jScroll.getParent();
279
                                JTabbedPane tabsCode = (JTabbedPane)panel.getParent();
280
                                JTabbedPane tabs = null;
281
                                JEditor jCode = null;
282
                                if (tabsCode.getParent() instanceof DefaultJCodeEditor){
283
                                        jCode = (DefaultJCodeEditor)tabsCode.getParent();
284
                                        
285
                                } else {
286
                                        if(tabsCode.getParent() instanceof DefaultJDialogEditor){
287
                                                jCode = (DefaultJDialogEditor)tabsCode.getParent();
288
                                        }
289
                                }
290
                                
291
                                tabs = (JTabbedPane)jCode.getParent();
292
                                int pestana = tabs.getSelectedIndex();
293
                                
294
                                String title = tabs.getTitleAt(pestana);
295
                                if (jCode.getScript().isSaved()){
296
                                        tabs.setTitleAt(pestana, "*"+title);
297
                                        jCode.getScript().setSaved(false);
298
                                }
299

    
300
                                if(keyPressed==KeyEvent.VK_ENTER){
301
                                        String text = jEditor.getText();
302
                                        text = text.substring(0, jEditor.getCaretPosition());
303
                                        
304
                                        String[] lines = text.split("\n");
305
                                        String lastLine="";
306
                                        if(lines.length>1){
307
                                                lastLine=lines[lines.length-1];
308
                                        }else{
309
                                                lastLine = text;
310
                                        }
311
                                        int i=0;
312
                                        String s = "";
313
                                        while(i<lastLine.length() && lastLine.charAt(i)==KeyEvent.VK_SPACE){
314
                                                i++;
315
                                                s=s+" ";
316
                                        }
317
                                        jEditor.select(jEditor.getCaretPosition(),jEditor.getCaretPosition());
318
                                        jEditor.replaceSelection(s);
319
                                }
320
                        }
321
                }
322
        }
323

    
324
        public ScriptingManager getManager() {
325
                return this.uimanager.getManager();
326
        }
327

    
328
        public ScriptingUIManager getUIManager() {
329
                return this.uimanager;
330
        }
331
        
332
}