KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > util > WidgetState


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.client.util;
21
22 import java.awt.*;
23 import javax.swing.*;
24
25 import org.lucane.client.LocalConfig;
26
27 /**
28  * Used to store a widget (JFrame, JScrollPane, ...) in a LocalConfig
29  */

30 public class WidgetState
31 {
32     /**
33      * Create a key based on the widget name
34      *
35      * @param component the widget
36      * @param property the property to store
37      * @return the key
38      */

39     private static String JavaDoc getKey(Component component, String JavaDoc property)
40     {
41         String JavaDoc name = (component != null) ? component.getName() : "null";
42         return "widget-" + name + "-" + property;
43     }
44     
45     /**
46      * Save a component
47      *
48      * @param config the LocalConfig
49      * @param widget the component
50      */

51     public static void save(LocalConfig config, Object JavaDoc widget)
52     {
53         if(widget instanceof JFrame)
54             save(config, (JFrame)widget);
55         else if(widget instanceof Window)
56             save(config, (Window)widget);
57         else if(widget instanceof JSlider)
58             save(config, (JSlider)widget);
59         else if(widget instanceof JSplitPane)
60             save(config, (JSplitPane)widget);
61     }
62     
63     /**
64      * Restore a component
65      *
66      * @param config the LocalConfig
67      * @param widget the component
68      */

69     public static void restore(LocalConfig config, Object JavaDoc widget)
70     {
71         if(widget instanceof JFrame)
72             restore(config, (JFrame)widget);
73         else if(widget instanceof Window)
74             restore(config, (Window)widget);
75         else if(widget instanceof JSlider)
76             restore(config, (JSlider)widget);
77         else if(widget instanceof JSplitPane)
78             restore(config, (JSplitPane)widget);
79     }
80
81     /**
82      * Save a window
83      *
84      * @param config the LocalConfig
85      * @param window the Window
86      */

87     public static void save(LocalConfig config, Window window)
88     {
89         config.set(getKey(window, "saved"), "true");
90         config.set(getKey(window, "location.x"), window.getLocation().x);
91         config.set(getKey(window, "location.y"), window.getLocation().y);
92         config.set(getKey(window, "height"), window.getHeight());
93         config.set(getKey(window, "width"), window.getWidth());
94     }
95     
96     /**
97      * Restore a window
98      *
99      * @param config the LocalConfig
100      * @param window the Window
101      */

102     public static void restore(LocalConfig config, Window window)
103     {
104         String JavaDoc saved = config.get(getKey(window, "saved"));
105         if(saved == null)
106             return;
107
108         int x = config.getInt(getKey(window, "location.x"));
109         int y = config.getInt(getKey(window, "location.y"));
110         window.setLocation(x, y);
111         
112         int height = config.getInt(getKey(window, "height"));
113         int width = config.getInt(getKey(window, "width"));
114         window.setSize(width, height);
115     }
116     
117     /**
118      * Save a frame
119      *
120      * @param config the LocalConfig
121      * @param frame the JFrame
122      */

123     public static void save(LocalConfig config, JFrame frame)
124     {
125         save(config, (Window)frame);
126         config.set(getKey(frame, "extended"), frame.getExtendedState());
127     }
128     
129     /**
130      * Restore a frame
131      *
132      * @param config the LocalConfig
133      * @param frame the JFrame
134      */

135     public static void restore(LocalConfig config, JFrame frame)
136     {
137         String JavaDoc saved = config.get(getKey(frame, "saved"));
138         if(saved == null)
139             return;
140         
141         restore(config, (Window)frame);
142         frame.setExtendedState(config.getInt(getKey(frame, "extended")));
143     }
144     
145     /**
146      * Save a slider
147      *
148      * @param config the LocalConfig
149      * @param slider the splider
150      */

151     public static void save(LocalConfig config, JSlider slider)
152     {
153         config.set(getKey(slider, "saved"), "true");
154         config.set(getKey(slider, "value"), slider.getValue());
155     }
156     
157     /**
158      * Restore a slider
159      *
160      * @param config the LocalConfig
161      * @param slider the JSlider
162      */

163     public static void restore(LocalConfig config, JSlider slider)
164     {
165         String JavaDoc saved = config.get(getKey(slider, "saved"));
166         if(saved == null)
167             return;
168         
169         slider.setValue(config.getInt(getKey(slider, "value")));
170     }
171     
172     /**
173      * Save a JSplitPane
174      *
175      * @param config the LocalConfig
176      * @param split the JSplitPane
177      */

178     public static void save(LocalConfig config, JSplitPane split)
179     {
180         config.set(getKey(split, "saved"), "true");
181         config.set(getKey(split, "dividerLocation"), split.getDividerLocation());
182     }
183     
184     /**
185      * Restore a JSplitPane
186      *
187      * @param config the LocalConfig
188      * @param split the JSplitPane
189      */

190     public static void restore(LocalConfig config, JSplitPane split)
191     {
192         String JavaDoc saved = config.get(getKey(split, "saved"));
193         if(saved == null)
194             return;
195         
196         split.setDividerLocation(config.getInt(getKey(split, "dividerLocation")));
197     }
198 }
Popular Tags