示例#1
0
  public static final LookAndFeelType getLAF() {
    if (lookAndFeelType == null) {
      try {
        initializeLookAndFeel();
      } catch (InitializationException e) {
        LogManager.log(e);
      }
      lookAndFeelType = LookAndFeelType.DEFAULT;

      if (UiMode.getCurrentUiMode() == UiMode.SWING) {
        LookAndFeel laf = UIManager.getLookAndFeel();
        if (laf != null) {
          String id = laf.getID();
          for (LookAndFeelType type : LookAndFeelType.values()) {
            if (id.equals(LookAndFeelType.WINDOWS_XP.getId())
                || id.equals(LookAndFeelType.WINDOWS_CLASSIC.getId())) {
              final Object object =
                  Toolkit.getDefaultToolkit().getDesktopProperty(WINDOWS_XP_THEME_MARKER_PROPERTY);
              boolean xpThemeActive = false;
              if (object != null) {
                xpThemeActive = (Boolean) object;
              }
              lookAndFeelType =
                  (xpThemeActive) ? LookAndFeelType.WINDOWS_XP : LookAndFeelType.WINDOWS_CLASSIC;
              break;
            } else if (id.equals(type.getId())) {
              lookAndFeelType = type;
              break;
            }
          }
        }
      }
    }
    return lookAndFeelType;
  }
示例#2
0
  public static String getDefaultLookAndFeelClassName() {
    switch (UiMode.getCurrentUiMode()) {
      case SWING:
        String className = UIManager.getSystemLookAndFeelClassName();

        // if the default look and feel is the cross-platform one, we might
        // need to correct this choice. E.g. - KDE, where GTK look and feel
        // would be much more appropriate
        if (className.equals(UIManager.getCrossPlatformLookAndFeelClassName())) {

          // if the current platform is Linux and the desktop manager is
          // KDE, then we should try to use the GTK look and feel
          try {
            if (System.getProperty("os.name").contains("Linux")
                && (System.getenv("KDE_FULL_SESSION") != null)) {
              // check whether the GTK look and feel class is
              // available -- we'll get CNFE if it is not and it will
              // not be set
              Class.forName(LookAndFeelType.GTK.getClassName());

              className = LookAndFeelType.GTK.getClassName();
            }
          } catch (ClassNotFoundException e) {
            ErrorManager.notifyDebug(
                ResourceUtils.getString(UiUtils.class, RESOURCE_FAILED_TO_FORCE_GTK), e);
          }
        }

        return className;
      default:
        return null;
    }
  }
示例#3
0
  /**
   * @param title The title of the dialog
   * @param message The message of the dialog
   * @param silentDefault The default return value if installer is running silently
   * @return true if user click YES option. In silent mode return <code>silentDefault</code>
   */
  public static boolean showYesNoDialog(
      final String title, final String message, final boolean silentDefault) {
    initLAF();
    switch (UiMode.getCurrentUiMode()) {
      case SWING:
        LogManager.logIndent("... showing Yes/No dialog");
        LogManager.log("title: " + title);
        LogManager.log("message: " + message);
        final int result = JOptionPane.showConfirmDialog(null, message, title, YES_NO_OPTION);
        LogManager.logUnindent(
            "... dialog closed, choice : "
                + (result == YES_OPTION ? "yes" : (result == NO_OPTION ? "no" : "closed")));
        return result == YES_OPTION;

      case SILENT:
        LogManager.log(message);
        final String option =
            StringUtils.format(
                ResourceUtils.getString(
                    UiUtils.class,
                    silentDefault ? RESOURCE_SILENT_DEFAULT_YES : RESOURCE_SILENT_DEFAULT_NO));
        System.err.println(message);
        System.err.println(option);
        LogManager.log(message);
        LogManager.log(option);
        return silentDefault;
    }
    // never get this line...
    return true;
  }
示例#4
0
  public static boolean showMessageDialog(
      final String message, final String title, final MessageType messageType) {
    initLAF();

    boolean exitInstaller = false;

    switch (UiMode.getCurrentUiMode()) {
      case SWING:
        int intMessageType = JOptionPane.INFORMATION_MESSAGE;

        LogManager.logIndent("... show message dialog");
        LogManager.log("title: " + title);
        LogManager.log("message: " + message);

        if (messageType == MessageType.WARNING) {
          intMessageType = JOptionPane.WARNING_MESSAGE;
        } else if (messageType == MessageType.CRITICAL) {
          intMessageType = JOptionPane.ERROR_MESSAGE;
          exitInstaller = true;
        }

        if (messageType == MessageType.ERROR) {
          int result =
              JOptionPane.showOptionDialog(
                  null,
                  message,
                  title,
                  JOptionPane.YES_NO_OPTION,
                  JOptionPane.ERROR_MESSAGE,
                  null,
                  null,
                  JOptionPane.YES_OPTION);
          if (result == JOptionPane.NO_OPTION) {
            exitInstaller = true;
            LogManager.logUnindent("... user selected: NO");
          } else {
            LogManager.logUnindent("... user selected: YES");
          }
        } else {
          JOptionPane.showMessageDialog(null, message, title, intMessageType);
        }

        LogManager.logUnindent("... dialog closed");
        break;
      case SILENT:
        LogManager.log(message);
        System.err.println(message);
        break;
    }

    return exitInstaller;
  }
示例#5
0
  /**
   * @param title The title of the dialog
   * @param message The message of the dialog
   * @param silentDefault The dafault return value if installer is running silently
   * @return true if user click YES option. In silent mode return <code>silentDefault</code>
   */
  public static int showYesNoCancelDialog(
      final String title, final String message, final int silentDefault) {
    initLAF();
    switch (UiMode.getCurrentUiMode()) {
      case SWING:
        LogManager.logIndent("... show Yes/No/Cancel dialog");
        LogManager.log("title: " + title);
        LogManager.log("message: " + message);
        int result = JOptionPane.showConfirmDialog(null, message, title, YES_NO_CANCEL_OPTION);
        LogManager.logUnindent(
            "... dialog closed, choice : "
                + (result == YES_OPTION
                    ? "yes"
                    : (result == NO_OPTION
                        ? "no"
                        : (result == CANCEL_OPTION ? "cancel" : "closed"))));
        return result;

      case SILENT:
        LogManager.log(message);
        String resource;
        switch (silentDefault) {
          case YES_OPTION:
            resource = RESOURCE_SILENT_DEFAULT_YES;
            break;
          case NO_OPTION:
            resource = RESOURCE_SILENT_DEFAULT_NO;
            break;
          case CANCEL_OPTION:
            resource = RESOURCE_SILENT_DEFAULT_CANCEL;
            break;
          default:
            resource = StringUtils.EMPTY_STRING;
            break;
        }

        final String option = StringUtils.format(ResourceUtils.getString(UiUtils.class, resource));
        System.err.println(message);
        System.err.println(option);
        LogManager.log(message);
        LogManager.log(option);
        return silentDefault;
    }
    // never get this line...
    return silentDefault;
  }
示例#6
0
  public static void initializeLookAndFeel() throws InitializationException {
    if (lookAndFeelInitialized) {
      return;
    }

    try {
      LogManager.log("... initializing look and feel");
      LogManager.indent();
      switch (UiMode.getCurrentUiMode()) {
        case SWING:
          String className = System.getProperty(LAF_CLASS_NAME_PROPERTY);
          if (className == null) {
            LogManager.log(
                "... custom look and feel class name was not specified, using system default");
            className = UiUtils.getDefaultLookAndFeelClassName();
          } else if (!className.contains(StringUtils.DOT)) { // short name of the L&F class
            className = UiUtils.getLookAndFeelClassNameByShortName(className);
          }

          LogManager.log("... class name: " + className);

          if (Boolean.getBoolean(LAF_DECORATED_WINDOWS_PROPERTY)) {
            JFrame.setDefaultLookAndFeelDecorated(true);
          }

          try {
            Thread jdkFileChooserWarningLogThread = null;
            try {
              // this helps to avoid some GTK L&F bugs for some locales
              LogManager.log("... get installed L&Fs");
              UIManager.getInstalledLookAndFeels();
              LogManager.log("... set specified L&F");
              UIManager.setLookAndFeel(className);
              LogManager.log("... check headless");
              if (GraphicsEnvironment.isHeadless()) {
                HeadlessException e =
                    new HeadlessException(
                        ResourceUtils.getString(
                            UiUtils.class, RESOURCE_FAILED_TO_INIT_UI_HEADLESS));
                System.err.println(
                    ResourceUtils.getString(UiUtils.class, RESOURCE_FAILED_TO_INIT_UI));
                System.err.println(e.getMessage());
                throw new InitializationException(
                    ResourceUtils.getString(UiUtils.class, RESOURCE_FAILED_TO_INIT_UI), e);
              }

              if (System.getProperty("os.name").startsWith("Windows")) {
                // workaround for the issue with further using JFileChooser
                // in case of missing system icons
                // Java Issue :
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6210674
                // NBI Issue :
                // http://www.netbeans.org/issues/show_bug.cgi?id=105065
                // it also a workaround for two more bugs
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6449933
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6489447
                LogManager.log("... creating JFileChooser object to check possible issues with UI");

                if (System.getProperty("java.version").startsWith("1.6")) {
                  File desktop = new File(SystemUtils.getUserHomeDirectory(), "Desktop");
                  File[] zips = null;
                  final List<String> names = new ArrayList<String>();
                  if (FileUtils.exists(desktop)) {
                    zips =
                        desktop.listFiles(
                            new FileFilter() {
                              public boolean accept(File pathname) {
                                boolean result =
                                    pathname.getName().endsWith(".zip")
                                        && pathname.length() > 1000000L;
                                if (result) {
                                  names.add(pathname.getName());
                                }
                                return result;
                              }
                            });
                  }
                  if (zips != null && zips.length > 0) {
                    jdkFileChooserWarningLogThread =
                        new NbiThread() {
                          @Override
                          public void run() {
                            try {
                              sleep(8000); // 8 seconds
                            } catch (InterruptedException e) {
                              return;
                            }
                            final File lock =
                                new File(
                                    Installer.getInstance().getLocalDirectory(),
                                    Installer.LOCK_FILE_NAME);
                            LogManager.log("\n... !!! WARNING !!!");
                            LogManager.log(
                                "... There are some big zip files on your desktop: "
                                    + StringUtils.asString(names));
                            LogManager.log(
                                "... In case installer UI does not appear for a long time:");
                            LogManager.log("...    1) kill the installer process");
                            LogManager.log(
                                "...    2) move those zip files somewhere from the desktop");
                            LogManager.log("...    3) delete " + lock);
                            LogManager.log("...    4) run installer again");
                            LogManager.log(
                                "... For more details see the following bugs descriptions: ");
                            LogManager.log(
                                "... http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6372808");
                            LogManager.log(
                                "... http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050516");
                          }
                        };
                    jdkFileChooserWarningLogThread.start();
                  }
                }

                if (SwingUtilities.isEventDispatchThread()) {
                  new JFileChooser();
                } else {
                  try {
                    SwingUtilities.invokeAndWait(
                        new Runnable() {
                          public void run() {
                            new JFileChooser();
                          }
                        });
                  } catch (InvocationTargetException e) {
                    throw (e.getCause() != null) ? e.getCause() : e;
                  }
                }

                if (jdkFileChooserWarningLogThread != null) {
                  jdkFileChooserWarningLogThread.interrupt();
                  jdkFileChooserWarningLogThread = null;
                }

                LogManager.log("... getting default Toolkit to check possible issues with UI");
                Toolkit.getDefaultToolkit();

                // workaround for JDK issue with JProgressBar using StyleXP
                // http://www.netbeans.org/issues/show_bug.cgi?id=106876
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337517
                LogManager.log("... creating JProgressBar object to check possible issues with UI");
                new JProgressBar().getMaximumSize();

                LogManager.log("... all UI checks done");
              }
              LogManager.log("... L&F is set");
            } catch (Throwable e) {
              if (jdkFileChooserWarningLogThread != null) {
                jdkFileChooserWarningLogThread.interrupt();
                jdkFileChooserWarningLogThread = null;
              }
              // we're catching Throwable here as pretty much anything can happen
              // while setting the look and feel and we have no control over it
              // if something wrong happens we should fall back to the default
              // cross-platform look and feel which is assumed to be working
              // correctly
              LogManager.log(
                  "... could not activate defined L&F, initializing cross-platfrom one", e);
              if (e instanceof InternalError) {
                System.err.println(e.getMessage());
              } else if (e instanceof ExceptionInInitializerError) {
                final Throwable cause = e.getCause();

                if ((cause != null) && (cause instanceof HeadlessException)) {
                  System.err.println(cause.getMessage());
                }
              }

              className = UIManager.getCrossPlatformLookAndFeelClassName();
              LogManager.log("... cross-platform L&F class-name : " + className);

              UIManager.setLookAndFeel(className);

              if (System.getProperty(LAF_CLASS_NAME_PROPERTY) != null) {
                // Throw exception only if user specified custom L&F,
                // otherwise just go to initialization of cross-platfrom L&F
                //     (Exception e is already logged above)
                // See also http://www.netbeans.org/issues/show_bug.cgi?id=122557
                // This exception would be thrown only if cross-platform LAF is successfully
                // installed
                throw new InitializationException(
                    ResourceUtils.getString(UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_DEFINED_LAF),
                    e);
              }
            }
          } catch (NoClassDefFoundError e) {
            throw new InitializationException(
                ResourceUtils.getString(
                    UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_CROSSPLATFORM_LAF),
                e);
          } catch (ClassNotFoundException e) {
            throw new InitializationException(
                ResourceUtils.getString(
                    UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_CROSSPLATFORM_LAF),
                e);
          } catch (InstantiationException e) {
            throw new InitializationException(
                ResourceUtils.getString(
                    UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_CROSSPLATFORM_LAF),
                e);
          } catch (IllegalAccessException e) {
            throw new InitializationException(
                ResourceUtils.getString(
                    UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_CROSSPLATFORM_LAF),
                e);
          } catch (UnsupportedLookAndFeelException e) {
            throw new InitializationException(
                ResourceUtils.getString(
                    UiUtils.class, RESOURCE_FAILED_TO_ACTIVATE_CROSSPLATFORM_LAF),
                e);
          }
          break;
      }
    } finally {
      LogManager.unindent();
      LogManager.log("... initializing L&F finished");

      if (Boolean.getBoolean("nbi.look.and.feel.dump.defaults")) {
        try {
          LogManager.logIndent("... dumping UIManger L&F defaults: ");
          Hashtable hash = (Hashtable) UIManager.getLookAndFeelDefaults();
          Enumeration keys = hash.keys();
          while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            LogManager.log("" + key + " = " + hash.get(key));
          }
        } catch (Exception e) {
          LogManager.log(e);
        } finally {
          LogManager.unindent();
        }
      }

      lookAndFeelInitialized = true;
      lookAndFeelType = getLAF();
    }
  }