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;
8 import java.io.BufferedReader;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import java.io.ObjectInputStream;
13 import java.io.UnsupportedEncodingException;
14 import java.net.MalformedURLException;
16 import java.net.URLConnection;
17 import java.net.URLEncoder;
20 * Applet providing a set of buttons. Each button is defined by three parameters:<BR>
22 * <li><i>icon<i></i></li>
23 * <li><i>tool<i></i></li>
24 * <li><i>file<i></i></li> - may be null
26 * where i is an index beginning from zero.
28 public class ToolbarApplet extends java.applet.Applet implements ActionListener {
31 * Serialization version id.
33 private static final long serialVersionUID = 3243053622061086715L;
36 * The response key string: '<i>"canCheckout"</i> :'.
38 public static final String CHECKOUT_RES = "\"canCheckout\" :";
40 // ==============================================================================================================================
41 // Overridden functions
42 // ==============================================================================================================================
47 * @see java.applet.Applet#init()
51 URL appurl = getCodeBase();
52 int hgap = 6; // Gap between icons
55 int width = getSize().width;
56 int height = getSize().height;
57 for (ntools = 0; ntools < (width + hgap) / (height + hgap); ntools++) {
58 String icon = this.getParameter("icon" + ntools);
59 String tool = this.getParameter("tool" + ntools);
60 String file = this.getParameter("file" + ntools); // May be null
61 if (icon != null && tool != null) {
62 ToolButton button = new ToolButton(height, getImage(appurl,
63 "../skin/" + icon), tool, file);
64 add(button); // For displaying the button
65 button.addActionListener(this);
68 setLayout(new GridLayout(1, ntools, hgap, 0)); // 1 row, {ntools} buttons
69 setBackground(Color.decode("#D2E7FF"));
70 applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
76 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
78 public void actionPerformed(final ActionEvent event) {
79 if (event.getSource() instanceof ToolButton) {
80 ToolButton clicked = (ToolButton) event.getSource();
81 String module = clicked.getTool();
82 String target = clicked.getTarget();
83 launch(module, target);
87 // ==============================================================================================================================
88 // Public member functions
89 // ==============================================================================================================================
92 * Launch an appropriate application for the given file.
99 public void launch(final String name, final String filename) {
100 String module = name;
103 // Opening a Web page in a new tab
104 if (module.startsWith("http")) {
105 getAppletContext().showDocument(new URL(module), "_blank");
108 // Opening a Web module in a new tab
109 if (module.startsWith("../") || module.startsWith("/")) {
110 module = getCodeBase().toString() + module;
111 if (filename != null) {
112 module = module + "?open=" + filename;
114 getAppletContext().showDocument(new URL(module), "_blank");
117 // Opening an application on the local machine
119 if ("runSalome".equals(module)) {
120 salomeCheckout(filename);
123 if (module.endsWith(".exe") || module.endsWith(".EXE")) {
124 String applikey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\"
126 String valkey = ""; // Default application and file keys
128 module = WindowsRegistry.readValue(applikey
129 + "\\shell\\open\\command", valkey);
130 if (module == null) {
131 module = WindowsRegistry.readValue(applikey
132 + "\\shell\\edit\\command", valkey);
134 // if (module == null) module = "\"C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
135 if (module == null) {
136 module = getCodeBase().toString()
137 + "error.jsp?message=launch&value=" + name;
138 getAppletContext().showDocument(new URL(module));
140 String[] parse = module.split("/");
141 String command = parse[0]; // Removing eventual options
142 if (filename != null) {
144 // Opening the application with a server side existing file
145 if (filename.startsWith("http")) {
146 path = " \"" + filename + "\"";
148 // Opening the application with a file previously created by the server
149 else if (filename.startsWith("/jsp")) { // Document created through a JSP
150 URL my = getDocumentBase();
151 String http = "http://" + my.getHost() + ":"
152 + my.getPort() + "/";
153 String[] jsppath = my.getPath().split("/"); // Parses "/webapp/..."
154 String location = http + jsppath[1] + filename;
156 URLConnection connection = new URL(location)
158 ObjectInputStream fromservlet = new ObjectInputStream(
159 connection.getInputStream());
160 URL docurl = (URL) fromservlet.readObject();
163 if (docurl != null) {
164 path = " " + docurl.toString();
167 command = command + path;
169 Runtime.getRuntime().exec(command);
172 } catch (Exception error) {
173 error.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
178 * Checkout a scenario and start Salome with it.
182 * @throws IOException
183 * if URL connection opening/reading is failed
184 * @throws ConfigurationException
185 * if SALOME_ROOT_DIR environment variable is not defined or Salome launcher script is not found
187 private void salomeCheckout(final String params)
188 throws ConfigurationException, IOException {
190 // To get the SALOME_HOME environment variable
191 String SALOME_HOME = System.getenv("SALOME_ROOT_DIR");
192 // If SALOME_ROOT_DIR does not exist the SALOME_HOME equals to null.
193 if (SALOME_HOME == null) {
194 // TODO: Use logger to be more user friendly
195 showError("SALOME_ROOT_DIR environment variable is not defined.");
196 throw new ConfigurationException(
197 "SALOME_ROOT_DIR nevironment variable is not defined.");
198 } else if (!SALOME_HOME.endsWith(File.separator)) {
199 SALOME_HOME += File.separator;
201 String pathToScript = SALOME_HOME + "runSalome";
203 File script = new File(pathToScript);
204 if (!script.exists()) {
205 script = new File(pathToScript + ".bat");
206 if (!script.exists()) {
207 script = new File(pathToScript + ".cmd");
211 if (script.exists()) {
213 // Checkout the scenario
214 URL checkoutUrl = new URL(getCodeBase().toString()
216 + params.replaceAll("siman-", "").replaceAll("--", "-")
217 .replaceAll("-", "&").replaceAll("\\s", ""));
219 BufferedReader buffer = new BufferedReader(new InputStreamReader(
220 checkoutUrl.openStream()));
221 boolean isOk = false;
222 String response = buffer.readLine();
223 while ((response != null)) {
224 if (response.contains(CHECKOUT_RES)) {
225 isOk = response.contains(CHECKOUT_RES + " \"true\"");
228 response = buffer.readLine();
233 // Execute the runSalome script.
234 // filename here indeed a string containing parameters for runSalome.
235 Runtime.getRuntime().exec(
236 new String[] { script.getAbsolutePath(), params });
238 // Checkout of the scenario is failed at the beginning.
239 if (response != null) {
242 response.indexOf(CHECKOUT_RES)
243 + CHECKOUT_RES.length())
244 .replace('}', ' ').replace('"', ' ');
249 // TODO: Use logger to be more user friendly
250 showError("SALOME module is not found: " + script.getAbsolutePath());
251 throw new ConfigurationException("SALOME module is not found: "
252 + script.getAbsolutePath());
257 * Show error message in the new window.
261 * @throws MalformedURLException
262 * if built URL is incorrect
263 * @throws UnsupportedEncodingException
264 * if UTF-8 encoding is not supported
266 private void showError(final String message) throws MalformedURLException,
267 UnsupportedEncodingException {
268 getAppletContext().showDocument(
269 new URL(getCodeBase().toString() + "../error.jsp?error="
270 + URLEncoder.encode(message, "UTF-8")), "_blank");