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.account;
019:
020: import java.awt.Component;
021: import java.awt.Font;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.Box;
030: import javax.swing.JButton;
031: import javax.swing.JCheckBox;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.border.Border;
035:
036: import org.columba.api.gui.frame.IFrameMediator;
037: import org.columba.core.gui.base.CheckBoxWithMnemonic;
038: import org.columba.core.gui.base.LabelWithMnemonic;
039: import org.columba.mail.config.AccountItem;
040: import org.columba.mail.config.MailConfig;
041: import org.columba.mail.config.SpecialFoldersItem;
042: import org.columba.mail.folder.IMailFolder;
043: import org.columba.mail.folder.IMailbox;
044: import org.columba.mail.gui.tree.FolderTreeModel;
045: import org.columba.mail.gui.tree.util.SelectFolderDialog;
046: import org.columba.mail.gui.tree.util.TreeNodeList;
047: import org.columba.mail.util.MailResourceLoader;
048:
049: public class SpecialFoldersPanel extends DefaultPanel implements
050: ActionListener {
051: private JLabel trashLabel;
052:
053: private JButton trashButton;
054:
055: private JLabel draftsLabel;
056:
057: private JButton draftsButton;
058:
059: private JLabel templatesLabel;
060:
061: private JButton templatesButton;
062:
063: private JLabel sentLabel;
064:
065: private JButton sentButton;
066:
067: private JLabel inboxLabel;
068:
069: private JButton inboxButton;
070:
071: private JCheckBox defaultAccountCheckBox;
072:
073: private SpecialFoldersItem item;
074:
075: private AccountItem accountItem;
076:
077: private IFrameMediator mediator;
078:
079: public SpecialFoldersPanel(IFrameMediator mediator,
080: AccountItem accountItem, SpecialFoldersItem item) {
081: super ();
082:
083: this .mediator = mediator;
084: this .item = item;
085: this .accountItem = accountItem;
086:
087: initComponents();
088:
089: updateComponents(true);
090: }
091:
092: protected String getPath(String uid) {
093:
094: IMailbox f = (IMailbox) FolderTreeModel.getInstance()
095: .getFolder(uid);
096:
097: if (f == null) {
098: return ""; //$NON-NLS-1$
099: }
100:
101: return f.getTreePath();
102: }
103:
104: protected String getUid(String treePath) {
105: TreeNodeList list = new TreeNodeList(treePath);
106: IMailbox f = (IMailbox) FolderTreeModel.getInstance()
107: .getFolder(list);
108:
109: if (f == null) {
110: return ""; //$NON-NLS-1$
111: }
112:
113: Integer i = new Integer(f.getId());
114:
115: return i.toString();
116: }
117:
118: protected boolean isPopAccount() {
119: return accountItem.isPopAccount();
120: }
121:
122: protected void updateComponents(boolean b) {
123: if (b) {
124: if (!isPopAccount()) {
125: trashButton.setText(getPath(item
126: .get(SpecialFoldersItem.TRASH)));
127: }
128:
129: draftsButton.setText(getPath(item
130: .get(SpecialFoldersItem.DRAFTS)));
131: templatesButton.setText(getPath(item
132: .get(SpecialFoldersItem.TEMPLATES)));
133: sentButton.setText(getPath(item
134: .get(SpecialFoldersItem.SENT)));
135:
136: if (isPopAccount()) {
137: inboxButton.setText(getPath(item
138: .get(SpecialFoldersItem.INBOX)));
139: }
140:
141: defaultAccountCheckBox
142: .setSelected(item
143: .getBoolean(SpecialFoldersItem.USE_DEFAULT_ACCOUNT));
144:
145: defaultAccountCheckBox
146: .setEnabled(MailConfig.getInstance()
147: .getAccountList().getDefaultAccountUid() == accountItem
148: .getInteger(SpecialFoldersItem.UID));
149:
150: if (defaultAccountCheckBox.isEnabled()
151: && defaultAccountCheckBox.isSelected()) {
152: showDefaultAccountWarning();
153: } else {
154: layoutComponents();
155: }
156: } else {
157: if (!isPopAccount()) {
158: item.setString(SpecialFoldersItem.TRASH,
159: getUid(trashButton.getText()));
160: }
161:
162: item.setString(SpecialFoldersItem.DRAFTS,
163: getUid(draftsButton.getText()));
164: item.setString(SpecialFoldersItem.TEMPLATES,
165: getUid(templatesButton.getText()));
166: item.setString(SpecialFoldersItem.SENT, getUid(sentButton
167: .getText()));
168:
169: if (isPopAccount()) {
170: item.setString(SpecialFoldersItem.INBOX,
171: getUid(inboxButton.getText()));
172: }
173:
174: item.setBoolean(SpecialFoldersItem.USE_DEFAULT_ACCOUNT,
175: defaultAccountCheckBox.isSelected());
176: }
177: }
178:
179: protected void layoutComponents() {
180: setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
181:
182: GridBagLayout mainLayout = new GridBagLayout();
183: GridBagConstraints mainConstraints = new GridBagConstraints();
184:
185: mainConstraints.anchor = GridBagConstraints.NORTHWEST;
186: mainConstraints.fill = GridBagConstraints.HORIZONTAL;
187: mainConstraints.weightx = 1.0;
188:
189: setLayout(mainLayout);
190:
191: mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
192: mainConstraints.insets = new Insets(0, 10, 5, 0);
193: mainLayout.setConstraints(defaultAccountCheckBox,
194: mainConstraints);
195: add(defaultAccountCheckBox);
196:
197: JPanel folderPanel = new JPanel();
198: Border b1 = BorderFactory.createEtchedBorder();
199: Border b2 = BorderFactory.createTitledBorder(b1,
200: MailResourceLoader.getString("dialog", "account",
201: "account_information"));
202:
203: Border emptyBorder = BorderFactory
204: .createEmptyBorder(5, 5, 5, 5);
205: Border border = BorderFactory.createCompoundBorder(b2,
206: emptyBorder);
207: folderPanel.setBorder(border);
208:
209: GridBagLayout layout = new GridBagLayout();
210: GridBagConstraints c = new GridBagConstraints();
211: folderPanel.setLayout(layout);
212:
213: c.fill = GridBagConstraints.HORIZONTAL;
214: c.anchor = GridBagConstraints.WEST;
215:
216: if (isPopAccount()) {
217: c.weightx = 0.1;
218: c.gridwidth = GridBagConstraints.RELATIVE;
219: layout.setConstraints(inboxLabel, c);
220: folderPanel.add(inboxLabel);
221:
222: c.gridwidth = GridBagConstraints.REMAINDER;
223: c.weightx = 0.9;
224: layout.setConstraints(inboxButton, c);
225: folderPanel.add(inboxButton);
226: }
227:
228: c.weightx = 0.1;
229: c.gridwidth = GridBagConstraints.RELATIVE;
230: layout.setConstraints(draftsLabel, c);
231: folderPanel.add(draftsLabel);
232: c.gridwidth = GridBagConstraints.REMAINDER;
233: c.weightx = 0.9;
234: layout.setConstraints(draftsButton, c);
235: folderPanel.add(draftsButton);
236:
237: c.weightx = 0.1;
238: c.gridwidth = GridBagConstraints.RELATIVE;
239: layout.setConstraints(templatesLabel, c);
240: folderPanel.add(templatesLabel);
241: c.gridwidth = GridBagConstraints.REMAINDER;
242: c.weightx = 0.9;
243: layout.setConstraints(templatesButton, c);
244: folderPanel.add(templatesButton);
245:
246: c.weightx = 0.1;
247: c.gridwidth = GridBagConstraints.RELATIVE;
248: layout.setConstraints(sentLabel, c);
249: folderPanel.add(sentLabel);
250: c.gridwidth = GridBagConstraints.REMAINDER;
251: c.weightx = 0.9;
252: layout.setConstraints(sentButton, c);
253: folderPanel.add(sentButton);
254:
255: if (!isPopAccount()) {
256: c.weightx = 0.1;
257: c.gridwidth = GridBagConstraints.RELATIVE;
258: layout.setConstraints(trashLabel, c);
259: folderPanel.add(trashLabel);
260: c.gridwidth = GridBagConstraints.REMAINDER;
261: c.weightx = 0.9;
262: layout.setConstraints(trashButton, c);
263: folderPanel.add(trashButton);
264: }
265:
266: mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
267: mainConstraints.insets = new Insets(0, 0, 0, 0);
268: mainLayout.setConstraints(folderPanel, mainConstraints);
269: add(folderPanel);
270:
271: mainConstraints.gridheight = GridBagConstraints.REMAINDER;
272: mainConstraints.weighty = 1.0;
273: mainConstraints.fill = GridBagConstraints.VERTICAL;
274:
275: Component vglue = Box.createVerticalGlue();
276: mainLayout.setConstraints(vglue, mainConstraints);
277: add(vglue);
278: }
279:
280: protected void showDefaultAccountWarning() {
281: setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
282:
283: GridBagLayout mainLayout = new GridBagLayout();
284: GridBagConstraints mainConstraints = new GridBagConstraints();
285:
286: setLayout(mainLayout);
287:
288: mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
289: mainConstraints.anchor = GridBagConstraints.NORTHWEST;
290: mainConstraints.weightx = 1.0;
291: mainConstraints.insets = new Insets(0, 10, 5, 0);
292: mainLayout.setConstraints(defaultAccountCheckBox,
293: mainConstraints);
294: add(defaultAccountCheckBox);
295:
296: mainConstraints = new GridBagConstraints();
297: mainConstraints.weighty = 1.0;
298: mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
299:
300: /*
301: * mainConstraints.fill = GridBagConstraints.BOTH;
302: * mainConstraints.insets = new Insets(0, 0, 0, 0);
303: * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
304: * mainConstraints.weightx = 1.0; mainConstraints.weighty = 1.0;
305: */
306: JLabel label = new JLabel(MailResourceLoader.getString(
307: "dialog", "account", "using_default_account_settings"));
308: Font newFont = label.getFont().deriveFont(Font.BOLD);
309: label.setFont(newFont);
310: mainLayout.setConstraints(label, mainConstraints);
311: add(label);
312: }
313:
314: protected void initComponents() {
315: defaultAccountCheckBox = new CheckBoxWithMnemonic(
316: MailResourceLoader.getString("dialog", "account",
317: "use_default_account_settings"));
318:
319: // defaultAccountCheckBox.setEnabled(false);
320: defaultAccountCheckBox.setActionCommand("DEFAULT_ACCOUNT");
321: defaultAccountCheckBox.addActionListener(this );
322:
323: if (isPopAccount()) {
324: inboxLabel = new LabelWithMnemonic(MailResourceLoader
325: .getString("dialog", "account", "inbox_folder")); //$NON-NLS-1$
326: inboxButton = new JButton();
327: inboxButton.setActionCommand("INBOX"); //$NON-NLS-1$
328: inboxButton.addActionListener(this );
329: inboxLabel.setLabelFor(inboxButton);
330: }
331:
332: draftsLabel = new LabelWithMnemonic(MailResourceLoader
333: .getString("dialog", "account", "drafts_folder")); //$NON-NLS-1$
334: draftsButton = new JButton();
335: draftsButton.setActionCommand("DRAFTS"); //$NON-NLS-1$
336: draftsButton.addActionListener(this );
337: draftsLabel.setLabelFor(draftsButton);
338:
339: templatesLabel = new LabelWithMnemonic(MailResourceLoader
340: .getString("dialog", "account", "templates_folder")); //$NON-NLS-1$
341: templatesButton = new JButton();
342: templatesButton.setActionCommand("TEMPLATES"); //$NON-NLS-1$
343: templatesButton.addActionListener(this );
344: templatesLabel.setLabelFor(templatesButton);
345:
346: sentLabel = new LabelWithMnemonic(MailResourceLoader.getString(
347: "dialog", "account", "sent_folder")); //$NON-NLS-1$
348: sentButton = new JButton();
349: sentButton.setActionCommand("SENT"); //$NON-NLS-1$
350: sentButton.addActionListener(this );
351: sentLabel.setLabelFor(sentButton);
352:
353: if (!isPopAccount()) {
354: trashLabel = new LabelWithMnemonic(MailResourceLoader
355: .getString("dialog", "account", "trash_folder")); //$NON-NLS-1$
356: trashButton = new JButton();
357: trashButton.setActionCommand("TRASH"); //$NON-NLS-1$
358: trashButton.addActionListener(this );
359: trashLabel.setLabelFor(trashButton);
360: }
361: }
362:
363: public void actionPerformed(ActionEvent e) {
364: String action = e.getActionCommand();
365:
366: if (action.equals("TRASH")) //$NON-NLS-1$
367: {
368: SelectFolderDialog dialog = new SelectFolderDialog(mediator);
369:
370: if (dialog.success()) {
371: IMailFolder selectedFolder = (IMailFolder) dialog
372: .getSelectedFolder();
373: String path = selectedFolder.getTreePath();
374:
375: trashButton.setText(path);
376:
377: // int uid = selectedFolder.getUid();
378: // item.setTrash( new Integer(uid).toString() );
379: }
380: } else if (action.equals("INBOX")) //$NON-NLS-1$
381: {
382: SelectFolderDialog dialog = new SelectFolderDialog(mediator);
383:
384: if (dialog.success()) {
385: IMailFolder selectedFolder = (IMailFolder) dialog
386: .getSelectedFolder();
387: String path = selectedFolder.getTreePath();
388:
389: inboxButton.setText(path);
390:
391: // int uid = selectedFolder.getUid();
392: // item.setInbox( new Integer(uid).toString() );
393: }
394: } else if (action.equals("DRAFTS")) //$NON-NLS-1$
395: {
396: SelectFolderDialog dialog = new SelectFolderDialog(mediator);
397:
398: if (dialog.success()) {
399: IMailFolder selectedFolder = (IMailFolder) dialog
400: .getSelectedFolder();
401: String path = selectedFolder.getTreePath();
402:
403: draftsButton.setText(path);
404:
405: // int uid = selectedFolder.getUid();
406: // item.setDrafts( new Integer(uid).toString() );
407: }
408: } else if (action.equals("TEMPLATES")) //$NON-NLS-1$
409: {
410: SelectFolderDialog dialog = new SelectFolderDialog(mediator);
411:
412: if (dialog.success()) {
413: IMailFolder selectedFolder = (IMailFolder) dialog
414: .getSelectedFolder();
415: String path = selectedFolder.getTreePath();
416:
417: templatesButton.setText(path);
418:
419: // int uid = selectedFolder.getUid();
420: // item.setTemplates( new Integer(uid).toString() );
421: }
422: } else if (action.equals("SENT")) //$NON-NLS-1$
423: {
424: SelectFolderDialog dialog = new SelectFolderDialog(mediator);
425:
426: if (dialog.success()) {
427: IMailFolder selectedFolder = (IMailFolder) dialog
428: .getSelectedFolder();
429: String path = selectedFolder.getTreePath();
430:
431: sentButton.setText(path);
432:
433: // int uid = selectedFolder.getUid();
434: // item.setSent( new Integer(uid).toString() );
435: }
436: } else if (action.equals("DEFAULT_ACCOUNT")) {
437: removeAll();
438:
439: if (defaultAccountCheckBox.isSelected()) {
440: showDefaultAccountWarning();
441: } else {
442: layoutComponents();
443: }
444:
445: revalidate();
446: }
447: }
448:
449: public boolean isFinished() {
450: boolean result = true;
451:
452: return result;
453: }
454: }
|