KickJava   Java API By Example, From Geeks To Geeks.

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


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 Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.folderoptions;
17
18 import java.util.Enumeration JavaDoc;
19
20 import javax.swing.table.TableColumn JavaDoc;
21
22 import org.columa.core.config.IDefaultItem;
23 import org.columba.core.config.DefaultItem;
24 import org.columba.core.xml.XmlElement;
25 import org.columba.mail.folder.IMailbox;
26 import org.columba.mail.gui.frame.MailFrameMediator;
27 import org.columba.mail.gui.frame.TableViewOwner;
28 import org.columba.mail.gui.table.ITableController;
29
30 /**
31  * Stores all visible columns of the message list.
32  *
33  * @author fdietz
34  */

35 public class ColumnOptionsPlugin extends AbstractFolderOptionsPlugin {
36     public final static String JavaDoc[] COLUMNS = { "Status", "Attachment", "Flagged",
37             "Priority", "Subject", "From", "Date", "Size", "Spam", "To", "Cc", "MultiLine" };
38
39     /**
40      * Constructor
41      *
42      * @param mediator
43      * mail frame mediator
44      */

45     public ColumnOptionsPlugin(MailFrameMediator mediator) {
46         super("columns", "ColumnOptions", mediator);
47     }
48
49     /**
50      * Get list of available columns.
51      *
52      * @return string array of columns
53      */

54     public static String JavaDoc[] getColumns() {
55         return COLUMNS;
56     }
57
58     /**
59      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#saveOptionsToXml(IMailbox)
60      */

61     public void saveOptionsToXml(IMailbox folder) {
62         XmlElement columns = getConfigNode(folder);
63
64         ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
65                 .getTableController());
66
67         Enumeration JavaDoc enumeration = tableController.getColumnModel().getColumns();
68
69         // check if there are columns which need to be saved
70
if ( tableController.getColumnModel().getColumnCount() == 0)
71             return;
72         
73         // remove all child nodes
74
columns.removeAllElements();
75         
76         while (enumeration.hasMoreElements()) {
77             TableColumn JavaDoc tc = (TableColumn JavaDoc) enumeration.nextElement();
78             String JavaDoc name = (String JavaDoc) tc.getHeaderValue();
79
80             XmlElement column = new XmlElement("column");
81             column.addAttribute("name", name);
82
83             // save width
84
int size = tc.getWidth();
85             column.addAttribute("width", Integer.toString(size));
86
87             columns.addElement(column);
88         }
89     }
90
91     /**
92      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#loadOptionsFromXml(IMailbox)
93      */

94     public void loadOptionsFromXml(IMailbox folder) {
95         XmlElement columns = getConfigNode(folder);
96
97         /*
98          * if ( columns.count() == 0) { // no columns specified // -> create new
99          * default columns XmlElement parent = columns.getParent();
100          * columns.removeFromParent(); columns = createDefaultElement(true);
101          * parent.addElement(columns);
102          * }
103          */

104
105         ITableController tableController = ((ITableController) ((TableViewOwner) getMediator())
106                 .getTableController());
107         tableController.resetColumnModel();
108
109         
110         // add columns
111
for (int i = 0; i < columns.count(); i++) {
112             XmlElement column = columns.getElement(i);
113             IDefaultItem columnItem = new DefaultItem(column);
114
115             String JavaDoc name = columnItem.get("name");
116             int size = columnItem.getInteger("width");
117
118             //int position= columnItem.getInteger("position");
119
// add column to table model
120
tableController.getHeaderTableModel().addColumn(name);
121
122             // add column to JTable column model
123
TableColumn JavaDoc tc = tableController.createTableColumn(name, size);
124
125             //tc.setModelIndex(position);
126
tc.setModelIndex(i);
127
128             // resize column width
129
tc.setPreferredWidth(size);
130
131             tableController.addColumn(tc);
132         }
133
134         
135     }
136
137     /**
138      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#createDefaultElement()
139      */

140     public XmlElement createDefaultElement(boolean global) {
141         XmlElement columns = super.createDefaultElement(global);
142
143         // these are the items, enabled as default
144
columns.addElement(createColumn("Status", "23"));
145         columns.addElement(createColumn("Attachment", "23"));
146         columns.addElement(createColumn("Flagged", "23"));
147         columns.addElement(createColumn("Priority", "23"));
148         columns.addElement(createColumn("Subject", "200"));
149         columns.addElement(createColumn("From", "150"));
150         columns.addElement(createColumn("Date", "60"));
151         columns.addElement(createColumn("Size", "30"));
152         columns.addElement(createColumn("Spam", "23"));
153
154         return columns;
155     }
156
157     /**
158      * Create new XmlElement with custom attributes.
159      *
160      * @param name
161      * name of column
162      * @param width
163      * column width
164      * @param position
165      * column position
166      * @return parent xml element
167      */

168     private static XmlElement createColumn(String JavaDoc name, String JavaDoc width) {
169         XmlElement column = new XmlElement("column");
170         column.addAttribute("name", name);
171         column.addAttribute("width", width);
172
173         return column;
174     }
175
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.columba.mail.folderoptions.AbstractFolderOptionsPlugin#restoreUISettings()
180      */

181     public void restoreUISettings() {
182
183     }
184 }
Popular Tags