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-2016 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      @Override
084      public void run() {
085        test.createAndDisplayGUI();
086      }
087    });
088  }
089
090  /**
091   * Method that creates the ControlCenterInfo object that will be in all the
092   * control panel.  Nothing is done here: the user must say whether the server
093   * is local or remote.
094   * @param args the arguments that are passed in the command line.  The code
095   * assumes that the arguments have been already validated.
096   */
097  public void initialize(String[] args)
098  {
099    info = ControlPanelInfo.getInstance();
100    // Call Installation because the LocalOrRemotePanel uses it to check
101    // whether the server is running or not and to get the install path.
102    Installation.getLocal();
103    argParser = new ControlPanelArgumentParser(ControlPanel.class.getName(),
104        INFO_CONTROL_PANEL_LAUNCHER_USAGE_DESCRIPTION.get());
105    try
106    {
107      argParser.initializeArguments();
108      argParser.parseArguments(args);
109    }
110    catch (ArgumentException ae)
111    {
112      // Bug
113      throw new IllegalStateException("Arguments not correctly parsed: "+ae,
114          ae);
115    }
116    if (argParser.isTrustAll())
117    {
118      info.setTrustManager(new BlindApplicationTrustManager());
119    }
120    info.setConnectTimeout(argParser.getConnectTimeout());
121  }
122
123  /** Creates the main Control Panel dialog and displays it. */
124  public void createAndDisplayGUI()
125  {
126    LocalOrRemotePanel localOrRemotePanel = new LocalOrRemotePanel();
127    localOrRemotePanel.setInfo(info);
128    final GenericFrame localOrRemote = new GenericFrame(localOrRemotePanel);
129    localOrRemote.pack();
130    Utilities.centerOnScreen(localOrRemote);
131
132    if (argParser.isRemote())
133    {
134      updateLocalOrRemotePanel(localOrRemote);
135    }
136
137    if (argParser.getBindPassword() != null)
138    {
139      updateLocalOrRemotePanel(localOrRemote);
140      getLocalOrRemotePanel(localOrRemote.getContentPane()).
141      setCallOKWhenVisible(true);
142    }
143
144    ComponentListener listener = new ComponentAdapter()
145    {
146      @Override
147      public void componentHidden(ComponentEvent e)
148      {
149        handleWindowClosed(localOrRemote, info);
150      }
151    };
152    localOrRemote.addComponentListener(listener);
153    localOrRemote.setVisible(true);
154  }
155
156  private void handleWindowClosed(GenericFrame localOrRemote,
157      final ControlPanelInfo info)
158  {
159    if (info.getServerDescriptor() == null)
160    {
161      MainMenuBar menuBar = new MainMenuBar(info);
162      // Assume that the user decided to quit the application
163      menuBar.quitClicked();
164    }
165
166    updateSharedLocalOrRemotePanel(localOrRemote, info);
167
168    // To be sure that the dialog receives the new configuration event before
169    // calling pack.
170    SwingUtilities.invokeLater(new Runnable()
171    {
172      @Override
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          @Override
183          public void windowClosing(WindowEvent e) {
184            menuBar.quitClicked();
185          }
186        });
187        dlg.setJMenuBar(menuBar);
188        String title = Utils.getCustomizedObject(
189            "INFO_CONTROL_PANEL_TITLE",
190            AdminToolMessages.INFO_CONTROL_PANEL_TITLE.get(
191            DynamicConstants.PRODUCT_NAME),
192            LocalizableMessage.class).toString();
193        dlg.setTitle(title);
194        dlg.setContentPane(controlCenterPane);
195        dlg.pack();
196        Utilities.centerOnScreen(dlg);
197
198        dlg.setVisible(true);
199      }
200    });
201  }
202
203  private static void initLookAndFeel() throws Throwable
204  {
205    UIFactory.initializeLookAndFeel();
206  }
207
208  private void updateLocalOrRemotePanel(RootPaneContainer localOrRemote)
209  {
210    LocalOrRemotePanel panel =
211      getLocalOrRemotePanel(localOrRemote.getContentPane());
212    if (panel != null)
213    {
214      if (argParser.isRemote())
215      {
216        panel.setRemote(true);
217      }
218      if (argParser.getExplicitHostName() != null)
219      {
220        panel.setHostName(argParser.getExplicitHostName());
221        panel.setRemote(true);
222      }
223      if (argParser.getExplicitPort() != -1)
224      {
225        panel.setPort(argParser.getExplicitPort());
226        panel.setRemote(true);
227      }
228      if (argParser.getExplicitBindDn() != null)
229      {
230        panel.setBindDN(argParser.getExplicitBindDn());
231      }
232      if (argParser.getBindPassword() != null)
233      {
234        panel.setBindPassword(argParser.getBindPassword().toCharArray());
235      }
236    }
237  }
238
239  /**
240   * A method used to update the contents of the dialog displayed when the user
241   * selects 'Server To Administer...'.  This is done because this class
242   * displays a GenericFrame and in the rest of the UI a GenericDialog is
243   * shown.
244   * @param localOrRemote the frame displayed by this class.
245   * @param info the generic info.
246   */
247  private void updateSharedLocalOrRemotePanel(RootPaneContainer localOrRemote,
248      ControlPanelInfo info)
249  {
250    LocalOrRemotePanel panel =
251      getLocalOrRemotePanel(localOrRemote.getContentPane());
252    LocalOrRemotePanel panelToUpdate = getLocalOrRemotePanel(
253        ControlCenterMainPane.getLocalOrRemoteDialog(info));
254    if (panel != null && panelToUpdate != null)
255    {
256      panelToUpdate.setRemote(panel.isRemote());
257      if (panel.getHostName() != null)
258      {
259        panelToUpdate.setHostName(panel.getHostName());
260      }
261      if (panel.getPort() != -1)
262      {
263        panelToUpdate.setPort(panel.getPort());
264      }
265      if (panel.getBindDN() != null)
266      {
267        panelToUpdate.setBindDN(panel.getBindDN());
268      }
269    }
270  }
271
272  private LocalOrRemotePanel getLocalOrRemotePanel(Container c)
273  {
274    LocalOrRemotePanel panel = null;
275    if (c instanceof LocalOrRemotePanel)
276    {
277      panel = (LocalOrRemotePanel)c;
278    }
279    else
280    {
281      for (Component comp : c.getComponents())
282      {
283        if (comp instanceof Container)
284        {
285          panel = getLocalOrRemotePanel((Container)comp);
286        }
287        if (panel != null)
288        {
289          break;
290        }
291      }
292    }
293    return panel;
294  }
295}