Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman / src / org / splat / launcher / ToolbarApplet.java
1 package org.splat.launcher;
2
3 import java.awt.ComponentOrientation;
4 import java.awt.GridLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.ObjectInputStream;
8 import java.net.URL;
9 import java.net.URLConnection;
10
11 public class ToolbarApplet extends java.applet.Applet implements ActionListener {
12
13         private static final long serialVersionUID = 3243053622061086715L;
14
15         // ==============================================================================================================================
16         // Overridden functions
17         // ==============================================================================================================================
18
19         @Override
20         public void init() {
21                 URL appurl = getCodeBase();
22                 int hgap = 6; // Gap between icons
23                 int ntools;
24
25                 int width = getSize().width;
26                 int height = getSize().height;
27                 for (ntools = 0; ntools < (width + hgap) / (height + hgap); ntools++) {
28                         String icon = this.getParameter("icon" + ntools);
29                         String tool = this.getParameter("tool" + ntools);
30                         String file = this.getParameter("file" + ntools); // May be null
31                         if (icon != null && tool != null) {
32                                 ToolButton button = new ToolButton(height, getImage(appurl,
33                                                 "../skin/" + icon), tool, file);
34                                 add(button); // For displaying the button
35                                 button.addActionListener(this);
36                         }
37                 }
38                 setLayout(new GridLayout(1, ntools, hgap, 0)); // 1 row, {ntools} buttons
39                 applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
40         }
41
42         public void actionPerformed(final ActionEvent event) {
43                 // -----------------------------------------------
44                 if (event.getSource() instanceof ToolButton) {
45                         ToolButton clicked = (ToolButton) event.getSource();
46                         String module = clicked.getTool();
47                         String target = clicked.getTarget();
48                         launch(module, target);
49                 }
50         }
51
52         // ==============================================================================================================================
53         // Public member functions
54         // ==============================================================================================================================
55
56         public void launch(final String name, final String filename) {
57                 // -------------------------------------------------
58                 String module = name;
59                 try {
60
61                         // Opening a Web page in a new tab
62                         if (module.startsWith("http")) {
63                                 getAppletContext().showDocument(new URL(module), "_blank");
64                         } else
65
66                         // Opening a Web module in a new tab
67                         if (module.startsWith("../") || module.startsWith("/")) {
68                                 module = getCodeBase().toString() + module;
69                                 if (filename != null) {
70                                         module = module + "?open=" + filename;
71                                 }
72                                 getAppletContext().showDocument(new URL(module), "_blank");
73                         } else
74
75                         // Opening an application on the local machine
76                         if (module.endsWith(".exe") || module.endsWith(".EXE")) {
77                                 String applikey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\"
78                                                 + module;
79                                 String valkey = ""; // Default application and file keys
80
81                                 module = WindowsRegistry.readValue(applikey
82                                                 + "\\shell\\open\\command", valkey);
83                                 if (module == null) {
84                                         module = WindowsRegistry.readValue(applikey
85                                                         + "\\shell\\edit\\command", valkey);
86                                 }
87                                 // if (module == null) module = "\"C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
88                                 if (module == null) {
89                                         module = getCodeBase().toString()
90                                                         + "error.jsp?message=launch&value=" + name;
91                                         getAppletContext().showDocument(new URL(module));
92                                 } else {
93                                         String[] parse = module.split("/");
94                                         String command = parse[0]; // Removing eventual options
95                                         if (filename != null) {
96                                                 String path = "";
97                                                 // Opening the application with a server side existing file
98                                                 if (filename.startsWith("http")) {
99                                                         path = " \"" + filename + "\"";
100                                                 }
101                                                 // Opening the application with a file previously created by the server
102                                                 else if (filename.startsWith("/jsp")) { // Document created through a JSP
103                                                         URL my = getDocumentBase();
104                                                         String http = "http://" + my.getHost() + ":"
105                                                                         + my.getPort() + "/";
106                                                         String[] jsppath = my.getPath().split("/"); // Parses "/webapp/..."
107                                                         String location = http + jsppath[1] + filename;
108
109                                                         URLConnection connection = new URL(location)
110                                                                         .openConnection();
111                                                         ObjectInputStream fromservlet = new ObjectInputStream(
112                                                                         connection.getInputStream());
113                                                         URL docurl = (URL) fromservlet.readObject();
114
115                                                         fromservlet.close();
116                                                         if (docurl != null) {
117                                                                 path = " " + docurl.toString();
118                                                         }
119                                                 }
120                                                 command = command + path;
121                                         }
122                                         Runtime.getRuntime().exec(command);
123                                 }
124                         }
125                 } catch (Exception error) {
126                         error.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
127                 }
128         }
129 }