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.tree.util;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Dimension;
022: import java.awt.GridLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.KeyEvent;
026: import java.util.Comparator;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JButton;
030: import javax.swing.JComponent;
031: import javax.swing.JDialog;
032: import javax.swing.JFrame;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035: import javax.swing.JTree;
036: import javax.swing.KeyStroke;
037: import javax.swing.event.TreeSelectionEvent;
038: import javax.swing.event.TreeSelectionListener;
039: import javax.swing.tree.TreeModel;
040: import javax.swing.tree.TreePath;
041:
042: import org.columba.api.gui.frame.IFrameMediator;
043: import org.columba.core.command.CommandProcessor;
044: import org.columba.core.folder.api.IFolder;
045: import org.columba.core.gui.base.ButtonWithMnemonic;
046: import org.columba.mail.command.MailFolderCommandReference;
047: import org.columba.mail.folder.IMailFolder;
048: import org.columba.mail.gui.frame.TreeViewOwner;
049: import org.columba.mail.gui.tree.FolderTreeModel;
050: import org.columba.mail.gui.tree.ISelectFolderDialog;
051: import org.columba.mail.gui.tree.command.CreateAndSelectSubFolderCommand;
052: import org.columba.mail.util.MailResourceLoader;
053: import org.frapuccino.swing.SortedJTree;
054: import org.frapuccino.swing.SortedTreeModelDecorator;
055:
056: /**
057: * Select folder dialog.
058: *
059: * @author fdietz
060: */
061: public class SelectFolderDialog extends JDialog implements
062: ActionListener, TreeSelectionListener, ISelectFolderDialog {
063:
064: private boolean bool = false;
065:
066: protected JTree tree;
067:
068: protected JButton okButton;
069:
070: protected JButton newButton;
071:
072: protected IMailFolder selectedFolder;
073:
074: private ButtonWithMnemonic cancelButton;
075:
076: private IFrameMediator mediator;
077:
078: public SelectFolderDialog(JFrame parent) {
079: super (parent, true);
080:
081: setTitle(MailResourceLoader.getString("dialog", "folder",
082: "select_folder"));
083:
084: initComponents();
085:
086: layoutComponents();
087:
088: getRootPane().setDefaultButton(okButton);
089: getRootPane().registerKeyboardAction(this , "CANCEL",
090: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
091: JComponent.WHEN_IN_FOCUSED_WINDOW);
092: pack();
093: setLocationRelativeTo(null);
094: setVisible(true);
095: }
096:
097: public SelectFolderDialog(IFrameMediator mediator) {
098: super (mediator.getView().getFrame(), true);
099:
100: this .mediator = mediator;
101:
102: setTitle(MailResourceLoader.getString("dialog", "folder",
103: "select_folder"));
104:
105: initComponents();
106:
107: layoutComponents();
108:
109: getRootPane().setDefaultButton(okButton);
110: getRootPane().registerKeyboardAction(this , "CANCEL",
111: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
112: JComponent.WHEN_IN_FOCUSED_WINDOW);
113: pack();
114: setLocationRelativeTo(null);
115: setVisible(true);
116: }
117:
118: protected void layoutComponents() {
119: JPanel contentPane = (JPanel) getContentPane();
120: contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12,
121: 11, 11));
122:
123: JScrollPane scrollPane = new JScrollPane(tree);
124: scrollPane.setPreferredSize(new Dimension(150, 300));
125: contentPane.add(scrollPane);
126:
127: JPanel bottomPanel = new JPanel(new BorderLayout());
128: bottomPanel.setBorder(BorderFactory.createEmptyBorder(17, 0, 0,
129: 0));
130:
131: JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 5, 0));
132: buttonPanel.add(okButton);
133: buttonPanel.add(newButton);
134:
135: buttonPanel.add(cancelButton);
136: bottomPanel.add(buttonPanel, BorderLayout.EAST);
137: contentPane.add(bottomPanel, BorderLayout.SOUTH);
138: }
139:
140: protected void initComponents() {
141: // get global sorting state
142:
143: // bug #999969 (fdietz): classcast exception
144: // if mediator contains a JTree
145: if (mediator instanceof TreeViewOwner) {
146:
147: TreeModel t = ((TreeViewOwner) mediator)
148: .getTreeController().getModel();
149: // if mediator contains a sortable treemodel
150: if (t instanceof SortedTreeModelDecorator) {
151: // sorting is enabled
152: SortedTreeModelDecorator treemodel = (SortedTreeModelDecorator) t;
153: Comparator c = treemodel.getSortingComparator();
154:
155: tree = new SortedJTree(FolderTreeModel.getInstance());
156: // apply sorting state
157: SortedTreeModelDecorator m = (SortedTreeModelDecorator) tree
158: .getModel();
159: m.setSortingComparator(c);
160: } else {
161: // sorting is disabled
162: tree = new SortedJTree(FolderTreeModel.getInstance());
163: }
164: } else {
165: // sorting is disabled
166: tree = new SortedJTree(FolderTreeModel.getInstance());
167: }
168:
169: tree.expandRow(0);
170: tree.expandRow(1);
171: tree.putClientProperty("JTree.lineStyle", "Angled");
172: tree.setShowsRootHandles(true);
173: tree.setRootVisible(false);
174:
175: // default selection is local Inbox
176: selectedFolder = FolderTreeModel.getInstance().getFolder("101");
177: tree.setSelectionPath(new TreePath(selectedFolder.getPath()));
178:
179: // add selection listener
180: tree.addTreeSelectionListener(this );
181:
182: FolderTreeCellRenderer renderer = new FolderTreeCellRenderer();
183: tree.setCellRenderer(renderer);
184:
185: okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
186: "", "ok"));
187: okButton.setEnabled(true);
188: okButton.setActionCommand("OK");
189: okButton.addActionListener(this );
190:
191: newButton = new ButtonWithMnemonic(MailResourceLoader
192: .getString("dialog", "folder", "new_folder"));
193: newButton.setEnabled(true);
194: newButton.setActionCommand("NEW");
195: newButton.addActionListener(this );
196:
197: cancelButton = new ButtonWithMnemonic(MailResourceLoader
198: .getString("", "cancel"));
199: cancelButton.setActionCommand("CANCEL");
200: cancelButton.addActionListener(this );
201:
202: }
203:
204: public boolean success() {
205: return bool;
206: }
207:
208: public IFolder getSelectedFolder() {
209: return selectedFolder;
210: }
211:
212: public void actionPerformed(ActionEvent e) {
213: String action = e.getActionCommand();
214:
215: if (action.equals("OK")) {
216: // name = textField.getText();
217: bool = true;
218:
219: dispose();
220: } else if (action.equals("CANCEL")) {
221: bool = false;
222:
223: dispose();
224: } else if (action.equals("NEW")) {
225: CreateFolderDialog dialog = new CreateFolderDialog(
226: mediator, tree.getSelectionPath());
227:
228: String name;
229:
230: if (dialog.success()) {
231: // ok pressed
232: name = dialog.getName();
233: } else {
234: // cancel pressed
235: return;
236: }
237:
238: MailFolderCommandReference r = new MailFolderCommandReference(
239: dialog.getSelected());
240: r.setFolderName(name);
241:
242: CommandProcessor.getInstance().addOp(
243: new CreateAndSelectSubFolderCommand(tree, r));
244: }
245: }
246:
247: /**
248: * ***************************** tree selection listener
249: * *******************************
250: */
251: public void valueChanged(TreeSelectionEvent e) {
252: IMailFolder node = (IMailFolder) tree
253: .getLastSelectedPathComponent();
254:
255: if (node == null) {
256: return;
257: }
258:
259: selectedFolder = node;
260:
261: if (node.supportsAddMessage()) {
262: okButton.setEnabled(true);
263: } else {
264: okButton.setEnabled(false);
265: }
266: }
267: }
|