| 1 20 21 package com.izforge.izpack.installer; 22 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.GraphicsEnvironment ; 28 import java.awt.GridBagConstraints ; 29 import java.awt.GridBagLayout ; 30 import java.awt.Insets ; 31 import java.awt.Point ; 32 import java.awt.Toolkit ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.WindowAdapter ; 36 import java.awt.event.WindowEvent ; 37 import java.io.InputStream ; 38 import java.io.ObjectInputStream ; 39 import java.lang.reflect.Method ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Locale ; 44 import java.util.Map ; 45 import java.util.TreeMap ; 46 47 import javax.swing.GrayFilter ; 48 import javax.swing.ImageIcon ; 49 import javax.swing.JButton ; 50 import javax.swing.JComboBox ; 51 import javax.swing.JDialog ; 52 import javax.swing.JFrame ; 53 import javax.swing.JLabel ; 54 import javax.swing.JList ; 55 import javax.swing.JOptionPane ; 56 import javax.swing.JPanel ; 57 import javax.swing.ListCellRenderer ; 58 import javax.swing.LookAndFeel ; 59 import javax.swing.SwingConstants ; 60 import javax.swing.SwingUtilities ; 61 import javax.swing.UIManager ; 62 import javax.swing.plaf.metal.MetalLookAndFeel ; 63 import javax.swing.plaf.metal.MetalTheme ; 64 65 import com.izforge.izpack.GUIPrefs; 66 import com.izforge.izpack.LocaleDatabase; 67 import com.izforge.izpack.gui.ButtonFactory; 68 import com.izforge.izpack.gui.IzPackMetalTheme; 69 import com.izforge.izpack.gui.LabelFactory; 70 import com.izforge.izpack.util.Debug; 71 import com.izforge.izpack.util.OsVersion; 72 import com.izforge.izpack.util.VariableSubstitutor; 73 74 79 public class GUIInstaller extends InstallerBase 80 { 81 82 83 private InstallData installdata; 84 85 86 protected String lnf; 87 88 89 private static final String [] LANGUAGE_DISPLAY_TYPES = { "iso3", "native", "default"}; 90 91 private static final String [][] LANG_CODES = { { "cat", "ca"}, { "chn", "zh"}, { "cze", "cs"}, 92 { "dan", "da"}, { "deu", "de"}, { "eng", "en"}, { "fin", "fi"}, { "fra", "fr"}, 93 { "hun", "hu"}, { "ita", "it"}, { "jpn", "ja"}, { "mys", "ms"}, { "ned", "nl"}, 94 { "nor", "no"}, { "pol", "pl"}, { "por", "pt"}, { "rom", "or"}, { "rus", "ru"}, 95 { "spa", "es"}, { "svk", "sk"}, { "swe", "sv"}, { "tur", "tr"}, { "ukr", "uk"}}; 96 97 98 private static HashMap isoTable; 99 100 105 public GUIInstaller() throws Exception  106 { 107 this.installdata = new InstallData(); 108 109 loadInstallData(installdata); 111 112 loadGUIInstallData(); 114 115 loadLookAndFeel(); 117 118 checkJavaVersion(); 120 121 SwingUtilities.invokeAndWait(new Runnable () { 123 public void run() 124 { 125 try 126 { 127 loadLangPack(); 128 } 129 catch (Exception e) 130 { 131 e.printStackTrace(); 132 } 133 } 134 }); 135 136 ResourceManager.create(this.installdata); 138 139 addCustomLangpack(installdata); 141 142 SwingUtilities.invokeLater(new Runnable () { 144 public void run() 145 { 146 try 147 { 148 loadGUI(); 149 } 150 catch (Exception e) 151 { 152 e.printStackTrace(); 153 } 154 } 155 }); 156 } 157 158 163 public void loadGUIInstallData() throws Exception  164 { 165 InputStream in = GUIInstaller.class.getResourceAsStream("/GUIPrefs"); 166 ObjectInputStream objIn = new ObjectInputStream (in); 167 this.installdata.guiPrefs = (GUIPrefs) objIn.readObject(); 168 objIn.close(); 169 } 170 171 176 private void checkJavaVersion() throws Exception  177 { 178 String version = System.getProperty("java.version"); 179 String required = this.installdata.info.getJavaVersion(); 180 if (version.compareTo(required) < 0) 181 { 182 StringBuffer msg = new StringBuffer (); 183 msg.append("The application that you are trying to install requires a "); 184 msg.append(required); 185 msg.append(" version or later of the Java platform.\n"); 186 msg.append("You are running a "); 187 msg.append(version); 188 msg.append(" version of the Java platform.\n"); 189 msg.append("Please upgrade to a newer version."); 190 191 System.out.println(msg.toString()); 192 JOptionPane.showMessageDialog(null, msg.toString(), "Error", JOptionPane.ERROR_MESSAGE); 193 System.exit(1); 194 } 195 } 196 197 202 private void loadLangPack() throws Exception  203 { 204 List availableLangPacks = getAvailableLangPacks(); 206 int npacks = availableLangPacks.size(); 207 if (npacks == 0) throw new Exception ("no language pack available"); 208 String selectedPack; 209 210 JFrame frame = new JFrame (); 212 frame.setIconImage(new ImageIcon (this.getClass().getResource("/img/JFrameIcon.png")) 213 .getImage()); 214 215 Dimension frameSize = frame.getSize(); 216 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 217 frame.setLocation((screenSize.width - frameSize.width) / 2, 218 (screenSize.height - frameSize.height) / 2 - 10); 219 220 if (npacks != 1) 222 { 223 LanguageDialog picker = new LanguageDialog(frame, availableLangPacks.toArray()); 224 picker.setSelection(Locale.getDefault().getISO3Country().toLowerCase()); 225 picker.setModal(true); 226 picker.toFront(); 227 frame.setVisible(false); 229 picker.setVisible(true); 230 231 selectedPack = (String ) picker.getSelection(); 232 if (selectedPack == null) throw new Exception ("installation canceled"); 233 } 234 else 235 selectedPack = (String ) availableLangPacks.get(0); 236 237 this.installdata.xmlData.setAttribute("langpack", selectedPack); 239 240 installdata.localeISO3 = selectedPack; 242 installdata.setVariable(ScriptParser.ISO3_LANG, installdata.localeISO3); 243 InputStream in = getClass().getResourceAsStream("/langpacks/" + selectedPack + ".xml"); 244 this.installdata.langpack = new LocaleDatabase(in); 245 } 246 247 253 private List getAvailableLangPacks() throws Exception  254 { 255 InputStream in = getClass().getResourceAsStream("/langpacks.info"); 257 ObjectInputStream objIn = new ObjectInputStream (in); 258 List available = (List ) objIn.readObject(); 259 objIn.close(); 260 261 return available; 262 } 263 264 269 protected void loadLookAndFeel() throws Exception  270 { 271 String syskey = "unix"; 273 if (OsVersion.IS_WINDOWS) 274 { 275 syskey = "windows"; 276 } 277 else if (OsVersion.IS_OSX) 278 { 279 syskey = "mac"; 280 } 281 String laf = null; 282 if (installdata.guiPrefs.lookAndFeelMapping.containsKey(syskey)) 283 { 284 laf = (String ) installdata.guiPrefs.lookAndFeelMapping.get(syskey); 285 } 286 287 boolean useButtonIcons = true; 290 if (installdata.guiPrefs.modifier.containsKey("useButtonIcons") 291 && "no".equalsIgnoreCase((String ) installdata.guiPrefs.modifier.get("useButtonIcons"))) useButtonIcons = false; 292 ButtonFactory.useButtonIcons(useButtonIcons); 293 boolean useLabelIcons = true; 294 if (installdata.guiPrefs.modifier.containsKey("useLabelIcons") 295 && "no".equalsIgnoreCase((String ) installdata.guiPrefs.modifier.get("useLabelIcons"))) useLabelIcons = false; 296 LabelFactory.setUseLabelIcons(useLabelIcons); 297 if (laf == null) 298 { 299 if (!"mac".equals(syskey)) 300 { 301 String syslaf = UIManager.getSystemLookAndFeelClassName(); 302 UIManager.setLookAndFeel(syslaf); 303 if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel ) 304 { 305 MetalLookAndFeel.setCurrentTheme(new IzPackMetalTheme()); 306 ButtonFactory.useHighlightButtons(); 307 ButtonFactory.useButtonIcons(useButtonIcons); 311 installdata.buttonsHColor = new Color (182, 182, 204); 312 } 313 } 314 lnf = "swing"; 315 return; 316 } 317 318 if ("kunststoff".equals(laf)) 320 { 321 ButtonFactory.useHighlightButtons(); 322 ButtonFactory.useButtonIcons(useButtonIcons); 325 installdata.buttonsHColor = new Color (255, 255, 255); 326 Class lafClass = Class.forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel"); 327 Class mtheme = Class.forName("javax.swing.plaf.metal.MetalTheme"); 328 Class [] params = { mtheme}; 329 Class theme = Class.forName("com.izforge.izpack.gui.IzPackKMetalTheme"); 330 Method setCurrentThemeMethod = lafClass.getMethod("setCurrentTheme", params); 331 332 LookAndFeel kunststoff = (LookAndFeel ) lafClass.newInstance(); 334 MetalTheme ktheme = (MetalTheme ) theme.newInstance(); 335 Object [] kparams = { ktheme}; 336 UIManager.setLookAndFeel(kunststoff); 337 setCurrentThemeMethod.invoke(kunststoff, kparams); 338 339 lnf = "kunststoff"; 340 return; 341 } 342 343 if ("liquid".equals(laf)) 345 { 346 UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel"); 347 lnf = "liquid"; 348 349 Map params = (Map ) installdata.guiPrefs.lookAndFeelParams.get(laf); 350 if (params.containsKey("decorate.frames")) 351 { 352 String value = (String ) params.get("decorate.frames"); 353 if ("yes".equals(value)) 354 { 355 JFrame.setDefaultLookAndFeelDecorated(true); 356 } 357 } 358 if (params.containsKey("decorate.dialogs")) 359 { 360 String value = (String ) params.get("decorate.dialogs"); 361 if ("yes".equals(value)) 362 { 363 JDialog.setDefaultLookAndFeelDecorated(true); 364 } 365 } 366 367 return; 368 } 369 370 if ("metouia".equals(laf)) 372 { 373 UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"); 374 lnf = "metouia"; 375 return; 376 } 377 378 if ("looks".equals(laf)) 380 { 381 Map variants = new TreeMap (); 382 variants.put("extwin", "com.jgoodies.plaf.windows.ExtWindowsLookAndFeel"); 383 variants.put("plastic", "com.jgoodies.plaf.plastic.PlasticLookAndFeel"); 384 variants.put("plastic3D", "com.jgoodies.plaf.plastic.Plastic3DLookAndFeel"); 385 variants.put("plasticXP", "com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 386 String variant = (String ) variants.get("plasticXP"); 387 388 Map params = (Map ) installdata.guiPrefs.lookAndFeelParams.get(laf); 389 if (params.containsKey("variant")) 390 { 391 String param = (String ) params.get("variant"); 392 if (variants.containsKey(param)) 393 { 394 variant = (String ) variants.get(param); 395 } 396 } 397 398 UIManager.setLookAndFeel(variant); 399 } 400 } 401 402 407 private void loadGUI() throws Exception  408 { 409 UIManager.put("OptionPane.yesButtonText", installdata.langpack.getString("installer.yes")); 410 UIManager.put("OptionPane.noButtonText", installdata.langpack.getString("installer.no")); 411 UIManager.put("OptionPane.cancelButtonText", installdata.langpack 412 .getString("installer.cancel")); 413 String title; 414 final String key = "installer.reversetitle"; 416 String message = installdata.langpack.getString(key); 417 if (message.indexOf(key) > -1) 419 title = installdata.langpack.getString("installer.title") 420 + installdata.info.getAppName(); 421 else 422 { VariableSubstitutor vs = new VariableSubstitutor(installdata.getVariables()); 425 title = vs.substitute(message, null); 426 } 427 new InstallerFrame(title, this.installdata); 428 } 429 430 435 protected boolean useFlags() 436 { 437 if (installdata.guiPrefs.modifier.containsKey("useFlags") 438 && "no".equalsIgnoreCase((String ) installdata.guiPrefs.modifier.get("useFlags"))) 439 return (false); 440 return (true); 441 } 442 443 449 protected String getLangType() 450 { 451 if (installdata.guiPrefs.modifier.containsKey("langDisplayType")) 452 { 453 String val = (String ) installdata.guiPrefs.modifier.get("langDisplayType"); 454 val = val.toLowerCase(); 455 for (int i = 0; i < LANGUAGE_DISPLAY_TYPES.length; ++i) 457 if (val.equalsIgnoreCase(LANGUAGE_DISPLAY_TYPES[i])) return (val); 458 Debug.trace("Value for language display type not valid; value: " + val); 459 } 460 return (LANGUAGE_DISPLAY_TYPES[0]); 461 } 462 463 472 private final class LanguageDialog extends JDialog implements ActionListener  473 { 474 475 private static final long serialVersionUID = 3256443616359887667L; 476 477 478 private JComboBox comboBox; 479 480 481 private HashMap iso3Toiso2 = null; 482 483 484 private boolean isoMapExpanded = false; 485 486 491 public LanguageDialog(JFrame frame, Object [] items) 492 { 493 super(frame); 494 495 try 496 { 497 loadLookAndFeel(); 498 } 499 catch (Exception err) 500 { 501 err.printStackTrace(); 502 } 503 504 addWindowListener(new WindowHandler()); 506 JPanel contentPane = (JPanel ) getContentPane(); 507 setTitle("Language selection"); 508 GridBagLayout layout = new GridBagLayout (); 509 contentPane.setLayout(layout); 510 GridBagConstraints gbConstraints = new GridBagConstraints (); 511 gbConstraints.anchor = GridBagConstraints.CENTER; 512 gbConstraints.insets = new Insets (5, 5, 5, 5); 513 gbConstraints.fill = GridBagConstraints.NONE; 514 gbConstraints.gridx = 0; 515 gbConstraints.weightx = 1.0; 516 gbConstraints.weighty = 1.0; 517 518 ImageIcon img = getImage(); 519 JLabel imgLabel = new JLabel (img); 520 gbConstraints.gridy = 0; 521 contentPane.add(imgLabel); 522 523 gbConstraints.fill = GridBagConstraints.HORIZONTAL; 524 String firstMessage = "Please select your language"; 525 if (getLangType().equals(LANGUAGE_DISPLAY_TYPES[0])) 526 firstMessage = "Please select your language (ISO3 code)"; 528 529 JLabel label1 = new JLabel (firstMessage, SwingConstants.CENTER); 530 gbConstraints.gridy = 1; 531 gbConstraints.insets = new Insets (5, 5, 0, 5); 532 layout.addLayoutComponent(label1, gbConstraints); 533 contentPane.add(label1); 534 JLabel label2 = new JLabel ("for install instructions:", SwingConstants.CENTER); 535 gbConstraints.gridy = 2; 536 gbConstraints.insets = new Insets (0, 5, 5, 5); 537 layout.addLayoutComponent(label2, gbConstraints); 538 contentPane.add(label2); 539 gbConstraints.insets = new Insets (5, 5, 5, 5); 540 541 items = reviseItems(items); 542 543 comboBox = new JComboBox (items); 544 if (useFlags()) comboBox.setRenderer(new FlagRenderer()); 545 gbConstraints.fill = GridBagConstraints.HORIZONTAL; 546 gbConstraints.gridy = 3; 547 layout.addLayoutComponent(comboBox, gbConstraints); 548 contentPane.add(comboBox); 549 550 JButton okButton = new JButton ("OK"); 551 okButton.addActionListener(this); 552 gbConstraints.fill = GridBagConstraints.NONE; 553 gbConstraints.gridy = 4; 554 gbConstraints.anchor = GridBagConstraints.CENTER; 555 layout.addLayoutComponent(okButton, gbConstraints); 556 contentPane.add(okButton); 557 getRootPane().setDefaultButton(okButton); 558 559 if (System.getProperty("mrj.version") == null) 562 pack(); 563 else 564 setSize(getPreferredSize()); 565 566 Dimension frameSize = getSize(); 567 Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); 568 setLocation(center.x - frameSize.width / 2, 569 center.y - frameSize.height / 2 - 10); 570 setResizable(true); 571 } 572 573 579 private Object [] reviseItems(Object [] items) 580 { 581 String langType = getLangType(); 582 if (langType.equals(LANGUAGE_DISPLAY_TYPES[0])) return (items); 584 if (langType.equals(LANGUAGE_DISPLAY_TYPES[1])) 586 return (expandItems(items, (new JComboBox ()).getFont())); 587 if (langType.equals(LANGUAGE_DISPLAY_TYPES[2])) return (expandItems(items, null)); 590 return (items); 592 } 593 594 603 private Object [] expandItems(Object [] items, Font testFont) 604 { 605 int i; 606 if (iso3Toiso2 == null) 607 { iso3Toiso2 = new HashMap (32); 609 isoTable = new HashMap (); 610 for (i = 0; i < LANG_CODES.length; ++i) 611 iso3Toiso2.put(LANG_CODES[i][0], LANG_CODES[i][1]); 612 } 613 for (i = 0; i < items.length; i++) 614 { 615 Object it = expandItem(items[i], testFont); 616 isoTable.put(it, items[i]); 617 items[i] = it; 618 } 619 return items; 620 } 621 622 631 private Object expandItem(Object item, Font testFont) 632 { 633 Object iso2Str = iso3Toiso2.get(item); 634 int i; 635 if (iso2Str == null && !isoMapExpanded) 636 { isoMapExpanded = true; 638 Locale [] loc = Locale.getAvailableLocales(); 639 for (i = 0; i < loc.length; ++i) 640 iso3Toiso2.put(loc[i].getISO3Language(), loc[i].getLanguage()); 641 iso2Str = iso3Toiso2.get(item); 642 } 643 if (iso2Str == null) 644 return (item); 646 Locale locale = new Locale ((String ) iso2Str); 647 if (testFont == null) 648 return (locale.getDisplayLanguage()); 650 String str = locale.getDisplayLanguage(locale); 652 int cdut = testFont.canDisplayUpTo(str); 653 if (cdut > -1) 654 str = locale.getDisplayLanguage(); 657 return (str); 658 } 659 660 665 public ImageIcon getImage() 666 { 667 ImageIcon img; 668 try 669 { 670 img = new ImageIcon (LanguageDialog.class.getResource("/res/installer.langsel.img")); 671 } 672 catch (NullPointerException err) 673 { 674 img = null; 675 } 676 return img; 677 } 678 679 684 public Object getSelection() 685 { 686 Object retval = null; 687 if (isoTable != null) retval = isoTable.get(comboBox.getSelectedItem()); 688 return (retval != null) ? retval : comboBox.getSelectedItem(); 689 } 690 691 696 public void setSelection(Object item) 697 { 698 Object mapped = null; 699 if (isoTable != null) 700 { 701 Iterator iter = isoTable.keySet().iterator(); 702 while (iter.hasNext()) 703 { 704 Object key = iter.next(); 705 if (isoTable.get(key).equals(item)) 706 { 707 mapped = key; 708 break; 709 } 710 } 711 } 712 if (mapped == null) mapped = item; 713 comboBox.setSelectedItem(mapped); 714 } 715 716 721 public void actionPerformed(ActionEvent e) 722 { 723 dispose(); 724 } 725 726 731 private class WindowHandler extends WindowAdapter  732 { 733 734 739 public void windowClosing(WindowEvent e) 740 { 741 System.exit(0); 742 } 743 } 744 } 745 746 751 private static class FlagRenderer extends JLabel implements ListCellRenderer  752 { 753 754 private static final long serialVersionUID = 3832899961942782769L; 755 756 757 private TreeMap icons = new TreeMap (); 758 759 760 private TreeMap grayIcons = new TreeMap (); 761 762 public FlagRenderer() 763 { 764 setOpaque(true); 765 } 766 767 777 public Component getListCellRendererComponent(JList list, Object value, int index, 778 boolean isSelected, boolean cellHasFocus) 779 { 780 String iso3 = (String ) value; 782 setText(iso3); 783 if (isoTable != null) iso3 = (String ) isoTable.get(iso3); 784 if (isSelected) 785 { 786 setForeground(list.getSelectionForeground()); 787 setBackground(list.getSelectionBackground()); 788 } 789 else 790 { 791 setForeground(list.getForeground()); 792 setBackground(list.getBackground()); 793 } 794 796 if (!icons.containsKey(iso3)) 797 { 798 ImageIcon icon; 799 icon = new ImageIcon (this.getClass().getResource("/res/flag." + iso3)); 800 icons.put(iso3, icon); 801 icon = new ImageIcon (GrayFilter.createDisabledImage(icon.getImage())); 802 grayIcons.put(iso3, icon); 803 } 804 if (isSelected || index == -1) 805 setIcon((ImageIcon ) icons.get(iso3)); 806 else 807 setIcon((ImageIcon ) grayIcons.get(iso3)); 808 809 return this; 811 } 812 } 813 } 814 | Popular Tags |