mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
20.36.2016 2a3158aad80fc910b83336485b3e545dea50066c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
 
package org.opends.quicksetup.installer.ui;
 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import java.util.TreeSet;
 
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;
 
import org.opends.quicksetup.event.MinimumSizeComponentListener;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.ui.Utilities;
import org.forgerock.i18n.LocalizableMessage;
import static org.opends.messages.QuickSetupMessages.*;
 
/**
 * This class is a dialog that appears when the user must choose the alias to
 * be used from a certificate keystore.
 */
public class SelectAliasDialog extends JDialog
{
  private JButton okButton;
  private JComboBox comboAliases;
  private boolean isCanceled;
 
  private static final long serialVersionUID = -8140704273612764046L;
 
  /**
   * Constructor of the SelectAliasDialog.
   * @param parent the parent frame for this dialog.
   */
  public SelectAliasDialog(JDialog parent)
  {
    super(parent);
    setTitle(INFO_SELECT_ALIAS_TITLE.get().toString());
    getContentPane().add(createPanel());
    pack();
    int minWidth = (int) getPreferredSize().getWidth();
    int minHeight = (int) getPreferredSize().getHeight();
    addComponentListener(new MinimumSizeComponentListener(this, minWidth,
        minHeight));
    getRootPane().setDefaultButton(okButton);
 
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        cancelClicked();
      }
    });
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 
    Utilities.centerOnComponent(this, parent);
    setModal(true);
  }
 
  /**
   * Displays this dialog with the provided aliases.
   *
   * @param aliases the aliases to display.
   */
  public void display(String[] aliases)
  {
    if (aliases == null || aliases.length ==0)
    {
      throw new IllegalArgumentException(
          "The provided aliases are null or empty.");
    }
    isCanceled = true;
    TreeSet<String> s = new TreeSet<>();
    Collections.addAll(s, aliases);
    String[] orderedAliases = new String[s.size()];
    s.toArray(orderedAliases);
    comboAliases.setModel(new DefaultComboBoxModel(orderedAliases));
    comboAliases.setSelectedIndex(0);
    setVisible(true);
  }
 
  /**
   * Returns <CODE>true</CODE> if the user clicked on cancel and
   * <CODE>false</CODE> otherwise.
   * @return <CODE>true</CODE> if the user clicked on cancel and
   * <CODE>false</CODE> otherwise.
   */
  public boolean isCanceled()
  {
    return isCanceled;
  }
 
  /**
   * Returns the selected certificate alias.
   * @return the selected certificate alias.
   */
  public String getSelectedAlias()
  {
    return (String) comboAliases.getSelectedItem();
  }
 
  /**
   * Creates and returns the panel of the dialog.
   * @return the panel of the dialog.
   */
  private JPanel createPanel()
  {
    JPanel p1 = new JPanel(new GridBagLayout());
    p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
    p1.setBorder(UIFactory.DIALOG_PANEL_BORDER);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.anchor = GridBagConstraints.NORTHWEST;
 
    Insets currentStepInsets = UIFactory.getCurrentStepPanelInsets();
    gbc.insets.top = currentStepInsets.top;
    gbc.insets.left = currentStepInsets.left;
 
    p1.add(UIFactory.makeJLabel(UIFactory.IconType.INFORMATION_LARGE, null,
        UIFactory.TextStyle.NO_STYLE), gbc);
    gbc.weightx = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
    gbc.fill = GridBagConstraints.BOTH;
    LocalizableMessage msg = INFO_SELECT_ALIAS_MSG.get();
    JTextComponent tf = UIFactory.makeHtmlPane(msg,
            UIFactory.INSTRUCTIONS_FONT);
    tf.setOpaque(false);
    tf.setEditable(false);
    p1.add(tf, gbc);
    gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
    gbc.insets.left = currentStepInsets.left;
    gbc.insets.right = currentStepInsets.right;
    gbc.insets.bottom = currentStepInsets.bottom;
    comboAliases = new JComboBox();
    comboAliases.setPrototypeDisplayValue("The prototype alias name");
    gbc.fill = GridBagConstraints.NONE;
    p1.add(comboAliases, gbc);
 
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.VERTICAL;
    p1.add(Box.createVerticalGlue(), gbc);
 
    JPanel p2 = new JPanel(new GridBagLayout());
    p2.setOpaque(false);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1.0;
    gbc.gridwidth = 3;
    p2.add(Box.createHorizontalGlue(), gbc);
    okButton = UIFactory.makeJButton(INFO_OK_BUTTON_LABEL.get(),
          INFO_SELECT_ALIAS_OK_BUTTON_TOOLTIP.get());
    okButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent ev)
      {
        okClicked();
      }
    });
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 0.0;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    p2.add(okButton, gbc);
    JButton cancelButton = UIFactory.makeJButton(INFO_CANCEL_BUTTON_LABEL.get(),
            INFO_SELECT_ALIAS_CANCEL_BUTTON_TOOLTIP.get());
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS;
    p2.add(cancelButton, gbc);
    cancelButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent ev)
      {
        cancelClicked();
      }
    });
 
    JPanel p = new JPanel(new GridBagLayout());
    p.setBackground(UIFactory.DEFAULT_BACKGROUND);
    gbc.insets = UIFactory.getEmptyInsets();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    p.add(p1, gbc);
    gbc.weighty = 0.0;
    gbc.insets = UIFactory.getButtonsPanelInsets();
    p.add(p2, gbc);
    return p;
  }
 
  /** Method called when user clicks on cancel. */
  private void cancelClicked()
  {
    isCanceled = true;
    dispose();
  }
 
  /** Method called when user clicks on OK. */
  private void okClicked()
  {
    isCanceled = false;
    dispose();
  }
 
  /**
   * Method written for testing purposes.
   * @param args the arguments to be passed to the test program.
   */
  public static void main(String[] args)
  {
    try
    {
      // UIFactory.initialize();
      SelectAliasDialog dlg =
          new SelectAliasDialog(new JDialog());
      dlg.display(new String[] {"test1", "test2"});
    } catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }
}