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