Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / DefaultScriptingDialog.java @ 441

History | View | Annotate | Download (4.54 KB)

1
package org.gvsig.scripting.impl;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.io.BufferedWriter;
6
import java.io.File;
7
import java.io.FileWriter;
8
import java.io.IOException;
9
import java.io.Writer;
10
import java.util.Map;
11

    
12
import org.apache.commons.io.FileUtils;
13
import org.apache.commons.io.FilenameUtils;
14
import org.gvsig.scripting.ExecuteErrorException;
15
import org.gvsig.scripting.ScriptingDialog;
16
import org.gvsig.scripting.ScriptingFolder;
17
import org.gvsig.scripting.ScriptingManager;
18
import org.gvsig.scripting.swing.api.JThinlet;
19
import org.gvsig.scripting.swing.api.ScriptingSwingLocator;
20
import org.gvsig.scripting.swing.api.ScriptingUIManager;
21
import org.ini4j.Ini;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

    
25
public class DefaultScriptingDialog  extends DefaultScriptingScript implements ScriptingDialog, ActionListener{
26

    
27
        private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingDialog.class);
28
        private int showMode;
29

    
30
        public DefaultScriptingDialog(ScriptingFolder parent, ScriptingManager manager, String id) {
31
                super(parent, ScriptingManager.UNIT_DIALOG,manager, id);
32
                this.setMainName("onload");
33
                this.showMode=MODE_WINDOW;
34
        }
35
        
36
        public void runAsTask(Object[] args) {
37
                // Los dialogos no se ejecutan en otro hilo.
38
                this.run(args);
39
        }
40
        
41
        public Object run(Object args[]) {
42
                ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager();
43

    
44
                JThinlet thinlet = uimanager.createJThinlet();
45
                thinlet.addActionListener(this);
46
                thinlet.load(this.getFileResource(".dlg"));
47

    
48
                this.put("dialog", thinlet.getThinlet());
49
                
50
                this.compile();
51
                try {
52
                        this.invokeFunction(this.getMainName(), args);
53
                } catch( ExecuteErrorException ex) {
54
                        Throwable e = ex.getCause();
55
                        if( !(e instanceof NoSuchMethodException) ) {
56
                                throw ex;
57
                        } else {
58
                                // When running from the composer messages of stdout shows in the console tab.
59
                                System.out.println("Code of dialog do not has function '"+ this.getMainName()+"'.");
60
                        }
61
                }
62

    
63
                switch(this.getShowMode()){
64
                case MODE_WINDOW:
65
                        // When running from the composer messages of stdout shows in the console tab.
66
                        System.out.println("Showing dialog as window "+ this.getName()+".");
67
                        uimanager.showWindow(thinlet, this.getName());
68
                        break;
69
                case MODE_TOOL:
70
                        // When running from the composer messages of stdout shows in the console tab.
71
                        System.out.println("Showing dialog as tool "+ this.getName()+".");
72
                        uimanager.showTool(thinlet, this.getName());
73
                        break;
74
                default:
75
                        // When running from the composer messages of stdout shows in the console tab.
76
                        System.out.println("Showing dialog as dialog "+ this.getName()+".");
77
                        uimanager.showDialog(thinlet, this.getName());
78
                        break;
79
                }
80
                return null;
81
        }
82
        
83
        protected void loadInf(Ini prefs){
84
                super.loadInf(prefs);
85
                this.showMode=Integer.parseInt(getInfValue(prefs,"Dialog","showMode",new Integer(MODE_WINDOW)).toString());
86
        }
87

    
88
        protected void save(Ini prefs){
89
                super.save(prefs);
90
                prefs.put("Dialog","showMode", new Integer(this.showMode));
91
        }
92
        
93
        public String[] getIconNames() {
94
                return new String[]{
95
                                "scripting_dialog",
96
                                "scripting_dialog_open"
97
                };
98
        }
99

    
100

    
101
        public void actionPerformed(ActionEvent arg0) {
102
                try {
103
                        this.invokeFunction(arg0.getActionCommand(),null);
104
                } catch (Throwable e) {
105
                        // Ignore, invokeFunction handle error it self.
106
                }
107
        }
108

    
109
        public File getDialogFile(){
110
                return this.getFileResource(".dlg");
111
        }
112
        
113
        public int getShowMode() {
114
                return this.showMode;
115
        }
116

    
117

    
118
        public void setShowMode(int mode) {
119
                this.showMode=mode;
120
        }
121

    
122
        public boolean remove() {
123
                boolean r = super.remove(); 
124
                File folder = this.getParent().getFile();
125
                File f = null;
126
                try {
127
                        f = new File(folder,this.getId()+".dlg");
128
                        FileUtils.forceDelete(f);
129
                } catch (IOException e) {
130
                        logger.warn("Can't remove dialog file '"+f.getAbsolutePath()+"'.",e);
131
                        r = false;
132
                }                
133
                return r;
134
        }
135

    
136
        public void create(ScriptingFolder folder, String id, String language) {
137
                super.create(folder, id, language);
138
                
139
                File fileDlg = this.getResource(this.getId()+".dlg"); 
140
                try {
141
                        fileDlg.createNewFile();
142
                } catch (IOException e) {
143
                        logger.warn("Can't create the dialog in '"+fileDlg.getAbsolutePath()+"'.",e);
144
                }
145
                
146
                // Escribimos en el fichero '.dlg' lo mi?nimo para que pueda ejecutarse
147
            Writer output = null; 
148
            try {
149
                        output = new BufferedWriter(new FileWriter(fileDlg));
150
                        output.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<panel/>");
151
                        output.close();
152
                } catch (IOException e) {
153
                        logger.warn("Can't write xml code of the dialog to '"+fileDlg.getAbsolutePath()+"'.",e);
154
                }
155
            
156
                this.save();                
157
        }
158
}