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.table.command;
017:
018: import org.columba.api.command.ICommandReference;
019: import org.columba.api.command.IWorkerStatusController;
020: import org.columba.api.gui.frame.IFrameMediator;
021: import org.columba.api.selection.ISelectionListener;
022: import org.columba.api.selection.SelectionChangedEvent;
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.mail.command.IMailFolderCommandReference;
027: import org.columba.mail.folder.IMailFolder;
028: import org.columba.mail.folder.IMailbox;
029: import org.columba.mail.gui.frame.MailFrameMediator;
030: import org.columba.mail.gui.frame.TableViewOwner;
031: import org.columba.mail.gui.table.action.ClearHeaderlistAction;
032: import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
033: import org.columba.mail.message.IHeaderList;
034:
035: /**
036: * Show header list in message list component.
037: * <p>
038: * Registers as tree selection listener.
039: *
040: * @author Timo Stich (tstich@users.sourceforge.net)
041: * @author fdietz
042: */
043: public class ViewHeaderListCommand extends Command implements
044: ISelectionListener {
045:
046: private IHeaderList headerList;
047:
048: private IMailbox folder;
049:
050: private boolean updateGui;
051:
052: private IFrameMediator mediator;
053:
054: public ViewHeaderListCommand(IFrameMediator mediator,
055: ICommandReference reference) {
056: super (reference);
057:
058: this .mediator = mediator;
059:
060: // Register as listener to the SelectionManger
061: // to check for selection changes
062: updateGui = true;
063:
064: ((MailFrameMediator) mediator)
065: .registerTreeSelectionListener(this );
066:
067: priority = Command.REALTIME_PRIORITY;
068: }
069:
070: /**
071: * @see org.columba.api.command.Command#updateGUI()
072: */
073: public void updateGUI() throws Exception {
074: ((MailFrameMediator) mediator)
075: .removeTreeSelectionListener(this );
076:
077: // Update only if the selection did not change
078: if (updateGui) {
079: ((TableViewOwner) mediator).getTableController()
080: .showHeaderList(folder, headerList);
081: }
082:
083: }
084:
085: /**
086: * @see org.columba.api.command.Command#execute(Worker)
087: */
088: public void execute(IWorkerStatusController worker)
089: throws Exception {
090: // Register as SelectionListener to track the selection
091: // of the tree
092: if (!updateGui)
093: return;
094:
095: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
096:
097: folder = (IMailbox) r.getSourceFolder();
098:
099: // register for status events
100: ((StatusObservableImpl) folder.getObservable())
101: .setWorker(worker);
102:
103: // fetch the headerlist
104: try {
105: headerList = (folder).getHeaderList();
106: } catch (Exception e) {
107: updateGui = false;
108:
109: // Reset the selection
110: ((MailFrameMediator) mediator).setTreeSelection(null);
111: new ClearHeaderlistAction(mediator).actionPerformed(null);
112:
113: throw e;
114: }
115:
116: updateGui &= !worker.cancelled();
117: }
118:
119: /**
120: * @see org.columba.api.selection.ISelectionListener#selectionChanged(org.columba.api.selection.SelectionChangedEvent)
121: */
122: public void selectionChanged(SelectionChangedEvent e) {
123:
124: // old command-specific selection
125: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
126:
127: // new selection
128: IMailFolder[] folders = ((TreeSelectionChangedEvent) e)
129: .getSelected();
130: // abort if nothing selected
131: if (folders.length == 0)
132: return;
133:
134: // cancel command execution/updateGUI methods, if folder selection
135: // has been modified
136: if (r.getSourceFolder().getId() != folders[0].getId())
137: updateGui = false;
138: }
139: }
|