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.folder.command;
017:
018: import java.util.logging.Logger;
019:
020: import org.columba.api.command.ICommandReference;
021: import org.columba.api.command.IWorkerStatusController;
022: import org.columba.api.gui.frame.IFrameMediator;
023: import org.columba.core.command.Command;
024: import org.columba.core.command.StatusObservableImpl;
025: import org.columba.core.command.Worker;
026: import org.columba.core.filter.Filter;
027: import org.columba.core.filter.FilterFactory;
028: import org.columba.mail.command.IMailFolderCommandReference;
029: import org.columba.mail.filter.MailFilterFactory;
030: import org.columba.mail.folder.IMailbox;
031: import org.columba.mail.gui.config.filter.FilterDialog;
032: import org.columba.ristretto.message.Header;
033:
034: /**
035: * This class is used to create a filter based on the currently selected message
036: * (if multiple selected, the first one in the selection array is used) - either
037: * using Subject, To or From.
038: *
039: * @author Karl Peder Olesen (karlpeder), 20030620
040: */
041: public class CreateFilterOnMessageCommand extends Command {
042:
043: /** JDK 1.4+ logging framework logger, used for logging. */
044: private static final Logger LOG = Logger
045: .getLogger("org.columba.mail.folder.command");
046:
047: /** Used for creating a filter based on Subject */
048: public static final String FILTER_ON_SUBJECT = "Subject";
049:
050: /** Used for creating a filter based on From */
051: public static final String FILTER_ON_FROM = "From";
052:
053: /** Used for creating a filter based on To */
054: public static final String FILTER_ON_TO = "To";
055:
056: /** Type of filter to create */
057: private String filterType;
058:
059: /** Filter created */
060: private Filter filter = null;
061:
062: private IFrameMediator mediator;
063:
064: /** The source folder where the filter should be added to. */
065: private IMailbox srcFolder;
066:
067: /**
068: * Constructor for CreateFilterOnMessageCommand. Calls super constructor and
069: * saves flag for which kind of filter to create. Default for filter type is
070: * FILTER_ON_SUBJECT.
071: *
072: * @param references
073: * @param filterType
074: * Which type of filter to create. Used defined constants
075: */
076: public CreateFilterOnMessageCommand(IFrameMediator mediator,
077: ICommandReference reference, String filterType) {
078: super (reference);
079: this .filterType = filterType;
080:
081: this .mediator = mediator;
082: }
083:
084: /**
085: * Displays filter dialog for user modifications after creation of the
086: * filter in execute. If the user cancels the dialog then the filter is not
087: * stored into the filter list in the source folder.
088: *
089: * @see org.columba.api.command.Command#updateGUI()
090: */
091: public void updateGUI() throws Exception {
092: if ((filter != null) && (srcFolder != null)) {
093: FilterDialog dialog = new FilterDialog(mediator, filter);
094:
095: if (!dialog.wasCancelled()) {
096: srcFolder.getFilterList().add(filter);
097: }
098: }
099: }
100:
101: /**
102: * This method generates filter based on Subject, From or To (depending on
103: * parameter transferred to constructor) of the currently selected message.
104: *
105: * @param worker
106: * @see org.columba.api.command.Command#execute(Worker)
107: */
108: public void execute(IWorkerStatusController worker)
109: throws Exception {
110: // get references to selected folder and message
111: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
112: Object[] uids = r.getUids(); // uid for messages to save
113:
114: if (uids.length == 0) {
115: LOG.fine("No filter created since no message was selected");
116:
117: return; // no message selected.
118: }
119:
120: Object uid = uids[0];
121: srcFolder = (IMailbox) r.getSourceFolder();
122:
123: // register for status events
124: ((StatusObservableImpl) srcFolder.getObservable())
125: .setWorker(worker);
126:
127: // get value of Subject, From or To header
128: Header header = srcFolder.getHeaderFields(uid, new String[] {
129: "Subject", "From", "To" });
130: String headerValue = (String) header.get(filterType);
131:
132: if (headerValue == null) {
133: LOG.warning("Error getting " + filterType
134: + " header. No filter created");
135:
136: return;
137: }
138:
139: // create filter
140: String descr = filterType + " contains [" + headerValue + "]";
141: filter = createFilter(descr, filterType, headerValue);
142: }
143:
144: /**
145: * Private utility for creating a filter on a given headerfield. The
146: * criteria used is "contains" and the action is set to "Mark as Read".
147: *
148: * @param filterDescr
149: * Name / description to assign to filter
150: * @param headerField
151: * The header field to base filter on
152: * @param pattern
153: * The pattern to use in the filter
154: * @return The filter created
155: */
156: public Filter createFilter(String filterDescr, String headerField,
157: String pattern) {
158:
159: Filter filter = FilterFactory.createEmptyFilter();
160: filter.setName(filterDescr);
161:
162: if (headerField.equals(FILTER_ON_SUBJECT)) {
163: filter.getFilterRule().add(
164: MailFilterFactory.createSubjectContains(pattern));
165: } else if (headerField.equals(FILTER_ON_FROM)) {
166: filter.getFilterRule().add(
167: MailFilterFactory.createFromContains(pattern));
168: } else if (headerField.equals(FILTER_ON_TO)) {
169: filter.getFilterRule().add(
170: MailFilterFactory.createToContains(pattern));
171: } else
172: throw new IllegalArgumentException("unsupported operation");
173:
174: filter.getFilterActionList().addEmptyAction();
175:
176: return filter;
177: }
178: }
|