001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel;
029
030import static org.opends.messages.AdminToolMessages.
031 INFO_CONTROL_PANEL_LAUNCHER_USAGE_DESCRIPTION;
032
033import java.awt.Component;
034import java.awt.Container;
035import java.awt.event.ComponentAdapter;
036import java.awt.event.ComponentEvent;
037import java.awt.event.ComponentListener;
038import java.awt.event.WindowAdapter;
039import java.awt.event.WindowEvent;
040
041import javax.swing.JFrame;
042import javax.swing.RootPaneContainer;
043import javax.swing.SwingUtilities;
044import javax.swing.WindowConstants;
045
046import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
047import org.opends.guitools.controlpanel.ui.ControlCenterMainPane;
048import org.opends.guitools.controlpanel.ui.GenericFrame;
049import org.opends.guitools.controlpanel.ui.LocalOrRemotePanel;
050import org.opends.guitools.controlpanel.ui.MainMenuBar;
051import org.opends.guitools.controlpanel.util.BlindApplicationTrustManager;
052import org.opends.guitools.controlpanel.util.Utilities;
053import org.opends.messages.AdminToolMessages;
054import org.opends.quicksetup.Installation;
055import org.forgerock.i18n.LocalizableMessage;
056import org.opends.quicksetup.ui.UIFactory;
057import org.opends.quicksetup.util.Utils;
058import org.opends.server.util.DynamicConstants;
059import com.forgerock.opendj.cli.ArgumentException;
060
061/**
062 * The class that is in charge of creating the main dialog of the ControlPanel
063 * and the ControlCenterInfo (the data structure that is used by all the GUI
064 * components and that contains information about the server status and server
065 * configuration).
066 */
067public class ControlPanel
068{
069  private JFrame dlg;
070  private ControlPanelInfo info;
071  private ControlPanelArgumentParser argParser;
072  private ControlCenterMainPane controlCenterPane;
073
074  /**
075   * Main method that is used for testing purposes.  The control-panel
076   * command-line is launched through the ControlPanelLauncher (which displays
077   * a splash screen and calls the <code>initialize</code> and
078   * <code>createAndDisplayMethods</code>.
079   * @param args the arguments that are passed.
080   */
081  public static void main(String[] args) {
082    try
083    {
084      initLookAndFeel();
085    }
086    catch (Throwable t)
087    {
088      t.printStackTrace();
089    }
090    final ControlPanel test = new ControlPanel();
091    test.initialize(args);
092    javax.swing.SwingUtilities.invokeLater(new Runnable() {
093      public void run() {
094        test.createAndDisplayGUI();
095      }
096    });
097  }
098
099  /**
100   * Method that creates the ControlCenterInfo object that will be in all the
101   * control panel.  Nothing is done here: the user must say whether the server
102   * is local or remote.
103   * @param args the arguments that are passed in the command line.  The code
104   * assumes that the arguments have been already validated.
105   */
106  public void initialize(String[] args)
107  {
108    info = ControlPanelInfo.getInstance();
109    // Call Installation because the LocalOrRemotePanel uses it to check
110    // whether the server is running or not and to get the install path.
111    Installation.getLocal();
112    argParser = new ControlPanelArgumentParser(ControlPanel.class.getName(),
113        INFO_CONTROL_PANEL_LAUNCHER_USAGE_DESCRIPTION.get());
114    try
115    {
116      argParser.initializeArguments();
117      argParser.parseArguments(args);
118    }
119    catch (ArgumentException ae)
120    {
121      // Bug
122      throw new IllegalStateException("Arguments not correctly parsed: "+ae,
123          ae);
124    }
125    if (argParser.isTrustAll())
126    {
127      info.setTrustManager(new BlindApplicationTrustManager());
128    }
129    info.setConnectTimeout(argParser.getConnectTimeout());
130  }
131
132  /**
133   * Creates the main Control Panel dialog and displays it.
134   */
135  public void createAndDisplayGUI()
136  {
137    LocalOrRemotePanel localOrRemotePanel = new LocalOrRemotePanel();
138    localOrRemotePanel.setInfo(info);
139    final GenericFrame localOrRemote = new GenericFrame(localOrRemotePanel);
140    localOrRemote.pack();
141    Utilities.centerOnScreen(localOrRemote);
142
143    if (argParser.isRemote())
144    {
145      updateLocalOrRemotePanel(localOrRemote);
146    }
147
148    if (argParser.getBindPassword() != null)
149    {
150      updateLocalOrRemotePanel(localOrRemote);
151      getLocalOrRemotePanel(localOrRemote.getContentPane()).
152      setCallOKWhenVisible(true);
153    }
154
155    ComponentListener listener = new ComponentAdapter()
156    {
157      /** {@inheritDoc} */
158      public void componentHidden(ComponentEvent e)
159      {
160        handleWindowClosed(localOrRemote, info);
161      }
162    };
163    localOrRemote.addComponentListener(listener);
164    localOrRemote.setVisible(true);
165  }
166
167  private void handleWindowClosed(GenericFrame localOrRemote,
168      final ControlPanelInfo info)
169  {
170    if (info.getServerDescriptor() == null)
171    {
172      MainMenuBar menuBar = new MainMenuBar(info);
173      // Assume that the user decided to quit the application
174      menuBar.quitClicked();
175    }
176
177    updateSharedLocalOrRemotePanel(localOrRemote, info);
178
179    // To be sure that the dialog receives the new configuration event before
180    // calling pack.
181    SwingUtilities.invokeLater(new Runnable()
182    {
183      public void run()
184      {
185        // Create and set up the content pane.
186        controlCenterPane = new ControlCenterMainPane(info);
187        //  Create and set up the window.
188        dlg = Utilities.createFrame();
189        dlg.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
190        final MainMenuBar menuBar = new MainMenuBar(info);
191        dlg.addWindowListener(new WindowAdapter() {
192          public void windowClosing(WindowEvent e) {
193            menuBar.quitClicked();
194          }
195        });
196        dlg.setJMenuBar(menuBar);
197        String title = Utils.getCustomizedObject(
198            "INFO_CONTROL_PANEL_TITLE",
199            AdminToolMessages.INFO_CONTROL_PANEL_TITLE.get(
200            DynamicConstants.PRODUCT_NAME),
201            LocalizableMessage.class).toString();
202        dlg.setTitle(title);
203        dlg.setContentPane(controlCenterPane);
204        dlg.pack();
205        Utilities.centerOnScreen(dlg);
206
207        dlg.setVisible(true);
208      }
209    });
210  }
211
212  private static void initLookAndFeel() throws Throwable
213  {
214    UIFactory.initializeLookAndFeel();
215  }
216
217  private void updateLocalOrRemotePanel(RootPaneContainer localOrRemote)
218  {
219    LocalOrRemotePanel panel =
220      getLocalOrRemotePanel(localOrRemote.getContentPane());
221    if (panel != null)
222    {
223      if (argParser.isRemote())
224      {
225        panel.setRemote(true);
226      }
227      if (argParser.getExplicitHostName() != null)
228      {
229        panel.setHostName(argParser.getExplicitHostName());
230        panel.setRemote(true);
231      }
232      if (argParser.getExplicitPort() != -1)
233      {
234        panel.setPort(argParser.getExplicitPort());
235        panel.setRemote(true);
236      }
237      if (argParser.getExplicitBindDn() != null)
238      {
239        panel.setBindDN(argParser.getExplicitBindDn());
240      }
241      if (argParser.getBindPassword() != null)
242      {
243        panel.setBindPassword(argParser.getBindPassword().toCharArray());
244      }
245    }
246  }
247
248  /**
249   * A method used to update the contents of the dialog displayed when the user
250   * selects 'Server To Administer...'.  This is done because this class
251   * displays a GenericFrame and in the rest of the UI a GenericDialog is
252   * shown.
253   * @param localOrRemote the frame displayed by this class.
254   * @param info the generic info.
255   */
256  private void updateSharedLocalOrRemotePanel(RootPaneContainer localOrRemote,
257      ControlPanelInfo info)
258  {
259    LocalOrRemotePanel panel =
260      getLocalOrRemotePanel(localOrRemote.getContentPane());
261    LocalOrRemotePanel panelToUpdate = getLocalOrRemotePanel(
262        ControlCenterMainPane.getLocalOrRemoteDialog(info));
263    if (panel != null && panelToUpdate != null)
264    {
265      panelToUpdate.setRemote(panel.isRemote());
266      if (panel.getHostName() != null)
267      {
268        panelToUpdate.setHostName(panel.getHostName());
269      }
270      if (panel.getPort() != -1)
271      {
272        panelToUpdate.setPort(panel.getPort());
273      }
274      if (panel.getBindDN() != null)
275      {
276        panelToUpdate.setBindDN(panel.getBindDN());
277      }
278    }
279  }
280
281  private LocalOrRemotePanel getLocalOrRemotePanel(Container c)
282  {
283    LocalOrRemotePanel panel = null;
284    if (c instanceof LocalOrRemotePanel)
285    {
286      panel = (LocalOrRemotePanel)c;
287    }
288    else
289    {
290      for (Component comp : c.getComponents())
291      {
292        if (comp instanceof Container)
293        {
294          panel = getLocalOrRemotePanel((Container)comp);
295        }
296        if (panel != null)
297        {
298          break;
299        }
300      }
301    }
302    return panel;
303  }
304}