1 import org.ejen.ext.Version; 22 import java.io.FileWriter ; 23 import java.awt.*; 24 import java.awt.event.*; 25 import javax.swing.*; 26 27 public class Install { 28 private static final String 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 [] args) { 49 try { 50 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 new Install(); 55 } 56 public class MainFrame extends JFrame { 57 JPanel contentPanel; 58 JTextField jdkTextField; 59 String javaRuntimeName; 60 String javaVersion; 61 String osName; 62 String osVersion; 63 String osArch; 64 String javaHome; 65 String strippedJavaHome; 66 String fileSeparator; 67 public MainFrame() { 68 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 69 try { 70 getSystemProperties(); 71 jInit(); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 private void jInit() throws Exception { 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 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 e) { 175 showError(e.toString()); 176 } 177 } 178 179 private void showError(String 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 msg) { 189 JOptionPane.showMessageDialog(this, msg, "Warning", 190 JOptionPane.WARNING_MESSAGE); 191 } 192 193 private void showInformation(String msg) { 194 JOptionPane.showMessageDialog(this, msg); 195 } 196 197 private void install() { 198 String fileName; 199 String 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 fw = null; 210 211 try { 212 fw = new FileWriter (fileName); 213 fw.write(fileContent); 214 } catch (Exception 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 e) {} 223 finally { 224 fw = null; 225 } 226 } 227 } 228 if (fileSeparator.equals("/")) { 230 try { 231 Runtime.getRuntime().exec("chmod +x " + fileName); 232 } catch (Exception 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
|