1 36 package org.ungoverned.oscar.installer; 37 38 import java.awt.*; 39 import java.awt.event.*; 40 import java.io.File ; 41 import java.util.*; 42 43 import javax.swing.*; 44 import javax.swing.border.BevelBorder ; 45 46 import org.ungoverned.oscar.installer.artifact.*; 47 import org.ungoverned.oscar.installer.editor.*; 48 import org.ungoverned.oscar.installer.property.*; 49 import org.ungoverned.oscar.util.OscarConstants; 50 51 public class Install extends JFrame 52 { 53 private static transient final String PROPERTY_FILE = "property.xml"; 54 private static transient final String ARTIFACT_FILE = "artifact.xml"; 55 56 public static transient final String JAVA_DIR = "Java directory"; 57 public static transient final String INSTALL_DIR = "Install directory"; 58 59 private PropertyPanel m_propPanel = null; 60 private JButton m_okayButton = null; 61 private JButton m_cancelButton = null; 62 private JLabel m_statusLabel = null; 63 64 private java.util.List m_propList = null; 65 private java.util.List m_artifactList = null; 66 67 public Install() 68 throws Exception 69 { 70 super("Install"); 71 72 m_propList = loadPropertyList(); 75 m_artifactList = loadArtifactList(); 76 77 getContentPane().setLayout(new BorderLayout()); 78 getContentPane().add( 79 m_propPanel = new PropertyPanel(m_propList), BorderLayout.CENTER); 80 getContentPane().add(createButtonPanel(), BorderLayout.SOUTH); 81 pack(); 82 setResizable(true); 83 centerWindow(this); 84 85 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 87 addWindowListener(new WindowAdapter() { 88 public void windowClosing(WindowEvent event) 89 { 90 doCancel(); 91 } 92 }); 93 } 94 95 public java.util.List loadPropertyList() 96 { 97 String installDir = System.getProperty("user.home"); 98 if (!installDir.endsWith(File.separator)) 99 { 100 installDir = installDir + File.separator; 101 } 102 103 Property prop = null; 104 105 java.util.List list = new ArrayList(); 107 108 prop = new BooleanPropertyImpl("Shell", true); 110 prop.setEditor(new BooleanEditor((BooleanProperty) prop, "Text", "GUI")); 111 list.add(prop); 112 113 prop = new StringPropertyImpl(JAVA_DIR, System.getProperty("java.home")); 115 prop.setEditor(new FileEditor((StringProperty) prop, true)); 116 list.add(prop); 117 118 prop = new StringPropertyImpl(INSTALL_DIR, installDir + "Oscar"); 120 prop.setEditor(new FileEditor((StringProperty) prop, true)); 121 list.add(prop); 122 123 prop = new BooleanStringPropertyImpl( 125 "User documentation", 126 true, 127 "http://download.forge.objectweb.org/oscar/oscar-doc-" 128 + OscarConstants.OSCAR_VERSION_VALUE + ".jar"); 129 list.add(prop); 130 131 prop = new BooleanStringPropertyImpl( 133 "API documentation", 134 true, 135 "http://download.forge.objectweb.org/oscar/oscar-api-" 136 + OscarConstants.OSCAR_VERSION_VALUE + ".jar"); 137 list.add(prop); 138 139 return list; 140 } 141 142 public java.util.List loadArtifactList() throws Exception 143 { 144 java.util.List list = new ArrayList(); 146 list.add( 147 new ArtifactHolder( 148 (BooleanProperty) getProperty("User documentation"), 149 new URLJarArtifact( 150 (StringProperty) getProperty("User documentation")))); 151 list.add( 152 new ArtifactHolder( 153 (BooleanProperty) getProperty("API documentation"), 154 new URLJarArtifact( 155 (StringProperty) getProperty("API documentation")))); 156 list.add( 157 new ArtifactHolder( 158 new ResourceJarArtifact( 159 new StringPropertyImpl("sourceName", "package.jar")))); 160 list.add( 161 new ArtifactHolder( 162 new ResourceFileArtifact( 163 new StringPropertyImpl("sourceName", "src.jar")))); 164 list.add( 165 new ArtifactHolder( 166 new ResourceFileArtifact( 167 new StringPropertyImpl("sourceName", "LICENSE.txt")))); 168 list.add( 169 new ArtifactHolder( 170 (BooleanProperty) getProperty("Shell"), 171 new ResourceFileArtifact( 172 new StringPropertyImpl("sourceName", "system.properties.text"), 173 new StringPropertyImpl("destName", "system.properties"), 174 new StringPropertyImpl("destDir", "lib")))); 175 list.add( 176 new ArtifactHolder( 177 new NotBooleanPropertyImpl((BooleanProperty) getProperty("Shell")), 178 new ResourceFileArtifact( 179 new StringPropertyImpl("sourceName", "system.properties.gui"), 180 new StringPropertyImpl("destName", "system.properties"), 181 new StringPropertyImpl("destDir", "lib")))); 182 list.add( 183 new ArtifactHolder( 184 new ResourceFileArtifact( 185 new StringPropertyImpl("sourceName", "example.policy")))); 186 list.add( 187 new ArtifactHolder( 188 new ResourceFileArtifact( 189 new StringPropertyImpl("sourceName", "oscar.bat"), 190 new StringPropertyImpl("destName" , "oscar.bat"), 191 new StringPropertyImpl("destDir", ""), 192 true))); 193 list.add( 194 new ArtifactHolder( 195 new ResourceFileArtifact( 196 new StringPropertyImpl("sourceName", "oscar.sh"), 197 new StringPropertyImpl("destName" , "oscar.sh"), 198 new StringPropertyImpl("destDir", ""), 199 true))); 200 201 return list; 202 } 203 204 private Property getProperty(String name) 205 { 206 for (int i = 0; i < m_propList.size(); i++) 207 { 208 Property prop = (Property) m_propList.get(i); 209 if (prop.getName().equals(name)) 210 { 211 return prop; 212 } 213 } 214 return null; 215 } 216 217 protected void doOkay() 218 { 219 m_propPanel.setEnabled(false); 220 m_okayButton.setEnabled(false); 221 m_cancelButton.setEnabled(false); 222 new Thread (new InstallRunnable()).start(); 223 } 224 225 protected void doCancel() 226 { 227 System.exit(0); 228 } 229 230 protected JPanel createButtonPanel() 231 { 232 JPanel buttonPanel = new JPanel(); 233 buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); 234 235 GridBagLayout grid = new GridBagLayout(); 237 GridBagConstraints c = new GridBagConstraints(); 238 239 buttonPanel.setLayout(grid); 240 241 c.insets = new Insets(2, 2, 2, 2); 243 244 c.gridx = 0; 246 c.gridy = 0; 247 c.gridwidth = 1; 248 c.gridheight = 1; 249 c.anchor = GridBagConstraints.EAST; 250 grid.setConstraints(m_okayButton = new JButton("OK"), c); 251 buttonPanel.add(m_okayButton); 252 m_okayButton.setDefaultCapable(true); 253 getRootPane().setDefaultButton(m_okayButton); 254 255 c.gridx = 1; 257 c.gridy = 0; 258 c.gridwidth = 1; 259 c.gridheight = 1; 260 c.anchor = GridBagConstraints.WEST; 261 grid.setConstraints(m_cancelButton = new JButton("Cancel"), c); 262 buttonPanel.add(m_cancelButton); 263 264 m_okayButton.addActionListener(new ActionListener() { 266 public void actionPerformed(ActionEvent event) 267 { 268 doOkay(); 269 } 270 }); 271 272 m_cancelButton.addActionListener(new ActionListener() { 273 public void actionPerformed(ActionEvent event) 274 { 275 doCancel(); 276 } 277 }); 278 279 m_statusLabel = new JLabel("Oscar installation"); 281 m_statusLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 282 283 JPanel panel = new JPanel(new BorderLayout()); 285 panel.add(buttonPanel, BorderLayout.CENTER); 286 panel.add(m_statusLabel, BorderLayout.SOUTH); 287 return panel; 288 } 289 290 public static void centerWindow(Component window) 291 { 292 Toolkit toolkit = Toolkit.getDefaultToolkit(); 293 Dimension dim = toolkit.getScreenSize(); 294 int screenWidth = dim.width; 295 int screenHeight = dim.height; 296 int x = (screenWidth - window.getSize().width) / 2; 297 int y = (screenHeight - window.getSize().height) / 2; 298 window.setLocation(x, y); 299 } 300 301 public static void main(String [] argv) throws Exception 302 { 303 String msg = "<html>" 304 + "<center><h1>Oscar " + OscarConstants.OSCAR_VERSION_VALUE + "</h1></center>" 305 + "You can download example bundles at the Oscar shell prompt by<br>" 306 + "using the <b><tt>obr</tt></b> command to access the Oscar Bundle Repository;<br>" 307 + "type <b><tt>obr help</tt></b> at the Oscar shell prompt for details." 308 + "</html>"; 309 JLabel label = new JLabel(msg); 310 label.setFont(new Font("SansSerif", Font.PLAIN, 11)); 311 final JDialog dlg = new JDialog((Frame) null, "Oscar Install", true); 312 dlg.getContentPane().setLayout(new BorderLayout(10, 10)); 313 dlg.getContentPane().add(label, BorderLayout.CENTER); 314 JPanel panel = new JPanel(); 315 JButton button = new JButton("OK"); 316 button.addActionListener(new ActionListener() { 317 public void actionPerformed(ActionEvent event) 318 { 319 dlg.hide(); 320 } 321 }); 322 panel.add(button); 323 dlg.getContentPane().add(panel, BorderLayout.SOUTH); 324 dlg.getContentPane().add(new JPanel(), BorderLayout.NORTH); 326 dlg.getContentPane().add(new JPanel(), BorderLayout.EAST); 327 dlg.getContentPane().add(new JPanel(), BorderLayout.WEST); 328 dlg.pack(); 329 centerWindow(dlg); 330 dlg.show(); 331 332 Install obj = new Install(); 333 obj.setVisible(true); 334 } 335 336 class InstallRunnable implements Runnable 337 { 338 public void run() 339 { 340 Map propMap = new HashMap(); 341 for (int i = 0; i < m_propList.size(); i++) 342 { 343 Property prop = (Property) m_propList.get(i); 344 propMap.put(prop.getName(), prop); 345 } 346 347 String installDir = ((StringProperty) propMap.get(INSTALL_DIR)).getStringValue(); 348 349 if (!installDir.endsWith(File.separator)) 351 { 352 installDir = installDir + File.separator; 353 } 354 355 File file = new File (installDir); 358 if (!file.exists()) 359 { 360 if (!file.mkdirs()) 361 { 362 JOptionPane.showMessageDialog(Install.this, 363 "Unable to create install directory.", 364 "Error", JOptionPane.ERROR_MESSAGE); 365 System.exit(-1); 366 } 367 } 368 else if (!file.isDirectory()) 369 { 370 JOptionPane.showMessageDialog(Install.this, 371 "The selected install location is not a directory.", 372 "Error", JOptionPane.ERROR_MESSAGE); 373 System.exit(-1); 374 } 375 376 StatusRunnable sr = new StatusRunnable(); 378 379 for (int i = 0; i < m_artifactList.size(); i++) 381 { 382 ArtifactHolder ah = (ArtifactHolder) m_artifactList.get(i); 383 if (ah.isIncluded()) 384 { 385 if (!ah.getArtifact().process(sr, propMap)) 386 { 387 JOptionPane.showMessageDialog(Install.this, 388 "An error occurred while processing the resources.", 389 "Error", JOptionPane.ERROR_MESSAGE); 390 System.exit(-1); 391 } 392 } 393 } 394 395 System.exit(0); 396 } 397 } 398 399 class StatusRunnable implements Status, Runnable 400 { 401 private String text = null; 402 403 public void setText(String s) 404 { 405 text = s; 406 try { 407 SwingUtilities.invokeAndWait(this); 408 } catch (Exception ex) { 409 } 411 } 412 413 public void run() 414 { 415 m_statusLabel.setText(text); 416 } 417 } 418 419 private static BooleanProperty m_trueProp = 421 new BooleanPropertyImpl("mandatory", true); 422 423 class ArtifactHolder 424 { 425 private BooleanProperty m_isIncluded = null; 426 private Artifact m_artifact = null; 427 428 public ArtifactHolder(Artifact artifact) 429 { 430 this(m_trueProp, artifact); 431 } 432 433 public ArtifactHolder(BooleanProperty isIncluded, Artifact artifact) 434 { 435 m_isIncluded = isIncluded; 436 m_artifact = artifact; 437 } 438 439 public boolean isIncluded() 440 { 441 return m_isIncluded.getBooleanValue(); 442 } 443 444 public Artifact getArtifact() 445 { 446 return m_artifact; 447 } 448 } 449 } 450 | Popular Tags |