View Javadoc

1   /***
2    * JColoredTableMemento.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.state;
27  
28  import java.util.prefs.Preferences;
29  
30  import org.jcreme.state.Memento;
31  import org.jcreme.state.MementoException;
32  import org.jcreme.swing.table.JColoredTable;
33  import org.jcreme.swing.table.TableColorModel;
34  import org.jcreme.swing.table.colormodel.TableColorModelType;
35  
36  /***
37   * This is the Memento class for the JColoredTable.
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.2 $
41   */
42  public class JColoredTableMemento extends JTableMemento {
43  	/***
44  	 *  
45  	 */
46  	public static final String TABLE_COLOR_MODEL_PREFERENCE = "tableColorModel";
47  
48  	/***
49  	 *  
50  	 */
51  	public static final String TABLE_COLOR_MODEL_CLASS_PREFERENCE = "tableColorModelClass";
52  
53  	private Class tableColorModelClass = null;
54  
55  	private Memento tableColorModelMemento = null;
56  
57  	/***
58  	 * @see JTableMemento#store(Preferences)
59  	 */
60  	public void store(Preferences preferences) throws MementoException {
61  		if (preferences != null)
62  			super.store(preferences);
63  		Preferences node = preferences.node(TABLE_COLOR_MODEL_PREFERENCE);
64  		if (this.tableColorModelMemento != null) {
65  			storeSubMemento(node, this.tableColorModelMemento, this.tableColorModelClass);
66  		}
67  	}
68  
69  	/***
70  	 * @see JTableMemento#load(Preferences)
71  	 */
72  	public void load(Preferences preferences) throws MementoException {
73  		if (preferences != null) {
74  			super.load(preferences);
75  			Preferences node = preferences.node(TABLE_COLOR_MODEL_PREFERENCE);
76  			this.tableColorModelClass = getObjectType(node);
77  			this.tableColorModelMemento = loadSubMemento(node);
78  		}
79  	}
80  
81  	/***
82  	 * @see Memento#updateMemento(Object)
83  	 */
84  	public void updateMemento(Object o) throws IllegalArgumentException, MementoException {
85  		if (!(o instanceof JColoredTable)) {
86  			throw new IllegalArgumentException("Expected a "
87  					+ JColoredTable.class.getName());
88  		}
89  		super.updateMemento(o);
90  		JColoredTable table = (JColoredTable) o;
91  		TableColorModel colorModel = table.getTableColorModel();
92  		if (colorModel != null) {
93  			this.tableColorModelClass = colorModel.getClass();
94  			TableColorModelType type = TableColorModelType
95  					.getFromImplementation(colorModel.getClass());
96  			if (type != null) {
97  				Class memento = type.getTableColorModelMenentoClass();
98  				if (memento != null) {
99  					try {
100 						this.tableColorModelMemento = (Memento) memento
101 								.newInstance();
102 						this.tableColorModelMemento.updateMemento(colorModel);
103 					} catch (IllegalAccessException e) {
104 						e.printStackTrace();
105 					} catch (InstantiationException e) {
106 						e.printStackTrace();
107 					} catch (IllegalArgumentException e) {
108 						e.printStackTrace();
109 					}
110 				}
111 			}
112 		}
113 
114 	}
115 
116 	/***
117 	 * @see Memento#updateObject(Object)
118 	 */
119 	public void updateObject(Object o) throws IllegalArgumentException, MementoException {
120 		if (!(o instanceof JColoredTable)) {
121 			throw new IllegalArgumentException("Expected a "
122 					+ JColoredTable.class.getName());
123 		}
124 		super.updateObject(o);
125 		JColoredTable table = (JColoredTable) o;
126 		if (this.tableColorModelClass != null) {
127 			try {
128 				TableColorModel colorModel = (TableColorModel) this.tableColorModelClass.newInstance();
129 				if (this.tableColorModelMemento != null) {
130 					this.tableColorModelMemento.updateObject(colorModel);
131 				}
132 				table
133 						.setTableColorModel(colorModel);
134 			} catch (IllegalAccessException e) {
135 				e.printStackTrace();
136 			} catch (InstantiationException e) {
137 				e.printStackTrace();
138 			}
139 		}
140 	}
141 }