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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.awt.Dimension;
033import java.awt.GridBagConstraints;
034import java.awt.GridBagLayout;
035import java.awt.Insets;
036
037import javax.swing.BorderFactory;
038import javax.swing.Box;
039import javax.swing.JLabel;
040import javax.swing.JPanel;
041import javax.swing.JScrollPane;
042import javax.swing.JSplitPane;
043import javax.swing.SwingUtilities;
044
045import org.opends.admin.ads.util.ConnectionUtils;
046import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
047import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
048import org.opends.guitools.controlpanel.event.ConfigChangeListener;
049import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
050import org.opends.guitools.controlpanel.util.Utilities;
051import org.forgerock.i18n.LocalizableMessage;
052
053/**
054 * The main panel of the control panel.  It contains a split pane.  On the left
055 * we have some actions and on the right some global information about the
056 * server.
057 *
058 */
059public class ControlCenterMainPane extends JPanel
060{
061  private static final long serialVersionUID = -8939025523701408656L;
062  private StatusPanel statusPane;
063  private JLabel lAuthenticatedAs =
064    Utilities.createInlineHelpLabel(LocalizableMessage.EMPTY);
065
066  /**
067   * Constructor.
068   * @param info the control panel info.
069   */
070  public ControlCenterMainPane(ControlPanelInfo info)
071  {
072    super(new GridBagLayout());
073    setOpaque(true); //content panes must be opaque
074    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
075    split.setOpaque(true); //content panes must be opaque
076
077    statusPane = new StatusPanel();
078    statusPane.setInfo(info);
079
080    MainActionsPane mainActionsPane = new MainActionsPane();
081    mainActionsPane.setInfo(info);
082    JScrollPane accordionScroll = Utilities.createScrollPane(mainActionsPane);
083    accordionScroll.getViewport().setBackground(
084        ColorAndFontConstants.greyBackground);
085
086//  Create a split pane with the two scroll panes in it.
087    split.setLeftComponent(accordionScroll);
088
089    split.setRightComponent(statusPane);
090    split.setResizeWeight(0.0);
091
092    split.setDividerLocation(accordionScroll.getPreferredSize().width + 2);
093
094    split.setPreferredSize(
095        new Dimension(split.getPreferredSize().width + 4,
096            split.getPreferredSize().height));
097    info.addConfigChangeListener(new ConfigChangeListener()
098    {
099      private boolean lastStatusStopped;
100      /** {@inheritDoc} */
101      public void configurationChanged(final ConfigurationChangeEvent ev)
102      {
103        final boolean displayLogin;
104        if (ev.getNewDescriptor().getStatus() !=
105          ServerDescriptor.ServerStatus.STARTED)
106        {
107          lastStatusStopped = true;
108          displayLogin = false;
109        }
110        else if (lastStatusStopped && !ev.getNewDescriptor().isAuthenticated())
111        {
112          lastStatusStopped = false;
113          displayLogin = true;
114        }
115        else
116        {
117          displayLogin = false;
118        }
119        SwingUtilities.invokeLater(new Runnable()
120        {
121          /** {@inheritDoc} */
122          public void run()
123          {
124            updateAuthenticationLabel(ev.getNewDescriptor());
125            if (displayLogin)
126            {
127              getLoginDialog().setVisible(true);
128              getLoginDialog().toFront();
129            }
130          }
131        });
132      }
133    });
134
135    GridBagConstraints gbc = new GridBagConstraints();
136    gbc.gridx = 0;
137    gbc.gridy = 0;
138    gbc.weightx = 1.0;
139    gbc.weighty = 1.0;
140    gbc.fill = GridBagConstraints.BOTH;
141    add(split, gbc);
142
143    JPanel infoPanel = new JPanel(new GridBagLayout());
144    gbc.gridy = 1;
145    gbc.weighty = 0.0;
146    add(infoPanel, gbc);
147
148    infoPanel.setOpaque(false);
149    infoPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
150        ColorAndFontConstants.defaultBorderColor));
151    gbc.weightx = 0.0;
152    gbc.weighty = 0.0;
153    gbc.insets = new Insets(5, 5, 5, 5);
154    infoPanel.add(lAuthenticatedAs, gbc);
155    gbc.gridx = 1;
156    gbc.weightx = 1.0;
157    gbc.insets.left = 0;
158    gbc.insets.right = 0;
159    lAuthenticatedAs.setText("Qjlabel");
160    infoPanel.add(
161        Box.createVerticalStrut(lAuthenticatedAs.getPreferredSize().height),
162        gbc);
163    if (info.getServerDescriptor() != null)
164    {
165      updateAuthenticationLabel(info.getServerDescriptor());
166    }
167    else
168    {
169      lAuthenticatedAs.setText("");
170    }
171  }
172
173  /**
174   * Returns the login dialog used to ask authentication to the user.
175   * @return the login dialog used to ask authentication to the user.
176   */
177  private GenericDialog getLoginDialog()
178  {
179    return statusPane.getLoginDialog();
180  }
181
182  private void updateAuthenticationLabel(ServerDescriptor server)
183  {
184    if (server.getStatus() ==
185      ServerDescriptor.ServerStatus.STARTED)
186    {
187      if (server.isAuthenticated())
188      {
189        try
190        {
191         String bindDN = ConnectionUtils.getBindDN(
192             statusPane.getInfo().getDirContext());
193         lAuthenticatedAs.setText(
194             INFO_CTRL_PANEL_AUTHENTICATED_AS.get(bindDN).toString());
195        }
196        catch (Throwable t)
197        {
198        }
199      }
200      else
201      {
202        lAuthenticatedAs.setText(
203            INFO_CTRL_PANEL_NOT_AUTHENTICATED.get().toString());
204      }
205    }
206    else if (server.isLocal())
207    {
208      lAuthenticatedAs.setText(
209         INFO_CTRL_PANEL_NOT_AUTHENTICATED_SERVER_NOT_RUNNING.get().toString());
210    }
211    else
212    {
213      lAuthenticatedAs.setText(
214          INFO_CTRL_PANEL_NOT_AUTHENTICATED_SERVER_REMOTE.get(
215              server.getHostname()).toString());
216    }
217  }
218
219  private static GenericDialog localOrRemoteDlg;
220  private static GenericDialog loginDlg;
221
222  /**
223   * Returns the dialog that is in charge of asking the user the server
224   * to be administer.  This method will return always the same dialog.  The
225   * dialog will do all the logic of updating the ControlPanelInfo object.
226   * @param info the control panel information object.
227   * @return the dialog that is in charge of asking the user the server
228   * to be administer.
229   */
230  public static GenericDialog getLocalOrRemoteDialog(ControlPanelInfo info)
231  {
232    if (localOrRemoteDlg == null)
233    {
234      LocalOrRemotePanel localOrRemotePanel = new LocalOrRemotePanel();
235      localOrRemotePanel.setInfo(info);
236      localOrRemoteDlg = new GenericDialog(Utilities.createFrame(),
237          localOrRemotePanel);
238      localOrRemoteDlg.setModal(true);
239      localOrRemoteDlg.pack();
240    }
241    return localOrRemoteDlg;
242  }
243
244  /**
245   * Returns the dialog that is in charge of asking the user the authentication
246   * for the local server.  This method will return always the same dialog.
247   * @param info the control panel information object.  The
248   * dialog will do all the logic of updating the ControlPanelInfo object.
249   * @return the dialog that is in charge of asking the user the authentication
250   * for the local server.
251   */
252  public static GenericDialog getLocalServerLoginDialog(ControlPanelInfo info)
253  {
254    if (loginDlg == null)
255    {
256      LoginPanel loginPanel = new LoginPanel();
257      loginDlg = new GenericDialog(Utilities.createFrame(), loginPanel);
258      loginPanel.setInfo(info);
259      loginDlg.setModal(true);
260    }
261    return loginDlg;
262  }
263}