1 21 22 package com.sshtools.ui.swing.options; 23 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.FlowLayout ; 27 import java.awt.Window ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 31 import javax.swing.BorderFactory ; 32 import javax.swing.Icon ; 33 import javax.swing.JButton ; 34 import javax.swing.JComponent ; 35 import javax.swing.JDialog ; 36 import javax.swing.JFrame ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import javax.swing.SwingUtilities ; 40 41 import com.sshtools.ui.swing.WrappingLabel; 42 43 48 public class OptionsDialog extends JDialog implements ActionListener { 49 private Option selectedOption; 50 private OptionCallback callback; 51 private JButton defaultButton; 52 53 65 public OptionsDialog(JDialog parent, Option[] options, Object message, String title, Option defaultOption, 66 OptionCallback callback, boolean modal, Icon icon) { 67 super(parent, title, modal); 68 init(options, message, defaultOption, callback, icon); 69 } 70 71 83 public OptionsDialog(JFrame parent, Option[] options, Object message, String title, Option defaultOption, 84 OptionCallback callback, boolean modal, Icon icon) { 85 super(parent, title, modal); 86 init(options, message, defaultOption, callback, icon); 87 } 88 89 private void init(Option[] options, Object message, Option defaultOption, OptionCallback callback, Icon icon) { 90 this.callback = callback; 92 93 JPanel b = new JPanel (new FlowLayout (FlowLayout.RIGHT, 2, 2)); 94 b.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 95 96 for (int i = 0; i < options.length; i++) { 97 JButton button = new JButton (options[i].getText()); 98 99 if (options[i] == defaultOption) { 100 button.setDefaultCapable(options[i] == defaultOption); 101 defaultButton = button; 102 getRootPane().setDefaultButton(defaultButton); 103 } 104 105 button.setMnemonic(options[i].getMnemonic()); 106 button.setToolTipText(options[i].getToolTipText()); 107 button.putClientProperty("option", options[i]); 108 button.addActionListener(this); 109 b.add(button); 110 } 111 112 JPanel s = new JPanel (new BorderLayout ()); 114 s.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0)); 115 s.add(b, BorderLayout.SOUTH); 117 118 JPanel z = new JPanel (new BorderLayout ()); 120 z.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 121 122 if (message instanceof JComponent ) { 124 z.add((JComponent ) message, BorderLayout.CENTER); 125 } else { 126 WrappingLabel l = new WrappingLabel(String.valueOf(message)); 127 z.add(l, BorderLayout.CENTER); 128 } 129 130 JLabel i = null; 132 133 if (icon != null) { 134 i = new JLabel (icon); 135 i.setVerticalAlignment(JLabel.NORTH); 136 i.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4)); 137 } 138 139 getContentPane().setLayout(new BorderLayout ()); 141 getContentPane().add(z, BorderLayout.CENTER); 142 143 if (i != null) { 144 getContentPane().add(i, BorderLayout.WEST); 145 } 146 147 getContentPane().add(s, BorderLayout.SOUTH); 148 149 pack(); 151 } 152 153 158 public JButton getDefaultButton() { 159 return defaultButton; 160 } 161 162 167 public Option getSelectedOption() { 168 return selectedOption; 169 } 170 171 176 public void actionPerformed(ActionEvent evt) { 177 selectedOption = (Option) ((JButton ) evt.getSource()).getClientProperty("option"); 178 179 if ((callback == null) || callback.canClose(this, selectedOption)) { 180 setVisible(false); 181 } 182 } 183 184 197 public static OptionsDialog createOptionDialog(Component parent, Option[] options, Object message, String title, 198 Option defaultOption, OptionCallback callback, Icon icon) { 199 OptionsDialog dialog = null; 201 Window w = (Window ) SwingUtilities.getAncestorOfClass(Window .class, parent); 202 203 if (w instanceof JFrame ) { 204 dialog = new OptionsDialog((JFrame ) w, options, message, title, defaultOption, callback, true, icon); 205 } else if (w instanceof JDialog ) { 206 dialog = new OptionsDialog((JDialog ) w, options, message, title, defaultOption, callback, true, icon); 207 } else { 208 dialog = new OptionsDialog((JFrame ) null, options, message, title, defaultOption, callback, true, icon); 209 } 210 211 if (dialog.getDefaultButton() != null) { 212 dialog.getRootPane().setDefaultButton(dialog.getDefaultButton()); 213 } 214 dialog.pack(); 215 216 return dialog; 217 } 218 } 219 | Popular Tags |