mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
20.38.2016 71695c2e99143ed6821b4ba50f898567c440750f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
 
package org.opends.guitools.controlpanel;
 
import static org.opends.messages.AdminToolMessages.
 INFO_CONTROL_PANEL_LAUNCHER_USAGE_DESCRIPTION;
 
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JFrame;
import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
 
import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
import org.opends.guitools.controlpanel.ui.ControlCenterMainPane;
import org.opends.guitools.controlpanel.ui.GenericFrame;
import org.opends.guitools.controlpanel.ui.LocalOrRemotePanel;
import org.opends.guitools.controlpanel.ui.MainMenuBar;
import org.opends.guitools.controlpanel.util.BlindApplicationTrustManager;
import org.opends.guitools.controlpanel.util.Utilities;
import org.opends.messages.AdminToolMessages;
import org.opends.quicksetup.Installation;
import org.forgerock.i18n.LocalizableMessage;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.util.Utils;
import org.opends.server.util.DynamicConstants;
import com.forgerock.opendj.cli.ArgumentException;
 
/**
 * The class that is in charge of creating the main dialog of the ControlPanel
 * and the ControlCenterInfo (the data structure that is used by all the GUI
 * components and that contains information about the server status and server
 * configuration).
 */
public class ControlPanel
{
  private JFrame dlg;
  private ControlPanelInfo info;
  private ControlPanelArgumentParser argParser;
  private ControlCenterMainPane controlCenterPane;
 
  /**
   * Main method that is used for testing purposes.  The control-panel
   * command-line is launched through the ControlPanelLauncher (which displays
   * a splash screen and calls the <code>initialize</code> and
   * <code>createAndDisplayMethods</code>.
   * @param args the arguments that are passed.
   */
  public static void main(String[] args) {
    try
    {
      initLookAndFeel();
    }
    catch (Throwable t)
    {
      t.printStackTrace();
    }
    final ControlPanel test = new ControlPanel();
    test.initialize(args);
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        test.createAndDisplayGUI();
      }
    });
  }
 
  /**
   * Method that creates the ControlCenterInfo object that will be in all the
   * control panel.  Nothing is done here: the user must say whether the server
   * is local or remote.
   * @param args the arguments that are passed in the command line.  The code
   * assumes that the arguments have been already validated.
   */
  public void initialize(String[] args)
  {
    info = ControlPanelInfo.getInstance();
    // Call Installation because the LocalOrRemotePanel uses it to check
    // whether the server is running or not and to get the install path.
    Installation.getLocal();
    argParser = new ControlPanelArgumentParser(ControlPanel.class.getName(),
        INFO_CONTROL_PANEL_LAUNCHER_USAGE_DESCRIPTION.get());
    try
    {
      argParser.initializeArguments();
      argParser.parseArguments(args);
    }
    catch (ArgumentException ae)
    {
      // Bug
      throw new IllegalStateException("Arguments not correctly parsed: "+ae,
          ae);
    }
    if (argParser.isTrustAll())
    {
      info.setTrustManager(new BlindApplicationTrustManager());
    }
    info.setConnectTimeout(argParser.getConnectTimeout());
  }
 
  /** Creates the main Control Panel dialog and displays it. */
  public void createAndDisplayGUI()
  {
    LocalOrRemotePanel localOrRemotePanel = new LocalOrRemotePanel();
    localOrRemotePanel.setInfo(info);
    final GenericFrame localOrRemote = new GenericFrame(localOrRemotePanel);
    localOrRemote.pack();
    Utilities.centerOnScreen(localOrRemote);
 
    if (argParser.isRemote())
    {
      updateLocalOrRemotePanel(localOrRemote);
    }
 
    if (argParser.getBindPassword() != null)
    {
      updateLocalOrRemotePanel(localOrRemote);
      getLocalOrRemotePanel(localOrRemote.getContentPane()).
      setCallOKWhenVisible(true);
    }
 
    ComponentListener listener = new ComponentAdapter()
    {
      @Override
      public void componentHidden(ComponentEvent e)
      {
        handleWindowClosed(localOrRemote, info);
      }
    };
    localOrRemote.addComponentListener(listener);
    localOrRemote.setVisible(true);
  }
 
  private void handleWindowClosed(GenericFrame localOrRemote,
      final ControlPanelInfo info)
  {
    if (info.getServerDescriptor() == null)
    {
      MainMenuBar menuBar = new MainMenuBar(info);
      // Assume that the user decided to quit the application
      menuBar.quitClicked();
    }
 
    updateSharedLocalOrRemotePanel(localOrRemote, info);
 
    // To be sure that the dialog receives the new configuration event before
    // calling pack.
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        // Create and set up the content pane.
        controlCenterPane = new ControlCenterMainPane(info);
        //  Create and set up the window.
        dlg = Utilities.createFrame();
        dlg.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        final MainMenuBar menuBar = new MainMenuBar(info);
        dlg.addWindowListener(new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
            menuBar.quitClicked();
          }
        });
        dlg.setJMenuBar(menuBar);
        String title = Utils.getCustomizedObject(
            "INFO_CONTROL_PANEL_TITLE",
            AdminToolMessages.INFO_CONTROL_PANEL_TITLE.get(
            DynamicConstants.PRODUCT_NAME),
            LocalizableMessage.class).toString();
        dlg.setTitle(title);
        dlg.setContentPane(controlCenterPane);
        dlg.pack();
        Utilities.centerOnScreen(dlg);
 
        dlg.setVisible(true);
      }
    });
  }
 
  private static void initLookAndFeel() throws Throwable
  {
    UIFactory.initializeLookAndFeel();
  }
 
  private void updateLocalOrRemotePanel(RootPaneContainer localOrRemote)
  {
    LocalOrRemotePanel panel =
      getLocalOrRemotePanel(localOrRemote.getContentPane());
    if (panel != null)
    {
      if (argParser.isRemote())
      {
        panel.setRemote(true);
      }
      if (argParser.getExplicitHostName() != null)
      {
        panel.setHostName(argParser.getExplicitHostName());
        panel.setRemote(true);
      }
      if (argParser.getExplicitPort() != -1)
      {
        panel.setPort(argParser.getExplicitPort());
        panel.setRemote(true);
      }
      if (argParser.getExplicitBindDn() != null)
      {
        panel.setBindDN(argParser.getExplicitBindDn());
      }
      if (argParser.getBindPassword() != null)
      {
        panel.setBindPassword(argParser.getBindPassword().toCharArray());
      }
    }
  }
 
  /**
   * A method used to update the contents of the dialog displayed when the user
   * selects 'Server To Administer...'.  This is done because this class
   * displays a GenericFrame and in the rest of the UI a GenericDialog is
   * shown.
   * @param localOrRemote the frame displayed by this class.
   * @param info the generic info.
   */
  private void updateSharedLocalOrRemotePanel(RootPaneContainer localOrRemote,
      ControlPanelInfo info)
  {
    LocalOrRemotePanel panel =
      getLocalOrRemotePanel(localOrRemote.getContentPane());
    LocalOrRemotePanel panelToUpdate = getLocalOrRemotePanel(
        ControlCenterMainPane.getLocalOrRemoteDialog(info));
    if (panel != null && panelToUpdate != null)
    {
      panelToUpdate.setRemote(panel.isRemote());
      if (panel.getHostName() != null)
      {
        panelToUpdate.setHostName(panel.getHostName());
      }
      if (panel.getPort() != -1)
      {
        panelToUpdate.setPort(panel.getPort());
      }
      if (panel.getBindDN() != null)
      {
        panelToUpdate.setBindDN(panel.getBindDN());
      }
    }
  }
 
  private LocalOrRemotePanel getLocalOrRemotePanel(Container c)
  {
    LocalOrRemotePanel panel = null;
    if (c instanceof LocalOrRemotePanel)
    {
      panel = (LocalOrRemotePanel)c;
    }
    else
    {
      for (Component comp : c.getComponents())
      {
        if (comp instanceof Container)
        {
          panel = getLocalOrRemotePanel((Container)comp);
        }
        if (panel != null)
        {
          break;
        }
      }
    }
    return panel;
  }
}