001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.gui.config.export;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Dimension;
021: import java.awt.GridLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.KeyEvent;
025: import java.awt.event.MouseAdapter;
026: import java.awt.event.MouseEvent;
027: import java.io.File;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Vector;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.JButton;
035: import javax.swing.JComponent;
036: import javax.swing.JDialog;
037: import javax.swing.JFileChooser;
038: import javax.swing.JFrame;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.JTree;
043: import javax.swing.KeyStroke;
044: import javax.swing.SwingConstants;
045: import javax.swing.tree.DefaultTreeModel;
046: import javax.swing.tree.TreeNode;
047: import javax.swing.tree.TreePath;
048: import javax.swing.tree.TreeSelectionModel;
049:
050: import org.columba.core.command.CommandProcessor;
051: import org.columba.core.gui.base.ButtonWithMnemonic;
052: import org.columba.core.gui.base.SingleSideEtchedBorder;
053: import org.columba.core.gui.util.DialogHeaderPanel;
054: import org.columba.core.help.HelpManager;
055: import org.columba.mail.command.MailFolderCommandReference;
056: import org.columba.mail.folder.IMailFolder;
057: import org.columba.mail.folder.command.ExportFolderCommand;
058: import org.columba.mail.gui.tree.FolderTreeModel;
059: import org.columba.mail.gui.tree.util.FolderTreeCellRenderer;
060: import org.columba.mail.util.MailResourceLoader;
061: import org.frapuccino.checkabletree.CheckableItem;
062: import org.frapuccino.checkabletree.CheckableTree;
063:
064: import com.jgoodies.forms.layout.CellConstraints;
065: import com.jgoodies.forms.layout.FormLayout;
066:
067: /**
068: * ExportDialog lets you select a number of folders for exporting messages into
069: * the MBOX format.
070: *
071: * @author fdietz
072: */
073:
074: public class ExportDialog extends JDialog implements ActionListener {
075: private ButtonWithMnemonic exportButton;
076:
077: private ButtonWithMnemonic selectAllButton;
078:
079: private JTree tree;
080:
081: private JButton helpButton;
082:
083: private JButton closeButton;
084:
085: public ExportDialog(JFrame parent) {
086: super (parent, MailResourceLoader.getString("dialog", "export",
087: "dialog_title"), false);
088:
089: initComponents();
090:
091: pack();
092: setLocationRelativeTo(null);
093: setVisible(true);
094: }
095:
096: private void createChildNodes(CheckableTreeNode root,
097: IMailFolder parent) {
098: for (int i = 0; i < parent.getChildCount(); i++) {
099: IMailFolder child = (IMailFolder) parent.getChildAt(i);
100:
101: CheckableTreeNode c = new CheckableTreeNode(child.getName());
102: c.setIcon(FolderTreeCellRenderer
103: .getFolderIcon(child, false));
104: c.setNode(child);
105: root.add(c);
106:
107: createChildNodes(c, child);
108: }
109: }
110:
111: protected void initComponents() {
112: JPanel mainPanel = new JPanel(new BorderLayout());
113: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
114: 12));
115: getContentPane().add(mainPanel);
116:
117: exportButton = new ButtonWithMnemonic(MailResourceLoader
118: .getString("dialog", "export", "export"));
119: exportButton.setActionCommand("EXPORT");
120: exportButton.addActionListener(this );
121:
122: selectAllButton = new ButtonWithMnemonic(MailResourceLoader
123: .getString("dialog", "export", "select_all"));
124: selectAllButton.setActionCommand("SELECTALL");
125: selectAllButton.addActionListener(this );
126:
127: IMailFolder parent = (IMailFolder) FolderTreeModel
128: .getInstance().getRoot();
129: CheckableTreeNode root = new CheckableTreeNode(parent.getName());
130: root.setNode(parent);
131: createChildNodes(root, parent);
132:
133: tree = new CheckableTree(root);
134: tree.setRootVisible(false);
135:
136: tree.getSelectionModel().setSelectionMode(
137: TreeSelectionModel.SINGLE_TREE_SELECTION);
138: tree.addMouseListener(new NodeSelectionListener(tree));
139: tree.expandRow(0);
140: tree.expandRow(1);
141:
142: getContentPane().add(createPanel(), BorderLayout.CENTER);
143:
144: getContentPane().add(createBottomPanel(), BorderLayout.SOUTH);
145:
146: getContentPane().add(
147: new DialogHeaderPanel(MailResourceLoader.getString(
148: "dialog", "export", "header_title"),
149: MailResourceLoader.getString("dialog",
150: "export", "header_description")),
151: BorderLayout.NORTH);
152: getRootPane().setDefaultButton(closeButton);
153: getRootPane().registerKeyboardAction(this , "CLOSE",
154: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
155: JComponent.WHEN_IN_FOCUSED_WINDOW);
156:
157: // associate with JavaHelp
158: HelpManager.getInstance().enableHelpOnButton(helpButton,
159: "organising_and_managing_your_email_5");
160: HelpManager.getInstance().enableHelpKey(getRootPane(),
161: "organising_and_managing_your_email_5");
162: }
163:
164: private JPanel createPanel() {
165: JPanel jpanel1 = new JPanel();
166: FormLayout formlayout1 = new FormLayout(
167: "FILL:DEFAULT:GROW(1.0),3DLU,FILL:DEFAULT:NONE",
168: "CENTER:DEFAULT:NONE,1DLU,FILL:DEFAULT:GROW(1.0),3DLU,CENTER:DEFAULT:NONE");
169: CellConstraints cc = new CellConstraints();
170: jpanel1.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
171: 12));
172: jpanel1.setLayout(formlayout1);
173:
174: JLabel jlabel1 = new JLabel();
175: jlabel1.setText("Select Folders:");
176: jpanel1.add(jlabel1, cc.xy(1, 1));
177:
178: JScrollPane scrollPane = new JScrollPane(tree);
179: scrollPane.setPreferredSize(new Dimension(350, 250));
180: jpanel1.add(scrollPane, cc.xy(1, 3));
181:
182: jpanel1.add(createPanel1(), new CellConstraints(3, 3, 1, 1,
183: CellConstraints.DEFAULT, CellConstraints.TOP));
184:
185: return jpanel1;
186: }
187:
188: private JPanel createPanel1() {
189: JPanel jpanel1 = new JPanel();
190: FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE",
191: "CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE");
192: CellConstraints cc = new CellConstraints();
193: jpanel1.setLayout(formlayout1);
194:
195: jpanel1.add(exportButton, cc.xy(1, 1));
196:
197: jpanel1.add(selectAllButton, cc.xy(1, 3));
198:
199: return jpanel1;
200: }
201:
202: private JPanel createBottomPanel() {
203: JPanel bottomPanel = new JPanel(new BorderLayout());
204: bottomPanel.setBorder(new SingleSideEtchedBorder(
205: SwingConstants.TOP));
206:
207: JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 6, 0));
208: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
209: 12, 12));
210:
211: closeButton = new ButtonWithMnemonic(MailResourceLoader
212: .getString("global", "close"));
213: closeButton.setActionCommand("CLOSE"); //$NON-NLS-1$
214: closeButton.addActionListener(this );
215: buttonPanel.add(closeButton);
216:
217: helpButton = new ButtonWithMnemonic(MailResourceLoader
218: .getString("global", "help"));
219: buttonPanel.add(helpButton);
220: bottomPanel.add(buttonPanel, BorderLayout.EAST);
221:
222: return bottomPanel;
223: }
224:
225: private void getTreeNodeIteration(TreeNode parent, List l) {
226: l.add(parent);
227:
228: for (int i = 0; i < parent.getChildCount(); i++) {
229: l.add(parent.getChildAt(i));
230: getTreeNodeIteration((TreeNode) parent.getChildAt(i), l);
231: }
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
238: */
239: public void actionPerformed(ActionEvent e) {
240: String action = e.getActionCommand();
241:
242: if (action.equals("CLOSE")) {
243: setVisible(false);
244: } else if (action.equals("SELECTALL")) {
245: List list = new LinkedList();
246: getTreeNodeIteration((TreeNode) tree.getModel().getRoot(),
247: list);
248:
249: Iterator iterator = list.iterator();
250: CheckableTreeNode node;
251:
252: while (iterator.hasNext()) {
253: node = (CheckableTreeNode) iterator.next();
254: node.setSelected(true);
255: ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
256: }
257: } else if (action.equals("EXPORT")) {
258: File destFile = null;
259:
260: // ask the user about the destination file
261: JFileChooser chooser = new JFileChooser();
262: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
263: chooser.setMultiSelectionEnabled(false);
264:
265: int result = chooser.showSaveDialog(this );
266:
267: if (result == JFileChooser.APPROVE_OPTION) {
268: File file = chooser.getSelectedFile();
269:
270: destFile = file;
271: } else {
272: return;
273: }
274:
275: setVisible(false);
276:
277: // get list of all folders
278: List list = new LinkedList();
279: getTreeNodeIteration((TreeNode) tree.getModel().getRoot(),
280: list);
281:
282: Iterator it = list.iterator();
283:
284: Vector v = new Vector();
285:
286: // get list of all selected folders
287: while (it.hasNext()) {
288: CheckableItem node = (CheckableItem) it.next();
289:
290: boolean export = (boolean) node.isSelected();
291:
292: if (export) {
293: v.add(node);
294: }
295: }
296:
297: // create command reference array for the command
298: MailFolderCommandReference r = null;
299:
300: for (int i = 0; i < v.size(); i++) {
301: IMailFolder node = (IMailFolder) ((CheckableTreeNode) v
302: .get(i)).getNode();
303:
304: r = new MailFolderCommandReference(node);
305: r.setDestFile(destFile);
306:
307: // execute the command
308: CommandProcessor.getInstance().addOp(
309: new ExportFolderCommand(r));
310: }
311:
312: }
313: }
314:
315: class NodeSelectionListener extends MouseAdapter {
316: JTree tree;
317:
318: NodeSelectionListener(JTree tree) {
319: this .tree = tree;
320: }
321:
322: public void mouseClicked(MouseEvent e) {
323: int x = e.getX();
324: int y = e.getY();
325: int row = tree.getRowForLocation(x, y);
326: TreePath path = tree.getPathForRow(row);
327:
328: // TreePath path = tree.getSelectionPath();
329: if (path != null) {
330: CheckableItem node = (CheckableItem) path
331: .getLastPathComponent();
332:
333: node.setSelected(!node.isSelected());
334:
335: ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
336:
337: // I need revalidate if node is root. but why?
338: if (row == 0) {
339: tree.revalidate();
340: tree.repaint();
341: }
342: }
343: }
344: }
345: }
|