1 19 package org.columba.core.gui.profiles; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.GridLayout ; 28 import java.awt.HeadlessException ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseListener ; 34 import java.io.File ; 35 36 import javax.swing.BorderFactory ; 37 import javax.swing.Box ; 38 import javax.swing.BoxLayout ; 39 import javax.swing.DefaultListModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JCheckBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JDialog ; 44 import javax.swing.JFileChooser ; 45 import javax.swing.JFrame ; 46 import javax.swing.JLabel ; 47 import javax.swing.JList ; 48 import javax.swing.JOptionPane ; 49 import javax.swing.JPanel ; 50 import javax.swing.JScrollPane ; 51 import javax.swing.KeyStroke ; 52 import javax.swing.SwingConstants ; 53 import javax.swing.event.ListSelectionEvent ; 54 import javax.swing.event.ListSelectionListener ; 55 56 import org.columba.core.gui.base.ButtonWithMnemonic; 57 import org.columba.core.gui.base.CheckBoxWithMnemonic; 58 import org.columba.core.gui.base.SingleSideEtchedBorder; 59 import org.columba.core.help.HelpManager; 60 import org.columba.core.resourceloader.GlobalResourceLoader; 61 import org.columba.core.xml.XmlElement; 62 63 73 public class ProfileChooserDialog extends JDialog 74 implements ActionListener , ListSelectionListener , MouseListener { 75 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 76 77 protected JButton okButton; 78 protected JButton cancelButton; 79 protected JButton helpButton; 80 protected JButton addButton; 81 protected JButton editButton; 82 private DefaultListModel model; 84 protected JList list; 85 protected String selection; 86 protected JLabel nameLabel; 87 protected JCheckBox checkBox; 88 89 public ProfileChooserDialog() throws HeadlessException { 90 super((JFrame )null, GlobalResourceLoader.getString(RESOURCE_PATH, 91 "profiles", "chooser.title"), true); 92 93 initComponents(); 94 95 layoutComponents(); 96 97 pack(); 98 setLocationRelativeTo(null); 99 setVisible(true); 100 } 101 102 protected void layoutComponents() { 103 JPanel mainPanel = new JPanel (new BorderLayout ()); 104 mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 105 106 JPanel topPanel = new JPanel (); 108 109 topPanel.setLayout(new BoxLayout (topPanel, BoxLayout.X_AXIS)); 110 111 GridBagLayout gridBagLayout = new GridBagLayout (); 112 GridBagConstraints c = new GridBagConstraints (); 113 114 JPanel topBorderPanel = new JPanel (); 116 topBorderPanel.setLayout(new BorderLayout ()); 117 118 topBorderPanel.add(topPanel); 121 122 124 topPanel.add(nameLabel); 125 126 topPanel.add(Box.createRigidArea(new Dimension (10, 0))); 127 topPanel.add(Box.createHorizontalGlue()); 128 129 Component glue = Box.createVerticalGlue(); 130 c.anchor = GridBagConstraints.EAST; 131 c.gridwidth = GridBagConstraints.REMAINDER; 132 133 gridBagLayout.setConstraints(glue, c); 135 136 gridBagLayout = new GridBagLayout (); 137 c = new GridBagConstraints (); 138 139 JPanel eastPanel = new JPanel (gridBagLayout); 140 mainPanel.add(eastPanel, BorderLayout.EAST); 141 142 c.fill = GridBagConstraints.HORIZONTAL; 143 c.weightx = 1.0; 144 c.gridwidth = GridBagConstraints.REMAINDER; 145 gridBagLayout.setConstraints(addButton, c); 146 eastPanel.add(addButton); 147 148 Component strut1 = Box.createRigidArea(new Dimension (30, 5)); 149 gridBagLayout.setConstraints(strut1, c); 150 eastPanel.add(strut1); 151 152 gridBagLayout.setConstraints(editButton, c); 153 eastPanel.add(editButton); 154 155 162 163 glue = Box.createVerticalGlue(); 164 c.fill = GridBagConstraints.BOTH; 165 c.weighty = 1.0; 166 gridBagLayout.setConstraints(glue, c); 167 eastPanel.add(glue); 168 169 JPanel centerPanel = new JPanel (new BorderLayout ()); 171 centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 6)); 172 173 JScrollPane scrollPane = new JScrollPane (list); 174 scrollPane.setPreferredSize(new Dimension (250, 150)); 175 scrollPane.getViewport().setBackground(Color.white); 176 centerPanel.add(scrollPane, BorderLayout.CENTER); 177 178 centerPanel.add(checkBox, BorderLayout.SOUTH); 179 180 mainPanel.add(topPanel, BorderLayout.NORTH); 181 182 mainPanel.add(centerPanel); 183 getContentPane().add(mainPanel, BorderLayout.CENTER); 184 185 JPanel bottomPanel = new JPanel (new BorderLayout ()); 186 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 187 188 JPanel buttonPanel = new JPanel (new GridLayout (1, 3, 6, 0)); 189 buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 190 191 buttonPanel.add(okButton); 192 buttonPanel.add(cancelButton); 193 buttonPanel.add(helpButton); 194 bottomPanel.add(buttonPanel, BorderLayout.EAST); 195 getContentPane().add(bottomPanel, BorderLayout.SOUTH); 196 } 197 198 protected void initComponents() { 199 addButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 200 RESOURCE_PATH, "profiles", "add")); 201 addButton.setActionCommand("ADD"); 202 addButton.addActionListener(this); 203 addButton.setEnabled(false); 204 205 editButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 206 RESOURCE_PATH, "profiles", "edit")); 207 editButton.setActionCommand("EDIT"); 208 editButton.addActionListener(this); 209 editButton.setEnabled(false); 210 211 nameLabel = new JLabel (GlobalResourceLoader.getString(RESOURCE_PATH, 212 "profiles", "label")); 213 214 checkBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString( 215 RESOURCE_PATH, "profiles", "dont_ask")); 216 217 okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("", 218 "", "ok")); 219 okButton.setActionCommand("OK"); 220 okButton.addActionListener(this); 221 222 cancelButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 223 "", "", "cancel")); 224 cancelButton.setActionCommand("CANCEL"); 225 cancelButton.addActionListener(this); 226 227 helpButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("", 228 "", "help")); 229 230 HelpManager.getInstance().enableHelpOnButton(helpButton, 232 "extending_columba_2"); 233 HelpManager.getInstance().enableHelpKey(getRootPane(), 234 "extending_columba_2"); 235 236 XmlElement profiles = ProfileManager.getInstance().getProfiles(); 237 model = new DefaultListModel (); 238 model.addElement("Default"); 239 240 for (int i = 0; i < profiles.count(); i++) { 241 XmlElement p = profiles.getElement(i); 242 String name = p.getAttribute("name"); 243 model.addElement(name); 244 } 245 246 list = new JList (model); 247 list.addListSelectionListener(this); 248 list.addMouseListener(this); 249 250 String selected = ProfileManager.getInstance().getSelectedProfile(); 251 if (selected != null) { 252 list.setSelectedValue(selected, true); 253 } 254 255 getRootPane().setDefaultButton(okButton); 256 getRootPane().registerKeyboardAction(this, "CANCEL", 257 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 258 JComponent.WHEN_IN_FOCUSED_WINDOW); 259 } 260 261 public void actionPerformed(ActionEvent e) { 262 String action = e.getActionCommand(); 263 if (action.equals("OK")) { 264 setVisible(false); 265 } else if (action.equals("CANCEL")) { 266 System.exit(0); 267 } else if (action.equals("ADD")) { 268 JFileChooser fc = new JFileChooser (); 269 fc.setMultiSelectionEnabled(true); 270 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 272 fc.setFileHidingEnabled(false); 273 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 274 File location = fc.getSelectedFile(); 275 Profile p = new Profile(location.getName(), location); 276 ProfileManager.getInstance().addProfile(p); 278 279 model.addElement(p.getName()); 281 list.setSelectedValue(p.getName(), true); 283 } 284 } else if (action.equals("EDIT")) { 285 String inputValue = JOptionPane.showInputDialog( 286 GlobalResourceLoader.getString(RESOURCE_PATH, "profiles", 287 "enter_name"), selection); 288 289 if (inputValue == null) { 290 return; 291 } 292 293 ProfileManager.getInstance().renameProfile(selection, inputValue); 295 296 model.setElementAt(inputValue, model.indexOf(selection)); 298 selection = inputValue; 299 } 300 } 301 302 public void valueChanged(ListSelectionEvent e) { 303 boolean enabled = !list.isSelectionEmpty(); 304 addButton.setEnabled(enabled); 305 306 okButton.setEnabled(enabled); 307 309 selection = (String ) list.getSelectedValue(); 310 311 if ((selection != null) && (!selection.equals("Default"))) { 313 editButton.setEnabled(true); 314 } else { 315 editButton.setEnabled(false); 316 } 317 } 318 319 322 public String getSelection() { 323 return selection; 324 } 325 326 public boolean isDontAskedSelected() { 327 return checkBox.isSelected(); 328 } 329 330 public void mouseClicked(MouseEvent arg0) { 331 if(arg0.getClickCount() >= 2 ) { 332 actionPerformed(new ActionEvent (list, 0, "OK")); 333 } 334 } 335 336 public void mouseEntered(MouseEvent arg0) { 337 } 338 339 public void mouseExited(MouseEvent arg0) { 340 } 341 342 public void mousePressed(MouseEvent arg0) { 343 } 344 345 public void mouseReleased(MouseEvent arg0) { 346 } 347 348 } 349 | Popular Tags |