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