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