KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folderoptions > SelectionOptionsPlugin


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.folderoptions;
19
20 import org.columa.core.config.IDefaultItem;
21 import org.columba.core.config.DefaultItem;
22 import org.columba.core.xml.XmlElement;
23 import org.columba.mail.folder.IMailbox;
24 import org.columba.mail.gui.frame.MailFrameMediator;
25 import org.columba.mail.gui.frame.TableViewOwner;
26 import org.columba.mail.gui.table.IMessageNode;
27 import org.columba.mail.gui.table.ITableController;
28
29 /**
30  * Handles selecting message after folder selection changes.
31  * <p>
32  * This implementation remembers the the selected message, and tries to reselect
33  * it again. As default fall back it selects the first or last message,
34  * depending on the sorting order.
35  *
36  * @author fdietz, waffel
37  */

38 public class SelectionOptionsPlugin extends AbstractFolderOptionsPlugin {
39
40     /**
41      * Constructor
42      *
43      * @param mediator
44      * mail frame mediator
45      */

46     public SelectionOptionsPlugin(MailFrameMediator mediator) {
47         super("selection", "SelectionOptions", mediator);
48     }
49
50     /**
51      *
52      * Save currently selected message.
53      *
54      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#saveOptionsToXml(IMailbox)
55      */

56     public void saveOptionsToXml(IMailbox folder) {
57         ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
58                 .getTableController());
59
60         if (tableController.getSelectedNodes() == null)
61             return;
62
63         if (tableController.getSelectedNodes().length == 0)
64             return;
65
66         IMessageNode node = tableController.getSelectedNodes()[0];
67         if ((node != null) && (folder != null))
68             folder.setLastSelection(node.getUid());
69     }
70
71     /**
72      * Restore selection.
73      *
74      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#loadOptionsFromXml(IMailbox)
75      */

76     public void loadOptionsFromXml(IMailbox folder) {
77         XmlElement parent = getConfigNode(folder);
78         IDefaultItem item = new DefaultItem(parent);
79
80         ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
81                 .getTableController());
82
83         // TableView view = tableController.getView();
84

85         // should we re-use the last remembered selection?
86
boolean remember = item.getBooleanWithDefault(
87                 "remember_last_selection", true);
88
89         // sorting order
90
boolean ascending = tableController.getSortingOrder();
91
92         // row count
93
int row = tableController.getRowCount();
94
95         // row count == 0 --> empty table
96
if (row == 0) {
97             // clear message viewer
98
// /tableController.valueChanged(new
99
// 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 JavaDoc 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 JavaDoc[] lastSelUids = { folder.getLastSelection() };
128
129             // no messages in this folder
130
if (lastSelUids[0] == null) {
131                 return;
132             }
133
134             Object JavaDoc 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 JavaDoc[] { 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 }
Popular Tags