From ff7ed17007414fbb17984b2daf5f380606ca8496 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Thu, 28 Apr 2016 08:34:11 +0000
Subject: [PATCH] OPENDJ-2971 Resurect graphical setup methods

---
 opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/UIFactory.java        |   30 ++++++++++
 opendj-server-legacy/src/main/java/org/opends/quicksetup/Application.java         |   44 ++++++++++++++
 opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetup.java       |   60 ++++++++++++++++++++
 opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetupDialog.java |   16 +++++
 4 files changed, 150 insertions(+), 0 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/Application.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/Application.java
index 0b1bcef..2facb8f 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/Application.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/Application.java
@@ -41,6 +41,7 @@
 import org.opends.admin.ads.util.ServerLoader;
 import org.opends.quicksetup.event.ProgressNotifier;
 import org.opends.quicksetup.event.ProgressUpdateListener;
+import org.opends.quicksetup.ui.GuiApplication;
 import org.opends.quicksetup.util.ProgressMessageFormatter;
 import org.opends.quicksetup.util.UIKeyStore;
 import org.opends.quicksetup.util.Utils;
@@ -77,6 +78,49 @@
   protected TempLogFile tempLogFile;
 
   /**
+   * Creates an application by instantiating the Application class
+   * denoted by the System property
+   * <code>org.opends.quicksetup.Application.class</code>.
+   * @return Application object that was newly instantiated
+   * @throws RuntimeException if there was a problem
+   *  creating the new Application object
+   */
+  public static GuiApplication create() throws RuntimeException {
+    GuiApplication app;
+    String appClassName =
+        System.getProperty("org.opends.quicksetup.Application.class");
+    if (appClassName != null) {
+      Class<?> appClass = null;
+      try {
+        appClass = Class.forName(appClassName);
+        app = (GuiApplication) appClass.newInstance();
+      } catch (ClassNotFoundException e) {
+        logger.info(LocalizableMessage.raw("error creating quicksetup application", e));
+        String msg = "Application class " + appClass + " not found";
+        throw new RuntimeException(msg, e);
+      } catch (IllegalAccessException e) {
+        logger.info(LocalizableMessage.raw("error creating quicksetup application", e));
+        String msg = "Could not access class " + appClass;
+        throw new RuntimeException(msg, e);
+      } catch (InstantiationException e) {
+        logger.info(LocalizableMessage.raw("error creating quicksetup application", e));
+        String msg = "Error instantiating class " + appClass;
+        throw new RuntimeException(msg, e);
+      } catch (ClassCastException e) {
+        String msg = "The class indicated by the system property " +
+            "'org.opends.quicksetup.Application.class' must " +
+            " must be of type Application";
+        throw new RuntimeException(msg, e);
+      }
+    } else {
+      String msg = "System property 'org.opends.quicksetup.Application.class'" +
+          " must specify class quicksetup application";
+      throw new RuntimeException(msg);
+    }
+    return app;
+  }
+
+  /**
    * Sets this instances user data.
    * @param userData UserData this application will use
    *        when executing
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetup.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetup.java
index 209ccda3..1906550 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetup.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetup.java
@@ -33,11 +33,13 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizableMessageBuilder;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
+import org.opends.quicksetup.Application;
 import org.opends.quicksetup.CurrentInstallStatus;
 import org.opends.quicksetup.Installation;
 import org.opends.quicksetup.ProgressDescriptor;
 import org.opends.quicksetup.ProgressStep;
 import org.opends.quicksetup.Step;
+import org.opends.quicksetup.TempLogFile;
 import org.opends.quicksetup.UserDataCertificateException;
 import org.opends.quicksetup.UserDataConfirmationException;
 import org.opends.quicksetup.UserDataException;
@@ -47,6 +49,8 @@
 import org.opends.quicksetup.event.ProgressUpdateEvent;
 import org.opends.quicksetup.event.ProgressUpdateListener;
 import org.opends.quicksetup.util.BackgroundTask;
+import org.opends.quicksetup.util.HtmlProgressMessageFormatter;
+import org.opends.quicksetup.util.ProgressMessageFormatter;
 import org.opends.server.util.SetupUtils;
 
 /**
@@ -81,6 +85,56 @@
   private static final String MAC_APPLICATIONS_OPENER = "/usr/bin/open";
 
   /**
+   * This method creates the install/uninstall dialogs and to check the current
+   * install status. This method must be called outside the event thread because
+   * it can perform long operations which can make the user think that the UI is
+   * blocked.
+   *
+   * @param tempLogFile
+   *          temporary log file where messages will be logged.
+   * @param args
+   *          for the moment this parameter is not used but we keep it in order
+   *          to (in case of need) pass parameters through the command line.
+   */
+  public void initialize(final TempLogFile tempLogFile, String[] args)
+  {
+    ProgressMessageFormatter formatter = new HtmlProgressMessageFormatter();
+
+    installStatus = new CurrentInstallStatus();
+
+    application = Application.create();
+    application.setProgressMessageFormatter(formatter);
+    application.setCurrentInstallStatus(installStatus);
+    application.setTempLogFile(tempLogFile);
+    if (args != null)
+    {
+      application.setUserArguments(args);
+    }
+    else
+    {
+      application.setUserArguments(new String[] {});
+    }
+    try
+    {
+      initLookAndFeel();
+    }
+    catch (Throwable t)
+    {
+      // This is likely a bug.
+      t.printStackTrace();
+    }
+
+    /* In the calls to setCurrentStep the dialog will be created */
+    setCurrentStep(application.getFirstWizardStep());
+  }
+
+  /** This method displays the setup dialog. This method must be called from the event thread. */
+  public void display()
+  {
+    getDialog().packAndShow();
+  }
+
+  /**
    * ButtonActionListener implementation. It assumes that we are called in the
    * event thread.
    *
@@ -562,6 +616,12 @@
     getDialog().displayFieldInvalid(fieldName, invalid);
   }
 
+  /** A method to initialize the look and feel. */
+  private void initLookAndFeel() throws Throwable
+  {
+    UIFactory.initialize();
+  }
+
   /**
    * A methods that creates an ProgressDescriptor based on the value of a
    * ProgressUpdateEvent.
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetupDialog.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetupDialog.java
index ba62918..90e7e60 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetupDialog.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/QuickSetupDialog.java
@@ -38,6 +38,8 @@
 import org.opends.quicksetup.WizardStep;
 import org.opends.quicksetup.event.ButtonActionListener;
 import org.opends.quicksetup.event.ButtonEvent;
+import org.opends.quicksetup.event.MinimumSizeComponentListener;
+
 /**
  * This class represents the dialog used by quicksetup applications.
  *
@@ -94,6 +96,20 @@
     Utilities.setFrameIcon(frame);
   }
 
+  /** Packs and displays this dialog. */
+  public void packAndShow()
+  {
+    frame.pack();
+    int minWidth = (int) frame.getPreferredSize().getWidth();
+    int minHeight = (int) frame.getPreferredSize().getHeight();
+    Utilities.centerOnScreen(frame);
+    setFocusOnButton(application.getInitialFocusButtonName());
+    frame.addComponentListener(new MinimumSizeComponentListener(frame,
+        minWidth, minHeight));
+
+    frame.setVisible(true);
+  }
+
   /**
    * This method is called when we detected that there is something installed
    * we inform of this to the user and the user wants to proceed with the
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/UIFactory.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/UIFactory.java
index 76e78f2..9a05615 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/UIFactory.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/ui/UIFactory.java
@@ -53,6 +53,7 @@
 import javax.swing.text.html.HTMLEditorKit;
 
 import org.forgerock.i18n.LocalizableMessage;
+import org.forgerock.i18n.slf4j.LocalizedLogger;
 
 /**
  * This class provides constants an methods to create Swing objects and to
@@ -63,7 +64,9 @@
  */
 public class UIFactory
 {
+  private static boolean initialized;
   private static String parentPackagePath;
+  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
   /** Specifies the horizontal insets between buttons. */
   public static final int HORIZONTAL_INSET_BETWEEN_BUTTONS = 5;
@@ -416,6 +419,33 @@
   }
 
   /**
+   * This method initialize the look and feel and UI settings specific to quick
+   * setup.
+   *
+   * @throws Throwable
+   *           if there is a problem initializing the look and feel.
+   */
+  public static void initialize() throws Throwable
+  {
+    if (!initialized)
+    {
+      try
+      {
+        UIManager.put("OptionPane.background", getColor(INFO_OPTIONPANE_BACKGROUND_COLOR.get()));
+        UIManager.put("Panel.background", getColor(INFO_PANEL_BACKGROUND_COLOR.get()));
+        UIManager.put("ComboBox.background", getColor(INFO_COMBOBOX_BACKGROUND_COLOR.get()));
+      }
+      catch (Throwable t)
+      {
+        // This might occur when we do not get the display
+        logger.warn(LocalizableMessage.raw("Error updating UIManager: " + t, t));
+      }
+      initializeLookAndFeel();
+      initialized = true;
+    }
+  }
+
+  /**
    * Creates a new JPanel.
    *
    * @return JPanel newly created

--
Gitblit v1.10.0