001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.gui.config.accountlist;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.GridLayout;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.KeyEvent;
030: import java.awt.event.MouseEvent;
031:
032: import javax.swing.BorderFactory;
033: import javax.swing.Box;
034: import javax.swing.BoxLayout;
035: import javax.swing.DefaultListSelectionModel;
036: import javax.swing.JButton;
037: import javax.swing.JComponent;
038: import javax.swing.JDialog;
039: import javax.swing.JLabel;
040: import javax.swing.JOptionPane;
041: import javax.swing.JPanel;
042: import javax.swing.JScrollPane;
043: import javax.swing.JTextField;
044: import javax.swing.KeyStroke;
045: import javax.swing.SwingConstants;
046: import javax.swing.event.ListSelectionEvent;
047: import javax.swing.event.ListSelectionListener;
048:
049: import org.columba.api.gui.frame.IFrameMediator;
050: import org.columba.core.config.Config;
051: import org.columba.core.gui.base.ButtonWithMnemonic;
052: import org.columba.core.gui.base.DoubleClickListener;
053: import org.columba.core.gui.base.SingleSideEtchedBorder;
054: import org.columba.core.help.HelpManager;
055: import org.columba.mail.config.AccountItem;
056: import org.columba.mail.config.AccountList;
057: import org.columba.mail.config.MailConfig;
058: import org.columba.mail.folder.IMailFolder;
059: import org.columba.mail.gui.config.account.AccountDialog;
060: import org.columba.mail.gui.config.accountwizard.AccountWizardLauncher;
061: import org.columba.mail.gui.tree.FolderTreeModel;
062: import org.columba.mail.mailchecking.MailCheckingManager;
063: import org.columba.mail.pop3.POP3ServerCollection;
064: import org.columba.mail.util.MailResourceLoader;
065:
066: /**
067: * A dialog showing a list with the user's accounts.
068: */
069:
070: public class AccountListDialog extends JDialog implements
071: ActionListener, ListSelectionListener {
072:
073: private AccountListTable listView;
074:
075: private AccountList accountList;
076:
077: private AccountItem accountItem;
078:
079: protected JTextField nameTextField = new JTextField();
080:
081: protected JButton addButton;
082:
083: protected JButton removeButton;
084:
085: protected JButton editButton;
086:
087: private int index;
088:
089: private IFrameMediator mediator;
090:
091: public AccountListDialog(IFrameMediator mediator) {
092: super (mediator.getView().getFrame(), MailResourceLoader
093: .getString("dialog", "account", "dialog_title"), true);
094: this .mediator = mediator;
095: accountList = MailConfig.getInstance().getAccountList();
096:
097: initComponents();
098: getRootPane().registerKeyboardAction(this , "CLOSE",
099: 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,
123: 12));
124:
125: addButton = new ButtonWithMnemonic(MailResourceLoader
126: .getString("dialog", "account", "addaccount"));
127:
128: addButton.setActionCommand("ADD");
129: addButton.addActionListener(this );
130:
131: removeButton = new ButtonWithMnemonic(MailResourceLoader
132: .getString("dialog", "account", "removeaccount"));
133:
134: removeButton.setActionCommand("REMOVE");
135:
136: removeButton.setEnabled(false);
137: removeButton.addActionListener(this );
138:
139: editButton = new ButtonWithMnemonic(MailResourceLoader
140: .getString("dialog", "account", "editsettings"));
141:
142: editButton.setActionCommand("EDIT");
143:
144: editButton.setEnabled(false);
145: editButton.addActionListener(this );
146:
147: // top panel
148: JPanel topPanel = new JPanel();
149: topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
150:
151: GridBagLayout gridBagLayout = new GridBagLayout();
152: GridBagConstraints c = new GridBagConstraints();
153:
154: // topPanel.setLayout( );
155: JPanel topBorderPanel = new JPanel();
156: topBorderPanel.setLayout(new BorderLayout());
157: topBorderPanel.setBorder(BorderFactory.createEmptyBorder(0, 0,
158: 5, 0));
159: topBorderPanel.add(topPanel, BorderLayout.CENTER);
160:
161: // mainPanel.add( topBorderPanel, BorderLayout.NORTH );
162: JLabel nameLabel = new JLabel(MailResourceLoader.getString(
163: "dialog", "account", "name"));
164: nameLabel.setEnabled(false);
165: topPanel.add(nameLabel);
166:
167: topPanel
168: .add(Box.createRigidArea(new java.awt.Dimension(10, 0)));
169: topPanel.add(Box.createHorizontalGlue());
170:
171: nameTextField.setText(MailResourceLoader.getString("dialog",
172: "account", "name"));
173: nameTextField.setEnabled(false);
174: topPanel.add(nameTextField);
175:
176: Component glue = Box.createVerticalGlue();
177: c.anchor = GridBagConstraints.EAST;
178: c.gridwidth = GridBagConstraints.REMAINDER;
179:
180: // c.fill = GridBagConstraints.HORIZONTAL;
181: gridBagLayout.setConstraints(glue, c);
182:
183: gridBagLayout = new GridBagLayout();
184: c = new GridBagConstraints();
185:
186: JPanel eastPanel = new JPanel(gridBagLayout);
187: mainPanel.add(eastPanel, BorderLayout.EAST);
188:
189: c.fill = GridBagConstraints.HORIZONTAL;
190: c.weightx = 1.0;
191: c.gridwidth = GridBagConstraints.REMAINDER;
192: gridBagLayout.setConstraints(addButton, c);
193: eastPanel.add(addButton);
194:
195: Component strut1 = Box.createRigidArea(new Dimension(30, 5));
196: gridBagLayout.setConstraints(strut1, c);
197: eastPanel.add(strut1);
198:
199: gridBagLayout.setConstraints(editButton, c);
200: eastPanel.add(editButton);
201:
202: Component strut = Box.createRigidArea(new Dimension(30, 5));
203: gridBagLayout.setConstraints(strut, c);
204: eastPanel.add(strut);
205:
206: gridBagLayout.setConstraints(removeButton, c);
207: eastPanel.add(removeButton);
208:
209: strut = Box.createRigidArea(new Dimension(30, 20));
210: gridBagLayout.setConstraints(strut, c);
211: eastPanel.add(strut);
212:
213: glue = Box.createVerticalGlue();
214: c.fill = GridBagConstraints.BOTH;
215: c.weighty = 1.0;
216: gridBagLayout.setConstraints(glue, c);
217: eastPanel.add(glue);
218:
219: listView = new AccountListTable(accountList, this );
220: listView.getSelectionModel().addListSelectionListener(this );
221: listView.addMouseListener(new DoubleClickListener() {
222: public void doubleClick(MouseEvent ev) {
223: actionPerformed(new ActionEvent(listView, 0, "EDIT"));
224: }
225: });
226: JScrollPane scrollPane = new JScrollPane(listView);
227: scrollPane.setPreferredSize(new Dimension(300, 250));
228: scrollPane.getViewport().setBackground(Color.white);
229: mainPanel.add(scrollPane);
230: getContentPane().add(mainPanel);
231:
232: JPanel bottomPanel = new JPanel(new BorderLayout());
233: bottomPanel.setBorder(new SingleSideEtchedBorder(
234: SwingConstants.TOP));
235:
236: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
237: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
238: 12, 12));
239:
240: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
241: MailResourceLoader.getString("global", "close"));
242: closeButton.setActionCommand("CLOSE");
243: closeButton.addActionListener(this );
244: buttonPanel.add(closeButton);
245:
246: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
247: MailResourceLoader.getString("global", "help"));
248: buttonPanel.add(helpButton);
249:
250: bottomPanel.add(buttonPanel, BorderLayout.EAST);
251: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
252: getRootPane().setDefaultButton(closeButton);
253:
254: // associate with JavaHelp
255: HelpManager.getInstance().enableHelpOnButton(helpButton,
256: "configuring_columba");
257: HelpManager.getInstance().enableHelpKey(getRootPane(),
258: "configuring_columba");
259: }
260:
261: public void valueChanged(ListSelectionEvent e) {
262: if (e.getValueIsAdjusting()) {
263: return;
264: }
265:
266: DefaultListSelectionModel theList = (DefaultListSelectionModel) e
267: .getSource();
268:
269: if (theList.isSelectionEmpty()) {
270: removeButton.setEnabled(false);
271: editButton.setEnabled(false);
272: } else {
273: removeButton.setEnabled(true);
274: editButton.setEnabled(true);
275:
276: // String value = (String) theList.getSelectedValue();
277: index = theList.getAnchorSelectionIndex();
278:
279: setSelected(accountList.get(index));
280: }
281: }
282:
283: protected void showAccountDialog() {
284: AccountItem parent = getSelected();
285:
286: if (parent != null) {
287: new AccountDialog(mediator, parent);
288: }
289: }
290:
291: public void actionPerformed(ActionEvent e) {
292: String action = e.getActionCommand();
293: if (action.equals("CLOSE")) {
294: try {
295: Config.getInstance().save();
296: } catch (Exception ex) {
297: ex.printStackTrace();
298: }
299:
300: setVisible(false);
301: } else if (action.equals("ADD")) {
302: try {
303: new AccountWizardLauncher().launchWizard(false);
304: listView.update();
305: } catch (Exception ex) {
306: ex.printStackTrace();
307: }
308: } else if (action.equals("REMOVE")) {
309: int n = JOptionPane.showConfirmDialog(this ,
310: MailResourceLoader.getString("dialog", "account",
311: "confirmDelete.msg"), MailResourceLoader
312: .getString("dialog", "account",
313: "confirmDelete.title"),
314: JOptionPane.YES_NO_OPTION,
315: JOptionPane.QUESTION_MESSAGE);
316:
317: if (n == JOptionPane.NO_OPTION) {
318: return;
319: }
320:
321: AccountItem item = accountList.remove(index);
322: if (item.isPopAccount()) {
323: POP3ServerCollection.getInstance().removePopServer(
324: item.getUid());
325: } else {
326: IMailFolder folder = (IMailFolder) FolderTreeModel
327: .getInstance().getImapFolder(item.getUid());
328: try {
329: IMailFolder parentFolder = (IMailFolder) folder
330: .getParent();
331: folder.removeFolder();
332: FolderTreeModel.getInstance().nodeStructureChanged(
333: parentFolder);
334: } catch (Exception ex) {
335: ex.printStackTrace();
336: }
337: }
338:
339: // remove mail-checking stuff
340: MailCheckingManager.getInstance().remove(item.getUid());
341:
342: // notify all observers
343: MailCheckingManager.getInstance().update();
344:
345: removeButton.setEnabled(false);
346: editButton.setEnabled(false);
347: listView.update();
348: } else if (action.equals("EDIT")) {
349: showAccountDialog();
350: listView.update();
351: }
352: }
353: }
|