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: package org.columba.mail.gui.config.search;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Dimension;
020: import java.awt.GridLayout;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024:
025: import javax.swing.BorderFactory;
026: import javax.swing.JButton;
027: import javax.swing.JCheckBox;
028: import javax.swing.JComboBox;
029: import javax.swing.JComponent;
030: import javax.swing.JDialog;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.KeyStroke;
034: import javax.swing.SwingConstants;
035:
036: import org.columba.api.gui.frame.IFrameMediator;
037: import org.columba.core.filter.FilterRule;
038: import org.columba.core.filter.IFilterRule;
039: import org.columba.core.folder.api.IFolder;
040: import org.columba.core.gui.base.ButtonWithMnemonic;
041: import org.columba.core.gui.base.CheckBoxWithMnemonic;
042: import org.columba.core.gui.base.LabelWithMnemonic;
043: import org.columba.core.gui.base.SingleSideEtchedBorder;
044: import org.columba.core.gui.util.DialogHeaderPanel;
045: import org.columba.core.resourceloader.IconKeys;
046: import org.columba.core.resourceloader.ImageLoader;
047: import org.columba.mail.command.MailFolderCommandReference;
048: import org.columba.mail.folder.IMailFolder;
049: import org.columba.mail.folder.IMailbox;
050: import org.columba.mail.folder.virtual.VirtualFolder;
051: import org.columba.mail.gui.config.filter.CriteriaList;
052: import org.columba.mail.gui.frame.MailFrameMediator;
053: import org.columba.mail.gui.tree.FolderTreeModel;
054: import org.columba.mail.gui.tree.util.SelectSearchFolderDialog;
055: import org.columba.mail.gui.tree.util.TreeNodeList;
056: import org.columba.mail.util.MailResourceLoader;
057:
058: import com.jgoodies.forms.builder.PanelBuilder;
059: import com.jgoodies.forms.layout.CellConstraints;
060: import com.jgoodies.forms.layout.FormLayout;
061:
062: /**
063: * Search message Dialog. Lets you specify a source-folder and the search
064: * criteria.
065: *
066: * @author fdietz
067: */
068: public class SearchFrame extends JDialog implements ActionListener {
069: private JLabel folderLabel;
070:
071: private JLabel nameLabel;
072:
073: private JButton selectButton;
074:
075: private JButton searchButton;
076:
077: private JCheckBox includeSubfolderButton;
078:
079: private CriteriaList criteriaList;
080:
081: private VirtualFolder destFolder;
082:
083: private JComboBox condList;
084:
085: private IFrameMediator frameController;
086:
087: public SearchFrame(IFrameMediator frameController,
088: IMailbox searchFolder) {
089: super (frameController.getView().getFrame(), true);
090:
091: this .frameController = frameController;
092: this .destFolder = (VirtualFolder) searchFolder;
093:
094: setTitle(MailResourceLoader.getString("dialog", "filter",
095: "searchdialog_title"));
096:
097: initComponents();
098: updateComponents(true);
099: pack();
100: setLocationRelativeTo(null);
101: setVisible(true);
102: }
103:
104: public SearchFrame(IFrameMediator frameController,
105: IMailbox searchFolder, IMailbox sourceFolder) {
106: super (frameController.getView().getFrame(), true);
107:
108: this .frameController = frameController;
109: this .destFolder = (VirtualFolder) searchFolder;
110:
111: setTitle(MailResourceLoader.getString("dialog", "filter",
112: "searchdialog_title"));
113:
114: initComponents();
115:
116: updateComponents(true);
117:
118: setSourceFolder(sourceFolder);
119:
120: pack();
121: setLocationRelativeTo(null);
122: setVisible(true);
123: }
124:
125: public JPanel createPanel() {
126:
127: FormLayout formlayout1 = new FormLayout(
128: "6DLU,FILL:DEFAULT:GROW(1.0)",
129: "CENTER:DEFAULT:NONE,6DLU,CENTER:DEFAULT:NONE,6DLU,FILL:DEFAULT:GROW(1.0)");
130: formlayout1.setRowGroups(new int[][] { { 5 } });
131: CellConstraints cc = new CellConstraints();
132: PanelBuilder builder = new PanelBuilder(formlayout1);
133: builder.setDefaultDialogBorder();
134:
135: builder.add(createPanel1(), cc.xywh(1, 1, 2, 1));
136:
137: builder.addSeparator(MailResourceLoader.getString("dialog",
138: "filter", "if"), cc.xywh(1, 3, 2, 1));
139:
140: builder.add(createPanel2(), cc.xy(2, 5));
141:
142: return builder.getPanel();
143: }
144:
145: public JPanel createPanel1() {
146: JPanel jpanel1 = new JPanel();
147: FormLayout formlayout1 = new FormLayout(
148: "LEFT:DEFAULT:NONE,3DLU,FILL:DEFAULT:GROW(1.0),3DLU,FILL:DEFAULT:NONE",
149: "FILL:DEFAULT:NONE");
150: CellConstraints cc = new CellConstraints();
151: jpanel1.setLayout(formlayout1);
152:
153: jpanel1.add(folderLabel, cc.xy(1, 1));
154:
155: jpanel1.add(selectButton, cc.xy(3, 1));
156:
157: jpanel1.add(includeSubfolderButton, cc.xy(5, 1));
158:
159: return jpanel1;
160: }
161:
162: public JPanel createPanel2() {
163: JPanel jpanel1 = new JPanel();
164: FormLayout formlayout1 = new FormLayout(
165: "FILL:DEFAULT:NONE,FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,3DLU,FILL:DEFAULT:NONE",
166: "FILL:DEFAULT:NONE,3DLU,FILL:DEFAULT:GROW(1.0)");
167: CellConstraints cc = new CellConstraints();
168: jpanel1.setLayout(formlayout1);
169:
170: jpanel1.add(nameLabel, cc.xy(3, 1));
171:
172: jpanel1.add(condList, cc.xy(5, 1));
173:
174: jpanel1.add(criteriaList, cc.xywh(1, 3, 5, 1));
175:
176: return jpanel1;
177: }
178:
179: /**
180: * init components
181: */
182: protected void initComponents() {
183:
184: folderLabel = new LabelWithMnemonic(MailResourceLoader
185: .getString("dialog", "filter", "choose_folder"));
186:
187: selectButton = new JButton();
188: folderLabel.setLabelFor(selectButton);
189: selectButton.setActionCommand("SELECT");
190: selectButton.addActionListener(this );
191:
192: includeSubfolderButton = new CheckBoxWithMnemonic(
193: MailResourceLoader.getString("dialog", "filter",
194: "include_subfolders"));
195:
196: nameLabel = new LabelWithMnemonic(MailResourceLoader.getString(
197: "dialog", "filter", "execute_actions"));
198:
199: String[] cond = {
200: MailResourceLoader.getString("dialog", "filter",
201: "all_criteria"),
202: MailResourceLoader.getString("dialog", "filter",
203: "any_criteria") };
204: condList = new JComboBox(cond);
205:
206: criteriaList = new CriteriaList(destFolder.getFilter());
207: criteriaList.setPreferredSize(new Dimension(500, 100));
208:
209: getContentPane().setLayout(new BorderLayout());
210: getContentPane().add(createPanel(), BorderLayout.CENTER);
211:
212: getContentPane().add(createBottomPanel(), BorderLayout.SOUTH);
213:
214: getContentPane().add(
215: new DialogHeaderPanel(MailResourceLoader.getString(
216: "dialog", "filter", "header_title"),
217: MailResourceLoader.getString("dialog",
218: "filter", "header_description"),
219: ImageLoader.getIcon(IconKeys.SEARCH)),
220: BorderLayout.NORTH);
221: }
222:
223: /**
224: * @param contentPane
225: */
226: private JPanel createBottomPanel() {
227: JPanel bottom = new JPanel(new BorderLayout());
228: bottom
229: .setBorder(new SingleSideEtchedBorder(
230: SwingConstants.TOP));
231:
232: JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 6, 0));
233: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
234: 12, 12));
235: searchButton = new JButton(MailResourceLoader.getString(
236: "dialog", "filter", "search"));
237: searchButton.setIcon(ImageLoader.getIcon(IconKeys.SEARCH));
238: searchButton.addActionListener(this );
239: searchButton.setActionCommand("SEARCH");
240: buttonPanel.add(searchButton);
241:
242: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
243: MailResourceLoader.getString("global", "close"));
244: closeButton.addActionListener(this );
245: closeButton.setActionCommand("CLOSE");
246: buttonPanel.add(closeButton);
247:
248: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
249: MailResourceLoader.getString("global", "help"));
250: helpButton.addActionListener(this );
251: helpButton.setActionCommand("HELP");
252: helpButton.setEnabled(false);
253: buttonPanel.add(helpButton);
254: bottom.add(buttonPanel, BorderLayout.EAST);
255:
256: getRootPane().setDefaultButton(searchButton);
257: getRootPane().registerKeyboardAction(this , "CLOSE",
258: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
259: JComponent.WHEN_IN_FOCUSED_WINDOW);
260:
261: return bottom;
262: }
263:
264: public void updateComponents(boolean b) {
265: if (b) {
266: IFilterRule filterRule = destFolder.getFilter()
267: .getFilterRule();
268: String value = filterRule.getCondition();
269:
270: if (value.equals("matchall")) {
271: condList.setSelectedIndex(0);
272: } else {
273: condList.setSelectedIndex(1);
274: }
275:
276: boolean isInclude = Boolean.valueOf(
277: destFolder.getConfiguration().getString("property",
278: "include_subfolders")).booleanValue();
279:
280: includeSubfolderButton.setSelected(isInclude);
281:
282: String uid = destFolder.getConfiguration().getString(
283: "property", "source_uid");
284:
285: IMailFolder f = (IMailFolder) FolderTreeModel.getInstance()
286: .getFolder(uid);
287:
288: // If f==null because of deleted AbstractMessageFolder fallback to
289: // Inbox
290: if (f == null) {
291: uid = "101";
292: destFolder.getConfiguration().setString("property",
293: "source_uid", uid);
294: f = (IMailbox) FolderTreeModel.getInstance().getFolder(
295: uid);
296: }
297:
298: selectButton.setText(f.getTreePath());
299:
300: criteriaList.updateComponents(b);
301: } else {
302: // get values from components
303: IFilterRule filterRule = destFolder.getFilter()
304: .getFilterRule();
305: int index = condList.getSelectedIndex();
306:
307: if (index == 0) {
308: filterRule.setCondition("matchall");
309: } else {
310: filterRule.setCondition("matchany");
311: }
312:
313: if (includeSubfolderButton.isSelected()) {
314: destFolder.getConfiguration().setString("property",
315: "include_subfolders", "true");
316: } else {
317: destFolder.getConfiguration().setString("property",
318: "include_subfolders", "false");
319: }
320:
321: String path = selectButton.getText();
322: TreeNodeList list = new TreeNodeList(path);
323: IMailFolder folder = (IMailFolder) FolderTreeModel
324: .getInstance().getFolder(list);
325: String uid = folder.getId();
326: destFolder.getConfiguration().setString("property",
327: "source_uid", uid);
328:
329: criteriaList.updateComponents(b);
330: }
331: }
332:
333: public void setSourceFolder(IMailbox f) {
334: selectButton.setText(f.getTreePath());
335: }
336:
337: public void actionPerformed(ActionEvent e) {
338: String action = e.getActionCommand();
339:
340: if (action.equals("CLOSE")) {
341: updateComponents(false);
342: setVisible(false);
343: } else if (action.equals("ADD_CRITERION")) {
344: criteriaList.add();
345: } else if (action.equals("SELECT")) {
346: SelectSearchFolderDialog dialog = new SelectSearchFolderDialog(
347: frameController);
348:
349: if (dialog.success()) {
350: IFolder folder = dialog.getSelectedFolder();
351: String path = folder.getTreePath();
352:
353: selectButton.setText(path);
354: }
355: } else if (action.equals("SEARCH")) {
356: updateComponents(false);
357: setVisible(false);
358:
359: try {
360: // Deactivate this vFolder because changes are about to happen
361: // and the search needs to be redone.
362: ((VirtualFolder) destFolder).deactivate();
363: ((VirtualFolder) destFolder).addSearchToHistory();
364: } catch (Exception ex) {
365: ex.printStackTrace();
366: }
367:
368: MailFolderCommandReference r = new MailFolderCommandReference(
369: destFolder);
370: ((MailFrameMediator) frameController).setTreeSelection(r);
371: }
372: }
373: }
|