001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.folderoptions;
019:
020: import org.columa.core.config.IDefaultItem;
021: import org.columba.core.config.DefaultItem;
022: import org.columba.core.xml.XmlElement;
023: import org.columba.mail.folder.IMailbox;
024: import org.columba.mail.gui.frame.MailFrameMediator;
025: import org.columba.mail.gui.frame.TableViewOwner;
026: import org.columba.mail.gui.table.IMessageNode;
027: import org.columba.mail.gui.table.ITableController;
028:
029: /**
030: * Handles selecting message after folder selection changes.
031: * <p>
032: * This implementation remembers the the selected message, and tries to reselect
033: * it again. As default fall back it selects the first or last message,
034: * depending on the sorting order.
035: *
036: * @author fdietz, waffel
037: */
038: public class SelectionOptionsPlugin extends AbstractFolderOptionsPlugin {
039:
040: /**
041: * Constructor
042: *
043: * @param mediator
044: * mail frame mediator
045: */
046: public SelectionOptionsPlugin(MailFrameMediator mediator) {
047: super ("selection", "SelectionOptions", mediator);
048: }
049:
050: /**
051: *
052: * Save currently selected message.
053: *
054: * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#saveOptionsToXml(IMailbox)
055: */
056: public void saveOptionsToXml(IMailbox folder) {
057: ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
058: .getTableController());
059:
060: if (tableController.getSelectedNodes() == null)
061: return;
062:
063: if (tableController.getSelectedNodes().length == 0)
064: return;
065:
066: IMessageNode node = tableController.getSelectedNodes()[0];
067: if ((node != null) && (folder != null))
068: folder.setLastSelection(node.getUid());
069: }
070:
071: /**
072: * Restore selection.
073: *
074: * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#loadOptionsFromXml(IMailbox)
075: */
076: public void loadOptionsFromXml(IMailbox folder) {
077: XmlElement parent = getConfigNode(folder);
078: IDefaultItem item = new DefaultItem(parent);
079:
080: ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
081: .getTableController());
082:
083: // TableView view = tableController.getView();
084:
085: // should we re-use the last remembered selection?
086: boolean remember = item.getBooleanWithDefault(
087: "remember_last_selection", true);
088:
089: // sorting order
090: boolean ascending = tableController.getSortingOrder();
091:
092: // row count
093: int row = tableController.getRowCount();
094:
095: // row count == 0 --> empty table
096: if (row == 0) {
097: // clear message viewer
098: // /tableController.valueChanged(new
099: // ListSelectionEvent(this,-1,-1,false));
100: tableController.clearSelection();
101: return;
102: }
103:
104: // if the last selection for the current folder is null, then we show
105: // the
106: // first/last message in the table and scroll to it.
107: if ((!remember) || (folder.getLastSelection() == null)) {
108: // changing the selection to the first/last row based on ascending
109: // state
110: Object uid = null;
111:
112: if (ascending) {
113: uid = tableController.selectLastRow();
114: } else {
115: uid = tableController.selectFirstRow();
116: }
117:
118: // no messages in this folder
119: if (uid == null) {
120: return;
121: }
122:
123: } else {
124:
125: // if a lastSelection for this folder is set
126: // getting the last selected uid
127: Object[] lastSelUids = { folder.getLastSelection() };
128:
129: // no messages in this folder
130: if (lastSelUids[0] == null) {
131: return;
132: }
133:
134: Object uid = lastSelUids[0];
135:
136: // this message doesn't exit in this folder anymore
137: if (tableController.getMessageNode(uid) == null) {
138:
139: if (ascending) {
140: uid = tableController.selectLastRow();
141: } else {
142: uid = tableController.selectFirstRow();
143: }
144:
145: } else {
146:
147: // selecting the message
148: tableController.setSelected(new Object[] { uid });
149: }
150:
151: }
152: }
153:
154: /**
155: * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#createDefaultElement()
156: */
157: public XmlElement createDefaultElement(boolean global) {
158: XmlElement parent = super .createDefaultElement(global);
159: parent.addAttribute("remember_last_selection", "true");
160:
161: return parent;
162: }
163: }
|