1 /***
2 * SortableTableColumnMemento.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing.table.columnmodel.state;
27
28 import java.util.prefs.Preferences;
29
30 import org.jcreme.enumerations.ComparatorType;
31 import org.jcreme.enumerations.SortOrder;
32 import org.jcreme.state.MementoException;
33 import org.jcreme.swing.table.SortableTableColumn;
34
35 /***
36 * This Memento enables to persist the state of a SortableTableColumn.
37 *
38 * @author $Author: dbregeon $
39 * @version $Revision: 1.2 $
40 */
41 public class SortableTableColumnMemento extends BaseTableColumnMemento {
42 /***
43 *
44 */
45 public static final String SORT_ORDER_PREFERENCE = "sortOrder";
46
47 /***
48 *
49 */
50 public static final String ORDER_IN_SORT_PREFERENCE = "orderInSort";
51
52 /***
53 *
54 */
55 public static final String COMPARATOR_PREFERENCE = "comparator";
56
57 /***
58 *
59 */
60 public static final String COLUMN_CLASS_PREFERENCE = "columnClass";
61
62 /***
63 * The SortOrder of the SortableTableColumn.
64 */
65 private SortOrder sortOrder = null;
66
67 /***
68 * The order in the sort of the SortableTableColumn.
69 */
70 private Integer orderInSort = null;
71
72 /***
73 * The ComparatorType that determines the Comparator for the
74 * SortableTableColumn.
75 */
76 private ComparatorType comparator = null;
77
78 /***
79 * The type of the elements presented in the column.
80 */
81 private Class columnClass = null;
82
83 /***
84 * @see org.jcreme.swing.table.state.TableColumnMemento#load(java.util.prefs.Preferences)
85 */
86 public void load(Preferences preferences) throws MementoException {
87 super.load(preferences);
88 if (preferences != null) {
89 this.sortOrder = SortOrder.get(preferences.get(
90 SORT_ORDER_PREFERENCE, SortOrder.S_NONE));
91 if (preferences.getInt(ORDER_IN_SORT_PREFERENCE, -1) != -1) {
92 this.orderInSort = new Integer(preferences.getInt(
93 ORDER_IN_SORT_PREFERENCE, -1));
94 } else {
95 this.orderInSort = null;
96 }
97 this.comparator = ComparatorType.get(preferences.get(
98 COMPARATOR_PREFERENCE, ComparatorType.S_NONE));
99 String columnClassName = preferences.get(COLUMN_CLASS_PREFERENCE,
100 null);
101 if (columnClassName != null) {
102 try {
103 this.columnClass = Thread.currentThread()
104 .getContextClassLoader().loadClass(columnClassName);
105 } catch (ClassNotFoundException e) {
106 e.printStackTrace();
107 }
108 }
109 }
110 }
111
112 /***
113 * @see org.jcreme.swing.table.state.TableColumnMemento#store(java.util.prefs.Preferences)
114 */
115 public void store(Preferences preferences) throws MementoException {
116 super.store(preferences);
117 if (preferences != null) {
118 if (this.sortOrder != null) {
119 preferences
120 .put(SORT_ORDER_PREFERENCE, this.sortOrder.getName());
121 }
122 if (this.orderInSort != null) {
123 preferences.putInt(ORDER_IN_SORT_PREFERENCE, this.orderInSort
124 .intValue());
125 }
126 if (this.comparator != null) {
127 preferences.put(COMPARATOR_PREFERENCE, this.comparator
128 .getName());
129 }
130 if (this.columnClass != null) {
131 preferences.put(COLUMN_CLASS_PREFERENCE, this.columnClass
132 .getName());
133 }
134 }
135 }
136
137 /***
138 * @see org.jcreme.swing.table.columnmodel.state.BaseTableColumnMemento#updateMemento()
139 */
140 public void updateMemento(Object o) throws IllegalArgumentException, MementoException{
141 super.updateMemento(o);
142 this.sortOrder = ((SortableTableColumn) this.tableColumn).getOrder();
143 this.orderInSort = ((SortableTableColumn) this.tableColumn)
144 .getOrderInSort();
145 this.comparator = ((SortableTableColumn) this.tableColumn)
146 .getComparator();
147 this.columnClass = ((SortableTableColumn) this.tableColumn)
148 .getColumnClass();
149 }
150
151 /***
152 * @see org.jcreme.swing.table.columnmodel.state.BaseTableColumnMemento#updateObject()
153 */
154 public void updateObject(Object o) throws IllegalArgumentException, MementoException {
155 super.updateObject(o);
156 ((SortableTableColumn) this.tableColumn).setOrder(this.sortOrder);
157 ((SortableTableColumn) this.tableColumn)
158 .setOrderInSort(this.orderInSort);
159 ((SortableTableColumn) this.tableColumn).setComparator(this.comparator);
160 ((SortableTableColumn) this.tableColumn)
161 .setColumnClass(this.columnClass);
162 }
163 }