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.subscribe;
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:
031: import javax.swing.BorderFactory;
032: import javax.swing.Box;
033: import javax.swing.BoxLayout;
034: import javax.swing.JButton;
035: import javax.swing.JComponent;
036: import javax.swing.JDialog;
037: import javax.swing.JFrame;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollPane;
040: import javax.swing.JTree;
041: import javax.swing.KeyStroke;
042: import javax.swing.SwingConstants;
043: import javax.swing.event.TreeSelectionEvent;
044: import javax.swing.event.TreeSelectionListener;
045: import javax.swing.tree.DefaultTreeModel;
046:
047: import org.columba.core.command.Command;
048: import org.columba.core.command.CommandProcessor;
049: import org.columba.core.gui.base.ButtonWithMnemonic;
050: import org.columba.core.gui.base.SingleSideEtchedBorder;
051: import org.columba.core.help.HelpManager;
052: import org.columba.mail.command.MailFolderCommandReference;
053: import org.columba.mail.folder.imap.IMAPRootFolder;
054: import org.columba.mail.gui.config.filter.FilterTransferHandler;
055: import org.columba.mail.imap.FetchSubFolderListCommand;
056: import org.columba.mail.util.MailResourceLoader;
057: import org.frapuccino.checkabletree.CheckableTree;
058:
059: /**
060: * Subscribe dialog used by IMAP accounts.
061: *
062: * @author fdietz
063: */
064: public class SubscribeDialog extends JDialog implements ActionListener,
065: TreeSelectionListener {
066: private JButton subscribeButton;
067:
068: private JButton syncButton;
069:
070: private JButton unsubscribeButton;
071:
072: private JTree tree;
073:
074: private ListInfoTreeNode selection;
075:
076: private IMAPRootFolder root;
077:
078: private DefaultTreeModel treeModel;
079:
080: public SubscribeDialog(JFrame parent, IMAPRootFolder rootFolder) {
081: super (parent, true);
082:
083: setTitle(MailResourceLoader.getString("dialog", "subscribe",
084: "dialog_title"));
085: root = rootFolder;
086:
087: initComponents();
088: pack();
089: setLocationRelativeTo(null);
090:
091: syncFolderList();
092:
093: setVisible(true);
094: }
095:
096: private void syncFolderList() {
097: setEnabled(false);
098:
099: Command c = new SynchronizeFolderListCommand(
100: new SubscribeCommandReference(root, this ));
101: CommandProcessor.getInstance().addOp(c);
102: }
103:
104: private void subscribe() {
105: setEnabled(false);
106:
107: Command c = new SubscribeFolderCommand(
108: new SubscribeCommandReference(root, this , selection
109: .getMailbox()));
110: CommandProcessor.getInstance().addOp(c);
111: }
112:
113: private void unsubscribe() {
114: setEnabled(false);
115:
116: Command c = new UnsubscribeFolderCommand(
117: new SubscribeCommandReference(root, this , selection
118: .getMailbox()));
119: CommandProcessor.getInstance().addOp(c);
120: }
121:
122: /**
123: * Inits the GUI components.
124: */
125: private void initComponents() {
126: JPanel mainPanel = new JPanel();
127: mainPanel.setLayout(new BorderLayout());
128: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
129: 12));
130: getContentPane().add(mainPanel);
131:
132: subscribeButton = new ButtonWithMnemonic(MailResourceLoader
133: .getString("dialog", "subscribe", "subscribe"));
134: subscribeButton.setActionCommand("SUBSCRIBE");
135: subscribeButton.addActionListener(this );
136: subscribeButton.setEnabled(false);
137:
138: syncButton = new ButtonWithMnemonic(MailResourceLoader
139: .getString("dialog", "subscribe", "sync"));
140: syncButton.setActionCommand("SYNC");
141: syncButton.setEnabled(false);
142: syncButton.addActionListener(this );
143:
144: unsubscribeButton = new ButtonWithMnemonic(MailResourceLoader
145: .getString("dialog", "subscribe", "unsubscribe"));
146: unsubscribeButton.setActionCommand("UNSUBSCRIBE");
147: unsubscribeButton.setEnabled(false);
148: unsubscribeButton.addActionListener(this );
149:
150: // top panel
151: JPanel topPanel = new JPanel();
152: topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
153:
154: GridBagLayout gridBagLayout = new GridBagLayout();
155: GridBagConstraints c = new GridBagConstraints();
156:
157: // topPanel.setLayout( );
158: JPanel topBorderPanel = new JPanel();
159: topBorderPanel.setLayout(new BorderLayout());
160:
161: Component glue = Box.createVerticalGlue();
162: c.anchor = GridBagConstraints.EAST;
163: c.gridwidth = GridBagConstraints.REMAINDER;
164:
165: // c.fill = GridBagConstraints.HORIZONTAL;
166: gridBagLayout.setConstraints(glue, c);
167:
168: gridBagLayout = new GridBagLayout();
169: c = new GridBagConstraints();
170:
171: JPanel eastPanel = new JPanel(gridBagLayout);
172: mainPanel.add(eastPanel, BorderLayout.EAST);
173:
174: c.fill = GridBagConstraints.HORIZONTAL;
175: c.weightx = 1.0;
176: c.gridwidth = GridBagConstraints.REMAINDER;
177: gridBagLayout.setConstraints(subscribeButton, c);
178: eastPanel.add(subscribeButton);
179:
180: Component strut1 = Box.createRigidArea(new Dimension(30, 6));
181: gridBagLayout.setConstraints(strut1, c);
182: eastPanel.add(strut1);
183:
184: gridBagLayout.setConstraints(unsubscribeButton, c);
185: eastPanel.add(unsubscribeButton);
186:
187: Component strut = Box.createRigidArea(new Dimension(30, 12));
188: gridBagLayout.setConstraints(strut, c);
189: eastPanel.add(strut);
190:
191: gridBagLayout.setConstraints(syncButton, c);
192: eastPanel.add(syncButton);
193:
194: glue = Box.createVerticalGlue();
195: c.fill = GridBagConstraints.BOTH;
196: c.weighty = 1.0;
197: gridBagLayout.setConstraints(glue, c);
198: eastPanel.add(glue);
199:
200: // centerpanel
201: JPanel centerPanel = new JPanel(new BorderLayout());
202: centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
203: 6));
204: tree = new CheckableTree();
205: tree.addTreeSelectionListener(this );
206: tree.setRootVisible(false);
207:
208: JScrollPane scrollPane = new JScrollPane(tree);
209: scrollPane.setPreferredSize(new Dimension(300, 250));
210: scrollPane.getViewport().setBackground(Color.white);
211: scrollPane.setTransferHandler(new FilterTransferHandler());
212: centerPanel.add(scrollPane);
213:
214: mainPanel.add(centerPanel);
215:
216: JPanel bottomPanel = new JPanel(new BorderLayout());
217: bottomPanel.setBorder(new SingleSideEtchedBorder(
218: SwingConstants.TOP));
219:
220: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 6, 0));
221: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
222: 12, 12));
223:
224: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
225: MailResourceLoader.getString("global", "close"));
226: closeButton.setActionCommand("CLOSE"); //$NON-NLS-1$
227: closeButton.addActionListener(this );
228: buttonPanel.add(closeButton);
229:
230: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
231: MailResourceLoader.getString("global", "help"));
232: buttonPanel.add(helpButton);
233: bottomPanel.add(buttonPanel, BorderLayout.EAST);
234: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
235: getRootPane().setDefaultButton(closeButton);
236: getRootPane().registerKeyboardAction(this , "CLOSE",
237: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
238: JComponent.WHEN_IN_FOCUSED_WINDOW);
239:
240: // associate with JavaHelp
241: HelpManager.getInstance().enableHelpOnButton(helpButton,
242: "organising_and_managing_your_email_3");
243: HelpManager.getInstance().enableHelpKey(getRootPane(),
244: "organising_and_managing_your_email_3");
245: }
246:
247: public void actionPerformed(ActionEvent e) {
248: String action = e.getActionCommand();
249:
250: if (action.equals("CLOSE")) {
251: syncAndExit();
252: } else if (action.equals("SUBSCRIBE")) {
253: subscribe();
254: } else if (action.equals("UNSUBSCRIBE")) {
255: unsubscribe();
256: } else if (action.equals("SYNC")) {
257: syncFolderList();
258: }
259: }
260:
261: private void syncAndExit() {
262: setEnabled(false);
263:
264: Command c = new FetchSubFolderListCommand(
265: new MailFolderCommandReference(root));
266: CommandProcessor.getInstance().addOp(c);
267:
268: setVisible(false);
269: }
270:
271: /** ********************** TreeSelectionListener ******************** */
272: /**
273: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
274: */
275: public void valueChanged(TreeSelectionEvent arg0) {
276: selection = (ListInfoTreeNode) arg0.getPath()
277: .getLastPathComponent();
278:
279: updateButtons();
280: }
281:
282: /**
283: *
284: */
285: private void updateButtons() {
286: if (selection != null) {
287: subscribeButton.setEnabled(!selection.isSelected());
288: unsubscribeButton.setEnabled(selection.isSelected());
289: } else {
290: subscribeButton.setEnabled(false);
291: unsubscribeButton.setEnabled(false);
292: }
293:
294: syncButton.setEnabled(true);
295: tree.setEnabled(true);
296: }
297:
298: /**
299: * @param model
300: */
301: public void syncFolderListDone(DefaultTreeModel model) {
302: tree.setModel(model);
303: tree.setRootVisible(true);
304: tree.expandRow(0);
305:
306: treeModel = model;
307:
308: updateButtons();
309: }
310:
311: public void setEnabled(boolean value) {
312: subscribeButton.setEnabled(value);
313: unsubscribeButton.setEnabled(value);
314: syncButton.setEnabled(value);
315:
316: tree.setEnabled(value);
317: }
318:
319: /**
320: *
321: */
322: public void subscribeDone() {
323: selection.setSelected(true);
324: treeModel.nodeChanged(selection);
325:
326: updateButtons();
327: }
328:
329: /**
330: *
331: */
332: public void unsubscribeDone() {
333: selection.setSelected(false);
334: treeModel.nodeChanged(selection);
335:
336: updateButtons();
337: }
338: }
|