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-2016 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 @Override 075 public void windowClosing(WindowEvent e) 076 { 077 cancelClicked(); 078 } 079 }); 080 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 081 082 Utilities.centerOnComponent(this, parent); 083 setModal(true); 084 } 085 086 /** 087 * Displays this dialog with the provided aliases. 088 * 089 * @param aliases the aliases to display. 090 */ 091 public void display(String[] aliases) 092 { 093 if (aliases == null || aliases.length ==0) 094 { 095 throw new IllegalArgumentException( 096 "The provided aliases are null or empty."); 097 } 098 isCanceled = true; 099 TreeSet<String> s = new TreeSet<>(); 100 Collections.addAll(s, aliases); 101 String[] orderedAliases = new String[s.size()]; 102 s.toArray(orderedAliases); 103 comboAliases.setModel(new DefaultComboBoxModel(orderedAliases)); 104 comboAliases.setSelectedIndex(0); 105 setVisible(true); 106 } 107 108 /** 109 * Returns <CODE>true</CODE> if the user clicked on cancel and 110 * <CODE>false</CODE> otherwise. 111 * @return <CODE>true</CODE> if the user clicked on cancel and 112 * <CODE>false</CODE> otherwise. 113 */ 114 public boolean isCanceled() 115 { 116 return isCanceled; 117 } 118 119 /** 120 * Returns the selected certificate alias. 121 * @return the selected certificate alias. 122 */ 123 public String getSelectedAlias() 124 { 125 return (String) comboAliases.getSelectedItem(); 126 } 127 128 /** 129 * Creates and returns the panel of the dialog. 130 * @return the panel of the dialog. 131 */ 132 private JPanel createPanel() 133 { 134 JPanel p1 = new JPanel(new GridBagLayout()); 135 p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND); 136 p1.setBorder(UIFactory.DIALOG_PANEL_BORDER); 137 GridBagConstraints gbc = new GridBagConstraints(); 138 gbc.gridwidth = GridBagConstraints.RELATIVE; 139 gbc.anchor = GridBagConstraints.NORTHWEST; 140 141 Insets currentStepInsets = UIFactory.getCurrentStepPanelInsets(); 142 gbc.insets.top = currentStepInsets.top; 143 gbc.insets.left = currentStepInsets.left; 144 145 p1.add(UIFactory.makeJLabel(UIFactory.IconType.INFORMATION_LARGE, null, 146 UIFactory.TextStyle.NO_STYLE), gbc); 147 gbc.weightx = 1.0; 148 gbc.gridwidth = GridBagConstraints.REMAINDER; 149 gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD; 150 gbc.fill = GridBagConstraints.BOTH; 151 LocalizableMessage msg = INFO_SELECT_ALIAS_MSG.get(); 152 JTextComponent tf = UIFactory.makeHtmlPane(msg, 153 UIFactory.INSTRUCTIONS_FONT); 154 tf.setOpaque(false); 155 tf.setEditable(false); 156 p1.add(tf, gbc); 157 gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD; 158 gbc.insets.left = currentStepInsets.left; 159 gbc.insets.right = currentStepInsets.right; 160 gbc.insets.bottom = currentStepInsets.bottom; 161 comboAliases = new JComboBox(); 162 comboAliases.setPrototypeDisplayValue("The prototype alias name"); 163 gbc.fill = GridBagConstraints.NONE; 164 p1.add(comboAliases, gbc); 165 166 gbc.insets = UIFactory.getEmptyInsets(); 167 gbc.weighty = 1.0; 168 gbc.fill = GridBagConstraints.VERTICAL; 169 p1.add(Box.createVerticalGlue(), gbc); 170 171 JPanel p2 = new JPanel(new GridBagLayout()); 172 p2.setOpaque(false); 173 gbc.fill = GridBagConstraints.HORIZONTAL; 174 gbc.weightx = 1.0; 175 gbc.gridwidth = 3; 176 p2.add(Box.createHorizontalGlue(), gbc); 177 okButton = UIFactory.makeJButton(INFO_OK_BUTTON_LABEL.get(), 178 INFO_SELECT_ALIAS_OK_BUTTON_TOOLTIP.get()); 179 okButton.addActionListener(new ActionListener() 180 { 181 @Override 182 public void actionPerformed(ActionEvent ev) 183 { 184 okClicked(); 185 } 186 }); 187 gbc.fill = GridBagConstraints.NONE; 188 gbc.weightx = 0.0; 189 gbc.gridwidth = GridBagConstraints.RELATIVE; 190 p2.add(okButton, gbc); 191 JButton cancelButton = UIFactory.makeJButton(INFO_CANCEL_BUTTON_LABEL.get(), 192 INFO_SELECT_ALIAS_CANCEL_BUTTON_TOOLTIP.get()); 193 gbc.gridwidth = GridBagConstraints.REMAINDER; 194 gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS; 195 p2.add(cancelButton, gbc); 196 cancelButton.addActionListener(new ActionListener() 197 { 198 @Override 199 public void actionPerformed(ActionEvent ev) 200 { 201 cancelClicked(); 202 } 203 }); 204 205 JPanel p = new JPanel(new GridBagLayout()); 206 p.setBackground(UIFactory.DEFAULT_BACKGROUND); 207 gbc.insets = UIFactory.getEmptyInsets(); 208 gbc.fill = GridBagConstraints.BOTH; 209 gbc.gridwidth = GridBagConstraints.REMAINDER; 210 gbc.weightx = 1.0; 211 gbc.weighty = 1.0; 212 p.add(p1, gbc); 213 gbc.weighty = 0.0; 214 gbc.insets = UIFactory.getButtonsPanelInsets(); 215 p.add(p2, gbc); 216 return p; 217 } 218 219 /** Method called when user clicks on cancel. */ 220 private void cancelClicked() 221 { 222 isCanceled = true; 223 dispose(); 224 } 225 226 /** Method called when user clicks on OK. */ 227 private void okClicked() 228 { 229 isCanceled = false; 230 dispose(); 231 } 232 233 /** 234 * Method written for testing purposes. 235 * @param args the arguments to be passed to the test program. 236 */ 237 public static void main(String[] args) 238 { 239 try 240 { 241 // UIFactory.initialize(); 242 SelectAliasDialog dlg = 243 new SelectAliasDialog(new JDialog()); 244 dlg.display(new String[] {"test1", "test2"}); 245 } catch (Exception ex) 246 { 247 ex.printStackTrace(); 248 } 249 } 250}