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