Exemple #1
0
  public static int getDimension(
      Properties props, final String defaultPropertyName, final int defaultValue) {
    int dimension = defaultValue;
    String propertyName = defaultPropertyName;
    if (props.getProperty(propertyName + "." + UiUtils.getLAF()) != null) {
      propertyName = propertyName + "." + UiUtils.getLAF();
    }

    if (props.getProperty(propertyName) != null) {
      try {
        dimension = Integer.parseInt(props.getProperty(propertyName).trim());
      } catch (NumberFormatException e) {
        final String warning =
            ResourceUtils.getString(
                UiUtils.class,
                RESOURCE_FAILED_TO_PARSE_SYSTEM_PROPERTY,
                propertyName,
                props.getProperty(propertyName));

        ErrorManager.notifyWarning(warning, e);
      }
    }
    return dimension;
  }
Exemple #2
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();
    }
  }