001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing;
018:
019: import com.l2fprod.common.util.converter.ConverterRegistry;
020:
021: import java.awt.Rectangle;
022: import java.awt.Window;
023: import java.awt.event.ComponentAdapter;
024: import java.awt.event.ComponentEvent;
025: import java.awt.event.ComponentListener;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.io.File;
029: import java.util.prefs.Preferences;
030:
031: import javax.swing.JFileChooser;
032: import javax.swing.JSplitPane;
033: import javax.swing.event.DocumentListener;
034: import javax.swing.text.JTextComponent;
035:
036: /**
037: * UserPreferences. <BR>
038: *
039: */
040: public class UserPreferences {
041:
042: /**
043: * Gets the default file chooser. Its current directory will be tracked and
044: * restored on subsequent calls.
045: *
046: * @return the default file chooser
047: */
048: public static JFileChooser getDefaultFileChooser() {
049: return getFileChooser("default");
050: }
051:
052: /**
053: * Gets the default directory chooser. Its current directory will be tracked
054: * and restored on subsequent calls.
055: *
056: * @return the default directory chooser
057: */
058: public static JFileChooser getDefaultDirectoryChooser() {
059: return getDirectoryChooser("default");
060: }
061:
062: /**
063: * Gets the file chooser with the given id. Its current directory will be
064: * tracked and restored on subsequent calls.
065: *
066: * @param id
067: * @return the file chooser with the given id
068: */
069: public static JFileChooser getFileChooser(final String id) {
070: JFileChooser chooser = new JFileChooser();
071: track(chooser, "FileChooser." + id + ".path");
072: return chooser;
073: }
074:
075: /**
076: * Gets the directory chooser with the given id. Its current directory will
077: * be tracked and restored on subsequent calls.
078: *
079: * @param id
080: * @return the directory chooser with the given id
081: */
082: public static JFileChooser getDirectoryChooser(String id) {
083: JFileChooser chooser;
084: try {
085: Class directoryChooserClass = Class
086: .forName("com.l2fprod.common.swing.JDirectoryChooser");
087: chooser = (JFileChooser) directoryChooserClass
088: .newInstance();
089: } catch (Exception e) {
090: chooser = new JFileChooser();
091: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
092: }
093: track(chooser, "DirectoryChooser." + id + ".path");
094: return chooser;
095: }
096:
097: private static void track(JFileChooser chooser, final String key) {
098: // get the path for the given filechooser
099: String path = node().get(key, null);
100: if (path != null) {
101: File file = new File(path);
102: if (file.exists()) {
103: chooser.setCurrentDirectory(file);
104: }
105: }
106:
107: PropertyChangeListener trackPath = new PropertyChangeListener() {
108: public void propertyChange(PropertyChangeEvent evt) {
109: /* everytime the path change, update the preferences */
110: if (evt.getNewValue() instanceof File) {
111: node().put(
112: key,
113: ((File) evt.getNewValue())
114: .getAbsolutePath());
115: }
116: }
117: };
118:
119: chooser.addPropertyChangeListener(
120: JFileChooser.DIRECTORY_CHANGED_PROPERTY, trackPath);
121: }
122:
123: /**
124: * Restores the window size, position and state if possible. Tracks the
125: * window size, position and state.
126: *
127: * @param window
128: */
129: public static void track(Window window) {
130: Preferences prefs = node().node("Windows");
131:
132: String bounds = prefs.get(window.getName() + ".bounds", null);
133: if (bounds != null) {
134: Rectangle rect = (Rectangle) ConverterRegistry.instance()
135: .convert(Rectangle.class, bounds);
136: window.setBounds(rect);
137: }
138:
139: window.addComponentListener(windowDimension);
140: }
141:
142: private static ComponentListener windowDimension = new ComponentAdapter() {
143: public void componentMoved(ComponentEvent e) {
144: store((Window) e.getComponent());
145: }
146:
147: public void componentResized(ComponentEvent e) {
148: store((Window) e.getComponent());
149: }
150:
151: private void store(Window w) {
152: String bounds = (String) ConverterRegistry.instance()
153: .convert(String.class, w.getBounds());
154: node().node("Windows").put(w.getName() + ".bounds", bounds);
155: }
156: };
157:
158: /**
159: * Restores the text. Stores the text.
160: *
161: * @param text
162: */
163: public static void track(JTextComponent text) {
164: new TextListener(text);
165: }
166:
167: private static class TextListener implements DocumentListener {
168: private JTextComponent text;
169:
170: public TextListener(JTextComponent text) {
171: this .text = text;
172: restore();
173: text.getDocument().addDocumentListener(this );
174: }
175:
176: public void changedUpdate(javax.swing.event.DocumentEvent e) {
177: store();
178: }
179:
180: public void insertUpdate(javax.swing.event.DocumentEvent e) {
181: store();
182: }
183:
184: public void removeUpdate(javax.swing.event.DocumentEvent e) {
185: store();
186: }
187:
188: void restore() {
189: Preferences prefs = node().node("JTextComponent");
190: text.setText(prefs.get(text.getName(), ""));
191: }
192:
193: void store() {
194: Preferences prefs = node().node("JTextComponent");
195: prefs.put(text.getName(), text.getText());
196: }
197: };
198:
199: public static void track(JSplitPane split) {
200: Preferences prefs = node().node("JSplitPane");
201:
202: // restore the previous location
203: int dividerLocation = prefs.getInt(split.getName()
204: + ".dividerLocation", -1);
205: if (dividerLocation >= 0) {
206: split.setDividerLocation(dividerLocation);
207: }
208:
209: // track changes
210: split
211: .addPropertyChangeListener(
212: JSplitPane.DIVIDER_LOCATION_PROPERTY,
213: splitPaneListener);
214: }
215:
216: private static PropertyChangeListener splitPaneListener = new PropertyChangeListener() {
217: public void propertyChange(PropertyChangeEvent evt) {
218: JSplitPane split = (JSplitPane) evt.getSource();
219: node().node("JSplitPane").put(
220: split.getName() + ".dividerLocation",
221: String.valueOf(split.getDividerLocation()));
222: }
223: };
224:
225: /**
226: * @return the Preference node where User Preferences are stored.
227: */
228: private static Preferences node() {
229: return Preferences.userNodeForPackage(UserPreferences.class)
230: .node("UserPreferences");
231: }
232:
233: }
|