KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > config > accountlist > AccountListDialog


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.config.accountlist;
19
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.GridLayout JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.Box JavaDoc;
34 import javax.swing.BoxLayout JavaDoc;
35 import javax.swing.DefaultListSelectionModel JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTextField JavaDoc;
44 import javax.swing.KeyStroke JavaDoc;
45 import javax.swing.SwingConstants JavaDoc;
46 import javax.swing.event.ListSelectionEvent JavaDoc;
47 import javax.swing.event.ListSelectionListener JavaDoc;
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 /**
67  * A dialog showing a list with the user's accounts.
68  */

69
70 public class AccountListDialog extends JDialog JavaDoc implements ActionListener JavaDoc,
71         ListSelectionListener JavaDoc {
72
73     private AccountListTable listView;
74
75     private AccountList accountList;
76
77     private AccountItem accountItem;
78
79     protected JTextField JavaDoc nameTextField = new JTextField JavaDoc();
80
81     protected JButton JavaDoc addButton;
82
83     protected JButton JavaDoc removeButton;
84
85     protected JButton JavaDoc 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 JavaDoc());
119
120         JPanel JavaDoc mainPanel = new JPanel JavaDoc();
121         mainPanel.setLayout(new BorderLayout JavaDoc(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         // top panel
147
JPanel JavaDoc topPanel = new JPanel JavaDoc();
148         topPanel.setLayout(new BoxLayout JavaDoc(topPanel, BoxLayout.X_AXIS));
149
150         GridBagLayout JavaDoc gridBagLayout = new GridBagLayout JavaDoc();
151         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
152
153         // topPanel.setLayout( );
154
JPanel JavaDoc topBorderPanel = new JPanel JavaDoc();
155         topBorderPanel.setLayout(new BorderLayout JavaDoc());
156         topBorderPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
157         topBorderPanel.add(topPanel, BorderLayout.CENTER);
158
159         // mainPanel.add( topBorderPanel, BorderLayout.NORTH );
160
JLabel JavaDoc nameLabel = new JLabel JavaDoc(MailResourceLoader.getString("dialog",
161                 "account", "name"));
162         nameLabel.setEnabled(false);
163         topPanel.add(nameLabel);
164
165         topPanel.add(Box.createRigidArea(new java.awt.Dimension JavaDoc(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 JavaDoc glue = Box.createVerticalGlue();
174         c.anchor = GridBagConstraints.EAST;
175         c.gridwidth = GridBagConstraints.REMAINDER;
176
177         // c.fill = GridBagConstraints.HORIZONTAL;
178
gridBagLayout.setConstraints(glue, c);
179
180         gridBagLayout = new GridBagLayout JavaDoc();
181         c = new GridBagConstraints JavaDoc();
182
183         JPanel JavaDoc eastPanel = new JPanel JavaDoc(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 JavaDoc strut1 = Box.createRigidArea(new Dimension JavaDoc(30, 5));
193         gridBagLayout.setConstraints(strut1, c);
194         eastPanel.add(strut1);
195
196         gridBagLayout.setConstraints(editButton, c);
197         eastPanel.add(editButton);
198
199         Component JavaDoc strut = Box.createRigidArea(new Dimension JavaDoc(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 JavaDoc(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 JavaDoc ev) {
220                 actionPerformed(new ActionEvent JavaDoc(listView, 0, "EDIT"));
221             }
222         });
223         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(listView);
224         scrollPane.setPreferredSize(new Dimension JavaDoc(300, 250));
225         scrollPane.getViewport().setBackground(Color.white);
226         mainPanel.add(scrollPane);
227         getContentPane().add(mainPanel);
228
229         JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
230         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
231
232         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(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         // associate with JavaHelp
250
HelpManager.getInstance().enableHelpOnButton(helpButton,
251                 "configuring_columba");
252         HelpManager.getInstance().enableHelpKey(getRootPane(),
253                 "configuring_columba");
254     }
255
256     public void valueChanged(ListSelectionEvent JavaDoc e) {
257         if (e.getValueIsAdjusting()) {
258             return;
259         }
260
261         DefaultListSelectionModel JavaDoc theList = (DefaultListSelectionModel JavaDoc) 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             // String value = (String) theList.getSelectedValue();
272
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 JavaDoc e) {
287         String JavaDoc action = e.getActionCommand();
288         if (action.equals("CLOSE")) {
289             try {
290                 Config.getInstance().save();
291             } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc ex) {
327                     ex.printStackTrace();
328                 }
329             }
330
331             // remove mail-checking stuff
332
MailCheckingManager.getInstance().remove(item.getUid());
333
334             // notify all observers
335
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