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 2008-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2013-2015 ForgeRock AS. 026 */ 027 028package org.opends.quicksetup.installer.ui; 029 030import java.awt.GridBagConstraints; 031import java.awt.GridBagLayout; 032import java.awt.Insets; 033import java.awt.event.ActionEvent; 034import java.awt.event.ActionListener; 035import java.awt.event.WindowAdapter; 036import java.awt.event.WindowEvent; 037import java.util.Collections; 038import java.util.TreeSet; 039 040import javax.swing.Box; 041import javax.swing.DefaultComboBoxModel; 042import javax.swing.JButton; 043import javax.swing.JComboBox; 044import javax.swing.JDialog; 045import javax.swing.JPanel; 046import javax.swing.text.JTextComponent; 047 048import org.opends.quicksetup.event.MinimumSizeComponentListener; 049import org.opends.quicksetup.ui.UIFactory; 050import org.opends.quicksetup.ui.Utilities; 051import org.forgerock.i18n.LocalizableMessage; 052import static org.opends.messages.QuickSetupMessages.*; 053 054/** 055 * This class is a dialog that appears when the user must choose the alias to 056 * be used from a certificate keystore. 057 */ 058public class SelectAliasDialog extends JDialog 059{ 060 private JButton okButton; 061 private JComboBox comboAliases; 062 private boolean isCanceled; 063 064 private static final long serialVersionUID = -8140704273612764046L; 065 066 /** 067 * Constructor of the SelectAliasDialog. 068 * @param parent the parent frame for this dialog. 069 */ 070 public SelectAliasDialog(JDialog parent) 071 { 072 super(parent); 073 setTitle(INFO_SELECT_ALIAS_TITLE.get().toString()); 074 getContentPane().add(createPanel()); 075 pack(); 076 int minWidth = (int) getPreferredSize().getWidth(); 077 int minHeight = (int) getPreferredSize().getHeight(); 078 addComponentListener(new MinimumSizeComponentListener(this, minWidth, 079 minHeight)); 080 getRootPane().setDefaultButton(okButton); 081 082 addWindowListener(new WindowAdapter() 083 { 084 public void windowClosing(WindowEvent e) 085 { 086 cancelClicked(); 087 } 088 }); 089 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 090 091 Utilities.centerOnComponent(this, parent); 092 setModal(true); 093 } 094 095 /** 096 * Displays this dialog with the provided aliases. 097 * 098 * @param aliases the aliases to display. 099 */ 100 public void display(String[] aliases) 101 { 102 if (aliases == null || aliases.length ==0) 103 { 104 throw new IllegalArgumentException( 105 "The provided aliases are null or empty."); 106 } 107 isCanceled = true; 108 TreeSet<String> s = new TreeSet<>(); 109 Collections.addAll(s, aliases); 110 String[] orderedAliases = new String[s.size()]; 111 s.toArray(orderedAliases); 112 comboAliases.setModel(new DefaultComboBoxModel(orderedAliases)); 113 comboAliases.setSelectedIndex(0); 114 setVisible(true); 115 } 116 117 /** 118 * Returns <CODE>true</CODE> if the user clicked on cancel and 119 * <CODE>false</CODE> otherwise. 120 * @return <CODE>true</CODE> if the user clicked on cancel and 121 * <CODE>false</CODE> otherwise. 122 */ 123 public boolean isCanceled() 124 { 125 return isCanceled; 126 } 127 128 /** 129 * Returns the selected certificate alias. 130 * @return the selected certificate alias. 131 */ 132 public String getSelectedAlias() 133 { 134 return (String) comboAliases.getSelectedItem(); 135 } 136 137 /** 138 * Creates and returns the panel of the dialog. 139 * @return the panel of the dialog. 140 */ 141 private JPanel createPanel() 142 { 143 JPanel p1 = new JPanel(new GridBagLayout()); 144 p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND); 145 p1.setBorder(UIFactory.DIALOG_PANEL_BORDER); 146 GridBagConstraints gbc = new GridBagConstraints(); 147 gbc.gridwidth = GridBagConstraints.RELATIVE; 148 gbc.anchor = GridBagConstraints.NORTHWEST; 149 150 Insets currentStepInsets = UIFactory.getCurrentStepPanelInsets(); 151 gbc.insets.top = currentStepInsets.top; 152 gbc.insets.left = currentStepInsets.left; 153 154 p1.add(UIFactory.makeJLabel(UIFactory.IconType.INFORMATION_LARGE, null, 155 UIFactory.TextStyle.NO_STYLE), gbc); 156 gbc.weightx = 1.0; 157 gbc.gridwidth = GridBagConstraints.REMAINDER; 158 gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD; 159 gbc.fill = GridBagConstraints.BOTH; 160 LocalizableMessage msg = INFO_SELECT_ALIAS_MSG.get(); 161 JTextComponent tf = UIFactory.makeHtmlPane(msg, 162 UIFactory.INSTRUCTIONS_FONT); 163 tf.setOpaque(false); 164 tf.setEditable(false); 165 p1.add(tf, gbc); 166 gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD; 167 gbc.insets.left = currentStepInsets.left; 168 gbc.insets.right = currentStepInsets.right; 169 gbc.insets.bottom = currentStepInsets.bottom; 170 comboAliases = new JComboBox(); 171 comboAliases.setPrototypeDisplayValue("The prototype alias name"); 172 gbc.fill = GridBagConstraints.NONE; 173 p1.add(comboAliases, gbc); 174 175 gbc.insets = UIFactory.getEmptyInsets(); 176 gbc.weighty = 1.0; 177 gbc.fill = GridBagConstraints.VERTICAL; 178 p1.add(Box.createVerticalGlue(), gbc); 179 180 JPanel p2 = new JPanel(new GridBagLayout()); 181 p2.setOpaque(false); 182 gbc.fill = GridBagConstraints.HORIZONTAL; 183 gbc.weightx = 1.0; 184 gbc.gridwidth = 3; 185 p2.add(Box.createHorizontalGlue(), gbc); 186 okButton = UIFactory.makeJButton(INFO_OK_BUTTON_LABEL.get(), 187 INFO_SELECT_ALIAS_OK_BUTTON_TOOLTIP.get()); 188 okButton.addActionListener(new ActionListener() 189 { 190 public void actionPerformed(ActionEvent ev) 191 { 192 okClicked(); 193 } 194 }); 195 gbc.fill = GridBagConstraints.NONE; 196 gbc.weightx = 0.0; 197 gbc.gridwidth = GridBagConstraints.RELATIVE; 198 p2.add(okButton, gbc); 199 JButton cancelButton = UIFactory.makeJButton(INFO_CANCEL_BUTTON_LABEL.get(), 200 INFO_SELECT_ALIAS_CANCEL_BUTTON_TOOLTIP.get()); 201 gbc.gridwidth = GridBagConstraints.REMAINDER; 202 gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS; 203 p2.add(cancelButton, gbc); 204 cancelButton.addActionListener(new ActionListener() 205 { 206 public void actionPerformed(ActionEvent ev) 207 { 208 cancelClicked(); 209 } 210 }); 211 212 JPanel p = new JPanel(new GridBagLayout()); 213 p.setBackground(UIFactory.DEFAULT_BACKGROUND); 214 gbc.insets = UIFactory.getEmptyInsets(); 215 gbc.fill = GridBagConstraints.BOTH; 216 gbc.gridwidth = GridBagConstraints.REMAINDER; 217 gbc.weightx = 1.0; 218 gbc.weighty = 1.0; 219 p.add(p1, gbc); 220 gbc.weighty = 0.0; 221 gbc.insets = UIFactory.getButtonsPanelInsets(); 222 p.add(p2, gbc); 223 return p; 224 } 225 226 /** 227 * Method called when user clicks on cancel. 228 * 229 */ 230 private void cancelClicked() 231 { 232 isCanceled = true; 233 dispose(); 234 } 235 236 /** 237 * Method called when user clicks on OK. 238 * 239 */ 240 private void okClicked() 241 { 242 isCanceled = false; 243 dispose(); 244 } 245 246 /** 247 * Method written for testing purposes. 248 * @param args the arguments to be passed to the test program. 249 */ 250 public static void main(String[] args) 251 { 252 try 253 { 254 // UIFactory.initialize(); 255 SelectAliasDialog dlg = 256 new SelectAliasDialog(new JDialog()); 257 dlg.display(new String[] {"test1", "test2"}); 258 } catch (Exception ex) 259 { 260 ex.printStackTrace(); 261 } 262 } 263}