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