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.Container;
022: import java.awt.Dimension;
023: import java.awt.GridLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.KeyEvent;
027: import java.util.Comparator;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.DefaultComboBoxModel;
033: import javax.swing.JButton;
034: import javax.swing.JComboBox;
035: import javax.swing.JComponent;
036: import javax.swing.JDialog;
037: import javax.swing.JLabel;
038: import javax.swing.JOptionPane;
039: import javax.swing.JPanel;
040: import javax.swing.JScrollPane;
041: import javax.swing.JTextField;
042: import javax.swing.JTree;
043: import javax.swing.KeyStroke;
044: import javax.swing.SwingConstants;
045: import javax.swing.event.TreeSelectionEvent;
046: import javax.swing.event.TreeSelectionListener;
047: import javax.swing.tree.TreeModel;
048: import javax.swing.tree.TreePath;
049: import javax.swing.tree.TreeSelectionModel;
050:
051: import org.columba.api.gui.frame.IFrameMediator;
052: import org.columba.core.gui.base.ButtonWithMnemonic;
053: import org.columba.core.gui.base.SingleSideEtchedBorder;
054: import org.columba.mail.folder.FolderFactory;
055: import org.columba.mail.folder.IMailFolder;
056: import org.columba.mail.gui.frame.TreeViewOwner;
057: import org.columba.mail.gui.tree.FolderTreeModel;
058: import org.columba.mail.util.MailResourceLoader;
059: import org.frapuccino.swing.SortedJTree;
060: import org.frapuccino.swing.SortedTreeModelDecorator;
061:
062: import com.jgoodies.forms.builder.DefaultFormBuilder;
063: import com.jgoodies.forms.layout.FormLayout;
064:
065: public class CreateFolderDialog extends JDialog implements
066: ActionListener {
067: protected boolean bool = false;
068:
069: protected JTextField textField;
070:
071: protected JButton okButton;
072:
073: protected JButton cancelButton;
074:
075: protected JButton helpButton;
076:
077: protected JComboBox typeBox;
078:
079: protected JTree tree;
080:
081: protected String name;
082:
083: protected TreePath selected;
084:
085: protected IFrameMediator mediator;
086:
087: public CreateFolderDialog(IFrameMediator mediator, TreePath selected) {
088: super (mediator.getView().getFrame(), MailResourceLoader
089: .getString("dialog", "folder", "edit_name"), true);
090:
091: this .mediator = mediator;
092:
093: name = MailResourceLoader.getString("dialog", "folder",
094: "new_folder_name");
095:
096: this .selected = selected;
097:
098: initComponents();
099: layoutComponents();
100: // try to set selection
101: if (selected != null) {
102: tree.setSelectionPath(selected);
103: }
104:
105: pack();
106: setLocationRelativeTo(null);
107: setVisible(true);
108: }
109:
110: protected void layoutComponents() {
111: Container contentPane = getContentPane();
112: contentPane.setLayout(new BorderLayout());
113:
114: // layout center panel
115: FormLayout layout = new FormLayout(
116: "left:max(20dlu;pref), 3dlu, 80dlu:grow", "");
117:
118: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
119:
120: // create EmptyBorder between components and dialog-frame
121: builder.setDefaultDialogBorder();
122:
123: // skip the first column
124: // builder.setLeadingColumnOffset(1);
125: // Add components to the panel:
126: builder.append(new JLabel(MailResourceLoader.getString(
127: "dialog", "folder", "name")));
128: builder.append(textField);
129: builder.nextLine();
130:
131: builder.append(new JLabel("Type:"));
132: builder.append(typeBox);
133:
134: builder.appendRow("3dlu");
135: builder.appendRow("fill:d:grow");
136: builder.nextLine(2);
137:
138: JScrollPane scrollPane = new JScrollPane(tree);
139: scrollPane.setPreferredSize(new Dimension(200, 300));
140: builder.append(scrollPane, 3);
141:
142: contentPane.add(builder.getPanel(), BorderLayout.CENTER);
143:
144: // init bottom panel with OK, Cancel buttons
145: JPanel bottomPanel = new JPanel(new BorderLayout(0, 0));
146: bottomPanel.setBorder(new SingleSideEtchedBorder(
147: SwingConstants.TOP));
148:
149: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 0));
150: buttonPanel.setBorder(BorderFactory.createEmptyBorder(11, 11,
151: 11, 11));
152:
153: buttonPanel.add(okButton);
154:
155: buttonPanel.add(cancelButton);
156: bottomPanel.add(buttonPanel, BorderLayout.EAST);
157: contentPane.add(bottomPanel, BorderLayout.SOUTH);
158:
159: getRootPane().setDefaultButton(okButton);
160: getRootPane().registerKeyboardAction(this , "CANCEL",
161: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
162: JComponent.WHEN_IN_FOCUSED_WINDOW);
163: }
164:
165: protected void initComponents() {
166: // Init components
167: textField = new JTextField(name, 15);
168: textField.setSelectionStart(0);
169: textField.setSelectionEnd(name.length());
170:
171: typeBox = new JComboBox();
172:
173: // get global sorting state
174: TreeModel t = ((TreeViewOwner) mediator).getTreeController()
175: .getModel();
176:
177: if (t instanceof SortedTreeModelDecorator) {
178: // sorting is enabled
179: SortedTreeModelDecorator treemodel = (SortedTreeModelDecorator) t;
180: Comparator c = treemodel.getSortingComparator();
181:
182: tree = new SortedJTree(FolderTreeModel.getInstance());
183: // apply sorting state
184: SortedTreeModelDecorator m = (SortedTreeModelDecorator) tree
185: .getModel();
186: m.setSortingComparator(c);
187: } else {
188: // sorting is disabled
189: tree = new SortedJTree(FolderTreeModel.getInstance());
190: }
191:
192: tree.setCellRenderer(new FolderTreeCellRenderer());
193: tree.setRootVisible(false);
194: tree.getSelectionModel().setSelectionMode(
195: TreeSelectionModel.SINGLE_TREE_SELECTION);
196: tree.expandRow(0);
197: tree.expandRow(1);
198:
199: tree.addTreeSelectionListener(new TreeSelectionListener() {
200: public void valueChanged(TreeSelectionEvent arg0) {
201: selectionChanged();
202: }
203: });
204:
205: // button panel
206: okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
207: "global", "ok"));
208: okButton.setActionCommand("OK");
209: okButton.addActionListener(this );
210: cancelButton = new ButtonWithMnemonic(MailResourceLoader
211: .getString("global", "cancel"));
212: cancelButton.setActionCommand("CANCEL");
213: cancelButton.addActionListener(this );
214: helpButton = new ButtonWithMnemonic(MailResourceLoader
215: .getString("global", "help"));
216: helpButton.setActionCommand("HELP");
217: helpButton.addActionListener(this );
218:
219: }
220:
221: protected void selectionChanged() {
222: IMailFolder selected = getSelected();
223: if (selected != null) {
224: List childs = FolderFactory.getInstance()
225: .getPossibleChilds(selected);
226: Iterator it = childs.iterator();
227: while (it.hasNext()) {
228: String type = (String) it.next();
229: if (!selected.supportsAddFolder(type)) {
230: it.remove();
231: }
232:
233: // We have a special Command for VFolders
234: if (type.equals("VirtualFolder")) {
235: it.remove();
236: }
237: }
238:
239: if (childs.size() > 0) {
240: typeBox.setModel(new DefaultComboBoxModel(childs
241: .toArray()));
242:
243: okButton.setEnabled(true);
244: typeBox.setEnabled(true);
245: } else {
246: okButton.setEnabled(false);
247: typeBox.setEnabled(false);
248: }
249: } else {
250: okButton.setEnabled(false);
251: typeBox.setEnabled(false);
252: }
253: }
254:
255: public String getName() {
256: return name;
257: }
258:
259: public IMailFolder getSelected() {
260: if (tree.getSelectionPath() == null)
261: return null;
262:
263: return (IMailFolder) tree.getSelectionPath()
264: .getLastPathComponent();
265: }
266:
267: public boolean success() {
268: return bool;
269: }
270:
271: public void actionPerformed(ActionEvent e) {
272: String action = e.getActionCommand();
273:
274: if (action.equals("OK")) {
275: name = textField.getText().trim();
276:
277: // fixing bug with id 553176
278: if (name.indexOf('/') != -1) {
279: // if the character / is found shows the user a error message
280: JOptionPane.showMessageDialog(this , MailResourceLoader
281: .getString("dialog", "folder",
282: "error_char_text"), MailResourceLoader
283: .getString("dialog", "folder", "error_title"),
284: JOptionPane.ERROR_MESSAGE);
285:
286: return;
287: }
288:
289: bool = true;
290: setVisible(false);
291: } else if (action.equals("CANCEL")) {
292: bool = false;
293:
294: dispose();
295: }
296: }
297:
298: /**
299: * @return
300: */
301: public String getType() {
302: return (String) typeBox.getSelectedItem();
303: }
304: }
|