1 package org.splat.launcher;
4 import java.awt.ComponentOrientation;
5 import java.awt.GridLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
9 import java.io.FileNotFoundException;
10 import java.io.ObjectInputStream;
12 import java.net.URLConnection;
15 * Applet providing a set of buttons. Each button is defined by three parameters:<BR>
17 * <li><i>icon<i></i></li>
18 * <li><i>tool<i></i></li>
19 * <li><i>file<i></i></li> - may be null
21 * where i is an index beginning from zero.
23 public class ToolbarApplet extends java.applet.Applet implements ActionListener {
26 * Serialization version id.
28 private static final long serialVersionUID = 3243053622061086715L;
30 // ==============================================================================================================================
31 // Overridden functions
32 // ==============================================================================================================================
37 * @see java.applet.Applet#init()
41 URL appurl = getCodeBase();
42 int hgap = 6; // Gap between icons
45 int width = getSize().width;
46 int height = getSize().height;
47 for (ntools = 0; ntools < (width + hgap) / (height + hgap); ntools++) {
48 String icon = this.getParameter("icon" + ntools);
49 String tool = this.getParameter("tool" + ntools);
50 String file = this.getParameter("file" + ntools); // May be null
51 if (icon != null && tool != null) {
52 ToolButton button = new ToolButton(height, getImage(appurl,
53 "../skin/" + icon), tool, file);
54 add(button); // For displaying the button
55 button.addActionListener(this);
58 setLayout(new GridLayout(1, ntools, hgap, 0)); // 1 row, {ntools} buttons
59 setBackground(Color.decode("#D2E7FF"));
60 applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
66 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
68 public void actionPerformed(final ActionEvent event) {
69 if (event.getSource() instanceof ToolButton) {
70 ToolButton clicked = (ToolButton) event.getSource();
71 String module = clicked.getTool();
72 String target = clicked.getTarget();
73 launch(module, target);
77 // ==============================================================================================================================
78 // Public member functions
79 // ==============================================================================================================================
82 * Launch an appropriate application for the given file.
89 public void launch(final String name, final String filename) {
93 // Opening a Web page in a new tab
94 if (module.startsWith("http")) {
95 getAppletContext().showDocument(new URL(module), "_blank");
98 // Opening a Web module in a new tab
99 if (module.startsWith("../") || module.startsWith("/")) {
100 module = getCodeBase().toString() + module;
101 if (filename != null) {
102 module = module + "?open=" + filename;
104 getAppletContext().showDocument(new URL(module), "_blank");
107 // Opening an application on the local machine
109 if (module.equals("runSalome")) {
111 // To get the SALOME_HOME environment variable
112 String SALOME_HOME = System.getenv("SALOME_ROOT_DIR");
113 // If SALOME_ROOT_DIR does not exist the SALOME_HOME equals to null.
114 if (SALOME_HOME == null) {
115 // TODO: Use logger to be more user friendly
119 getCodeBase().toString()
120 + "error.jsp?message=launch&value="
121 + "SALOME_ROOT_DIR environment variable is not defined."),
124 "SALOME_ROOT_DIR nevironment variable is not defined.");
125 } else if (!SALOME_HOME.endsWith(File.separator)) {
126 SALOME_HOME += File.separator;
128 String pathToScript = SALOME_HOME + "runSalome";
130 File script = new File(pathToScript);
131 if (!script.exists()) {
132 script = new File(pathToScript + ".bat");
133 if (!script.exists()) {
134 script = new File(pathToScript + ".cmd");
138 if (script.exists()) {
139 // Execute the runSalome script.
140 // filename here indeed a string containing parameters for runSalome.
141 Runtime.getRuntime().exec(
142 new String[] { script.getAbsolutePath(), filename });
144 // TODO: Use logger to be more user friendly
145 getAppletContext().showDocument(
146 new URL(getCodeBase().toString()
147 + "error.jsp?message=launch&value="
148 + "SALOME module is not found: "
149 + script.getAbsolutePath()), "_blank");
150 throw new FileNotFoundException(
151 "SALOME module is not found: " + script.getAbsolutePath());
155 if (module.endsWith(".exe") || module.endsWith(".EXE")) {
156 String applikey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\"
158 String valkey = ""; // Default application and file keys
160 module = WindowsRegistry.readValue(applikey
161 + "\\shell\\open\\command", valkey);
162 if (module == null) {
163 module = WindowsRegistry.readValue(applikey
164 + "\\shell\\edit\\command", valkey);
166 // if (module == null) module = "\"C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
167 if (module == null) {
168 module = getCodeBase().toString()
169 + "error.jsp?message=launch&value=" + name;
170 getAppletContext().showDocument(new URL(module));
172 String[] parse = module.split("/");
173 String command = parse[0]; // Removing eventual options
174 if (filename != null) {
176 // Opening the application with a server side existing file
177 if (filename.startsWith("http")) {
178 path = " \"" + filename + "\"";
180 // Opening the application with a file previously created by the server
181 else if (filename.startsWith("/jsp")) { // Document created through a JSP
182 URL my = getDocumentBase();
183 String http = "http://" + my.getHost() + ":"
184 + my.getPort() + "/";
185 String[] jsppath = my.getPath().split("/"); // Parses "/webapp/..."
186 String location = http + jsppath[1] + filename;
188 URLConnection connection = new URL(location)
190 ObjectInputStream fromservlet = new ObjectInputStream(
191 connection.getInputStream());
192 URL docurl = (URL) fromservlet.readObject();
195 if (docurl != null) {
196 path = " " + docurl.toString();
199 command = command + path;
201 Runtime.getRuntime().exec(command);
204 } catch (Exception error) {
205 error.printStackTrace(); // RKV: NOPMD: TODO: try to use logger