001/*
002 * Copyright (c) 2004-2007 by Michael Connor. All Rights Reserved.
003 *
004 * Redistribution and use in source and binary forms, with or without
005 * modification, are permitted provided that the following conditions are met:
006 *
007 *  o Redistributions of source code must retain the above copyright notice,
008 *    this list of conditions and the following disclaimer.
009 *
010 *  o Redistributions in binary form must reproduce the above copyright notice,
011 *    this list of conditions and the following disclaimer in the documentation
012 *    and/or other materials provided with the distribution.
013 *
014 *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of
015 *    its contributors may be used to endorse or promote products derived
016 *    from this software without specific prior written permission.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030
031/*
032 * UserPrefs.java
033 *
034 * Created on March 23, 2005, 7:54 PM by Kevin Routley.
035 */
036
037package org.mlc.swing.layout;
038
039import java.awt.Dimension;
040import java.awt.Point;
041import java.awt.Rectangle;
042import java.awt.Window;
043import java.util.prefs.Preferences;
044
045/**
046 * This is a singleton container for handling user preferences. These include
047 * window size and positions, debug frame visibility, etc.
048 * @author Kevin Routley
049@version $Id$
050@since Ptolemy II 8.0
051 */
052public class UserPrefs {
053
054    private Preferences prefStore = null;
055
056    static UserPrefs usersPrefs = new UserPrefs();
057
058    /**
059     * Access the singleton instance.
060     */
061    public static UserPrefs getPrefs() {
062        return usersPrefs;
063    }
064
065    /**
066     * Creates a new instance of UserPrefs - private to prevent external
067     * instantiation
068     */
069    private UserPrefs() {
070        prefStore = Preferences.userNodeForPackage(
071                org.mlc.swing.layout.LayoutConstraintsManager.class);
072    }
073
074    boolean showPreviewBorder() {
075        return prefStore.get("preview_border", "1").equals("1");
076    }
077
078    /**
079     * Should we show the debug panel?
080     */
081    public boolean showDebugPanel() {
082        return prefStore.get("debug_panel", "0").equals("1");
083    }
084
085    void saveWinLoc(String winname, Point screenloc, Dimension size) {
086        prefStore.putInt(winname + "_WinX", screenloc.x < 0 ? 0 : screenloc.x);
087        prefStore.putInt(winname + "_WinY", screenloc.y < 0 ? 0 : screenloc.y);
088        prefStore.putInt(winname + "_WinH", size.height);
089        prefStore.putInt(winname + "_WinW", size.width);
090    }
091
092    Rectangle getWinLoc(String winname) {
093        Rectangle r = new Rectangle(prefStore.getInt(winname + "_WinX", 0),
094                prefStore.getInt(winname + "_WinY", 0),
095                prefStore.getInt(winname + "_WinW", 800),
096                prefStore.getInt(winname + "_WinH", 600));
097        return r;
098    }
099
100    /**
101     * Save the current state of the panel - debug on/off.
102     */
103    public void saveDebugState(boolean b) {
104        prefStore.put("debug_panel", b ? "1" : "0");
105    }
106
107    /** Fetch a window's size and position data and apply it. The window name
108     * must be the same as supplied when calling @see saveWinLoc.
109     */
110    public void useSavedBounds(String winname, Window window) {
111        if (window == null) {
112            return;
113        }
114        Rectangle r = UserPrefs.getPrefs().getWinLoc(winname);
115        window.setLocation(r.x, r.y);
116        window.setSize(r.width, r.height);
117    }
118
119    /** Save a window's size and position data under the provided name.
120     * (The data must be fetched using the same name).
121     */
122    public void saveWinLoc(String winname, Window window) {
123        if (window == null) {
124            return;
125        }
126        saveWinLoc(winname, window.getLocationOnScreen(), window.getSize());
127    }
128}