View Javadoc

1   /***
2    * JTableMemento.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 javax.swing.JTable;
31  import javax.swing.table.TableColumnModel;
32  
33  import org.jcreme.state.MementoException;
34  import org.jcreme.swing.state.BaseUIMemento;
35  
36  /***
37   * This is the Memento class for the JTable.
38   * 
39   * @author $Author: dbregeon $
40   * @version $Revision: 1.2 $
41   */
42  public class JTableMemento extends BaseUIMemento {
43  	/***
44  	 *  
45  	 */
46  	public static final String MEMENTO_CLASS_PREFERENCE = "mementoClass";
47  
48  	/***
49  	 *  
50  	 */
51  	public static final String TABLE_COLUMN_MODEL_PREFERENCE = "tableColumnModel";
52  
53  	/***
54  	 *  
55  	 */
56  	public static final String AUTO_RESIZE_MODE_PREFERENCE = "autoResizeMode";
57  
58  	/***
59  	 *  
60  	 */
61  	public static final String DRAG_ENABLED_PREFERENCE = "dragEnabled";
62  
63  	private int autoResizeMode = JTable.AUTO_RESIZE_ALL_COLUMNS;
64  
65  	private boolean dragEnabled = false;
66  
67  	/***
68  	 * The object in charge of persisting the TableColumnModel.
69  	 */
70  	private TableColumnModelMemento tableColumnModelMemento = null;
71  
72  	/***
73  	 *  
74  	 */
75  	private Class tableColumnModelType = null;
76  
77  	/***
78  	 * 
79  	 * @param memento
80  	 */
81  	public void setTableColumnModelMemento(TableColumnModelMemento memento) {
82  		this.tableColumnModelMemento = memento;
83  	}
84  
85  	/***
86  	 * 
87  	 * @return the Memento for the TableColumnModel.
88  	 */
89  	public TableColumnModelMemento getTableColumnModelMemento() {
90  		return this.tableColumnModelMemento;
91  	}
92  
93  	/***
94  	 * @see org.jcreme.state.Memento#updateMemento(Object)
95  	 */
96  	public void updateMemento(Object o) throws IllegalArgumentException,
97  			MementoException {
98  		if (!(o instanceof JTable)) {
99  			throw new IllegalArgumentException("Expected a "
100 					+ JTable.class.getName());
101 		}
102 		JTable table = (JTable) o;
103 		this.autoResizeMode = table.getAutoResizeMode();
104 		this.dragEnabled = table.getDragEnabled();
105 		if (this.tableColumnModelMemento != null) {
106 			this.tableColumnModelType = table.getColumnModel().getClass();
107 			this.tableColumnModelMemento.updateMemento(table.getColumnModel());
108 		}
109 	}
110 
111 	/***
112 	 * @see org.jcreme.state.Memento#updateObject(Object)
113 	 */
114 	public void updateObject(Object o) throws IllegalArgumentException,
115 			MementoException {
116 		if (!(o instanceof JTable)) {
117 			throw new IllegalArgumentException("Expected a "
118 					+ JTable.class.getName());
119 		}
120 		JTable table = (JTable) o;
121 		table.setAutoResizeMode(this.autoResizeMode);
122 		table.setDragEnabled(this.dragEnabled);
123 		if (this.tableColumnModelMemento != null) {
124 			try {
125 				table
126 						.setColumnModel((TableColumnModel) this.tableColumnModelType
127 								.newInstance());
128 				this.tableColumnModelMemento.updateObject(table
129 						.getColumnModel());
130 			} catch (InstantiationException e) {
131 				e.printStackTrace();
132 			} catch (IllegalAccessException e) {
133 				e.printStackTrace();
134 			} catch (IllegalArgumentException e) {
135 				e.printStackTrace();
136 			}
137 		}
138 	}
139 
140 	/***
141 	 * @see org.jcreme.state.Memento#store(Preferences)
142 	 */
143 	public void store(Preferences preferences) throws MementoException {
144 		if (preferences != null) {
145 			preferences
146 					.putInt(AUTO_RESIZE_MODE_PREFERENCE, this.autoResizeMode);
147 			preferences.putBoolean(DRAG_ENABLED_PREFERENCE, this.dragEnabled);
148 			if (this.tableColumnModelMemento != null) {
149 				Preferences node = preferences
150 						.node(TABLE_COLUMN_MODEL_PREFERENCE);
151 				storeSubMemento(node, this.tableColumnModelMemento,
152 						this.tableColumnModelType);
153 			}
154 		}
155 	}
156 
157 	/***
158 	 * @see org.jcreme.state.Memento#load(Preferences)
159 	 */
160 	public void load(Preferences preferences) throws MementoException {
161 		if (preferences != null) {
162 			this.autoResizeMode = preferences
163 					.getInt(AUTO_RESIZE_MODE_PREFERENCE,
164 							JTable.AUTO_RESIZE_ALL_COLUMNS);
165 			this.dragEnabled = preferences.getBoolean(DRAG_ENABLED_PREFERENCE,
166 					false);
167 			Preferences node = preferences.node(TABLE_COLUMN_MODEL_PREFERENCE);
168 			this.tableColumnModelMemento = (TableColumnModelMemento) loadSubMemento(node);
169 			this.tableColumnModelType = getObjectType(node);
170 		}
171 	}
172 }