View Javadoc

1   /***
2    * BaseTableColumnMemento.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.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.IOException;
31  import java.io.ObjectInputStream;
32  import java.io.ObjectOutputStream;
33  import java.io.Serializable;
34  import java.util.prefs.Preferences;
35  
36  import javax.swing.table.TableColumn;
37  
38  import org.jcreme.state.MementoException;
39  import org.jcreme.swing.state.BaseUIMemento;
40  
41  /***
42   * This Memento enables to persist the state of a TableColumn.
43   * 
44   * @author $Author: dbregeon $
45   * @version $Revision: 1.2 $
46   */
47  public class BaseTableColumnMemento extends BaseUIMemento {
48  	/***
49  	 *  
50  	 */
51  	public static final String RESIZABLE_PREFERENCE = "resizable";
52  
53  	/***
54  	 *  
55  	 */
56  	public static final String MODEL_INDEX_PREFERENCE = "modelIndex";
57  
58  	/***
59  	 *  
60  	 */
61  	public static final String HEADER_VALUE_PREFERENCE = "headerValue";
62  
63  	/***
64  	 *  
65  	 */
66  	public static final String IDENTIFIER_PREFERENCE = "identifier";
67  
68  	/***
69  	 *  
70  	 */
71  	public static final String COLUMN_WIDTH_PREFERENCE = "columnWidth";
72  
73  	/***
74  	 *  
75  	 */
76  	public static final String MIN_COLUMN_WIDTH_PREFERENCE = "minColumnWidth";
77  
78  	/***
79  	 *  
80  	 */
81  	public static final String MAX_COLUMN_WIDTH_PREFERENCE = "maxColumnWidth";
82  
83  	/***
84  	 * The TableColumn to persist.
85  	 */
86  	protected transient TableColumn tableColumn = null;
87  
88  	/***
89  	 * Whether the column is resizable or not.
90  	 */
91  	private boolean resizable = true;
92  
93  	/***
94  	 * The index of the column in the TableColumnModel.
95  	 */
96  	private int modelIndex = -1;
97  
98  	/***
99  	 * The headerValue of the TableColumn.
100 	 */
101 	private Object headerValue = null;
102 
103 	/***
104 	 * The identifier of the TableColumn.
105 	 */
106 	private Object identifier = null;
107 
108 	/***
109 	 * The width of the TableColumn.
110 	 */
111 	private int width = 0;
112 
113 	/***
114 	 * The minimum width of the TableColumn.
115 	 */
116 	private int minWidth = 0;
117 
118 	/***
119 	 * The maximum width of the TableColumn.
120 	 */
121 	private int maxWidth = 0;
122 
123 	/***
124 	 * @see org.jcreme.swing.table.state.TableColumnMemento#store(java.util.prefs.Preferences)
125 	 */
126 	public void store(Preferences preferences) throws MementoException {
127 		if (preferences != null) {
128 			preferences.putBoolean(RESIZABLE_PREFERENCE, this.resizable);
129 			preferences.putInt(MODEL_INDEX_PREFERENCE, this.modelIndex);
130 			preferences.putInt(COLUMN_WIDTH_PREFERENCE, this.width);
131 			preferences.putInt(MAX_COLUMN_WIDTH_PREFERENCE, this.maxWidth);
132 			preferences.putInt(MIN_COLUMN_WIDTH_PREFERENCE, this.minWidth);
133 			if (!(this.headerValue instanceof Serializable)) {
134 				this.headerValue = (this.headerValue != null ? this.headerValue
135 						.toString() : null);
136 			}
137 			storeObject((Serializable) this.headerValue, preferences,
138 					HEADER_VALUE_PREFERENCE);
139 			if (!(this.identifier instanceof Serializable)) {
140 				this.identifier = (this.identifier != null ? this.identifier
141 						.toString() : null);
142 			}
143 			storeObject((Serializable) this.identifier, preferences,
144 					IDENTIFIER_PREFERENCE);
145 		}
146 	}
147 
148 	/***
149 	 * @see org.jcreme.swing.table.state.TableColumnMemento#load(java.util.prefs.Preferences)
150 	 */
151 	public void load(Preferences preferences) throws MementoException {
152 		if (preferences != null) {
153 			this.resizable = preferences.getBoolean(RESIZABLE_PREFERENCE,
154 					this.resizable);
155 			this.modelIndex = preferences.getInt(MODEL_INDEX_PREFERENCE, -1);
156 			this.width = preferences.getInt(COLUMN_WIDTH_PREFERENCE, 0);
157 			this.minWidth = preferences.getInt(MIN_COLUMN_WIDTH_PREFERENCE, 0);
158 			this.maxWidth = preferences.getInt(MAX_COLUMN_WIDTH_PREFERENCE, 0);
159 			this.headerValue = loadObject(preferences, HEADER_VALUE_PREFERENCE);
160 			this.identifier = loadObject(preferences, IDENTIFIER_PREFERENCE);
161 		}
162 	}
163 
164 	/***
165 	 * @see org.jcreme.state.Memento#updateMemento(Object)
166 	 */
167 	public void updateMemento(Object o) throws IllegalArgumentException, MementoException {
168 		if (!(o instanceof TableColumn)) {
169 			throw new IllegalArgumentException("Expected a "
170 					+ TableColumn.class.getName());
171 		}
172 		this.resizable = this.tableColumn.getResizable();
173 		this.modelIndex = this.tableColumn.getModelIndex();
174 		this.headerValue = this.tableColumn.getHeaderValue();
175 		this.identifier = this.tableColumn.getIdentifier();
176 		this.width = this.tableColumn.getWidth();
177 		this.minWidth = this.tableColumn.getMinWidth();
178 		this.maxWidth = this.tableColumn.getMaxWidth();
179 	}
180 
181 	/***
182 	 * @see org.jcreme.state.Memento#updateObject(Object)
183 	 */
184 	public void updateObject(Object o) throws IllegalArgumentException, MementoException {
185 		if (!(o instanceof TableColumn)) {
186 			throw new IllegalArgumentException("Expected a "
187 					+ TableColumn.class.getName());
188 		}
189 		this.tableColumn.setModelIndex(this.modelIndex);
190 		this.tableColumn.setResizable(this.resizable);
191 		this.tableColumn.setHeaderValue(this.headerValue);
192 		this.tableColumn.setIdentifier(this.identifier);
193 		this.tableColumn.setWidth(this.width);
194 		this.tableColumn.setMinWidth(this.minWidth);
195 		this.tableColumn.setMaxWidth(this.maxWidth);
196 	}
197 
198 	/***
199 	 * This method stores a Serializable as a Byte array.
200 	 * 
201 	 * @param o
202 	 *            the object to store.
203 	 * @param p
204 	 *            the Preferences node to use.
205 	 * @param propertyName
206 	 *            the property name associated to the object.
207 	 */
208 	protected void storeObject(Serializable o, Preferences p,
209 			String propertyName) {
210 		ByteArrayOutputStream bos = new ByteArrayOutputStream();
211 		try {
212 			ObjectOutputStream os = new ObjectOutputStream(bos);
213 			os.writeObject(o);
214 			os.flush();
215 			p.putByteArray(propertyName, bos.toByteArray());
216 			os.close();
217 		} catch (IOException e) {
218 			e.printStackTrace();
219 		}
220 	}
221 
222 	/***
223 	 * This method restore a Serializable from a Byte array.
224 	 * 
225 	 * @param p
226 	 *            the Preferences node that contains the object.
227 	 * @param propertyName
228 	 *            the property name associated to the object.
229 	 * @return the Serializable object that was stored in the preferences.
230 	 */
231 	protected Serializable loadObject(Preferences p, String propertyName) {
232 		Serializable result = null;
233 		ByteArrayInputStream is = new ByteArrayInputStream(p.getByteArray(
234 				propertyName, new byte[0]));
235 		try {
236 			ObjectInputStream ois = new ObjectInputStream(is);
237 			result = (Serializable) ois.readObject();
238 			ois.close();
239 		} catch (IOException e) {
240 			e.printStackTrace();
241 		} catch (ClassNotFoundException e) {
242 			e.printStackTrace();
243 		}
244 		return result;
245 	}
246 
247 }