Statistics
| Revision:

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 / DefaultJScriptingComposer.java @ 441

History | View | Annotate | Download (41.1 KB)

1
package org.gvsig.scripting.swing.impl.composer;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Dimension;
5
import java.awt.Font;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.io.File;
9
import java.io.OutputStream;
10
import java.io.PrintStream;
11
import java.util.ArrayList;
12
import java.util.Iterator;
13
import java.util.List;
14

    
15
import javax.swing.JButton;
16
import javax.swing.JLabel;
17
import javax.swing.JMenu;
18
import javax.swing.JMenuBar;
19
import javax.swing.JMenuItem;
20
import javax.swing.JOptionPane;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23
import javax.swing.JSplitPane;
24
import javax.swing.JTabbedPane;
25
import javax.swing.JTable;
26
import javax.swing.JTextArea;
27
import javax.swing.JToolBar;
28
import javax.swing.ListSelectionModel;
29
import javax.swing.SwingConstants;
30
import javax.swing.SwingUtilities;
31
import javax.swing.event.ChangeEvent;
32
import javax.swing.event.ChangeListener;
33
import javax.swing.event.ListSelectionEvent;
34
import javax.swing.event.ListSelectionListener;
35
import javax.swing.table.DefaultTableModel;
36
import javax.swing.text.JTextComponent;
37

    
38
import org.gvsig.scripting.CompileErrorException;
39
import org.gvsig.scripting.ExecuteErrorException;
40
import org.gvsig.scripting.ScriptingBaseScript;
41
import org.gvsig.scripting.ScriptingDialog;
42
import org.gvsig.scripting.ScriptingFolder;
43
import org.gvsig.scripting.ScriptingManager;
44
import org.gvsig.scripting.ScriptingNotification;
45
import org.gvsig.scripting.ScriptingScript;
46
import org.gvsig.scripting.ScriptingUnit;
47
import org.gvsig.scripting.swing.api.JCodeEditor;
48
import org.gvsig.scripting.swing.api.JEditor;
49
import org.gvsig.scripting.swing.api.JScriptingBrowser;
50
import org.gvsig.scripting.swing.api.JScriptingComposer;
51
import org.gvsig.scripting.swing.api.ScriptingUIManager;
52
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent.UpdateCaretPositionActionEvent;
53
import org.gvsig.scripting.swing.impl.DefaultJScriptingBrowser;
54
import org.gvsig.scripting.swing.impl.DefaultJScriptingLauncher;
55
import org.gvsig.scripting.swing.impl.DefaultJScriptingLauncher.LauncherActionEvent;
56
import org.gvsig.scripting.swing.impl.JDialogContent;
57
import org.gvsig.tools.observer.Observable;
58
import org.gvsig.tools.observer.Observer;
59
import org.gvsig.tools.swing.api.ToolsSwingLocator;
60
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
61
import org.gvsig.tools.task.AbstractMonitorableTask;
62
import org.gvsig.tools.task.SimpleTaskStatus;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66

    
67
public class DefaultJScriptingComposer extends JScriptingComposer implements Observer, ActionListener{
68

    
69
        /**
70
         * 
71
         */
72
        private static final long serialVersionUID = 1L;
73

    
74
        private static final Logger logger = LoggerFactory.getLogger(DefaultJScriptingComposer.class);
75
        
76
        public static final int CLOSE_ACTION = 1;
77
        public List<JEditor> unitsEditor;
78

    
79
        /* Generales */ 
80
        ScriptingUIManager uimanager;
81
        ScriptingManager manager;
82
        ClassLoader loader;
83

    
84
        /* Ventana inicial y componentes*/
85
        JSplitPane mainPane;
86
        JSplitPane editionPane;
87
        JTabbedPane problemsPane;
88
        JTabbedPane scriptEditors;
89
        JMenuBar menuBar;
90
        JToolBar file;
91
        JToolBar edit;
92
        JToolBar script;
93
        JToolBar help;
94
        JPanel statusBar;
95
        JLabel currentLine;
96
        JLabel statusBarMessage;
97
        JTextArea problems;
98
        JTextArea console;
99
        DefaultJScriptingLauncher launcher;
100
        JToolBar launcherButtons;
101
        PrintStream consolePrintStream = null;
102
        WindowManager windowsManager = null;
103
        
104
        /* JFrame para la creación de nuevos ScriptingBaseScripts o modificación de atributos */
105
        //JFrame NewUnitFrame; 
106

    
107
        /* Para el mantenimiento de la tabla de problemas en el código*/
108
        Object[][] tableData; 
109
        JTable table;
110
        DefaultTableModel tableModel;
111
        protected ActionListener defaultActionlistener = null;
112

    
113
        /**
114
         * Instanciación de un JComposer con todos sus elementos y acciones
115
         * @param manager
116
         */
117
        public DefaultJScriptingComposer(final ScriptingUIManager uimanager) {
118
                loader = this.getClass().getClassLoader();
119
                this.uimanager = uimanager;
120
                this.manager = uimanager.getManager();
121
            this.setWindowManager( ToolsSwingLocator.getWindowManager());
122

    
123
                unitsEditor = new ArrayList<JEditor>();
124

    
125
                initUI();
126
                
127
            System.setOut(this.getConsolePrintStream());         
128
            System.setErr(this.getConsolePrintStream());
129

    
130
        }
131
        
132
        public WindowManager getWindowManager() {
133
                return this.windowsManager;
134
        }
135
        
136
        public void setWindowManager(WindowManager windowsManager) {
137
                this.windowsManager = windowsManager;
138
        }
139
        
140
        private void initUI() {
141
                // Barra de menu
142
                menuBar = new JMenuBar();
143

    
144
                // Construimos los componentes del menu
145
                JMenu menu_file = new JMenu("File");
146
                JMenuItem menuItem_new = new JMenuItem("New",uimanager.getIcon("document-new"));
147
                menuItem_new.addActionListener(new ActionListener(){
148
                        public void actionPerformed(ActionEvent e) {
149
                                try {
150
                                        newScriptingBaseScript();
151
                                } catch (Exception e1) {
152
                                        // TODO Auto-generated catch block
153
                                        e1.printStackTrace();
154
                                }
155
                        }
156
                });
157
                menu_file.add(menuItem_new);
158
                menu_file.addSeparator();
159
                JMenuItem menuItem_close = new JMenuItem("Close document",uimanager.getIcon("emblem-unreadable"));
160
                menuItem_close.addActionListener(new ActionListener(){
161
                        public void actionPerformed(ActionEvent e) {
162
                                closeScriptingBaseScript();
163
                        }
164
                });
165
                menu_file.add(menuItem_close);
166
                JMenuItem menuItem_closeAll = new JMenuItem("Close all documents",uimanager.getIcon("emblem-unreadable"));
167
                menuItem_closeAll.addActionListener(new ActionListener(){
168
                        public void actionPerformed(ActionEvent e) {
169
                                closeAllScriptingBaseScript();
170
                        }
171
                });
172
                menu_file.add(menuItem_closeAll);
173
                menu_file.addSeparator();
174
                JMenuItem menuItem_run = new JMenuItem("Run",uimanager.getIcon("applications-system"));
175
                menuItem_run.addActionListener(new ActionListener(){
176
                        public void actionPerformed(ActionEvent e) {
177
                                runScriptingBaseScript();
178
                        }
179
                });
180
                menu_file.add(menuItem_run);
181
                menu_file.addSeparator();
182
                JMenuItem menuItem_save = new JMenuItem("Save",uimanager.getIcon("media-floppy"));
183
                menuItem_save.addActionListener(new ActionListener(){
184
                        public void actionPerformed(ActionEvent e) {
185
                                saveScriptingBaseScript();
186
                        }
187
                });
188
                menu_file.add(menuItem_save);
189
                menu_file.addSeparator();
190
                JMenuItem menuItem_exit = new JMenuItem("Close",uimanager.getIcon("system-log-out"));
191
                menuItem_exit.addActionListener(new ActionListener(){
192
                        public void actionPerformed(ActionEvent e) {
193
                                closeAllScriptingBaseScript();
194
                                closeWindow();
195
                                if(defaultActionlistener!=null){
196
                                        ActionEvent event = new ActionEvent(this,CLOSE_ACTION,"close");
197
                                        defaultActionlistener.actionPerformed(event);
198
                                }
199
                        
200
                        }
201
                });
202
                menu_file.add(menuItem_exit);
203

    
204
                JMenu menu_edit = new JMenu("Edit");
205
                JMenuItem menuItem_cut = new JMenuItem("cut",uimanager.getIcon("edit-cut"));
206
                menuItem_cut.addActionListener(new ActionListener(){
207
                        public void actionPerformed(ActionEvent e) {
208
                                cutScriptingBaseScript();
209
                        }
210
                });
211
                menu_edit.add(menuItem_cut);
212
                JMenuItem menuItem_copy = new JMenuItem("copy",uimanager.getIcon("edit-copy"));
213
                menuItem_copy.addActionListener(new ActionListener(){
214
                        public void actionPerformed(ActionEvent e) {
215
                                copyScriptingBaseScript();
216
                        }
217
                });
218
                menu_edit.add(menuItem_copy);
219
                JMenuItem menuItem_paste = new JMenuItem("paste",uimanager.getIcon("edit-paste"));
220
                menuItem_paste.addActionListener(new ActionListener(){
221
                        public void actionPerformed(ActionEvent e) {
222
                                pasteScriptingBaseScript();
223
                        }
224
                });
225
                menu_edit.add(menuItem_paste);
226
                menu_edit.addSeparator();
227
                JMenuItem menuItem_selectAll = new JMenuItem("select all",uimanager.getIcon("edit-select-all"));
228
                menuItem_selectAll.addActionListener(new ActionListener(){
229
                        public void actionPerformed(ActionEvent e) {
230
                                selectAllScriptingBaseScript();
231
                        }
232
                });
233
                menu_edit.add(menuItem_selectAll);
234

    
235
                JMenu menu_tools = new JMenu("Tools");
236
                JMenuItem menuItem_launcher = new JMenuItem("Launcher");
237
                menuItem_launcher.addActionListener(new ActionListener(){
238
                        public void actionPerformed(ActionEvent e) {
239
                                getWindowManager().showWindow(uimanager.createLauncher(), "Scripting Launcher", WindowManager.MODE.TOOL);
240
                        }
241
                });
242
                menu_tools.add(menuItem_launcher);
243
                
244

    
245
                JMenu menu_help = new JMenu("Help");
246
                JMenuItem menuItem_import = new JMenuItem("Import JavaDoc",uimanager.getIcon("list-add"));
247
                menuItem_import.addActionListener(new ActionListener(){
248
                        public void actionPerformed(ActionEvent e) {
249
                                getWindowManager().showWindow(getImportHelpDialog(), "JavaDoc", WindowManager.MODE.WINDOW);
250
                        }
251
                });
252
                menu_help.add(menuItem_import);
253
                JMenuItem menuItem_remove = new JMenuItem("Remove JavaDoc",uimanager.getIcon("list-remove"));
254
                menuItem_remove.addActionListener(new ActionListener(){
255
                        public void actionPerformed(ActionEvent e) {
256
                                try {
257
                                        getRemoveHelpDialog();
258
                                } catch (Exception e1) {
259
                                        // TODO Auto-generated catch block
260
                                        e1.printStackTrace();
261
                                }
262
                        }
263
                });
264
                menu_help.add(menuItem_remove);
265
                JMenuItem menuItem_show = new JMenuItem("Show JavaDoc",uimanager.getIcon("help-browser"));
266
                menuItem_show.addActionListener(new ActionListener(){
267
                        public void actionPerformed(ActionEvent e) {
268
                                getWindowManager().showWindow(uimanager.getAPIHelp(), "JavaDoc", WindowManager.MODE.WINDOW);
269
                        }
270
                });
271
                menu_help.add(menuItem_show);
272
                menu_help.addSeparator();
273
                JMenuItem menuItem_about = new JMenuItem("About us",uimanager.getIcon("system-users"));
274
                menuItem_about.addActionListener(new ActionListener(){
275
                        public void actionPerformed(ActionEvent e) {
276
                                getWindowManager().showWindow(getAboutUsDialog(), "About", WindowManager.MODE.WINDOW);
277

    
278
                        }
279
                });
280
                menu_help.add(menuItem_about);
281
                JMenuItem menuItem_help = new JMenuItem("Get help",uimanager.getIcon("help-browser"));
282
                menuItem_help.addActionListener(new ActionListener(){
283
                        public void actionPerformed(ActionEvent e) {
284
                                getWindowManager().showWindow(uimanager.getUserHelp(), "Scripting Framework Help", WindowManager.MODE.WINDOW);
285
                        }
286
                });
287
                menu_help.add(menuItem_help);
288

    
289
                menuBar.add(menu_file);
290
                menuBar.add(menu_edit);
291
                menuBar.add(menu_tools);
292
                menuBar.add(menu_help);
293

    
294
                // Botones de acción
295
                file = new JToolBar();
296
                help = new JToolBar();
297
                edit = new JToolBar();
298
                script = new JToolBar();
299
                setDefaultButtons();
300
                JPanel scriptButtonsPane = new JPanel(new BorderLayout());
301
                scriptButtonsPane.add(edit,BorderLayout.WEST);
302
                scriptButtonsPane.add(script,BorderLayout.CENTER);
303
                JPanel buttonsPane = new JPanel(new BorderLayout());
304
                buttonsPane.add(file, BorderLayout.WEST);
305
                buttonsPane.add(help, BorderLayout.EAST);
306
                buttonsPane.add(scriptButtonsPane, BorderLayout.CENTER);
307

    
308
                // Panel de selección de proyecto
309
                JPanel launcherPane = new JPanel(new BorderLayout());
310
                launcher = (DefaultJScriptingLauncher)uimanager.createLauncher();
311
                launcher.addDefaultActionListener(this);
312
                launcher.setPreferredSize(new Dimension(200, 450));
313
                launcherButtons = new JToolBar(SwingConstants.VERTICAL);
314
                setLauncherPaneButtons();
315

    
316
                launcherPane.add(launcher,BorderLayout.CENTER);
317
                launcherPane.add(launcherButtons,BorderLayout.WEST);
318

    
319

    
320
                // Panel de Edición
321
                scriptEditors = new JTabbedPane();
322
                scriptEditors.addChangeListener(new ChangeListener(){
323
                        public void stateChanged(ChangeEvent arg0) {
324
                                JPanel editor = (JPanel)scriptEditors.getSelectedComponent();
325
                                if(scriptEditors.getSelectedIndex()==-1)
326
                                        setDefaultButtons();
327
                                else if(editor instanceof DefaultJDialogEditor)
328
                                        setDialogButtons();
329
                                else if(editor instanceof DefaultJCodeEditor)
330
                                        setScriptButtons();
331
                        }
332
                });
333
                scriptEditors.setPreferredSize(new Dimension(500, 350));
334
                problemsPane = new JTabbedPane();
335

    
336
                tableData = null;
337
                tableModel = new AbstractTableModel();
338
                tableModel.addColumn("Description");
339
                tableModel.addColumn("Resource");
340
                tableModel.addColumn("Location");
341
                table = new JTable(tableModel);
342
                table.getColumnModel().getColumn(0).setPreferredWidth(300);
343
                //table.setFont(new Font("Serif",Font.PLAIN,13));
344
                
345
                table.getSelectionModel().addListSelectionListener(new RowListener());
346
                table.setRowSelectionAllowed(true);
347
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
348
                
349
                JScrollPane scrollProblems = new JScrollPane(table);        
350
                scrollProblems.setAutoscrolls(true);
351

    
352
                console = new JTextArea();
353
                console.setFont(new Font("monospaced",Font.PLAIN,13));
354
                
355
                JScrollPane scrollConsole = new JScrollPane(console);
356
                problemsPane.add("Problems", scrollProblems);
357
                problemsPane.add("Console", scrollConsole);
358
                problemsPane.setPreferredSize(new Dimension(450, 100));
359
                problemsPane.setSelectedIndex(1);
360

    
361
                /* Si no se desea cargar datos para que arranque con ellos,
362
                 * este if nunca será cierto */
363
                if(!unitsEditor.isEmpty()){;
364
            Iterator<JEditor> it = unitsEditor.iterator();
365
                while(it.hasNext()){
366
                        JEditor editor = it.next();
367
                        editor.addDefaultActionListener(this);
368
                        scriptEditors.addTab(editor.getScript().getName(), editor);
369
                }
370
                }
371
                editionPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scriptEditors,problemsPane);
372
                editionPane.setOneTouchExpandable(true);
373

    
374
                mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,launcherPane, editionPane);
375
                mainPane.setOneTouchExpandable(true);
376

    
377
                // Barra de estado
378
                statusBar = new JPanel(new BorderLayout());
379
                statusBarMessage = new JLabel();
380
                statusBarMessage.setText("Welcome to ScriptingFramework v1.0");
381
                currentLine = new JLabel();
382
                currentLine.setText("Line 0:0");
383

    
384
                statusBar.add(statusBarMessage,BorderLayout.WEST);
385
                statusBar.add(currentLine,BorderLayout.EAST);
386
                
387
                // Integracíón en el frame principal de todos los componentes
388
                JPanel buttons = new JPanel();
389
                buttons.setLayout( new BorderLayout() );
390
                buttons.add(BorderLayout.NORTH, menuBar);
391
                buttons.add(BorderLayout.SOUTH, buttonsPane);
392
                
393
                this.setLayout(new BorderLayout());
394
                this.add(BorderLayout.NORTH, buttons);
395
                this.add(BorderLayout.CENTER, mainPane);
396
                this.add(BorderLayout.SOUTH, statusBar);
397
                this.setPreferredSize(new Dimension(800,600));
398
        }
399

    
400
        private PrintStream getConsolePrintStream() {
401
                if( this.consolePrintStream == null ) {
402
                        JTextAreaOutputStream outputConsole = new JTextAreaOutputStream(console);   
403
                        this.consolePrintStream = new PrintStream(outputConsole);
404
                }
405
                return this.consolePrintStream;
406
        }
407
        
408
        private class AbstractTableModel extends DefaultTableModel {
409

    
410
           /**
411
                 * 
412
                 */
413
                private static final long serialVersionUID = -5314938428821653641L;
414

    
415
        @Override
416
    public boolean isCellEditable(int row, int col){
417
                   return false;
418
           }
419
        }
420

    
421
        
422
    private class RowListener implements ListSelectionListener {
423
        public void valueChanged(ListSelectionEvent event) {
424
            if (event.getValueIsAdjusting()) {
425
                return;
426
            }
427
            if(!table.getSelectionModel().isSelectionEmpty()){
428
                    int index = table.getSelectionModel().getLeadSelectionIndex();
429
                    String[] lineAndColumn = ((String)table.getValueAt(index, 2)).split(":");
430
                    int line = -1;
431
                    if(lineAndColumn.length>0) {
432
                            try {
433
                                    line = Integer.parseInt(lineAndColumn[0].trim());
434
                            } catch (Exception e) {
435
                                                // Do nothing
436
                                        }
437
                    }
438
                    if( line >= 0 ) {
439
                            setEditorSelection(((String)table.getValueAt(index, 1)), line);
440
                    }
441
            }
442
        }
443
    }
444
        
445
    private void setEditorSelection(String name, int line){
446
            for (int i=0; i<scriptEditors.getTabCount(); i++){
447
                    
448
                    if(((JEditor)scriptEditors.getComponent(i)).getScript().getId().equals(name)){
449
                            scriptEditors.setSelectedIndex(i);
450
                            
451
                            JTextComponent editor = ((JCodeEditor)scriptEditors.getComponent(i)).getJTextComponent();
452
                            editor.requestFocusInWindow();
453
                            
454
                            String code = editor.getText();
455
                            int lineCounter = 0;
456
                            int initialSelection = 0;
457
                            int finalSelection = 0;
458
                            for(int j=0; j<code.length(); j++){
459
                                    if (code.charAt(j) == '\n'){
460
                                            lineCounter++;
461
                                            if(lineCounter == line-1){
462
                                                    initialSelection = j;
463
                                            }
464
                                            if(lineCounter == line || (finalSelection==0 && j==code.length()-1)){
465
                                                    finalSelection = j;
466
                                            }
467
                                    }
468
                            }
469
                            
470
                            editor.select(initialSelection,finalSelection);   
471
                    }
472
            }
473
    }
474
    
475
        public void addDefaultActionListener(ActionListener actionlistener) {
476
                this.defaultActionlistener = actionlistener;  
477
        }
478
        
479
        /**
480
         * Función para obtener el elemento seleccionado del Launcher
481
         * @return Selected ScriptingUnit on the tree 
482
         */
483
        private ScriptingUnit getLauncherSelectedNode(){
484
                JTabbedPane tabbed = launcher.getTabbedPane();
485
                DefaultJScriptingBrowser browser = (DefaultJScriptingBrowser)tabbed.getSelectedComponent();
486
                ScriptingUnit unit = browser.getSelectedNode();
487
                return unit;
488
        }
489

    
490
        /**
491
         * Lanza el modo edición de un ScriptingBaseScript
492
         * @param unit
493
         */        
494
        private void editSelectedNode(ScriptingUnit unit){
495
                if(unit instanceof ScriptingBaseScript){
496
                        editUnit((ScriptingBaseScript)unit);
497
                }
498
        }
499

    
500
        /**
501
         * Lanza la ejecución de un ScriptingBaseScript
502
         * @param unit
503
         */
504
        private void runSelectedNode(ScriptingUnit unit){
505
                if(unit instanceof ScriptingBaseScript){
506
                        ((ScriptingBaseScript)unit).run();
507
                }
508
        }
509

    
510
        /**
511
         * Funciones para obtener las botoneras en función del tipo de ScriptingBaseScript
512
         * que se esté mostrando en ese momento. Está la estándar 'getDefaultButtons()' para
513
         * cuando no hay ninguno, 'getScriptButtons()' con los botones característicos para
514
         * el manejo de ScriptingScripts y 'getDialogButtons()' para los ScriptingDialogs.
515
         * También se incluye 'getEditButtons()' con las funciones sobre la edición de texto
516
         * como copiar, cortar y pegar.
517
         *
518
         */
519
        private void setDefaultButtons(){
520
                file.removeAll();
521
                edit.removeAll();
522
                script.removeAll();
523
                help.removeAll();
524
                JButton newb = new JButton(uimanager.getIcon("document-new"));
525
                newb.setToolTipText("New");
526
                newb.setBorderPainted(false);
527
                newb.addActionListener(new ActionListener(){
528
                        public void actionPerformed(ActionEvent e) {
529
                                try {
530
                                        newScriptingBaseScript();
531
                                } catch (Exception e1) {
532
                                        // TODO Auto-generated catch block
533
                                        e1.printStackTrace();
534
                                }
535
                        }
536
                });
537
                file.add(newb);
538
                file.repaint();
539

    
540
                help.removeAll();
541
                JButton helpb = new JButton(uimanager.getIcon("help-browser"));
542
                helpb.setBorderPainted(false);
543
                helpb.setToolTipText("Help");
544
                helpb.addActionListener(new ActionListener(){
545
                        public void actionPerformed(ActionEvent e) {
546
                                uimanager.showWindow(uimanager.getAPIHelp(),"JavaDoc");
547
                        }
548
                });
549
                help.add(helpb);
550
                help.repaint();
551
                edit.setVisible(false);
552
                script.setVisible(false);
553
        }
554

    
555
        private void setScriptButtons(){
556
                file.removeAll();
557
                edit.removeAll();
558
                script.removeAll();
559
                setDefaultButtons();
560

    
561
                edit.setVisible(true);
562
                script.setVisible(true);
563
                setEditButtons();
564
                JButton close = new JButton(uimanager.getIcon("emblem-unreadable"));
565
                close.setBorderPainted(false);
566
                close.setToolTipText("Close current tab");
567
                close.addActionListener(new ActionListener(){
568
                        public void actionPerformed(ActionEvent e) {
569
                                closeScriptingBaseScript();
570
                        }
571
                });
572
                file.add(close);
573
                JButton save = new JButton(uimanager.getIcon("media-floppy"));
574
                save.setBorderPainted(false);
575
                save.setToolTipText("Save");
576
                save.addActionListener(new ActionListener(){
577
                        public void actionPerformed(ActionEvent e) {
578
                                saveScriptingBaseScript();
579
                        }
580
                });
581
                file.add(save);
582
                file.repaint();
583

    
584
                JButton run = new JButton(uimanager.getIcon("applications-system"));
585
                run.setBorderPainted(false);
586
                run.setToolTipText("Run");
587
                run.addActionListener(new ActionListener(){
588
                        public void actionPerformed(ActionEvent e) {
589
                                runScriptingBaseScript();
590
                        }
591
                });
592
                script.add(run);
593
                script.repaint();
594
        }
595

    
596
        private void setDialogButtons(){
597
                script.removeAll();
598
                setScriptButtons();
599
                script.addSeparator();
600
                JButton help = new JButton(uimanager.getIcon("help-browser"));
601
                help.setBorderPainted(false);
602
                help.setToolTipText("Help");
603
                script.add(help);        
604
                script.repaint();
605
        }
606

    
607
        private void setEditButtons(){
608
                JButton cut = new JButton(uimanager.getIcon("edit-cut"));
609
                cut.setBorderPainted(false);
610
                cut.setToolTipText("Cut");
611
                cut.addActionListener(new ActionListener(){
612
                        public void actionPerformed(ActionEvent e) {
613
                                cutScriptingBaseScript();
614
                        }
615
                });
616
                edit.add(cut);
617
                JButton copy = new JButton(uimanager.getIcon("edit-copy"));
618
                copy.setBorderPainted(false);
619
                copy.setToolTipText("Copy");
620
                copy.addActionListener(new ActionListener(){
621
                        public void actionPerformed(ActionEvent e) {
622
                                copyScriptingBaseScript();
623
                        }
624
                });
625
                edit.add(copy);
626
                JButton paste = new JButton(uimanager.getIcon("edit-paste"));
627
                paste.setBorderPainted(false);
628
                paste.setToolTipText("Paste");
629
                paste.addActionListener(new ActionListener(){
630
                        public void actionPerformed(ActionEvent e) {
631
                                pasteScriptingBaseScript();
632
                        }
633
                });
634
                edit.add(paste);
635
                edit.repaint();
636
        }
637

    
638
        /**
639
         * Botonera con posibles acciones a realizar con el Panel Launcher
640
         *
641
         */
642
        private void setLauncherPaneButtons(){
643

    
644
                JButton newb = new JButton(uimanager.getIcon("document-new"));
645
                newb.setBorderPainted(false);
646
                newb.setToolTipText("New");
647
                newb.addActionListener(new ActionListener(){
648
                        public void actionPerformed(ActionEvent e) {
649
                                try {
650
                                        newScriptingBaseScript();
651
                                } catch (Exception e1) {
652
                                        // TODO Auto-generated catch block
653
                                        e1.printStackTrace();
654
                                }
655
                        }
656
                });
657
                launcherButtons.add(newb);
658
                JButton edit = new JButton(uimanager.getIcon("applications-accessories"));
659
                edit.setBorderPainted(false);
660
                edit.setToolTipText("Open edition mode");
661
                edit.addActionListener(new ActionListener(){
662
                        public void actionPerformed(ActionEvent e) {
663
                                editSelectedNode(getLauncherSelectedNode());
664
                        }
665
                });
666
                launcherButtons.add(edit);
667
                JButton run = new JButton(uimanager.getIcon("applications-system"));
668
                run.setBorderPainted(false);
669
                run.setToolTipText("Run selected script");
670
                run.addActionListener(new ActionListener(){
671
                        public void actionPerformed(ActionEvent e) {
672
                                runSelectedNode(getLauncherSelectedNode());
673
                        }
674
                });
675
                launcherButtons.add(run);
676
                launcherButtons.addSeparator();
677
                JButton refresh = new JButton(uimanager.getIcon("view-refresh"));
678
                refresh.setBorderPainted(false);
679
                refresh.setToolTipText("Refresh directories");
680
                refresh.addActionListener(new ActionListener(){
681
                        public void actionPerformed(ActionEvent e) {
682
                                launcherRefresh();
683
                        }
684
                });
685
                launcherButtons.add(refresh);
686
                launcherButtons.addSeparator();
687
                JButton rename = new JButton(uimanager.getIcon("preferences-system"));
688
                rename.setBorderPainted(false);
689
                rename.setToolTipText("Set file properties");
690
                rename.addActionListener(new ActionListener(){
691
                        public void actionPerformed(ActionEvent e) {
692
                                try {
693
                                        renameSelectedNode(getLauncherSelectedNode());
694
                                } catch (Exception e1) {
695
                                        // TODO Auto-generated catch block
696
                                        e1.printStackTrace();
697
                                }
698
                        }
699
                });
700
                launcherButtons.add(rename);
701
                JButton move = new JButton(uimanager.getIcon("format-indent-more"));
702
                move.setBorderPainted(false);
703
                move.setToolTipText("Move files");
704
                move.addActionListener(new ActionListener(){
705
                        public void actionPerformed(ActionEvent e){
706
                                try {
707
                                        moveSelectedNode(getLauncherSelectedNode());
708
                                } catch (Exception e1) {
709
                                        // TODO Auto-generated catch block
710
                                        e1.printStackTrace();
711
                                }
712
                        }
713
                });
714
                launcherButtons.add(move);
715
                launcherButtons.addSeparator();
716
                JButton delete = new JButton(uimanager.getIcon("user-trash"));
717
                delete.setBorderPainted(false);
718
                delete.setToolTipText("Delete selected script");
719
                delete.addActionListener(new ActionListener(){
720
                        public void actionPerformed(ActionEvent e) {
721
                                deleteSelectedNode(getLauncherSelectedNode(),false);
722
                        }
723
                });
724
                launcherButtons.add(delete);
725
        }
726

    
727
        /**
728
         * Función que abre en modo edición un ScriptingBaseScript
729
         * @param unit
730
         */
731
        public void editUnit(ScriptingBaseScript unit){
732
                JEditor panel = null;
733
                if( unit instanceof ScriptingDialog ) {
734
                        panel = new DefaultJDialogEditor(uimanager, (ScriptingDialog) unit);
735
                        if(panel instanceof DefaultJDialogEditor){
736
                                ((DefaultJDialogEditor)panel).addDefaultActionListener(this);
737
                        }
738
                }else if( unit instanceof ScriptingScript ) {
739
                        panel = new DefaultJCodeEditor(uimanager, (ScriptingScript) unit);
740
                        if(panel instanceof DefaultJCodeEditor){
741
                                ((DefaultJCodeEditor)panel).addDefaultActionListener(this);
742
                        }        
743
                }else{
744
                        JOptionPane.showMessageDialog(null, "Can't open editon view from this script type", "Info", JOptionPane.INFORMATION_MESSAGE);
745
                        return ;
746
                }
747
                unitsEditor.add(panel);
748

    
749
        Iterator<JEditor> it = unitsEditor.iterator();
750
                while(it.hasNext()){
751
                        JEditor editor = it.next();
752
                        String title;
753
                        if (editor.getScript().isSaved()){
754
                                title = editor.getScript().getName();
755
                        }else{
756
                                title = "*"+editor.getScript().getName();
757
                        }
758
                        
759
                        scriptEditors.addTab(title, editor);
760
                }
761
                scriptEditors.setSelectedIndex(unitsEditor.size()-1);
762
                editionPane.repaint();
763
                file.repaint();
764
        }
765

    
766

    
767
        /**
768
         * Ventana para creación de nuevos ScriptingBaseScripts
769
         * @throws Exception 
770
         *
771
         */
772
        public void newScriptingBaseScript() throws Exception{
773
                JNewScriptModel model = new JNewScriptModel(manager);
774
                JDialogContent dialog = new JDialogContent(
775
                                uimanager, 
776
                                "document-new",
777
                                "New Script",
778
                                "Create a new Script, Dialog, Project or Folder",
779
                                new JNewScriptPanel(uimanager,model)
780
                );
781
                dialog.showDialog();
782
                if(model.getAction()==JNewScriptModel.ACTION_ACCEPT){
783
                        this.createNewScriptingBaseScript(model);
784
                }
785
        }
786
        
787
        public void createNewScriptingBaseScript(JNewScriptModel model) throws Exception {
788
                model.validate();
789
                ScriptingUnit unit= this.manager.createUnit(
790
                                model.getType(),
791
                                manager.getFolder(new File(model.getPath())),
792
                                model.getName(),
793
                                model.getLanguage()
794
                );
795
                if(unit instanceof ScriptingBaseScript){
796
                        this.editUnit((ScriptingBaseScript) unit);
797
                }
798
                
799
                this.launcherRefresh();
800
        }
801
        
802
        public JPanel getImportHelpDialog(){
803
                return new JDialogContent(uimanager, "list-add","Import JavaDoc","Insert JavaDocs from system directories to the ScriptingFramework JavaDoc",new JImportHelp(uimanager,this));
804
        }
805
        
806
        public void getRemoveHelpDialog() throws Exception{
807
                JRemoveHelpModel model = new JRemoveHelpModel(manager);
808
                JDialogContent dialog = new JDialogContent(
809
                                uimanager, 
810
                                "list-remove",
811
                                "Remove JavaDoc",
812
                                "Remove JavaDocs from the ScriptingFramework JavaDoc",
813
                                new JRemoveHelpPanel(uimanager,model));
814
                dialog.showDialog();
815
                if(model.getAction()==JNewScriptModel.ACTION_ACCEPT){
816
                        this.removeHelps(model);
817
                }
818
        }
819
        
820
        public void removeHelps(JRemoveHelpModel model) throws Exception {
821
                model.validate();
822
                Object[] helps = model.getHelps();
823
                for (int i=0;i<helps.length;i++){
824
                        this.manager.getHelpManager().removeHelp(helps[i].toString());
825
                }
826
                //Recargar JavaHelp
827
                this.manager.getHelpManager().reloadHelp();
828
                JOptionPane.showMessageDialog(null, "Deleting JavaDocs successfully", "Info", JOptionPane.INFORMATION_MESSAGE);
829
                uimanager.showWindow(uimanager.getAPIHelp(),"JavaDoc");
830
        }
831
                
832
        public void moveSelectedNode(final ScriptingUnit unit) throws Exception{
833
                if (unit.getParent()==null){
834
                        JOptionPane.showMessageDialog(null, "Please, select first the file to move on the tree directory", "Error", JOptionPane.ERROR_MESSAGE);
835
                }else{
836
                        JMoveScriptModel model = new JMoveScriptModel(manager,unit);
837
                        JDialogContent dialog = new JDialogContent(
838
                                        uimanager, 
839
                                        "format-indent-more", 
840
                                        "Move '"+unit.getName()+ "' script", 
841
                                        "Change the location of the selected script", 
842
                                        new JMoveScriptPanel(uimanager,model)
843
                        );
844
                        dialog.showDialog();
845
                        if(model.getAction()==JMoveScriptModel.ACTION_ACCEPT){
846
                                this.moveScriptingUnitScript(model);
847
                        }
848
                }
849

    
850
        }
851
        
852
        public void moveScriptingUnitScript(JMoveScriptModel model) throws Exception {
853
                model.validate();
854
                ScriptingFolder folderDest = manager.getFolder(model.getMoveTo());
855
                if(model.getUnit().move(folderDest)){
856
                        JOptionPane.showMessageDialog(null, "File moved succesfully.", "Info", JOptionPane.INFORMATION_MESSAGE);
857
                        this.launcherRefresh();
858
                }else{
859
                        JOptionPane.showMessageDialog(null, "Unexpected error moving the file", "Error", JOptionPane.ERROR_MESSAGE);
860
                }
861
        }
862

    
863
        public void renameSelectedNode(final ScriptingUnit unit) throws Exception{
864
                if (unit.getParent()==null){
865
                        JOptionPane.showMessageDialog(null, "Please, select first the file to move on the tree directory", "Error", JOptionPane.ERROR_MESSAGE);
866
                }else{
867
                        JRenameModel model = new JRenameModel(manager,unit);
868
                        JDialogContent dialog = new JDialogContent(
869
                                        uimanager, 
870
                                        "preferences-system", 
871
                                        "Rename files of '"+unit.getName()+ "' script on the filesystem", 
872
                                        "Change the filenames of the selected script and other properties", 
873
                                        new JRenamePanel(uimanager,model)
874
                        );
875
                        dialog.showDialog();
876
                        if(model.getAction()==JRenameModel.ACTION_ACCEPT){
877
                                this.renameScriptingUnitScript(model);
878
                        }
879
                }
880
        }
881
        
882
        public void renameScriptingUnitScript(JRenameModel model) throws Exception {
883
                model.validate();
884
                ScriptingUnit unit = model.getUnit();
885
                if (!model.getNewName().equals(unit.getId())){
886
                        if(unit.rename(model.getNewName())){
887
                                JOptionPane.showMessageDialog(null, "Rename succesfully", "Info", JOptionPane.INFORMATION_MESSAGE);
888
                                this.launcherRefresh();
889
                        }else{
890
                                JOptionPane.showMessageDialog(null, "Unexpected error renaming the file", "Error", JOptionPane.ERROR_MESSAGE);
891
                        }
892
                        if(unit instanceof ScriptingScript){
893
                                ((ScriptingScript)unit).save();
894
                        }        
895
                }
896
        }
897
        
898
        @Override
899
    public JPanel getAboutUsDialog(){
900
                return new JDialogContent(uimanager, "system-users","About us","Information about the contributors to gvSIG project",new  JDefaultDialog(uimanager,this,uimanager.getAboutManager().getAboutPanel()));
901
        }
902
        
903

    
904
        public void deleteSelectedNode(final ScriptingUnit unit, boolean isRecursive){
905
                int n = 0;
906
                if(!isRecursive){
907
                n=JOptionPane.showConfirmDialog(
908
                            this.getRootPane(),
909
                            "Are you sure?\nAll the contents will be deleted too.",
910
                            "Delete this Unit and all the contents",
911
                            JOptionPane.YES_NO_OPTION);
912
                }
913
                if (n==0){
914
                        //Si es un folder borramos recursivamente sus subdirectorios
915
                        if(unit instanceof ScriptingFolder){
916
                List<ScriptingUnit> subunits =
917
                    ((ScriptingFolder) unit).getUnits();
918
                Iterator<ScriptingUnit> it = subunits.iterator();
919
                                while(it.hasNext()){
920
                                        ScriptingUnit subunit = it.next();
921
                                        deleteSelectedNode(subunit, true);
922
                                }
923
                        }
924
                        // Por último borramos el nodo seleccionado
925
                        unit.getParent().remove(unit);
926
                        launcherRefresh();
927
                        
928
                }
929
        }
930
        
931
        /**
932
         * Función que cierra la pestaña de edición actual
933
         *
934
         */
935
        public void closeScriptingBaseScript(){
936
                if (!unitsEditor.isEmpty()){
937
                        int position = scriptEditors.getSelectedIndex();
938
                        String title = scriptEditors.getTitleAt(position);
939
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(position);
940
                        int respuesta=1;
941
                        if(!pestanaEditor.getScript().isSaved()){
942
                                respuesta = JOptionPane.showConfirmDialog(
943
                                            scriptEditors, 
944
                                            "'" + title.substring(1)+"' has been modified. Save changes?",
945
                                            "An Inane Question",
946
                                            JOptionPane.YES_NO_CANCEL_OPTION);
947
                                if(respuesta == 0)
948
                                        saveScriptingBaseScript();
949
                        }
950
                        
951
                        if(respuesta<2){
952
                                pestanaEditor.getScript().setSaved(true);
953
                                scriptEditors.remove(position);
954
                                unitsEditor.remove(position);
955
                                scriptEditors.repaint();
956
                        } 
957
                }
958
        }
959

    
960
        /**
961
         * Función que cierra todas las pestañas de edición abiertas
962
         *
963
         */
964
        public void closeAllScriptingBaseScript(){
965
                boolean exit=false;
966
                while (!unitsEditor.isEmpty() && !exit){
967
                        int action=1;
968
                        int i=0;
969
                        boolean answered=false;
970
                        while(i<scriptEditors.getComponentCount() && !answered){
971
                                JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(i);
972
                                if(!pestanaEditor.getScript().isSaved()){
973
                                        action = JOptionPane.showConfirmDialog(
974
                                                    scriptEditors, 
975
                                                    "Some projects has been modified. Save changes?",
976
                                                    "An Inane Question",
977
                                                    JOptionPane.YES_NO_CANCEL_OPTION);
978
                                        answered=true;
979
                                }
980
                                i++;
981
                        }
982
                        if(action == 0){
983
                                for(i=0;i<scriptEditors.getComponentCount();i++){
984
                                        scriptEditors.setSelectedIndex(i);
985
                                        saveScriptingBaseScript();                        
986
                                }
987
                        } 
988
                        if(action < 2){
989
                                for(i=scriptEditors.getComponentCount()-1;i>=0;i--){
990
                                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(i);
991
                                        pestanaEditor.getScript().setSaved(true);
992
                                        scriptEditors.setSelectedIndex(i);
993
                                        closeScriptingBaseScript();
994
                                }
995
                        } else{
996
                                exit=true;
997
                        }
998
                        
999
                }
1000
        }
1001

    
1002
        /**
1003
         * Función que ejecuta el ScriptingBaseScript de la pestaña actual
1004
         *
1005
         */
1006
        public void runScriptingBaseScript(){
1007
                if (!unitsEditor.isEmpty()){
1008
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1009
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1010
                        consoleClear();
1011
                        errorsClear();
1012
                        pestanaEditor.getScript().addObserver(this);
1013
                        try {
1014
                                ScriptingScript script = pestanaEditor.getScript();
1015
                                script.compile();
1016
                                script.runAsTask(null);
1017
                                /*
1018
                                if( script instanceof ScriptingDialog ) {
1019
                                        script.run();
1020
                                } else {
1021
                                        ScriptTask task = new ScriptTask(this, script);
1022
                                        task.start();
1023
                                }
1024
                                */
1025
                        } catch (Throwable e) {
1026
                                // Ignore it, compile and run handle errors correctlty
1027
                                logger.info("Can't execute dialog-", e);
1028
                        }
1029
                }
1030
        }
1031

    
1032
        /*
1033
        class ScriptTask extends AbstractMonitorableTask {
1034

1035
                ScriptingScript script;
1036
                DefaultJScriptingComposer composer; 
1037
                
1038
                protected ScriptTask(DefaultJScriptingComposer composer,  ScriptingScript script) {
1039
                        super(script.getName());
1040
                        this.composer = composer;
1041
                        this.script = script;
1042
                        this.script.put("task",this);
1043
                        this.script.put("taskStatus",this.getTaskStatus());
1044
                }
1045
                
1046
                public void run() {
1047
                        try {
1048
                                composer.consolePrintln("Running '"+ script.getName() +"'...");
1049
                                script.run();
1050
                                composer.consolePrintln("Quit '"+ script.getName() +"'");
1051
                        } catch (Throwable e) {
1052
                                ((SimpleTaskStatus)this.getTaskStatus()).abort();
1053
                                composer.consolePrintln("Aborted '"+ script.getName() +"'");
1054
                        }
1055
                }
1056
        }
1057
        */
1058

    
1059
        /**
1060
         * Funciones de 'cortar', 'copiar', 'pegar' y 'seleccionar todo' para los JEditors
1061
         * 
1062
         */
1063
        public void copyScriptingBaseScript(){
1064
                if (!unitsEditor.isEmpty()){
1065
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1066
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1067
                        pestanaEditor.getJTextComponent().copy();
1068
                }
1069
        }
1070

    
1071
        public void cutScriptingBaseScript(){
1072
                if (!unitsEditor.isEmpty()){
1073
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1074
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1075
                        pestanaEditor.getJTextComponent().cut();
1076
                        JTabbedPane tabs = (JTabbedPane)pestanaEditor.getParent();
1077
                        
1078
                        String title = tabs.getTitleAt(pestanaIndex);
1079
                        if(title.length()>0 && !title.substring(0,1).equals("*")){
1080
                                tabs.setTitleAt(tabs.getSelectedIndex(), "*"+title);
1081
                                pestanaEditor.getScript().setSaved(false);
1082
                        }
1083
                }
1084
        }
1085

    
1086
        public void pasteScriptingBaseScript(){
1087
                if (!unitsEditor.isEmpty()){
1088
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1089
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1090
                        pestanaEditor.getJTextComponent().paste();
1091
                        JTabbedPane tabs = (JTabbedPane)pestanaEditor.getParent();
1092
                        
1093
                        String title = tabs.getTitleAt(pestanaIndex);
1094
                        if(title.length()>0 && !title.substring(0,1).equals("*")){
1095
                                tabs.setTitleAt(tabs.getSelectedIndex(), "*"+title);
1096
                                pestanaEditor.getScript().setSaved(false);
1097
                        }
1098
                }
1099
        }
1100

    
1101
        public void selectAllScriptingBaseScript(){
1102
                if (!unitsEditor.isEmpty()){
1103
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1104
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1105
                        pestanaEditor.getJTextComponent().selectAll();
1106
                }
1107
        }
1108

    
1109
        /**
1110
         * Función para el guardado del ScriptingBaseScript de la pestaña actual
1111
         *
1112
         */
1113
        public void saveScriptingBaseScript(){
1114
                if (!unitsEditor.isEmpty()){
1115
                        int pestanaIndex = scriptEditors.getSelectedIndex();
1116
                        JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1117
                        pestanaEditor.save();
1118
                        String title = scriptEditors.getTitleAt(pestanaIndex);
1119
                        if (title.substring(0, 1).equals("*")){
1120
                                scriptEditors.setTitleAt(pestanaIndex, title.substring(1));
1121
                        }
1122
                }
1123
        }
1124

    
1125
        /**
1126
         * Actualización del Launcher
1127
         *
1128
         */
1129
        public void launcherRefresh(){
1130
                launcher.refresh();
1131
                launcher.addDefaultActionListener(this);
1132
                launcher.setPreferredSize(new Dimension(200, 450));
1133
        }
1134

    
1135
        /**
1136
         * Función para indicar la posición del cursor en un JEditor
1137
         * @param line
1138
         * @param column
1139
         */
1140
        public void setEditorCursorPosition(int line, int column){
1141
                if (line==0 && column ==0)
1142
                        currentLine.setText("");
1143
                else
1144
                        currentLine.setText("Line "+line+":"+column);
1145
                statusBar.repaint();
1146
        }
1147

    
1148
        /**
1149
         * Función para la actualización del mensaje de la barra de estado
1150
         * @param message
1151
         */
1152
        public void setMessage(String message){
1153
                statusBarMessage.setText(message);
1154
                statusBar.repaint();
1155
        }
1156

    
1157
        /**
1158
         * Función para la actualización del mensaje de la barra de estado un tiempo limitado
1159
         * @param message
1160
         * @param seconds
1161
         */
1162
        public void setMessage(String message, int seconds){
1163
                setMessage(message);
1164
        }
1165

    
1166
        /**
1167
         * 
1168
         */
1169
        public void actionPerformed(ActionEvent e) {
1170

    
1171
                if(e instanceof UpdateCaretPositionActionEvent){
1172
                        UpdateCaretPositionActionEvent e2 = (UpdateCaretPositionActionEvent) e;
1173

    
1174
                        if( e2.hasLineAndColumn() ) {
1175
                                setEditorCursorPosition(e2.getLine(),e2.getColumn());
1176
                        }else{
1177
                                setEditorCursorPosition(0,0);
1178
                        }
1179
                }else if (e instanceof LauncherActionEvent){
1180
                        LauncherActionEvent e2 = (LauncherActionEvent) e;
1181

    
1182
                        switch(e.getID()){
1183
                        case JScriptingBrowser.DEFAULT_ACTION:
1184
                                editUnit(e2.getScript());
1185
                                break;                
1186
                        case JScriptingBrowser.DROPDOWN_ACTION:
1187
                        case JScriptingBrowser.SELECTION_ACTION:
1188

    
1189
                                break;
1190
                        }
1191
                }
1192
        }
1193

    
1194
        /**
1195
         * Clase empleada para crear un objeto JTextArea que hereda 
1196
         * de OutputStream en el que volcar las salidas estándar.
1197
         */
1198

    
1199
        public class JTextAreaOutputStream extends OutputStream {
1200
                JTextArea ta;
1201

    
1202
                public JTextAreaOutputStream(JTextArea t) {
1203
                        super();
1204
                        ta = t;
1205
                }
1206

    
1207
                @Override
1208
        public void write(int i) {
1209
                        ta.append(Character.toString((char)i));
1210
                }
1211

    
1212
                public void write(char[] buf, int off, int len) {
1213
                        String s = new String(buf, off, len);
1214
                        ta.append(s);
1215
                }
1216
        }
1217

    
1218
        public void update(final Observable observable, final Object notification) {
1219
                if( !SwingUtilities.isEventDispatchThread() ) {
1220
            SwingUtilities.invokeLater(new Runnable() {
1221
                public void run() {
1222
                    update(observable,notification);
1223
                }
1224
            });
1225
            return;
1226
        }   
1227
                if(notification instanceof ScriptingNotification){
1228
                        ScriptingNotification e= (ScriptingNotification) notification;
1229

    
1230
                        if(e.getException() instanceof CompileErrorException){
1231
                                CompileErrorException ce =  (CompileErrorException) e.getException();
1232
                                errorsAdd(ce.getMessage(), ce.getScriptName(), ce.getLineNumber(), ce.getColumnNumber()) ;
1233
                                
1234
                        } else if(e.getException() instanceof ExecuteErrorException){
1235
                                ExecuteErrorException ee =  (ExecuteErrorException) e.getException();
1236
                                errorsAdd(ee.getMessage(), ee.getScriptName(), ee.getLineNumber(), ee.getColumnNumber()) ;
1237
                                consolePrintln(ee.getMessage());
1238
                                ee.printStackTrace(this.getConsolePrintStream());
1239
                                
1240
                        } else {
1241
                                Throwable ex =  e.getException();
1242
                                errorsAdd(ex.getMessage()) ;
1243
                                consolePrintln(ex.getMessage());
1244
                                ex.printStackTrace(this.getConsolePrintStream());
1245
                        }
1246
                }
1247
        }
1248

    
1249
        public void errorsAdd(final String description, final String resource, final int line, final int column) {
1250
                if( !SwingUtilities.isEventDispatchThread() ) {
1251
            SwingUtilities.invokeLater(new Runnable() {
1252
                public void run() {
1253
                        errorsAdd(description, resource, line, column);
1254
                }
1255
            });
1256
            return;
1257
        }   
1258
                String location1;
1259
                if( line < 0 ) {
1260
                        location1 = "(no line)";
1261
                } else if( column < 0) {
1262
                        location1 = String.valueOf(line);
1263
                } else {
1264
                        location1 = String.valueOf(line)+":"+String.valueOf(column);
1265
                }
1266
                Object[] rowData = {description, resource,location1};
1267
                tableModel.addRow(rowData);
1268
                problemsPane.setSelectedIndex(0);                
1269
        }
1270

    
1271
        public void errorsAdd(String description, String resource) {
1272
                errorsAdd(description, resource, -1, -1);
1273
        }
1274

    
1275
        public void errorsAdd(String description) {
1276
                errorsAdd(description, null, -1, -1);
1277
        }
1278

    
1279
        public void errorsClear() {
1280
                if( !SwingUtilities.isEventDispatchThread() ) {
1281
            SwingUtilities.invokeLater(new Runnable() {
1282
                public void run() {
1283
                        errorsClear();
1284
                }
1285
            });
1286
            return;
1287
        }   
1288
                while( tableModel.getRowCount()>0) {
1289
                        tableModel.removeRow(0);
1290
                }
1291
        }
1292
        
1293
        public void consoleClear() {
1294
                if( !SwingUtilities.isEventDispatchThread() ) {
1295
            SwingUtilities.invokeLater(new Runnable() {
1296
                public void run() {
1297
                        consoleClear();
1298
                }
1299
            });
1300
            return;
1301
        }   
1302
                console.setText("");
1303
                problemsPane.setSelectedIndex(1);
1304
        }
1305

    
1306
        public void consolePrintln(final String str) {
1307
                if( !SwingUtilities.isEventDispatchThread() ) {
1308
            SwingUtilities.invokeLater(new Runnable() {
1309
                public void run() {
1310
                        consolePrintln(str);
1311
                }
1312
            });
1313
            return;
1314
        }   
1315
                console.append(str);
1316
                console.append("\n");
1317
                problemsPane.setSelectedIndex(1);
1318
        }
1319

    
1320
        public void consolePrint(final String str) {
1321
                if( !SwingUtilities.isEventDispatchThread() ) {
1322
            SwingUtilities.invokeLater(new Runnable() {
1323
                public void run() {
1324
                        consolePrint(str);
1325
                }
1326
            });
1327
            return;
1328
        }   
1329
                console.append(str);
1330
                problemsPane.setSelectedIndex(1);
1331
        }
1332
        
1333
        public void closeWindow() {
1334
                this.setVisible(false);
1335
        }
1336
}