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