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