001: package org.swingml;
002:
003: import java.awt.*;
004: import java.io.*;
005: import java.util.*;
006:
007: import javax.swing.*;
008:
009: import org.swingml.system.*;
010:
011: public class SwingMLDesktop extends JFrame implements
012: XMLStateTranslatable {
013:
014: private String propsDirectoryName = null;
015: private String propsFileName = "/configuration.props";
016:
017: public SwingMLDesktop() {
018: super ();
019: setName("SwingMLDesktop");
020: }
021:
022: /**
023: * Loads and applys the settings from the local file to this window.
024: */
025: public void applySettings() {
026: Properties settings = loadSettings();
027:
028: Dimension theScreenSize = Toolkit.getDefaultToolkit()
029: .getScreenSize();
030: Dimension theFrameSize = getSize();
031: int theHorizontalPosition = (int) (theScreenSize.getWidth() - theFrameSize
032: .getWidth()) / 2;
033: int theVerticalPosition = (int) (theScreenSize.getHeight() - theFrameSize
034: .getHeight()) / 2;
035:
036: String name = getName();
037: int x = getInt(settings, name + ".x", theHorizontalPosition);
038: int y = getInt(settings, name + ".y", theVerticalPosition);
039: int w = getInt(settings, name + ".w", 800);
040: int h = getInt(settings, name + ".h", 600);
041: boolean max = Boolean.getBoolean(settings.getProperty(name
042: + ".max", "false"));
043:
044: setLocation(x, y);
045: setSize(new Dimension(w, h));
046: setExtendedState(max ? MAXIMIZED_BOTH : NORMAL);
047: }
048:
049: /**
050: * Evaluates the property identified by the given name in the given Properties and either returns its int value, or the default int value passed in.
051: *
052: * @param props
053: * @param name
054: * @param value
055: * @return
056: */
057: private int getInt(Properties props, String name, int value) {
058: String v = props.getProperty(name);
059: if (v == null) {
060: return value;
061: }
062: return Integer.parseInt(v);
063: }
064:
065: /**
066: * Returns the name of the directory containing the local properties.
067: * @return
068: */
069: private String getPropsDirectoryName() {
070: return propsDirectoryName;
071: }
072:
073: /**
074: * Returns the full path to the local properties file (including directory name).
075: * @return
076: */
077: public String getPropsFileLocation() {
078: String result = getPropsFileName();
079: String dir = getPropsDirectoryName();
080: if (dir != null && dir.length() > 0) {
081: result = dir + result;
082: }
083: return result;
084: }
085:
086: /**
087: * Returns the name of the properties file itself.
088: * @return
089: */
090: public String getPropsFileName() {
091: return propsFileName;
092: }
093:
094: public String getXMLState() {
095: StringBuffer buffer = new StringBuffer();
096: buffer.append("<WINDOW ");
097: buffer.append(Constants.NAME);
098: buffer.append("=\"");
099: buffer.append(getName());
100: buffer.append("\" ");
101: buffer.append(Constants.WIDTH);
102: buffer.append("=\"");
103: buffer.append(getWidth());
104: buffer.append("\" ");
105: buffer.append(Constants.HEIGHT);
106: buffer.append("=\"");
107: buffer.append(getHeight());
108: buffer.append("\" ");
109: buffer.append(Constants.XOPEN);
110: buffer.append("=\"");
111: buffer.append(getX());
112: buffer.append("\" ");
113: buffer.append(Constants.YOPEN);
114: buffer.append("=\"");
115: buffer.append(getY());
116: buffer.append("\" ");
117: buffer.append(Constants.MAXIMUM);
118: buffer.append("=\"");
119: buffer
120: .append(getExtendedState() == MAXIMIZED_BOTH ? Constants.TRUE
121: : Constants.FALSE);
122: buffer.append("\" />");
123: return buffer.toString();
124: }
125:
126: /**
127: * Returns true if the given directory name has a properties file in it.
128: * @param fileDir
129: * @return
130: */
131: public boolean hasSettings() {
132: boolean result = false;
133:
134: File file = new File(getPropsFileLocation());
135: result = file.exists();
136:
137: return result;
138: }
139:
140: public boolean hasState() {
141: return true;
142: }
143:
144: /**
145: * Load the properties from the current local properties file.
146: * @return
147: */
148: private Properties loadSettings() {
149: Properties result = new Properties();
150: try {
151: result.load(new FileInputStream(getPropsFileLocation()));
152: } catch (IOException ioe) {
153: SwingMLLogger.getInstance().log(
154: SwingMLLogger.ERROR,
155: "Unable to open custom window property file: "
156: + getPropsFileLocation());
157: }
158:
159: return result;
160: }
161:
162: /**
163: * Deletes the current local properties file.
164: */
165: public void removeSettings() {
166: File currentSettings = new File(getPropsFileLocation());
167: if (currentSettings.exists()) {
168: currentSettings.delete();
169: }
170: }
171:
172: /**
173: * Saves the current settings to the properties file.
174: *
175: */
176: public void saveSettings() {
177: Properties settings = new Properties();
178: File currentSettings = new File(getPropsFileLocation());
179: try {
180: if (!currentSettings.exists()) {
181: currentSettings.createNewFile();
182: } else {
183: settings.load(new FileInputStream(currentSettings));
184: }
185: } catch (IOException ioe) {
186: SwingMLLogger.getInstance().log(
187: SwingMLLogger.ERROR,
188: "Unable to create custom window property file "
189: + getPropsFileName());
190: return;
191: }
192:
193: String name = getName();
194: settings.setProperty(name + ".x", Integer.toString(getX()));
195: settings.setProperty(name + ".y", Integer.toString(getY()));
196: settings.setProperty(name + ".w", Integer.toString(getWidth()));
197: settings
198: .setProperty(name + ".h", Integer.toString(getHeight()));
199: settings.setProperty(name + ".max", Boolean
200: .toString(getExtendedState() == MAXIMIZED_BOTH ? true
201: : false));
202: try {
203: settings.store(new FileOutputStream(currentSettings), null);
204: } catch (IOException ioe) {
205: SwingMLLogger.getInstance().log(SwingMLLogger.ERROR,
206: "Unable to save custom window properties.");
207: return;
208: }
209: }
210:
211: public void setPropsDirectoryName(String directoryName) {
212: this.propsDirectoryName = directoryName;
213: }
214: }
|