1 19 package org.columba.core.gui.profiles; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Dimension ; 23 import java.awt.GridLayout ; 24 import java.awt.HeadlessException ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.MouseEvent ; 29 import java.io.File ; 30 31 import javax.swing.BorderFactory ; 32 import javax.swing.DefaultListModel ; 33 import javax.swing.JButton ; 34 import javax.swing.JCheckBox ; 35 import javax.swing.JComponent ; 36 import javax.swing.JDialog ; 37 import javax.swing.JFileChooser ; 38 import javax.swing.JLabel ; 39 import javax.swing.JList ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.KeyStroke ; 44 import javax.swing.ListSelectionModel ; 45 import javax.swing.SwingConstants ; 46 import javax.swing.event.ListSelectionEvent ; 47 import javax.swing.event.ListSelectionListener ; 48 49 import org.columba.api.gui.frame.IFrameMediator; 50 import org.columba.core.gui.base.ButtonWithMnemonic; 51 import org.columba.core.gui.base.DoubleClickListener; 52 import org.columba.core.gui.base.SingleSideEtchedBorder; 53 import org.columba.core.gui.util.DialogHeaderPanel; 54 import org.columba.core.help.HelpManager; 55 import org.columba.core.resourceloader.GlobalResourceLoader; 56 import org.columba.core.resourceloader.IconKeys; 57 import org.columba.core.resourceloader.ImageLoader; 58 import org.columba.core.xml.XmlElement; 59 60 import com.jgoodies.forms.layout.CellConstraints; 61 import com.jgoodies.forms.layout.FormLayout; 62 63 73 public class ProfileManagerDialog extends JDialog implements ActionListener , 74 ListSelectionListener { 75 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 76 77 protected JButton okButton; 78 79 protected JButton helpButton; 80 81 protected JButton addButton; 82 83 protected JButton editButton; 84 85 protected JButton removeButton; 86 87 protected JButton importButton; 88 89 protected JButton exportButton; 90 91 private DefaultListModel model; 93 94 protected JList list; 95 96 protected String selection; 97 98 protected JLabel nameLabel; 99 100 protected JCheckBox checkBox; 101 102 public ProfileManagerDialog(IFrameMediator mediator) 103 throws HeadlessException { 104 super(mediator.getView().getFrame(), GlobalResourceLoader.getString( 105 RESOURCE_PATH, "profiles", "manager.title"), true); 106 107 108 initComponents(); 109 110 layoutComponents(); 111 112 pack(); 113 114 setLocationRelativeTo(null); 115 setVisible(true); 116 } 117 118 protected void layoutComponents() { 119 120 getContentPane().add(createPanel(), BorderLayout.CENTER); 121 122 JPanel bottomPanel = createBottomPanel(); 123 getContentPane().add(bottomPanel, BorderLayout.SOUTH); 124 125 getContentPane().add( 126 new DialogHeaderPanel(GlobalResourceLoader.getString( 127 RESOURCE_PATH, "profiles", "header_title"), 128 GlobalResourceLoader.getString(RESOURCE_PATH, 129 "profiles", "header_description"), ImageLoader 130 .getIcon(IconKeys.USER)), 131 BorderLayout.NORTH); 132 } 133 134 private JPanel createPanel() { 135 JPanel jpanel1 = new JPanel (); 136 FormLayout formlayout1 = new FormLayout( 137 "FILL:DEFAULT:GROW(1.0),3DLU,FILL:DEFAULT:NONE", 138 "CENTER:DEFAULT:NONE,1DLU,FILL:DEFAULT:GROW(1.0),3DLU,CENTER:DEFAULT:NONE"); 139 CellConstraints cc = new CellConstraints(); 140 jpanel1.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 141 jpanel1.setLayout(formlayout1); 142 143 JLabel jlabel1 = new JLabel (); 144 jlabel1.setText("Profiles:"); 145 jpanel1.add(jlabel1, cc.xy(1, 1)); 146 147 JScrollPane scrollPane = new JScrollPane (list); 148 scrollPane.setPreferredSize(new Dimension (250, 150)); 149 jpanel1.add(scrollPane, cc.xy(1, 3)); 150 151 jpanel1.add(checkBox, cc.xy(1, 5)); 152 153 jpanel1.add(createPanel1(), new CellConstraints(3, 3, 1, 1, 154 CellConstraints.DEFAULT, CellConstraints.TOP)); 155 156 return jpanel1; 157 } 158 159 private JPanel createPanel1() { 160 JPanel jpanel1 = new JPanel (); 161 FormLayout formlayout1 = new FormLayout( 162 "FILL:DEFAULT:NONE", 163 "CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU, CENTER:DEFAULT:NONE"); 164 CellConstraints cc = new CellConstraints(); 165 jpanel1.setLayout(formlayout1); 166 167 jpanel1.add(addButton, cc.xy(1, 1)); 168 169 jpanel1.add(editButton, cc.xy(1, 3)); 170 171 jpanel1.add(removeButton, cc.xy(1, 5)); 172 173 return jpanel1; 174 } 175 176 179 private JPanel createBottomPanel() { 180 JPanel bottomPanel = new JPanel (new BorderLayout ()); 181 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 182 183 JPanel buttonPanel = new JPanel (new GridLayout (1, 3, 6, 0)); 184 buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 185 186 buttonPanel.add(okButton); 187 188 buttonPanel.add(helpButton); 189 bottomPanel.add(buttonPanel, BorderLayout.EAST); 190 return bottomPanel; 191 } 192 193 protected void initComponents() { 194 addButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 195 RESOURCE_PATH, "profiles", "add")); 196 addButton.setActionCommand("ADD"); 197 addButton.addActionListener(this); 198 addButton.setEnabled(false); 199 200 editButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 201 RESOURCE_PATH, "profiles", "edit")); 202 editButton.setActionCommand("EDIT"); 203 editButton.addActionListener(this); 204 editButton.setEnabled(false); 205 206 removeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 207 RESOURCE_PATH, "profiles", "remove")); 208 removeButton.setActionCommand("REMOVE"); 209 removeButton.addActionListener(this); 210 removeButton.setEnabled(false); 211 212 importButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 213 RESOURCE_PATH, "profiles", "import")); 214 importButton.setActionCommand("IMPORT"); 215 importButton.addActionListener(this); 216 217 exportButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 218 RESOURCE_PATH, "profiles", "export")); 219 exportButton.setActionCommand("EXPORT"); 220 exportButton.addActionListener(this); 221 exportButton.setEnabled(false); 222 223 nameLabel = new JLabel ("Choose Profile:"); 224 225 checkBox = new JCheckBox ("Always ask on startup."); 226 checkBox.setSelected(ProfileManager.getInstance().isAlwaysAsk()); 227 checkBox.setActionCommand("CHECKBOX"); 228 checkBox.addActionListener(this); 229 230 okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("", 231 "", "close")); 232 okButton.setActionCommand("CLOSE"); 233 okButton.addActionListener(this); 234 235 helpButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("", 236 "", "help")); 237 238 HelpManager.getInstance().enableHelpOnButton(helpButton, 240 "extending_columba_2"); 241 HelpManager.getInstance().enableHelpKey(getRootPane(), 242 "extending_columba_2"); 243 244 XmlElement profiles = ProfileManager.getInstance().getProfiles(); 245 model = new DefaultListModel (); 246 model.addElement("Default"); 247 248 for (int i = 0; i < profiles.count(); i++) { 249 XmlElement p = profiles.getElement(i); 250 String name = p.getAttribute("name"); 251 model.addElement(name); 252 } 253 254 list = new JList (model); 255 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 256 list.addListSelectionListener(this); 257 list.addMouseListener(new DoubleClickListener() { 258 public void doubleClick(MouseEvent e) { 259 actionPerformed(new ActionEvent (list, 0, "EDIT")); 260 } 261 }); 262 263 String selected = ProfileManager.getInstance().getSelectedProfile(); 264 if (selected != null) { 265 list.setSelectedValue(selected, true); 266 } 267 268 getRootPane().setDefaultButton(okButton); 269 getRootPane().registerKeyboardAction(this, "CLOSE", 270 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 271 JComponent.WHEN_IN_FOCUSED_WINDOW); 272 } 273 274 public void actionPerformed(ActionEvent e) { 275 String action = e.getActionCommand(); 276 if (action.equals("CLOSE")) { 277 ProfileManager.getInstance().setAlwaysAsk(isAlwaysAskSelected()); 278 279 setVisible(false); 280 } else if (action.equals("CHECKBOX")) { 281 282 } else if (action.equals("ADD")) { 283 JFileChooser fc = new JFileChooser (); 284 fc.setMultiSelectionEnabled(true); 285 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 287 fc.setFileHidingEnabled(false); 288 289 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 290 File location = fc.getSelectedFile(); 291 Profile p = new Profile(location.getName(), location); 292 ProfileManager.getInstance().addProfile(p); 294 295 model.addElement(p.getName()); 297 list.setSelectedValue(p.getName(), true); 299 } 300 } else if (action.equals("EDIT")) { 301 String inputValue = JOptionPane.showInputDialog( 302 GlobalResourceLoader.getString(RESOURCE_PATH, "profiles", 303 "enter_name"), selection); 304 305 if (inputValue == null) { 306 return; 307 } 308 309 ProfileManager.getInstance().renameProfile(selection, inputValue); 311 312 model.setElementAt(inputValue, model.indexOf(selection)); 314 315 selection = inputValue; 316 } else if (action.equals("REMOVE")) { 317 if (ProfileManager.getInstance().getCurrentProfile().getName() 318 .equals(selection)) { 319 JOptionPane.showMessageDialog(this, GlobalResourceLoader 321 .getString(RESOURCE_PATH, "profiles", "errDelete.msg"), 322 GlobalResourceLoader.getString(RESOURCE_PATH, 323 "profiles", "errDelete.title"), 324 JOptionPane.ERROR_MESSAGE); 325 } else { 326 Profile p = ProfileManager.getInstance().getProfileForName( 327 selection); 328 if (p != null) { 329 int n = JOptionPane.showConfirmDialog(this, 330 GlobalResourceLoader.getString(RESOURCE_PATH, 331 "profiles", "confirmDelete.msg"), 332 GlobalResourceLoader.getString(RESOURCE_PATH, 333 "profiles", "confirmDelete.title"), 334 JOptionPane.YES_NO_OPTION, 335 JOptionPane.QUESTION_MESSAGE); 336 if (n == JOptionPane.NO_OPTION) { 337 return; 338 } 339 340 ProfileManager.getInstance().removeProfile(selection); 341 model.removeElement(selection); 342 343 } 344 } 345 } else if (action.equals("IMPORT")) { 346 361 } else if (action.equals("EXPORT")) { 362 } 364 } 365 366 public void valueChanged(ListSelectionEvent e) { 367 boolean enabled = !list.isSelectionEmpty(); 368 addButton.setEnabled(enabled); 369 370 exportButton.setEnabled(enabled); 371 372 selection = (String ) list.getSelectedValue(); 374 375 if ((selection != null) && (!selection.equals("Default"))) { 377 removeButton.setEnabled(true); 378 editButton.setEnabled(true); 379 } else { 380 removeButton.setEnabled(false); 381 editButton.setEnabled(false); 382 } 383 } 384 385 388 public String getSelection() { 389 return selection; 390 } 391 392 public boolean isAlwaysAskSelected() { 393 return checkBox.isSelected(); 394 } 395 } | Popular Tags |