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
013: // Stich.
014: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015: //
016: //All Rights Reserved.
017: package org.columba.mail.gui.message.command;
018:
019: import java.util.Date;
020:
021: import javax.swing.JOptionPane;
022:
023: import org.columba.api.command.ICommandReference;
024: import org.columba.api.command.IWorkerStatusController;
025: import org.columba.api.gui.frame.IFrameMediator;
026: import org.columba.api.selection.ISelectionListener;
027: import org.columba.api.selection.SelectionChangedEvent;
028: import org.columba.core.command.Command;
029: import org.columba.core.command.CommandCancelledException;
030: import org.columba.core.command.StatusObservableImpl;
031: import org.columba.core.command.Worker;
032: import org.columba.core.context.base.api.IStructureValue;
033: import org.columba.core.context.semantic.api.ISemanticContext;
034: import org.columba.core.gui.frame.FrameManager;
035: import org.columba.core.util.NameParser;
036: import org.columba.mail.command.IMailFolderCommandReference;
037: import org.columba.mail.folder.AbstractMessageFolder;
038: import org.columba.mail.folder.FolderInconsistentException;
039: import org.columba.mail.folder.IMailFolder;
040: import org.columba.mail.folder.IMailbox;
041: import org.columba.mail.folder.headercache.SyncHeaderList;
042: import org.columba.mail.gui.frame.MailFrameMediator;
043: import org.columba.mail.gui.frame.MessageViewOwner;
044: import org.columba.mail.gui.message.IMessageController;
045: import org.columba.mail.gui.message.viewer.MarkAsReadTimer;
046: import org.columba.mail.gui.table.selection.TableSelectionChangedEvent;
047: import org.columba.mail.util.MailResourceLoader;
048: import org.columba.ristretto.message.Address;
049: import org.columba.ristretto.message.Flags;
050:
051: /**
052: * @author Timo Stich (tstich@users.sourceforge.net)
053: *
054: */
055: public class ViewMessageCommand extends Command implements
056: ISelectionListener {
057:
058: private Flags flags;
059:
060: private IMailbox srcFolder;
061:
062: private Object uid;
063:
064: private IFrameMediator mediator;
065:
066: private boolean updateGui;
067:
068: private IStructureValue value;
069:
070: private String subject;
071:
072: private String bodyText;
073:
074: private Date date;
075:
076: private NameParser.Name name;
077:
078: private Address from;
079:
080: /**
081: * Constructor for ViewMessageCommand.
082: *
083: * @param references
084: */
085: public ViewMessageCommand(IFrameMediator mediator,
086: ICommandReference reference) {
087: super (reference);
088:
089: this .mediator = mediator;
090: priority = Command.REALTIME_PRIORITY;
091: commandType = Command.NORMAL_OPERATION;
092:
093: updateGui = true;
094:
095: // Register as listener to the SelectionManger
096: // to check for selection changes
097:
098: ((MailFrameMediator) mediator)
099: .registerTableSelectionListener(this );
100:
101: }
102:
103: /**
104: * @see org.columba.api.command.Command#updateGUI()
105: */
106: public void updateGUI() throws Exception {
107: ((MailFrameMediator) mediator)
108: .removeTableSelectionListener(this );
109:
110: // Update only if the selection did not change
111: if (updateGui) {
112: IMessageController messageController = ((MessageViewOwner) mediator)
113: .getMessageController();
114:
115: // display changes
116: messageController.updateGUI();
117:
118: fillContext();
119: }
120:
121: }
122:
123: private void fillContext() {
124: if (value == null)
125: return;
126:
127: // create identity value
128: IStructureValue identity = value.addChild(
129: ISemanticContext.CONTEXT_NODE_IDENTITY,
130: ISemanticContext.CONTEXT_NAMESPACE_CORE);
131: if (name != null && name.toString() != null)
132: identity.setString(
133: ISemanticContext.CONTEXT_ATTR_DISPLAY_NAME,
134: ISemanticContext.CONTEXT_NAMESPACE_CORE, name
135: .toString());
136: if (from != null && from.getMailAddress() != null)
137: identity.setString(
138: ISemanticContext.CONTEXT_ATTR_EMAIL_ADDRESS,
139: ISemanticContext.CONTEXT_NAMESPACE_CORE, from
140: .getMailAddress());
141: if (name != null && name.getFirstName() != null)
142: identity.setString(
143: ISemanticContext.CONTEXT_ATTR_FIRST_NAME,
144: ISemanticContext.CONTEXT_NAMESPACE_CORE, name
145: .getFirstName());
146: if (name != null && name.getLastName() != null)
147: identity.setString(ISemanticContext.CONTEXT_ATTR_LAST_NAME,
148: ISemanticContext.CONTEXT_NAMESPACE_CORE, name
149: .getLastName());
150:
151: // create message value
152: IStructureValue message = value.addChild(
153: ISemanticContext.CONTEXT_NODE_MESSAGE,
154: ISemanticContext.CONTEXT_NAMESPACE_CORE);
155: if (subject != null)
156: message.setString(ISemanticContext.CONTEXT_ATTR_SUBJECT,
157: ISemanticContext.CONTEXT_NAMESPACE_CORE, subject);
158: if (date != null)
159: message.setDate(ISemanticContext.CONTEXT_ATTR_DATE,
160: ISemanticContext.CONTEXT_NAMESPACE_CORE, date);
161:
162: IMessageController messageController = ((MessageViewOwner) mediator)
163: .getMessageController();
164:
165: bodyText = messageController.getText();
166:
167: // @TODO: assert(bodyText != null) or if (bodyText != null)
168: message.setString(ISemanticContext.CONTEXT_ATTR_BODY_TEXT,
169: ISemanticContext.CONTEXT_NAMESPACE_CORE, bodyText);
170:
171: // set value
172: mediator.getSemanticContext().setValue(value);
173: }
174:
175: /**
176: * @see org.columba.api.command.Command#execute(Worker)
177: */
178: public void execute(IWorkerStatusController wsc) throws Exception {
179: if (!updateGui)
180: return;
181:
182: // get command reference
183: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
184:
185: // get selected folder
186: srcFolder = (IMailbox) r.getSourceFolder();
187:
188: // register for status events
189: ((StatusObservableImpl) srcFolder.getObservable())
190: .setWorker(wsc);
191:
192: // get selected message UID
193: uid = r.getUids()[0];
194:
195: if (!srcFolder.exists(uid)) {
196: return;
197: }
198:
199: try {
200: // get flags
201: flags = srcFolder.getFlags(uid);
202:
203: // get messagecontroller of frame
204: IMessageController messageController = ((MessageViewOwner) mediator)
205: .getMessageController();
206:
207: messageController.showMessage(srcFolder, uid);
208:
209: restartMarkAsReadTimer(flags, messageController, r);
210:
211: // fill semantic context
212: prepareContextData();
213: } catch (FolderInconsistentException e) {
214: Object[] options = new String[] { MailResourceLoader
215: .getString("", "global", "ok").replaceAll("&", ""), };
216: int result = JOptionPane.showOptionDialog(FrameManager
217: .getInstance().getActiveFrame(), MailResourceLoader
218: .getString("dialog", "error", "message_deleted"),
219: "Error", JOptionPane.YES_NO_OPTION,
220: JOptionPane.ERROR_MESSAGE, null, null, null);
221:
222: if (result == JOptionPane.YES_OPTION) {
223: // Synchronize the complete folder
224: SyncHeaderList.sync((AbstractMessageFolder) srcFolder,
225: srcFolder.getHeaderList());
226: }
227:
228: throw new CommandCancelledException();
229: }
230: }
231:
232: private void prepareContextData() throws Exception {
233:
234: // create empty value
235: value = mediator.getSemanticContext().createValue();
236:
237: // from email address
238: Object fromObj = srcFolder.getAttribute(uid, "columba.from");
239: if (fromObj instanceof Address) {
240: name = NameParser.getInstance().parseDisplayName(
241: ((Address) fromObj).getDisplayName());
242: from = (Address) fromObj;
243: } else if (fromObj instanceof String) {
244: name = NameParser.getInstance().parseDisplayName(
245: (String) fromObj);
246: }
247:
248: subject = (String) srcFolder.getAttribute(uid,
249: "columba.subject");
250: date = (Date) srcFolder.getAttribute(uid, "columba.date");
251:
252: }
253:
254: private void restartMarkAsReadTimer(Flags flags,
255: IMessageController messageController,
256: IMailFolderCommandReference r) throws Exception {
257:
258: if (flags == null)
259: return;
260: // if the message it not yet seen
261: if (!flags.getSeen() && !srcFolder.isReadOnly()) {
262: MarkAsReadTimer.getInstance().start(messageController, r);
263: }
264: }
265:
266: /**
267: * @see org.columba.api.selection.ISelectionListener#selectionChanged(org.columba.api.selection.SelectionChangedEvent)
268: */
269: public void selectionChanged(SelectionChangedEvent e) {
270:
271: // old command-specific selection
272: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
273:
274: // get selected folder
275: IMailbox folder = (IMailbox) r.getSourceFolder();
276:
277: // get selected message UID
278: Object[] uid = r.getUids();
279:
280: // new selection
281: IMailFolder newFolder = ((TableSelectionChangedEvent) e)
282: .getFolder();
283: Object[] newUid = ((TableSelectionChangedEvent) e).getUids();
284:
285: // abort if nothing selected
286: if (folder == null)
287: return;
288: if (newUid == null || newUid.length == 0)
289: return;
290:
291: // cancel command execution/updateGUI methods, if folder or message
292: // selection
293: // has been modified
294: if (folder.getId() != newFolder.getId())
295: updateGui = false;
296:
297: if (uid[0].equals(newUid[0]) == false)
298: updateGui = false;
299:
300: }
301: }
|