]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/launcher/ToolbarApplet.java
Salome HOME
3a7c5903d7c89e5240fefc851c52b99a9c67b680
[tools/siman.git] / Workspace / Siman / src / org / splat / launcher / ToolbarApplet.java
1 package org.splat.launcher;
2
3 import java.awt.Color;
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.File;
9 import java.io.FileNotFoundException;
10 import java.io.ObjectInputStream;
11 import java.net.URL;
12 import java.net.URLConnection;
13
14 /**
15  * Applet providing a set of buttons. Each button is defined by three parameters:<BR>
16  * <ul>
17  * <li><i>icon&lt;i&gt;</i></li>
18  * <li><i>tool&lt;i&gt;</i></li>
19  * <li><i>file&lt;i&gt;</i></li> - may be null
20  * </ul>
21  * where i is an index beginning from zero.
22  */
23 public class ToolbarApplet extends java.applet.Applet implements ActionListener {
24
25         /**
26          * Serialization version id.
27          */
28         private static final long serialVersionUID = 3243053622061086715L;
29
30         // ==============================================================================================================================
31         // Overridden functions
32         // ==============================================================================================================================
33
34         /**
35          * {@inheritDoc}
36          * 
37          * @see java.applet.Applet#init()
38          */
39         @Override
40         public void init() {
41                 URL appurl = getCodeBase();
42                 int hgap = 6; // Gap between icons
43                 int ntools;
44
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);
56                         }
57                 }
58                 setLayout(new GridLayout(1, ntools, hgap, 0)); // 1 row, {ntools} buttons
59                 setBackground(Color.decode("#D2E7FF"));
60                 applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
61         }
62
63         /**
64          * {@inheritDoc}
65          * 
66          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
67          */
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);
74                 }
75         }
76
77         // ==============================================================================================================================
78         // Public member functions
79         // ==============================================================================================================================
80
81         /**
82          * Launch an appropriate application for the given file.
83          * 
84          * @param name
85          *            module name
86          * @param filename
87          *            file name
88          */
89         public void launch(final String name, final String filename) {
90                 String module = name;
91                 try {
92
93                         // Opening a Web page in a new tab
94                         if (module.startsWith("http")) {
95                                 getAppletContext().showDocument(new URL(module), "_blank");
96                         } else
97
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;
103                                 }
104                                 getAppletContext().showDocument(new URL(module), "_blank");
105                         } else
106
107                         // Opening an application on the local machine
108
109                         if (module.equals("runSalome")) {
110                                 // Run SALOME
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
116                                         getAppletContext()
117                                                         .showDocument(
118                                                                         new URL(
119                                                                                         getCodeBase().toString()
120                                                                                                         + "error.jsp?message=launch&value="
121                                                                                                         + "SALOME_ROOT_DIR environment variable is not defined."),
122                                                                         "_blank");
123                                         throw new Exception(
124                                                         "SALOME_ROOT_DIR nevironment variable is not defined.");
125                                 } else if (!SALOME_HOME.endsWith(File.separator)) {
126                                         SALOME_HOME += File.separator;
127                                 }
128                                 String pathToScript = SALOME_HOME + "runSalome";
129
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");
135                                         }
136                                 }
137                                 
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 });
143                                 } else {
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());
152                                 }
153                         } else
154
155                         if (module.endsWith(".exe") || module.endsWith(".EXE")) {
156                                 String applikey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\"
157                                                 + module;
158                                 String valkey = ""; // Default application and file keys
159
160                                 module = WindowsRegistry.readValue(applikey
161                                                 + "\\shell\\open\\command", valkey);
162                                 if (module == null) {
163                                         module = WindowsRegistry.readValue(applikey
164                                                         + "\\shell\\edit\\command", valkey);
165                                 }
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));
171                                 } else {
172                                         String[] parse = module.split("/");
173                                         String command = parse[0]; // Removing eventual options
174                                         if (filename != null) {
175                                                 String path = "";
176                                                 // Opening the application with a server side existing file
177                                                 if (filename.startsWith("http")) {
178                                                         path = " \"" + filename + "\"";
179                                                 }
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;
187
188                                                         URLConnection connection = new URL(location)
189                                                                         .openConnection();
190                                                         ObjectInputStream fromservlet = new ObjectInputStream(
191                                                                         connection.getInputStream());
192                                                         URL docurl = (URL) fromservlet.readObject();
193
194                                                         fromservlet.close();
195                                                         if (docurl != null) {
196                                                                 path = " " + docurl.toString();
197                                                         }
198                                                 }
199                                                 command = command + path;
200                                         }
201                                         Runtime.getRuntime().exec(command);
202                                 }
203                         }
204                 } catch (Exception error) {
205                         error.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
206                 }
207         }
208 }