View Javadoc

1   /***
2    * BaseMemento.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.state;
27  
28  import java.awt.Color;
29  import java.awt.Dimension;
30  import java.awt.Point;
31  import java.util.prefs.Preferences;
32  
33  import org.jcreme.state.BaseMemento;
34  
35  /***
36   * This class is used as a base class for all the Creme Mementos. It specifies
37   * that the update of a Memento should be done through an updateMemento method.
38   * That method is also used to ensure that a Memento is up to date before it is
39   * serialized.
40   * 
41   * @author $Author: dbregeon $
42   * @version $Revision: 1.1 $
43   */
44  public abstract class BaseUIMemento extends BaseMemento {
45      /***
46       *  
47       */
48      public static final String RED_PREFERENCE = "red";
49  
50      /***
51       *  
52       */
53      public static final String GREEN_PREFERENCE = "green";
54  
55      /***
56       *  
57       */
58      public static final String BLUE_PREFERENCE = "blue";
59  
60      /***
61       *  
62       */
63      public static final String WIDTH_PREFERENCE = "width";
64  
65      /***
66       *  
67       */
68      public static final String HEIGHT_PREFERENCE = "height";
69  
70      /***
71       *  
72       */
73      public static final String X_PREFERENCE = "x";
74  
75      /***
76       *  
77       */
78      public static final String Y_PREFERENCE = "y";
79  
80      /***
81       * Enables to store a Color into the preferences as its RGB components.
82       * 
83       * @param color
84       *            the Color to store.
85       * @param node
86       *            the node to store the color in.
87       */
88      protected void storeColor(Color color, Preferences node) {
89          if (color != null) {
90              node.putInt(RED_PREFERENCE, color.getRed());
91              node.putInt(GREEN_PREFERENCE, color.getGreen());
92              node.putInt(BLUE_PREFERENCE, color.getBlue());
93          }
94      }
95  
96      /***
97       * Enables to recover a Color stored in the preferences as its RGB
98       * components.
99       * 
100      * @param node
101      *            the node in which the color was saved.
102      * @return the recovered Color.
103      */
104     protected Color loadColor(Preferences node) {
105         int red = node.getInt(RED_PREFERENCE, 255);
106         int green = node.getInt(GREEN_PREFERENCE, 255);
107         int blue = node.getInt(BLUE_PREFERENCE, 255);
108         return new Color(red, green, blue);
109     }
110 
111     /***
112      * Enables to store a Dimension into the preferences.
113      * 
114      * @param dimension
115      *            the Dimension to store.
116      * @param node
117      *            the node to store the Dimension in.
118      */
119     protected void storeDimension(Dimension dimension, Preferences node) {
120         if (dimension != null) {
121             node.putInt(HEIGHT_PREFERENCE, dimension.height);
122             node.putInt(WIDTH_PREFERENCE, dimension.width);
123         }
124     }
125 
126     /***
127      * Enables to recover Dimension stored in the preferences.
128      * 
129      * @param node
130      *            the node in which the Dimension was stored.
131      * @return the recovered Dimension.
132      */
133     protected Dimension loadDimension(Preferences node) {
134         int width = node.getInt(WIDTH_PREFERENCE, 0);
135         int height = node.getInt(HEIGHT_PREFERENCE, 0);
136         return new Dimension(width, height);
137     }
138 
139     /***
140      * Enables to store a Point into the preferences.
141      * 
142      * @param point
143      *            the Point to store.
144      * @param node
145      *            the node to store the Point in.
146      */
147     protected void storePoint(Point point, Preferences node) {
148         if (point != null) {
149             node.putInt(X_PREFERENCE, point.x);
150             node.putInt(Y_PREFERENCE, point.y);
151         }
152     }
153 
154     /***
155      * Enables to store a Point into the preferences.
156      * 
157      * @param node
158      *            the node in which the Point was stored.
159      * @return the recovered Point.
160      */
161     protected Point loadPoint(Preferences node) {
162         int x = node.getInt(X_PREFERENCE, 0);
163         int y = node.getInt(Y_PREFERENCE, 0);
164         return new Point(x, y);
165     }
166 
167 }