SwingMLDesktop.java :  » XML-UI » SwingML » org » swingml » Java Open Source

Java Open Source » XML UI » SwingML 
SwingML » org » swingml » SwingMLDesktop.java
package org.swingml;

import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

import org.swingml.system.*;


public class SwingMLDesktop extends JFrame implements XMLStateTranslatable {

    private String propsDirectoryName = null;
    private String propsFileName = "/configuration.props";

    public SwingMLDesktop () {
        super();
        setName("SwingMLDesktop");
    }

    /**
     * Loads and applys the settings from the local file to this window.
     */
    public void applySettings () {
        Properties settings = loadSettings();

        Dimension theScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension theFrameSize = getSize();
        int theHorizontalPosition = (int) (theScreenSize.getWidth() - theFrameSize.getWidth()) / 2;
        int theVerticalPosition = (int) (theScreenSize.getHeight() - theFrameSize.getHeight()) / 2;

        String name = getName();
        int x = getInt(settings, name + ".x", theHorizontalPosition);
        int y = getInt(settings, name + ".y", theVerticalPosition);
        int w = getInt(settings, name + ".w", 800);
        int h = getInt(settings, name + ".h", 600);
        boolean max = Boolean.getBoolean(settings.getProperty(name + ".max", "false"));

        setLocation(x, y);
        setSize(new Dimension(w, h));
        setExtendedState(max ? MAXIMIZED_BOTH : NORMAL);
    }

    /**
     * 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.
     * 
     * @param props
     * @param name
     * @param value
     * @return
     */
    private int getInt (Properties props, String name, int value) {
        String v = props.getProperty(name);
        if (v == null) {
            return value;
        }
        return Integer.parseInt(v);
    }

    /**
     * Returns the name of the directory containing the local properties.
     * @return
     */
    private String getPropsDirectoryName () {
        return propsDirectoryName;
    }

    /**
     * Returns the full path to the local properties file (including directory name).
     * @return
     */
    public String getPropsFileLocation () {
        String result = getPropsFileName();
        String dir = getPropsDirectoryName();
        if (dir != null && dir.length() > 0) {
            result = dir + result;
        }
        return result;
    }

    /**
     * Returns the name of the properties file itself.
     * @return
     */
    public String getPropsFileName () {
        return propsFileName;
    }

    public String getXMLState () {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<WINDOW ");
        buffer.append(Constants.NAME);
        buffer.append("=\"");
        buffer.append(getName());
        buffer.append("\" ");
        buffer.append(Constants.WIDTH);
        buffer.append("=\"");
        buffer.append(getWidth());
        buffer.append("\" ");
        buffer.append(Constants.HEIGHT);
        buffer.append("=\"");
        buffer.append(getHeight());
        buffer.append("\" ");
        buffer.append(Constants.XOPEN);
        buffer.append("=\"");
        buffer.append(getX());
        buffer.append("\" ");
        buffer.append(Constants.YOPEN);
        buffer.append("=\"");
        buffer.append(getY());
        buffer.append("\" ");
        buffer.append(Constants.MAXIMUM);
        buffer.append("=\"");
        buffer.append(getExtendedState() == MAXIMIZED_BOTH ? Constants.TRUE : Constants.FALSE);
        buffer.append("\" />");
        return buffer.toString();
    }

    /**
     * Returns true if the given directory name has a properties file in it.
     * @param fileDir
     * @return
     */
    public boolean hasSettings () {
        boolean result = false;

        File file = new File(getPropsFileLocation());
        result = file.exists();

        return result;
    }

    public boolean hasState () {
        return true;
    }

    /**
     * Load the properties from the current local properties file.
     * @return
     */
    private Properties loadSettings () {
        Properties result = new Properties();
        try {
            result.load(new FileInputStream(getPropsFileLocation()));
        } catch (IOException ioe) {
            SwingMLLogger.getInstance().log(SwingMLLogger.ERROR, "Unable to open custom window property file: " + getPropsFileLocation());
        }

        return result;
    }

    /**
     * Deletes the current local properties file.
     */
    public void removeSettings () {
        File currentSettings = new File(getPropsFileLocation());
        if (currentSettings.exists()) {
            currentSettings.delete();
        }
    }

    /**
     * Saves the current settings to the properties file.
     *
     */
    public void saveSettings () {
        Properties settings = new Properties();
        File currentSettings = new File(getPropsFileLocation());
        try {
            if (!currentSettings.exists()) {
                currentSettings.createNewFile();
            } else {
                settings.load(new FileInputStream(currentSettings));
            }
        } catch (IOException ioe) {
            SwingMLLogger.getInstance().log(SwingMLLogger.ERROR, "Unable to create custom window property file " + getPropsFileName());
            return;
        }

        String name = getName();
        settings.setProperty(name + ".x", Integer.toString(getX()));
        settings.setProperty(name + ".y", Integer.toString(getY()));
        settings.setProperty(name + ".w", Integer.toString(getWidth()));
        settings.setProperty(name + ".h", Integer.toString(getHeight()));
        settings.setProperty(name + ".max", Boolean.toString(getExtendedState() == MAXIMIZED_BOTH ? true : false));
        try {
            settings.store(new FileOutputStream(currentSettings), null);
        } catch (IOException ioe) {
            SwingMLLogger.getInstance().log(SwingMLLogger.ERROR, "Unable to save custom window properties.");
            return;
        }
    }

    public void setPropsDirectoryName (String directoryName) {
        this.propsDirectoryName = directoryName;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.