1 package org.columba.mail.gui.config.accountlist; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.GridLayout ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.MouseEvent ; 31 32 import javax.swing.BorderFactory ; 33 import javax.swing.Box ; 34 import javax.swing.BoxLayout ; 35 import javax.swing.DefaultListSelectionModel ; 36 import javax.swing.JButton ; 37 import javax.swing.JComponent ; 38 import javax.swing.JDialog ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.JTextField ; 44 import javax.swing.KeyStroke ; 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.config.Config; 51 import org.columba.core.gui.base.ButtonWithMnemonic; 52 import org.columba.core.gui.base.DoubleClickListener; 53 import org.columba.core.gui.base.SingleSideEtchedBorder; 54 import org.columba.core.help.HelpManager; 55 import org.columba.mail.config.AccountItem; 56 import org.columba.mail.config.AccountList; 57 import org.columba.mail.config.MailConfig; 58 import org.columba.mail.folder.IMailFolder; 59 import org.columba.mail.gui.config.account.AccountDialog; 60 import org.columba.mail.gui.config.accountwizard.AccountWizardLauncher; 61 import org.columba.mail.gui.tree.FolderTreeModel; 62 import org.columba.mail.mailchecking.MailCheckingManager; 63 import org.columba.mail.pop3.POP3ServerCollection; 64 import org.columba.mail.util.MailResourceLoader; 65 66 69 70 public class AccountListDialog extends JDialog implements ActionListener , 71 ListSelectionListener { 72 73 private AccountListTable listView; 74 75 private AccountList accountList; 76 77 private AccountItem accountItem; 78 79 protected JTextField nameTextField = new JTextField (); 80 81 protected JButton addButton; 82 83 protected JButton removeButton; 84 85 protected JButton editButton; 86 87 private int index; 88 89 private IFrameMediator mediator; 90 91 public AccountListDialog(IFrameMediator mediator) { 92 super(mediator.getView().getFrame(), MailResourceLoader.getString( 93 "dialog", "account", "dialog_title"), true); 94 this.mediator = mediator; 95 accountList = MailConfig.getInstance().getAccountList(); 96 97 initComponents(); 98 getRootPane().registerKeyboardAction(this, "CLOSE", 99 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 100 JComponent.WHEN_IN_FOCUSED_WINDOW); 101 getRootPane().registerKeyboardAction(this, "HELP", 102 KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), 103 JComponent.WHEN_IN_FOCUSED_WINDOW); 104 pack(); 105 setLocationRelativeTo(null); 106 setVisible(true); 107 } 108 109 public AccountItem getSelected() { 110 return accountItem; 111 } 112 113 public void setSelected(AccountItem item) { 114 accountItem = item; 115 } 116 117 public void initComponents() { 118 getContentPane().setLayout(new BorderLayout ()); 119 120 JPanel mainPanel = new JPanel (); 121 mainPanel.setLayout(new BorderLayout (5, 0)); 122 mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 123 124 addButton = new ButtonWithMnemonic(MailResourceLoader.getString( 125 "dialog", "account", "addaccount")); 126 127 addButton.setActionCommand("ADD"); 128 addButton.addActionListener(this); 129 130 removeButton = new ButtonWithMnemonic(MailResourceLoader.getString( 131 "dialog", "account", "removeaccount")); 132 133 removeButton.setActionCommand("REMOVE"); 134 135 removeButton.setEnabled(false); 136 removeButton.addActionListener(this); 137 138 editButton = new ButtonWithMnemonic(MailResourceLoader.getString( 139 "dialog", "account", "editsettings")); 140 141 editButton.setActionCommand("EDIT"); 142 143 editButton.setEnabled(false); 144 editButton.addActionListener(this); 145 146 JPanel topPanel = new JPanel (); 148 topPanel.setLayout(new BoxLayout (topPanel, BoxLayout.X_AXIS)); 149 150 GridBagLayout gridBagLayout = new GridBagLayout (); 151 GridBagConstraints c = new GridBagConstraints (); 152 153 JPanel topBorderPanel = new JPanel (); 155 topBorderPanel.setLayout(new BorderLayout ()); 156 topBorderPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0)); 157 topBorderPanel.add(topPanel, BorderLayout.CENTER); 158 159 JLabel nameLabel = new JLabel (MailResourceLoader.getString("dialog", 161 "account", "name")); 162 nameLabel.setEnabled(false); 163 topPanel.add(nameLabel); 164 165 topPanel.add(Box.createRigidArea(new java.awt.Dimension (10, 0))); 166 topPanel.add(Box.createHorizontalGlue()); 167 168 nameTextField.setText(MailResourceLoader.getString("dialog", "account", 169 "name")); 170 nameTextField.setEnabled(false); 171 topPanel.add(nameTextField); 172 173 Component glue = Box.createVerticalGlue(); 174 c.anchor = GridBagConstraints.EAST; 175 c.gridwidth = GridBagConstraints.REMAINDER; 176 177 gridBagLayout.setConstraints(glue, c); 179 180 gridBagLayout = new GridBagLayout (); 181 c = new GridBagConstraints (); 182 183 JPanel eastPanel = new JPanel (gridBagLayout); 184 mainPanel.add(eastPanel, BorderLayout.EAST); 185 186 c.fill = GridBagConstraints.HORIZONTAL; 187 c.weightx = 1.0; 188 c.gridwidth = GridBagConstraints.REMAINDER; 189 gridBagLayout.setConstraints(addButton, c); 190 eastPanel.add(addButton); 191 192 Component strut1 = Box.createRigidArea(new Dimension (30, 5)); 193 gridBagLayout.setConstraints(strut1, c); 194 eastPanel.add(strut1); 195 196 gridBagLayout.setConstraints(editButton, c); 197 eastPanel.add(editButton); 198 199 Component strut = Box.createRigidArea(new Dimension (30, 5)); 200 gridBagLayout.setConstraints(strut, c); 201 eastPanel.add(strut); 202 203 gridBagLayout.setConstraints(removeButton, c); 204 eastPanel.add(removeButton); 205 206 strut = Box.createRigidArea(new Dimension (30, 20)); 207 gridBagLayout.setConstraints(strut, c); 208 eastPanel.add(strut); 209 210 glue = Box.createVerticalGlue(); 211 c.fill = GridBagConstraints.BOTH; 212 c.weighty = 1.0; 213 gridBagLayout.setConstraints(glue, c); 214 eastPanel.add(glue); 215 216 listView = new AccountListTable(accountList, this); 217 listView.getSelectionModel().addListSelectionListener(this); 218 listView.addMouseListener(new DoubleClickListener() { 219 public void doubleClick(MouseEvent ev) { 220 actionPerformed(new ActionEvent (listView, 0, "EDIT")); 221 } 222 }); 223 JScrollPane scrollPane = new JScrollPane (listView); 224 scrollPane.setPreferredSize(new Dimension (300, 250)); 225 scrollPane.getViewport().setBackground(Color.white); 226 mainPanel.add(scrollPane); 227 getContentPane().add(mainPanel); 228 229 JPanel bottomPanel = new JPanel (new BorderLayout ()); 230 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 231 232 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 6, 0)); 233 buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 234 235 ButtonWithMnemonic closeButton = new ButtonWithMnemonic( 236 MailResourceLoader.getString("global", "close")); 237 closeButton.setActionCommand("CLOSE"); 238 closeButton.addActionListener(this); 239 buttonPanel.add(closeButton); 240 241 ButtonWithMnemonic helpButton = new ButtonWithMnemonic( 242 MailResourceLoader.getString("global", "help")); 243 buttonPanel.add(helpButton); 244 245 bottomPanel.add(buttonPanel, BorderLayout.EAST); 246 getContentPane().add(bottomPanel, BorderLayout.SOUTH); 247 getRootPane().setDefaultButton(closeButton); 248 249 HelpManager.getInstance().enableHelpOnButton(helpButton, 251 "configuring_columba"); 252 HelpManager.getInstance().enableHelpKey(getRootPane(), 253 "configuring_columba"); 254 } 255 256 public void valueChanged(ListSelectionEvent e) { 257 if (e.getValueIsAdjusting()) { 258 return; 259 } 260 261 DefaultListSelectionModel theList = (DefaultListSelectionModel ) e 262 .getSource(); 263 264 if (theList.isSelectionEmpty()) { 265 removeButton.setEnabled(false); 266 editButton.setEnabled(false); 267 } else { 268 removeButton.setEnabled(true); 269 editButton.setEnabled(true); 270 271 index = theList.getAnchorSelectionIndex(); 273 274 setSelected(accountList.get(index)); 275 } 276 } 277 278 protected void showAccountDialog() { 279 AccountItem parent = getSelected(); 280 281 if (parent != null) { 282 new AccountDialog(mediator, parent); 283 } 284 } 285 286 public void actionPerformed(ActionEvent e) { 287 String action = e.getActionCommand(); 288 if (action.equals("CLOSE")) { 289 try { 290 Config.getInstance().save(); 291 } catch (Exception ex) { 292 ex.printStackTrace(); 293 } 294 295 setVisible(false); 296 } else if (action.equals("ADD")) { 297 try { 298 new AccountWizardLauncher().launchWizard(false); 299 listView.update(); 300 } catch (Exception ex) { 301 ex.printStackTrace(); 302 } 303 } else if (action.equals("REMOVE")) { 304 int n = JOptionPane.showConfirmDialog(this, MailResourceLoader 305 .getString("dialog", "account", "confirmDelete.msg"), 306 MailResourceLoader.getString("dialog", "account", 307 "confirmDelete.title"), JOptionPane.YES_NO_OPTION, 308 JOptionPane.QUESTION_MESSAGE); 309 310 if (n == JOptionPane.NO_OPTION) { 311 return; 312 } 313 314 AccountItem item = accountList.remove(index); 315 if (item.isPopAccount()) { 316 POP3ServerCollection.getInstance().removePopServer( 317 item.getUid()); 318 } else { 319 IMailFolder folder = (IMailFolder) FolderTreeModel 320 .getInstance().getImapFolder(item.getUid()); 321 try { 322 IMailFolder parentFolder = (IMailFolder) folder.getParent(); 323 folder.removeFolder(); 324 FolderTreeModel.getInstance().nodeStructureChanged( 325 parentFolder); 326 } catch (Exception ex) { 327 ex.printStackTrace(); 328 } 329 } 330 331 MailCheckingManager.getInstance().remove(item.getUid()); 333 334 MailCheckingManager.getInstance().update(); 336 337 removeButton.setEnabled(false); 338 editButton.setEnabled(false); 339 listView.update(); 340 } else if (action.equals("EDIT")) { 341 showAccountDialog(); 342 listView.update(); 343 } 344 } 345 } 346 | Popular Tags |