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

History | View | Annotate | Download (9.81 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.sql.Savepoint;
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.Set;
16

    
17
import javax.swing.DefaultListModel;
18
import javax.swing.JEditorPane;
19
import javax.swing.JFrame;
20
import javax.swing.JList;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23
import javax.swing.JTabbedPane;
24
import javax.swing.JViewport;
25
import javax.swing.ListSelectionModel;
26
import javax.swing.event.CaretEvent;
27
import javax.swing.event.CaretListener;
28
import javax.swing.event.ListSelectionEvent;
29
import javax.swing.event.ListSelectionListener;
30
import javax.swing.text.BadLocationException;
31

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

    
40

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

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

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

    
108
                return panel;
109
        }
110

    
111
        public void launchException(CaretListener listener, int excId, String command, int lineNumber, int columnNumber){
112
                this.defaultActionlistener.actionPerformed(
113
                                new EditorActionEvent(listener, excId, command, lineNumber, columnNumber));
114
        }
115
        
116
        public void getSuggestions(final JEditorPane editor,final String text) {
117
                HashMap methods = (HashMap) 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 keys = methods.keySet();
154
                        Object[] l = keys.toArray();
155

    
156
                        ScriptingHelpMethod helpMethod = (ScriptingHelpMethod) methods.get(l[x]);
157
                        Iterator it = helpMethod.iterator();
158
                        while(it.hasNext()){
159
                                ScriptingHelpClass cn = (ScriptingHelpClass)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 JDialog(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 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
                                        DefaultJCodeEditor jCode = (DefaultJCodeEditor)tabsCode.getParent();
239
                
240
                                        if(keyPressed==KeyEvent.VK_F5){
241
                                                jCode.getScript().run();
242
                                        }else{
243
                                                if(keyPressed==KeyEvent.VK_S && modifier==KeyEvent.CTRL_MASK){
244
                                                        JTabbedPane tabs = (JTabbedPane)jCode.getParent();
245
                                                                                
246
                                                        int pestana = tabs.getSelectedIndex();
247
                                                        
248
                                                        String title = tabs.getTitleAt(pestana);
249
                                                        if (!jCode.getScript().isSaved()){
250
                                                                if(title.charAt(0)=='*' && title.length()>1){
251
                                                                        tabs.setTitleAt(pestana, title.substring(1));
252
                                                                }
253
                                                                jCode.save();
254
                                                        }
255
                                                }
256
                                        }
257
                                }
258
                        }
259
                }
260

    
261
                public void keyReleased(KeyEvent arg0) {
262
                        // TODO Auto-generated method stub
263

    
264
                }
265

    
266
                public void keyTyped(KeyEvent arg0) {
267
                        int keyPressed = arg0.getKeyChar();
268
                        int modifier = arg0.getModifiers();
269
                        
270
                        if(modifier==0){
271
                                JEditorPane jEditor = (JEditorPane)arg0.getSource();
272
                                JViewport jView = (JViewport)jEditor.getParent();
273
                                JScrollPane jScroll = (JScrollPane)jView.getParent();
274
                                JPanel panel = (JPanel)jScroll.getParent();
275
                                JTabbedPane tabsCode = (JTabbedPane)panel.getParent();
276
                                DefaultJCodeEditor jCode = (DefaultJCodeEditor)tabsCode.getParent();
277
                                JTabbedPane tabs = (JTabbedPane)jCode.getParent();
278
                                                        
279
                                int pestana = tabs.getSelectedIndex();
280
                                
281
                                String title = tabs.getTitleAt(pestana);
282
                                if (jCode.getScript().isSaved()){
283
                                        tabs.setTitleAt(pestana, "*"+title);
284
                                        jCode.getScript().setSaved(false);
285
                                }
286
                                if(keyPressed==KeyEvent.VK_ENTER){
287
                                        String text = jEditor.getText();
288
                                        text = text.substring(0, jEditor.getCaretPosition());
289
                                        
290
                                        String[] lines = text.split("\n");
291
                                        String lastLine="";
292
                                        if(lines.length>1){
293
                                                lastLine=lines[lines.length-1];
294
                                        }else{
295
                                                lastLine = text;
296
                                        }
297
                                        int i=0;
298
                                        String s = "";
299
                                        while(i<lastLine.length() && lastLine.charAt(i)==KeyEvent.VK_SPACE){
300
                                                i++;
301
                                                s=s+" ";
302
                                        }
303
                                        jEditor.select(jEditor.getCaretPosition(),jEditor.getCaretPosition());
304
                                        jEditor.replaceSelection(s);
305
                                }
306
                        }
307
                }
308
        }
309

    
310
        public ScriptingManager getManager() {
311
                return this.uimanager.getManager();
312
        }
313

    
314
        public ScriptingUIManager getUIManager() {
315
                return this.uimanager;
316
        }
317
        
318
}