gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.main / src / main / java / org / gvsig / scripting / main / Main.java @ 639
History | View | Annotate | Download (6.57 KB)
1 |
package org.gvsig.scripting.main; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.File; |
7 |
import java.net.URL; |
8 |
import java.util.Calendar; |
9 |
import java.util.Date; |
10 |
import java.util.HashMap; |
11 |
import java.util.Map; |
12 |
import javax.swing.JComponent; |
13 |
|
14 |
import javax.swing.JFrame; |
15 |
import javax.swing.JMenu; |
16 |
import javax.swing.JMenuBar; |
17 |
import javax.swing.JMenuItem; |
18 |
import javax.swing.WindowConstants; |
19 |
|
20 |
import org.apache.commons.cli.CommandLine; |
21 |
import org.apache.commons.cli.CommandLineParser; |
22 |
import org.apache.commons.cli.Options; |
23 |
import org.apache.commons.cli.ParseException; |
24 |
import org.apache.commons.cli.PosixParser; |
25 |
import org.gvsig.about.AboutLocator; |
26 |
import org.gvsig.about.AboutManager; |
27 |
import org.gvsig.scripting.ScriptingDialog; |
28 |
import org.gvsig.scripting.ScriptingManager; |
29 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
30 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
31 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
32 |
|
33 |
public class Main { |
34 |
|
35 |
/**
|
36 |
*
|
37 |
*/
|
38 |
private static final long serialVersionUID = -6853523688803006280L; |
39 |
ScriptingUIManager uimanager; |
40 |
AboutManager aboutManager; |
41 |
ScriptingManager manager; |
42 |
|
43 |
public static void main(String args[]) { |
44 |
Main main = new Main();
|
45 |
main.doMain(args); |
46 |
} |
47 |
|
48 |
private void initializeAbout() { |
49 |
aboutManager = AboutLocator.getManager(); |
50 |
|
51 |
Map<String, String> gvsig_vars = new HashMap<String, String>(); |
52 |
gvsig_vars.put("version", "2.0.0"); |
53 |
gvsig_vars.put("build", "2020"); |
54 |
aboutManager.setProject("gvSIG Desktop", getResource("gvsig.html"), |
55 |
null, gvsig_vars);
|
56 |
aboutManager.getProject().set("build", "2021"); |
57 |
|
58 |
aboutManager.addSponsor("Generalitat Valenciana",
|
59 |
getResource("gva.html"), 1); |
60 |
|
61 |
aboutManager.addDeveloper("gvSIG Asocicion",
|
62 |
getResource("gvSIG-asociacion.html"), 2); |
63 |
|
64 |
} |
65 |
|
66 |
private void registerPluginFolders(File pluginsFolder) { |
67 |
|
68 |
File[] folders = pluginsFolder.listFiles(); |
69 |
for (File folder : folders) { |
70 |
File scriptsFolder = new File(folder,"scripting/scripts"); |
71 |
if( scriptsFolder.exists() ) {
|
72 |
manager.registerSystemFolder(folder.getName(), scriptsFolder); |
73 |
} |
74 |
File libFolder = new File(folder,"scripting/lib"); |
75 |
if( libFolder.exists() ) {
|
76 |
manager.addLibFolder(libFolder); |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
public void doMain(String args[]) { |
82 |
|
83 |
new DefaultLibrariesInitializer().fullInitialize();
|
84 |
|
85 |
initializeAbout(); |
86 |
|
87 |
uimanager = ScriptingSwingLocator.getUIManager(); |
88 |
manager = uimanager.getManager(); |
89 |
|
90 |
Options options = new Options();
|
91 |
options.addOption("l", "show-launcher", false, |
92 |
"Show the launcher window");
|
93 |
options.addOption("c", "show-composer", false, |
94 |
"Show the composer window");
|
95 |
options.addOption("r", "run", true, "Run the specified script"); |
96 |
options.addOption("f", "plugins-folder", true, "Register scripts and libs folder in the plugins located in this location"); |
97 |
options.addOption("s", "show", true, "Show the specified dialog"); |
98 |
options.addOption("h", "home", true, |
99 |
"Specify for the home of the script framework");
|
100 |
CommandLineParser parser = new PosixParser();
|
101 |
CommandLine line = null;
|
102 |
try {
|
103 |
line = parser.parse(options, args); |
104 |
if (line.hasOption("home")) { |
105 |
manager.setHomeFolder(new File(line.getOptionValue("home"))); |
106 |
} |
107 |
if (line.hasOption("plugins-folder")) { |
108 |
registerPluginFolders(new File(line.getOptionValue("plugins-folder"))); |
109 |
} |
110 |
if (line.hasOption("show-launcher")) { |
111 |
showLauncher(); |
112 |
} |
113 |
if (line.hasOption("show-composer")) { |
114 |
showComposer(); |
115 |
} |
116 |
if (line.hasOption("run")) { |
117 |
manager.getScript(new File(line.getOptionValue("run"))).run(); |
118 |
} |
119 |
if (line.hasOption("show")) { |
120 |
showDialog(line.getOptionValue("show"));
|
121 |
if ( !(line.hasOption("show-launcher") || line.hasOption("show-composer")) ) { |
122 |
System.exit(0); |
123 |
} |
124 |
} |
125 |
} catch (ParseException exp) { |
126 |
System.out.println("Unexpected exception:" + exp.getMessage()); |
127 |
} |
128 |
String[] moreArgs = line.getArgs(); |
129 |
if( moreArgs.length > 0 ) { |
130 |
showDialog(moreArgs[0]);
|
131 |
System.exit(0); |
132 |
} |
133 |
showLauncher(); |
134 |
} |
135 |
|
136 |
private void showDialog(String name) { |
137 |
if (!name.endsWith(".dlg")) { |
138 |
name = name + ".dlg";
|
139 |
} |
140 |
ScriptingDialog dlg = (ScriptingDialog) manager |
141 |
.getScript(new File(name)); |
142 |
dlg.setShowMode(ScriptingDialog.MODE_DIALOG); |
143 |
dlg.run(); |
144 |
} |
145 |
|
146 |
private void showLauncher() { |
147 |
JFrame frame = new JFrame("gvSIG's Script Framework"); |
148 |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
149 |
|
150 |
// Create the menu bar.
|
151 |
JMenuBar menuBar = new JMenuBar(); |
152 |
|
153 |
// Build the menu.
|
154 |
JMenu menu_file = new JMenu("File"); |
155 |
|
156 |
JMenuItem menuItem_composer = new JMenuItem("Composer"); |
157 |
menuItem_composer.addActionListener(new ActionListener() { |
158 |
|
159 |
public void actionPerformed(ActionEvent e) { |
160 |
showComposer(); |
161 |
} |
162 |
}); |
163 |
menu_file.add(menuItem_composer); |
164 |
|
165 |
JMenuItem menuItem_installer = new JMenuItem("Install package"); |
166 |
menuItem_installer.addActionListener(new ActionListener() { |
167 |
|
168 |
public void actionPerformed(ActionEvent e) { |
169 |
showPackageInstaller(manager); |
170 |
} |
171 |
}); |
172 |
menu_file.add(menuItem_installer); |
173 |
|
174 |
JMenuItem menuItem_installPackage = new JMenuItem("Build package"); |
175 |
menuItem_installer.addActionListener(new ActionListener() { |
176 |
|
177 |
public void actionPerformed(ActionEvent e) { |
178 |
showPackageBuilder(manager); |
179 |
} |
180 |
}); |
181 |
menu_file.add(menuItem_installPackage); |
182 |
|
183 |
menu_file.addSeparator(); |
184 |
|
185 |
JMenuItem menuItem_exit = new JMenuItem("Exit"); |
186 |
menuItem_exit.addActionListener(new ActionListener() { |
187 |
|
188 |
public void actionPerformed(ActionEvent e) { |
189 |
System.exit(0); |
190 |
} |
191 |
}); |
192 |
menu_file.add(menuItem_exit); |
193 |
|
194 |
menuBar.add(menu_file); |
195 |
|
196 |
frame.add(menuBar, BorderLayout.NORTH);
|
197 |
frame.add(uimanager.createLauncher().asJComponent(), BorderLayout.CENTER);
|
198 |
|
199 |
// Display the window.
|
200 |
frame.pack(); |
201 |
frame.setVisible(true);
|
202 |
|
203 |
} |
204 |
|
205 |
@SuppressWarnings("unused") |
206 |
private Date getDate(int year, int month, int day) { |
207 |
Calendar cal = Calendar.getInstance(); |
208 |
cal.set(year, month, day); |
209 |
return cal.getTime();
|
210 |
} |
211 |
|
212 |
private URL getResource(String name) { |
213 |
URL resource = this.getClass().getResource(name); |
214 |
return resource;
|
215 |
} |
216 |
|
217 |
private void showComposer() { |
218 |
showWindow(uimanager.createComposer().asJComponent(), "Scripting Composer");
|
219 |
} |
220 |
|
221 |
private void showPackageInstaller(ScriptingManager manager) { |
222 |
// not implemented
|
223 |
} |
224 |
|
225 |
private void showPackageBuilder(ScriptingManager manager) { |
226 |
// not implemented
|
227 |
} |
228 |
|
229 |
private void showWindow(JComponent component, String title) { |
230 |
JFrame frame = new JFrame(title); |
231 |
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
232 |
|
233 |
frame.setContentPane(component); |
234 |
|
235 |
// Display the window.
|
236 |
frame.pack(); |
237 |
frame.setVisible(true);
|
238 |
} |
239 |
} |