KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Install


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
import org.ejen.ext.Version;
22 import java.io.FileWriter JavaDoc;
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 public class Install {
28     private static final String JavaDoc ejenVersion = "Ejen-" + Version.toString(null);
29     ;
30     public Install() {
31         MainFrame frame = new MainFrame();
32
33         frame.pack();
34         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
35         Dimension frameSize = frame.getSize();
36
37         if (frameSize.height > screenSize.height) {
38             frameSize.height = screenSize.height;
39         }
40         if (frameSize.width > screenSize.width) {
41             frameSize.width = screenSize.width;
42         }
43         frame.setLocation((screenSize.width - frameSize.width) / 2,
44                 (screenSize.height - frameSize.height) / 2);
45         frame.setVisible(true);
46     }
47
48     public static void main(String JavaDoc[] args) {
49         try {
50             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
51         } catch (Exception JavaDoc e) {
52             e.printStackTrace();
53         }
54         new Install();
55     }
56     public class MainFrame extends JFrame {
57         JPanel contentPanel;
58         JTextField jdkTextField;
59         String JavaDoc javaRuntimeName;
60         String JavaDoc javaVersion;
61         String JavaDoc osName;
62         String JavaDoc osVersion;
63         String JavaDoc osArch;
64         String JavaDoc javaHome;
65         String JavaDoc strippedJavaHome;
66         String JavaDoc fileSeparator;
67         public MainFrame() {
68             enableEvents(AWTEvent.WINDOW_EVENT_MASK);
69             try {
70                 getSystemProperties();
71                 jInit();
72             } catch (Exception JavaDoc e) {
73                 e.printStackTrace();
74             }
75         }
76
77         private void jInit() throws Exception JavaDoc {
78             this.setTitle(ejenVersion + " Installation");
79             contentPanel = (JPanel) (this.getContentPane());
80             contentPanel.setLayout(new BorderLayout());
81             contentPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
82             JTextArea welcome = new JTextArea(javaRuntimeName + ", version "
83                     + javaVersion + ",\n" + osName + " " + osVersion + " ("
84                     + osArch + ").\n\n" + "Based on the file separator ("
85                     + fileSeparator + "), this is a "
86                     + (fileSeparator.equals("/") ? "Unix" : "Windows")
87                     + " system.\n" + "Your JDK installation directory is:");
88
89             welcome.setEditable(false);
90             welcome.setBackground(contentPanel.getBackground());
91             welcome.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
92             ActionListener actionListener = new ActionListener() {
93                 public void actionPerformed(ActionEvent e) {
94                     String JavaDoc actionCommand = e.getActionCommand();
95
96                     if (actionCommand.equals("fileChooserButton")) {
97                         JFileChooser chooser = new JFileChooser();
98
99                         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
100                         chooser.setDialogTitle("Choose your JDK installation directory");
101                         if (chooser.showOpenDialog(contentPanel)
102                                 == JFileChooser.APPROVE_OPTION) {
103                             jdkTextField.setText(chooser.getSelectedFile().getAbsolutePath());
104                         }
105                     } else if (actionCommand.equals("okButton")) {
106                         install();
107                     } else if (actionCommand.equals("cancelButton")) {
108                         System.exit(0);
109                     }
110                 }
111             };
112             JPanel jdkPanel = new JPanel(new BorderLayout());
113
114             jdkTextField = new JTextField(strippedJavaHome);
115             JButton fileChooserButton = new JButton("...");
116
117             fileChooserButton.setActionCommand("fileChooserButton");
118             fileChooserButton.addActionListener(actionListener);
119             jdkPanel.add(jdkTextField, BorderLayout.CENTER);
120             jdkPanel.add(fileChooserButton, BorderLayout.EAST);
121             JPanel actionPanel = new JPanel(new BorderLayout());
122             JTextArea label = new JTextArea("Check those values before installing.");
123
124             label.setEditable(false);
125             label.setBackground(contentPanel.getBackground());
126             label.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
127             JButton okButton = new JButton("Install");
128
129             okButton.setActionCommand("okButton");
130             okButton.addActionListener(actionListener);
131             JButton cancelButton = new JButton("Cancel");
132
133             cancelButton.setActionCommand("cancelButton");
134             cancelButton.addActionListener(actionListener);
135             actionPanel.add(label, BorderLayout.NORTH);
136             actionPanel.add(okButton, BorderLayout.CENTER);
137             actionPanel.add(cancelButton, BorderLayout.EAST);
138             contentPanel.add(welcome, BorderLayout.NORTH);
139             contentPanel.add(jdkPanel, BorderLayout.CENTER);
140             contentPanel.add(actionPanel, BorderLayout.SOUTH);
141         }
142
143         protected void processWindowEvent(WindowEvent e) {
144             super.processWindowEvent(e);
145             if (e.getID() == WindowEvent.WINDOW_CLOSING) {
146                 System.exit(0);
147             }
148         }
149         
150         private void getSystemProperties() {
151             try {
152                 javaRuntimeName = System.getProperty("java.runtime.name");
153                 javaVersion = System.getProperty("java.version");
154                 osName = System.getProperty("os.name");
155                 osVersion = System.getProperty("os.version");
156                 osArch = System.getProperty("os.arch");
157                 javaHome = System.getProperty("java.home");
158                 fileSeparator = System.getProperty("file.separator");
159                 if (fileSeparator == null || fileSeparator.length() == 0) {
160                     showError("Empty file separator !");
161                 }
162                 if (!fileSeparator.equals("/") && !fileSeparator.equals("\\")) {
163                     showError("Unknown file separator [" + fileSeparator + "] !");
164                 }
165                 if (javaHome == null) {
166                     showError("Could not get java home directory !");
167                 }
168                 int i = javaHome.lastIndexOf(fileSeparator);
169
170                 if (i == -1) {
171                     showError("Invalid java home directory: " + javaHome);
172                 }
173                 strippedJavaHome = javaHome.substring(0, i);
174             } catch (Exception JavaDoc e) {
175                 showError(e.toString());
176             }
177         }
178         
179         private void showError(String JavaDoc msg) {
180             JOptionPane.showMessageDialog(this,
181                     msg + "\n\nPlease send a bug report to <ejen@noos.fr>,"
182                     + "\nSorry.",
183                     "Fatal error",
184                     JOptionPane.ERROR_MESSAGE);
185             System.exit(-1);
186         }
187         
188         private void showWarning(String JavaDoc msg) {
189             JOptionPane.showMessageDialog(this, msg, "Warning",
190                     JOptionPane.WARNING_MESSAGE);
191         }
192         
193         private void showInformation(String JavaDoc msg) {
194             JOptionPane.showMessageDialog(this, msg);
195         }
196     
197         private void install() {
198             String JavaDoc fileName;
199             String JavaDoc fileContent;
200
201             if (fileSeparator.equals("/")) {
202                 fileName = "java-env";
203                 fileContent = "#!/bin/sh\nexport JAVA_HOME=\""
204                         + strippedJavaHome + "\"";
205             } else {
206                 fileName = "java-env.bat";
207                 fileContent = "set JAVA_HOME=\"" + strippedJavaHome + "\"";
208             }
209             FileWriter JavaDoc fw = null;
210
211             try {
212                 fw = new FileWriter JavaDoc(fileName);
213                 fw.write(fileContent);
214             } catch (Exception JavaDoc e) {
215                 showError("Could not create \"" + fileName + "\": "
216                         + e.toString());
217             }
218             finally {
219                 if (fw != null) {
220                     try {
221                         fw.close();
222                     } catch (Exception JavaDoc e) {}
223                     finally {
224                         fw = null;
225                     }
226                 }
227             }
228             // UNIX...
229
if (fileSeparator.equals("/")) {
230                 try {
231                     Runtime.getRuntime().exec("chmod +x " + fileName);
232                 } catch (Exception JavaDoc e) {
233                     showWarning("Could not set the execute permission for \""
234                             + fileName + "\": " + e.toString()
235                             + "\nDo it yourself...");
236                 }
237             }
238             showInformation("\"" + fileName + "\" created.\n\n" + ejenVersion
239                     + " installation finished.");
240             System.exit(0);
241         }
242     }
243 }
244
Popular Tags