View Javadoc

1   /***
2    * EditableTableColumnModelMemento.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.Enumeration;
29  import java.util.Hashtable;
30  import java.util.Iterator;
31  import java.util.prefs.Preferences;
32  
33  import javax.swing.table.TableColumn;
34  import javax.swing.table.TableColumnModel;
35  
36  import org.jcreme.state.Memento;
37  import org.jcreme.state.MementoException;
38  import org.jcreme.swing.state.BaseUIMemento;
39  import org.jcreme.swing.table.columnmodel.EditableTableColumnModel;
40  import org.jcreme.swing.table.columnmodel.TableColumnType;
41  
42  /***
43   * This Memento enables to persist the state of an EditableTableColumnModel.
44   * 
45   * @author $Author: dbregeon $
46   * @version $Revision: 1.2 $
47   */
48  public class EditableTableColumnModelMemento extends BaseUIMemento {
49      /***
50       *  
51       */
52      public static final String VISIBLE_COLUMNS_COUNT_PREFERENCE = "visibleColumns";
53  
54      /***
55       *  
56       */
57      public static final String HIDDEN_COLUMNS_COUNT_PREFERENCE = "hiddenColumns";
58  
59      /***
60       *  
61       */
62      public static final String MEMENTO_CLASS_PREFERENCE = "mementoClass";
63  
64      /***
65       * The TableColumnModel remembered by this Memento.
66       */
67      private transient TableColumnModel rememberedModel = null;
68  
69      /***
70       * The Mementos of the columns used in the TableColumnModel.
71       */
72      private Memento[] columnMementos = null;
73  
74      /***
75       * The number of visible columns in the TableColumnModel.
76       */
77      private int visibleColumnsCount = 0;
78  
79      /***
80       * The number of hidden columns in the TableColumnModel.
81       */
82      private int hiddenColumnsCount = 0;
83  
84      /***
85       * @see org.jcreme.swing.table.state.TableColumnModelMemento#store(java.util.prefs.Preferences)
86       */
87      public void store(Preferences preferences) throws MementoException{
88          if (preferences != null) {
89              preferences.putInt(VISIBLE_COLUMNS_COUNT_PREFERENCE,
90                      this.visibleColumnsCount);
91              preferences.putInt(HIDDEN_COLUMNS_COUNT_PREFERENCE,
92                      this.hiddenColumnsCount);
93              for (int i = 0; i < this.columnMementos.length; i++) {
94                  storeTableColumnMemento(this.columnMementos[i], preferences
95                          .node(Integer.toString(i)));
96              }
97          }
98      }
99  
100     /***
101      * @see org.jcreme.swing.table.state.TableColumnModelMemento#load(java.util.prefs.Preferences)
102      */
103     public void load(Preferences preferences) throws MementoException {
104         if (preferences != null) {
105             this.visibleColumnsCount = preferences.getInt(
106                     VISIBLE_COLUMNS_COUNT_PREFERENCE, 0);
107             this.hiddenColumnsCount = preferences.getInt(
108                     HIDDEN_COLUMNS_COUNT_PREFERENCE, 0);
109             this.columnMementos = new Memento[this.visibleColumnsCount
110                     + this.hiddenColumnsCount];
111             for (int i = 0; i < this.columnMementos.length; i++) {
112                 this.columnMementos[i] = loadTableColumnMemento(preferences
113                         .node(Integer.toString(i)));
114             }
115         }
116 
117     }
118     
119     public void updateObject(Object o) throws IllegalArgumentException, MementoException {
120     		//TODO
121     }
122 
123     public void updateMemento(Object o) throws IllegalArgumentException, MementoException {
124         if (this.rememberedModel != null) {
125             this.visibleColumnsCount = this.rememberedModel.getColumnCount();
126             Enumeration visibleColumns = this.rememberedModel.getColumns();
127             TableColumn[] hiddenColumns = ((EditableTableColumnModel) this.rememberedModel)
128                     .getHiddenColumns();
129             this.hiddenColumnsCount = hiddenColumns.length;
130             this.columnMementos = new Memento[this.visibleColumnsCount
131                     + this.hiddenColumnsCount];
132             TableColumn current = null;
133             while (visibleColumns.hasMoreElements()) {
134                 current = (TableColumn) visibleColumns.nextElement();
135                 this.columnMementos[this.rememberedModel.getColumnIndex(current
136                         .getIdentifier())] = getColumnMemento(current);
137             }
138             for (int i = 0; i < this.hiddenColumnsCount; i++) {
139                 this.columnMementos[i + this.visibleColumnsCount] = getColumnMemento(hiddenColumns[i]);
140             }
141         }
142     }
143 
144     protected Memento getColumnMemento(TableColumn c) throws MementoException {
145         Memento result = null;
146         TableColumnType type = TableColumnType.getFromImplementation(c
147                 .getClass());
148         if ((type != null) && (type.getTableColumnMementoClass() != null)
149                 && (type.getTableColumnEditorClass() != null)) {
150             try {
151                 result = (Memento) type.getTableColumnMementoClass()
152                         .newInstance();
153                 result.updateObject(c);
154             } catch (InstantiationException e) {
155                 e.printStackTrace();
156             } catch (IllegalAccessException e) {
157                 e.printStackTrace();
158             } catch (IllegalArgumentException e) {
159                 e.printStackTrace();
160             }
161         }
162         return result;
163     }
164 
165     protected void storeTableColumnMemento(Memento memento,
166             Preferences node) throws MementoException {
167         node.put(MEMENTO_CLASS_PREFERENCE, memento.getClass().getName());
168         memento.store(node);
169     }
170 
171     protected Memento loadTableColumnMemento(Preferences node) throws MementoException {
172         Memento result = null;
173         String className = node.get(MEMENTO_CLASS_PREFERENCE, null);
174         if (className != null) {
175             try {
176                 result = (Memento) Class.forName(className)
177                         .newInstance();
178                 result.load(node);
179             } catch (ClassNotFoundException e) {
180                 e.printStackTrace();
181             } catch (IllegalAccessException e) {
182                 e.printStackTrace();
183             } catch (InstantiationException e) {
184                 e.printStackTrace();
185             }
186         }
187         return result;
188     }
189 
190     /***
191      * Ensure that the models are synched up.
192      * 
193      * @param newModel
194      *            the model restored (to sync).
195      * @param oldModel
196      *            the model newly created for the table (reference for sync).
197      */
198     protected void filterColumns(EditableTableColumnModel newModel,
199             TableColumnModel oldModel) {
200         Hashtable newColumns = new Hashtable();
201         Hashtable oldColumns = new Hashtable();
202         TableColumn[] hiddenColumns = newModel.getHiddenColumns();
203         TableColumn current = null;
204         Enumeration e = newModel.getColumns();
205         for (int i = 0; i < hiddenColumns.length; i++) {
206             newColumns.put(hiddenColumns[i].getIdentifier(), hiddenColumns[i]);
207         }
208         while (e.hasMoreElements()) {
209             current = (TableColumn) e.nextElement();
210             newColumns.put(current.getIdentifier(), current);
211         }
212         if (oldModel instanceof EditableTableColumnModel) {
213             hiddenColumns = ((EditableTableColumnModel) oldModel)
214                     .getHiddenColumns();
215         } else {
216             hiddenColumns = new TableColumn[0];
217         }
218         e = oldModel.getColumns();
219         for (int i = 0; i < hiddenColumns.length; i++) {
220             oldColumns.put(hiddenColumns[i].getIdentifier(), hiddenColumns[i]);
221         }
222         while (e.hasMoreElements()) {
223             current = (TableColumn) e.nextElement();
224             oldColumns.put(current.getIdentifier(), current);
225         }
226         Iterator iter = newColumns.keySet().iterator();
227         Object id = null;
228         TableColumn old = null;
229         // Ensure that the Columns that do not exist anymore are removed.
230         // Also ensures that the common columns have a correct model index.
231         while (iter.hasNext()) {
232             id = iter.next();
233             current = (TableColumn) newColumns.get(id);
234             if (oldColumns.get(id) == null) {
235                 newModel.removeColumn(current);
236             } else {
237                 old = (TableColumn) oldColumns.get(id);
238                 current.setModelIndex(old.getModelIndex());
239                 oldColumns.remove(id);
240             }
241             iter.remove();
242         }
243         // Add the columns that did not exist in the saved model are added.
244         iter = oldColumns.values().iterator();
245         while (iter.hasNext()) {
246             newModel.addColumn((TableColumn) iter.next());
247         }
248     }
249 }