1 /***
2 * FrameMemento.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
27 package org.jcreme.swing.state;
28
29 import java.awt.Dimension;
30 import java.awt.Frame;
31 import java.awt.Point;
32 import java.util.prefs.Preferences;
33
34 import org.jcreme.state.Memento;
35 import org.jcreme.state.MementoException;
36
37 /***
38 *
39 * @author $Author: dbregeon $
40 * @version $Version$
41 */
42 public class FrameMemento extends BaseUIMemento {
43 /***
44 *
45 */
46 public static final String SIZE_PREFERENCE = "size";
47
48 /***
49 *
50 */
51 public static final String LOCATION_PREFERENCE = "location";
52
53 /***
54 *
55 */
56 public static final String EXTENDED_STATE_PREFERENCE = "extendedState";
57
58 private Dimension size = new Dimension(100, 100);
59
60 private Point location = new Point(0, 0);
61
62 private int extendedState = Frame.NORMAL;
63
64 /***
65 * @see Memento#updateMemento(Object)
66 */
67 public void updateMemento(Object o) throws IllegalArgumentException, MementoException {
68 if (!(o instanceof Frame)) {
69 throw new IllegalArgumentException("Expected a "
70 + Frame.class.getName());
71 }
72 Frame frame = (Frame) o;
73 this.size = frame.getSize();
74 this.location = frame.getLocation();
75 this.extendedState = frame.getExtendedState();
76 }
77
78 /***
79 * @see Memento#updateObject(Object)
80 */
81 public void updateObject(Object o) throws IllegalArgumentException, MementoException {
82 if (!(o instanceof Frame)) {
83 throw new IllegalArgumentException("Expected a "
84 + Frame.class.getName());
85 }
86 Frame frame = (Frame) o;
87 frame.setExtendedState(this.extendedState);
88 frame.setSize(this.size);
89 frame.setLocation(this.location);
90 }
91
92 /***
93 * @see org.jcreme.state.Memento#load(java.util.prefs.Preferences)
94 */
95 public void load(Preferences preferences) throws MementoException {
96 if (preferences != null) {
97 this.extendedState = preferences.getInt(EXTENDED_STATE_PREFERENCE,
98 Frame.NORMAL);
99 this.size = loadDimension(preferences.node(SIZE_PREFERENCE));
100 this.location = loadPoint(preferences.node(LOCATION_PREFERENCE));
101 }
102 }
103
104 /***
105 * @see org.jcreme.state.Memento#store(java.util.prefs.Preferences)
106 */
107 public void store(Preferences preferences) throws MementoException {
108 if (preferences != null) {
109 preferences.putInt(EXTENDED_STATE_PREFERENCE, this.extendedState);
110 storeDimension(this.size, preferences.node(SIZE_PREFERENCE));
111 storePoint(this.location, preferences.node(LOCATION_PREFERENCE));
112 }
113
114 }
115 }