KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > command > ViewMessageCommand


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo
13
// Stich.
14
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15
//
16
//All Rights Reserved.
17
package org.columba.mail.gui.message.command;
18
19 import java.util.Date JavaDoc;
20
21 import javax.swing.JOptionPane JavaDoc;
22
23 import org.columba.api.command.ICommandReference;
24 import org.columba.api.command.IWorkerStatusController;
25 import org.columba.api.gui.frame.IFrameMediator;
26 import org.columba.api.selection.ISelectionListener;
27 import org.columba.api.selection.SelectionChangedEvent;
28 import org.columba.core.command.Command;
29 import org.columba.core.command.CommandCancelledException;
30 import org.columba.core.command.StatusObservableImpl;
31 import org.columba.core.command.Worker;
32 import org.columba.core.context.base.api.IStructureValue;
33 import org.columba.core.context.semantic.api.ISemanticContext;
34 import org.columba.core.gui.frame.FrameManager;
35 import org.columba.core.util.NameParser;
36 import org.columba.mail.command.IMailFolderCommandReference;
37 import org.columba.mail.folder.AbstractMessageFolder;
38 import org.columba.mail.folder.FolderInconsistentException;
39 import org.columba.mail.folder.IMailFolder;
40 import org.columba.mail.folder.IMailbox;
41 import org.columba.mail.folder.headercache.SyncHeaderList;
42 import org.columba.mail.gui.frame.MailFrameMediator;
43 import org.columba.mail.gui.frame.MessageViewOwner;
44 import org.columba.mail.gui.message.IMessageController;
45 import org.columba.mail.gui.message.viewer.MarkAsReadTimer;
46 import org.columba.mail.gui.table.selection.TableSelectionChangedEvent;
47 import org.columba.mail.util.MailResourceLoader;
48 import org.columba.ristretto.message.Address;
49 import org.columba.ristretto.message.Flags;
50
51 /**
52  * @author Timo Stich (tstich@users.sourceforge.net)
53  *
54  */

55 public class ViewMessageCommand extends Command implements ISelectionListener {
56
57     private Flags flags;
58
59     private IMailbox srcFolder;
60
61     private Object JavaDoc uid;
62
63     private IFrameMediator mediator;
64
65     private boolean updateGui;
66
67     private IStructureValue value;
68
69     private String JavaDoc subject;
70
71     private String JavaDoc bodyText;
72
73     private Date JavaDoc date;
74
75     private NameParser.Name name;
76
77     private Address from;
78
79     /**
80      * Constructor for ViewMessageCommand.
81      *
82      * @param references
83      */

84     public ViewMessageCommand(IFrameMediator mediator,
85             ICommandReference reference) {
86         super(reference);
87
88         this.mediator = mediator;
89         priority = Command.REALTIME_PRIORITY;
90         commandType = Command.NORMAL_OPERATION;
91
92         updateGui = true;
93
94         // Register as listener to the SelectionManger
95
// to check for selection changes
96

97         ((MailFrameMediator) mediator).registerTableSelectionListener(this);
98
99     }
100
101     /**
102      * @see org.columba.api.command.Command#updateGUI()
103      */

104     public void updateGUI() throws Exception JavaDoc {
105         ((MailFrameMediator) mediator).removeTableSelectionListener(this);
106
107         // Update only if the selection did not change
108
if (updateGui) {
109             IMessageController messageController = ((MessageViewOwner) mediator)
110                     .getMessageController();
111
112             // display changes
113
messageController.updateGUI();
114
115             fillContext();
116         }
117
118
119     }
120
121     private void fillContext() {
122         if ( value == null) return;
123
124         // create identity value
125
IStructureValue identity = value.addChild(
126                 ISemanticContext.CONTEXT_NODE_IDENTITY,
127                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
128         if (name != null && name.toString() != null)
129             identity.setString(ISemanticContext.CONTEXT_ATTR_DISPLAY_NAME,
130                     ISemanticContext.CONTEXT_NAMESPACE_CORE, name.toString());
131         if (from != null && from.getMailAddress() != null)
132             identity.setString(ISemanticContext.CONTEXT_ATTR_EMAIL_ADDRESS,
133                     ISemanticContext.CONTEXT_NAMESPACE_CORE, from
134                             .getMailAddress());
135         if (name != null && name.getFirstName() != null)
136             identity.setString(ISemanticContext.CONTEXT_ATTR_FIRST_NAME,
137                     ISemanticContext.CONTEXT_NAMESPACE_CORE, name
138                             .getFirstName());
139         if (name != null && name.getLastName() != null)
140             identity
141                     .setString(ISemanticContext.CONTEXT_ATTR_LAST_NAME,
142                             ISemanticContext.CONTEXT_NAMESPACE_CORE, name
143                                     .getLastName());
144
145         // create message value
146
IStructureValue message = value.addChild(
147                 ISemanticContext.CONTEXT_NODE_MESSAGE,
148                 ISemanticContext.CONTEXT_NAMESPACE_CORE);
149         if (subject != null)
150             message.setString(ISemanticContext.CONTEXT_ATTR_SUBJECT,
151                     ISemanticContext.CONTEXT_NAMESPACE_CORE, subject);
152         if (date != null)
153             message.setDate(ISemanticContext.CONTEXT_ATTR_DATE,
154                     ISemanticContext.CONTEXT_NAMESPACE_CORE, date);
155
156         IMessageController messageController = ((MessageViewOwner) mediator)
157                 .getMessageController();
158
159         bodyText = messageController.getText();
160
161         // @TODO: assert(bodyText != null) or if (bodyText != null)
162
message.setString(ISemanticContext.CONTEXT_ATTR_BODY_TEXT,
163                 ISemanticContext.CONTEXT_NAMESPACE_CORE, bodyText);
164
165         // set value
166
mediator.getSemanticContext().setValue(value);
167     }
168
169     /**
170      * @see org.columba.api.command.Command#execute(Worker)
171      */

172     public void execute(IWorkerStatusController wsc) throws Exception JavaDoc {
173         if (!updateGui)
174             return;
175
176         // get command reference
177
IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
178
179         // get selected folder
180
srcFolder = (IMailbox) r.getSourceFolder();
181
182         // register for status events
183
((StatusObservableImpl) srcFolder.getObservable()).setWorker(wsc);
184
185         // get selected message UID
186
uid = r.getUids()[0];
187
188         if (!srcFolder.exists(uid)) {
189             return;
190         }
191
192         try {
193             // get flags
194
flags = srcFolder.getFlags(uid);
195
196             // get messagecontroller of frame
197
IMessageController messageController = ((MessageViewOwner) mediator)
198                     .getMessageController();
199
200             messageController.showMessage(srcFolder, uid);
201
202             restartMarkAsReadTimer(flags, messageController, r);
203
204             // fill semantic context
205
prepareContextData();
206         } catch (FolderInconsistentException e) {
207             Object JavaDoc[] options = new String JavaDoc[] { MailResourceLoader.getString("",
208                     "global", "ok").replaceAll("&", ""), };
209             int result = JOptionPane.showOptionDialog(FrameManager.getInstance()
210                     .getActiveFrame(), MailResourceLoader
211                     .getString("dialog", "error", "message_deleted"), "Error",
212                     JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null,
213                     null, null);
214
215             if (result == JOptionPane.YES_OPTION) {
216                 // Synchronize the complete folder
217
SyncHeaderList.sync((AbstractMessageFolder) srcFolder,
218                         srcFolder.getHeaderList());
219             }
220
221             throw new CommandCancelledException();
222         }
223     }
224
225     private void prepareContextData() throws Exception JavaDoc {
226
227         // create empty value
228
value = mediator.getSemanticContext().createValue();
229
230         // from email address
231
Object JavaDoc fromObj = srcFolder.getAttribute(uid, "columba.from");
232         if (fromObj instanceof Address) {
233             name = NameParser.getInstance().parseDisplayName(((Address)fromObj).getDisplayName());
234             from = (Address) fromObj;
235         } else if ( fromObj instanceof String JavaDoc){
236             name = NameParser.getInstance().parseDisplayName((String JavaDoc) fromObj);
237         }
238
239         subject = (String JavaDoc) srcFolder.getAttribute(uid, "columba.subject");
240         date = (Date JavaDoc) srcFolder.getAttribute(uid, "columba.date");
241
242     }
243
244     private void restartMarkAsReadTimer(Flags flags,
245             IMessageController messageController, IMailFolderCommandReference r)
246             throws Exception JavaDoc {
247
248         if (flags == null)
249             return;
250         // if the message it not yet seen
251
if (!flags.getSeen() && !srcFolder.isReadOnly()) {
252             MarkAsReadTimer.getInstance().start(messageController, r);
253         }
254     }
255
256     /**
257      * @see org.columba.api.selection.ISelectionListener#selectionChanged(org.columba.api.selection.SelectionChangedEvent)
258      */

259     public void selectionChanged(SelectionChangedEvent e) {
260
261         // old command-specific selection
262
IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
263
264         // get selected folder
265
IMailbox folder = (IMailbox) r.getSourceFolder();
266
267         // get selected message UID
268
Object JavaDoc[] uid = r.getUids();
269
270         // new selection
271
IMailFolder newFolder = ((TableSelectionChangedEvent) e).getFolder();
272         Object JavaDoc[] newUid = ((TableSelectionChangedEvent) e).getUids();
273
274         // abort if nothing selected
275
if (folder == null)
276             return;
277         if (newUid == null || newUid.length == 0)
278             return;
279
280         // cancel command execution/updateGUI methods, if folder or message
281
// selection
282
// has been modified
283
if (folder.getId() != newFolder.getId())
284             updateGui = false;
285
286         if (uid[0].equals(newUid[0]) == false)
287             updateGui = false;
288
289     }
290 }
Popular Tags