Revision 468 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

View differences:

DefaultScriptingDialog.java
7 7
import java.io.FileWriter;
8 8
import java.io.IOException;
9 9
import java.io.Writer;
10
import java.util.Map;
11 10

  
12 11
import org.apache.commons.io.FileUtils;
13
import org.apache.commons.io.FilenameUtils;
14 12
import org.gvsig.scripting.ExecuteErrorException;
15 13
import org.gvsig.scripting.ScriptingDialog;
16 14
import org.gvsig.scripting.ScriptingFolder;
......
22 20
import org.slf4j.Logger;
23 21
import org.slf4j.LoggerFactory;
24 22

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

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

  
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();
28
    public DefaultScriptingDialog(ScriptingFolder parent, ScriptingManager manager, String id) {
29
        super(parent, ScriptingManager.UNIT_DIALOG, manager, id);
30
        this.setMainName("onload");
31
        this.showMode = MODE_WINDOW;
32
    }
43 33

  
44
		JThinlet thinlet = uimanager.createJThinlet();
45
		thinlet.addActionListener(this);
46
		thinlet.load(this.getFileResource(".dlg"));
34
    @Override
35
    public void runAsTask(Object[] args) {
36
        // Los dialogos no se ejecutan en otro hilo.
37
        this.run(args);
38
    }
47 39

  
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
		}
40
    @Override
41
    public Object run(Object args[]) {
42
        ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager();
62 43

  
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
	}
44
        JThinlet thinlet = uimanager.createJThinlet();
45
        thinlet.addActionListener(this);
46
        thinlet.load(this.getFileResource(".dlg"));
87 47

  
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
	}
48
        this.put("dialog", thinlet.getThinlet());
99 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
                // When running from the composer messages of stdout shows in the console tab.
57
                System.out.println("Code of dialog do not has function '" + this.getMainName() + "'.");
58
            } else {
59
                throw ex;
60
            }
61
        }
100 62

  
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
	}
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
    }
108 82

  
109
	public File getDialogFile(){
110
		return this.getFileResource(".dlg");
111
	}
112
	
113
	public int getShowMode() {
114
		return this.showMode;
115
	}
83
    @Override
84
    protected void loadInf(Ini prefs) {
85
        super.loadInf(prefs);
86
        this.showMode = Integer.parseInt(getInfValue(prefs, "Dialog", "showMode", MODE_WINDOW).toString());
87
    }
116 88

  
89
    @Override
90
    protected void save(Ini prefs) {
91
        super.save(prefs);
92
        prefs.put("Dialog", "showMode", this.showMode);
93
    }
117 94

  
118
	public void setShowMode(int mode) {
119
		this.showMode=mode;
120
	}
95
    @Override
96
    public String[] getIconNames() {
97
        return new String[]{
98
            "scripting_dialog",
99
            "scripting_dialog_open"
100
        };
101
    }
121 102

  
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
	}
103
    @Override
104
    public void actionPerformed(ActionEvent arg0) {
105
        try {
106
            this.invokeFunction(arg0.getActionCommand(), null);
107
        } catch (Throwable e) {
108
            // Ignore, invokeFunction handle error it self.
109
        }
110
    }
135 111

  
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
	}
112
    @Override
113
    public File getDialogFile() {
114
        return this.getFileResource(".dlg");
115
    }
116

  
117
    @Override
118
    public int getShowMode() {
119
        return this.showMode;
120
    }
121

  
122
    @Override
123
    public void setShowMode(int mode) {
124
        this.showMode = mode;
125
    }
126

  
127
    @Override
128
    public boolean remove() {
129
        boolean r = super.remove();
130
        File folder = this.getParent().getFile();
131
        File f = new File(folder, this.getId() + ".dlg");
132
        try {
133
            FileUtils.forceDelete(f);
134
        } catch (IOException e) {
135
            logger.warn("Can't remove dialog file '" + f.getAbsolutePath() + "'.", e);
136
            r = false;
137
        }
138
        return r;
139
    }
140

  
141
    @Override
142
    public void create(ScriptingFolder folder, String id, String language) {
143
        super.create(folder, id, language);
144

  
145
        File fileDlg = this.getResource(this.getId() + ".dlg");
146
        try {
147
            fileDlg.createNewFile();
148
        } catch (IOException e) {
149
            logger.warn("Can't create the dialog in '" + fileDlg.getAbsolutePath() + "'.", e);
150
        }
151

  
152
        // Escribimos en el fichero '.dlg' lo mi?nimo para que pueda ejecutarse
153
        Writer output;
154
        try {
155
            output = new BufferedWriter(new FileWriter(fileDlg));
156
            output.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<panel/>");
157
            output.close();
158
        } catch (IOException e) {
159
            logger.warn("Can't write xml code of the dialog to '" + fileDlg.getAbsolutePath() + "'.", e);
160
        }
161

  
162
        this.save();
163
    }
164

  
165
    @Override
166
    public String getMimeType() {
167
        return "text";
168
    }
158 169
}

Also available in: Unified diff