001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.client.util;
021:
022: import java.awt.*;
023: import javax.swing.*;
024:
025: import org.lucane.client.LocalConfig;
026:
027: /**
028: * Used to store a widget (JFrame, JScrollPane, ...) in a LocalConfig
029: */
030: public class WidgetState {
031: /**
032: * Create a key based on the widget name
033: *
034: * @param component the widget
035: * @param property the property to store
036: * @return the key
037: */
038: private static String getKey(Component component, String property) {
039: String name = (component != null) ? component.getName()
040: : "null";
041: return "widget-" + name + "-" + property;
042: }
043:
044: /**
045: * Save a component
046: *
047: * @param config the LocalConfig
048: * @param widget the component
049: */
050: public static void save(LocalConfig config, Object widget) {
051: if (widget instanceof JFrame)
052: save(config, (JFrame) widget);
053: else if (widget instanceof Window)
054: save(config, (Window) widget);
055: else if (widget instanceof JSlider)
056: save(config, (JSlider) widget);
057: else if (widget instanceof JSplitPane)
058: save(config, (JSplitPane) widget);
059: }
060:
061: /**
062: * Restore a component
063: *
064: * @param config the LocalConfig
065: * @param widget the component
066: */
067: public static void restore(LocalConfig config, Object widget) {
068: if (widget instanceof JFrame)
069: restore(config, (JFrame) widget);
070: else if (widget instanceof Window)
071: restore(config, (Window) widget);
072: else if (widget instanceof JSlider)
073: restore(config, (JSlider) widget);
074: else if (widget instanceof JSplitPane)
075: restore(config, (JSplitPane) widget);
076: }
077:
078: /**
079: * Save a window
080: *
081: * @param config the LocalConfig
082: * @param window the Window
083: */
084: public static void save(LocalConfig config, Window window) {
085: config.set(getKey(window, "saved"), "true");
086: config
087: .set(getKey(window, "location.x"),
088: window.getLocation().x);
089: config
090: .set(getKey(window, "location.y"),
091: window.getLocation().y);
092: config.set(getKey(window, "height"), window.getHeight());
093: config.set(getKey(window, "width"), window.getWidth());
094: }
095:
096: /**
097: * Restore a window
098: *
099: * @param config the LocalConfig
100: * @param window the Window
101: */
102: public static void restore(LocalConfig config, Window window) {
103: String saved = config.get(getKey(window, "saved"));
104: if (saved == null)
105: return;
106:
107: int x = config.getInt(getKey(window, "location.x"));
108: int y = config.getInt(getKey(window, "location.y"));
109: window.setLocation(x, y);
110:
111: int height = config.getInt(getKey(window, "height"));
112: int width = config.getInt(getKey(window, "width"));
113: window.setSize(width, height);
114: }
115:
116: /**
117: * Save a frame
118: *
119: * @param config the LocalConfig
120: * @param frame the JFrame
121: */
122: public static void save(LocalConfig config, JFrame frame) {
123: save(config, (Window) frame);
124: config.set(getKey(frame, "extended"), frame.getExtendedState());
125: }
126:
127: /**
128: * Restore a frame
129: *
130: * @param config the LocalConfig
131: * @param frame the JFrame
132: */
133: public static void restore(LocalConfig config, JFrame frame) {
134: String saved = config.get(getKey(frame, "saved"));
135: if (saved == null)
136: return;
137:
138: restore(config, (Window) frame);
139: frame
140: .setExtendedState(config.getInt(getKey(frame,
141: "extended")));
142: }
143:
144: /**
145: * Save a slider
146: *
147: * @param config the LocalConfig
148: * @param slider the splider
149: */
150: public static void save(LocalConfig config, JSlider slider) {
151: config.set(getKey(slider, "saved"), "true");
152: config.set(getKey(slider, "value"), slider.getValue());
153: }
154:
155: /**
156: * Restore a slider
157: *
158: * @param config the LocalConfig
159: * @param slider the JSlider
160: */
161: public static void restore(LocalConfig config, JSlider slider) {
162: String saved = config.get(getKey(slider, "saved"));
163: if (saved == null)
164: return;
165:
166: slider.setValue(config.getInt(getKey(slider, "value")));
167: }
168:
169: /**
170: * Save a JSplitPane
171: *
172: * @param config the LocalConfig
173: * @param split the JSplitPane
174: */
175: public static void save(LocalConfig config, JSplitPane split) {
176: config.set(getKey(split, "saved"), "true");
177: config.set(getKey(split, "dividerLocation"), split
178: .getDividerLocation());
179: }
180:
181: /**
182: * Restore a JSplitPane
183: *
184: * @param config the LocalConfig
185: * @param split the JSplitPane
186: */
187: public static void restore(LocalConfig config, JSplitPane split) {
188: String saved = config.get(getKey(split, "saved"));
189: if (saved == null)
190: return;
191:
192: split.setDividerLocation(config.getInt(getKey(split,
193: "dividerLocation")));
194: }
195: }
|