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 2006-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2015 ForgeRock AS.
016 */
017
018package org.opends.quicksetup.ui;
019
020import static org.opends.messages.QuickSetupMessages.*;
021
022import java.awt.*;
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Set;
027
028import org.opends.quicksetup.event.ButtonActionListener;
029import org.opends.quicksetup.*;
030
031import javax.swing.*;
032
033/**
034 * This is the class that contains the panel on the right-top part of the
035 * QuickSetupDialog).  It uses a CardLayout that contains all
036 * the panels that are specific to each step (WelcomePanel, ReviewPanel, etc.).
037 *
038 * To specify which is the panel to be displayed the method setCurrentStep
039 * method is called.
040 *
041 * There is only one instance of this class for a given QuickSetupDialog (and
042 * there are only 1 instance of each of the panels that are contained in its
043 * CardLayout).
044 *
045 */
046public class CurrentStepPanel extends QuickSetupPanel
047{
048  private static final long serialVersionUID = 5474803491510999334L;
049
050  private static final String LOADING_PANEL = "loading";
051
052  private Map<WizardStep, QuickSetupStepPanel> hmPanels = new HashMap<>();
053
054  /**
055   * The constructor of this class.
056   * @param app Application used to create panels for populating the layout
057   * @param qs QuickSetup acting as controller
058   */
059  public CurrentStepPanel(GuiApplication app, QuickSetup qs)
060  {
061    super(app);
062    setQuickSetup(qs);
063    createLayout(app);
064  }
065
066  /**
067   * Returns the value corresponding to the provided FieldName.
068   * @param fieldName the FieldName for which we want to obtain the value.
069   * @return the value corresponding to the provided FieldName.
070   */
071  public Object getFieldValue(FieldName fieldName)
072  {
073    Object value = null;
074    for (WizardStep s : hmPanels.keySet())
075    {
076      value = getPanel(s).getFieldValue(fieldName);
077      if (value != null)
078      {
079        break;
080      }
081    }
082    return value;
083  }
084
085  /**
086   * Marks as invalid (or valid depending on the value of the invalid parameter)
087   * a field corresponding to FieldName.  This basically implies updating the
088   * style of the JLabel associated with fieldName (the association is done
089   * using the LabelFieldDescriptor class).
090   * @param fieldName the FieldName to be marked as valid or invalid.
091   * @param invalid whether to mark the field as valid or invalid.
092   */
093  public void displayFieldInvalid(FieldName fieldName, boolean invalid)
094  {
095    for (WizardStep s : hmPanels.keySet())
096    {
097      getPanel(s).displayFieldInvalid(fieldName, invalid);
098    }
099  }
100
101
102  /**
103   * Create the layout of the panel.
104   * @param app Application used to create panels for populating the layout
105   */
106  private void createLayout(GuiApplication app)
107  {
108    Set<? extends WizardStep> steps = app.getWizardSteps();
109    if (steps != null) {
110      for (WizardStep step : steps) {
111        QuickSetupStepPanel panel = app.createWizardStepPanel(step);
112        if (panel != null) {
113          panel.setQuickSetup(getQuickSetup());
114          panel.initialize();
115          hmPanels.put(step, panel);
116        }
117      }
118    }
119
120    int minWidth = 0;
121    int minHeight = 0;
122    setLayout(new CardLayout());
123    for (WizardStep s : hmPanels.keySet())
124    {
125      minWidth = Math.max(minWidth, getPanel(s).getMinimumWidth());
126      minHeight = Math.max(minHeight, getPanel(s).getMinimumHeight());
127      add(getPanel(s), s.toString());
128    }
129
130    // Add a special panel to display while panels are
131    // initializing themselves
132    JPanel loadingPanel = UIFactory.makeJPanel();
133    loadingPanel.setLayout(new GridBagLayout());
134    loadingPanel.add(UIFactory.makeJLabel(UIFactory.IconType.NO_ICON,
135            INFO_GENERAL_LOADING.get(),
136            UIFactory.TextStyle.PRIMARY_FIELD_VALID),
137            new GridBagConstraints());
138    add(loadingPanel, LOADING_PANEL);
139
140    // For aesthetic reasons we add a little bit of height
141    minHeight += getApplication().getExtraDialogHeight();
142
143    setPreferredSize(new Dimension(minWidth, minHeight));
144    setMinimumSize(new Dimension(minWidth, minHeight));
145  }
146
147  /**
148   * Adds a button listener.  All the button listeners will be notified when
149   * the buttons are clicked (by the user or programmatically).
150   * @param l the ButtonActionListener to be added.
151   */
152  public void addButtonActionListener(ButtonActionListener l)
153  {
154    for (WizardStep s : hmPanels.keySet())
155    {
156      getPanel(s).addButtonActionListener(l);
157    }
158  }
159
160
161  /**
162   * Displays the panel corresponding to the provided step.  The panel contents
163   * are updated with the contents of the UserData object.
164   * @param step the step that we want to display.
165   * @param userData the UserData object that must be used to populate
166   * the panels.
167   */
168  public void setDisplayedStep(final WizardStep step, final UserData userData)
169  {
170    final CardLayout cl = (CardLayout) getLayout();
171
172    if (getPanel(step).blockingBeginDisplay())
173    {
174      // Show the 'loading...' panel and invoke begin
175      // display in another thread in case the panel
176      // taske a while to initialize.
177      cl.show(this, LOADING_PANEL);
178      new Thread(new Runnable() {
179        public void run() {
180          getPanel(step).beginDisplay(userData);
181          SwingUtilities.invokeLater(new Runnable() {
182            public void run() {
183              cl.show(CurrentStepPanel.this, step.toString());
184              getPanel(step).endDisplay();
185            }
186          });
187        }
188      },"panel begin display thread").start();
189    }
190    else
191    {
192      getPanel(step).beginDisplay(userData);
193      cl.show(CurrentStepPanel.this, step.toString());
194      getPanel(step).endDisplay();
195    }
196  }
197
198  /**
199   * Forwards the different panels the ProgressDescriptor so that they
200   * can update their contents accordingly.
201   * @param descriptor the descriptor of the Application progress.
202   */
203  public void displayProgress(ProgressDescriptor descriptor)
204  {
205    for (WizardStep s : hmPanels.keySet())
206    {
207      getPanel(s).displayProgress(descriptor);
208    }
209  }
210
211  /**
212   * This method displays a working progress icon in the panel.
213   * @param visible whether the icon must be displayed or not.
214   */
215  public void setCheckingVisible(boolean visible)
216  {
217    for (WizardStep s : hmPanels.keySet())
218    {
219      getPanel(s).setCheckingVisible(visible);
220    }
221  }
222
223  /**
224   * Retrieves the panel for the provided step.
225   * @param step the step for which we want to get the panel.
226   * @return the panel for the provided step.
227   */
228  private QuickSetupStepPanel getPanel(WizardStep step)
229  {
230    return hmPanels.get(step);
231  }
232}