All pastes #256099 Raw Edit

Someone

public java v1 · immutable
#256099 ·published 2006-11-23 19:16 UTC
rendered paste body
/* * WindowSaver.java * * Created on November 22, 2006, 7:41 PM * * WindowSaver - uses a file to store the width, height, and x and y positions * and allows that same window to reload this information and this script will * change the window accordingly */package games.stendhal.client.gui;import java.awt.AWTEvent;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.AWTEventListener;import java.awt.event.ActionEvent;import java.awt.event.ComponentEvent;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JOptionPane;/** * * @author zenix */public class WindowSaver implements AWTEventListener {        private static WindowSaver saver;    private Map framemap;    public static String configFileName = "window.properties";    private static File configFile = new File(configFileName);    configFile.createNewFile();        /** Creates a new instance of WindowSaver */    public WindowSaver() {        framemap = new HashMap();    }        public static WindowSaver getInstance() {        if (saver == null) {            saver = new WindowSaver();        }        return saver;    }    public void eventDispatched(AWTEvent event) {        try {            if(event.getID() == WindowEvent.WINDOW_OPENED) {                ComponentEvent cev = (ComponentEvent)event;                if (cev.getComponent() instanceof JFrame) {                    JFrame frame = (JFrame)cev.getComponent();                    loadSettings(frame);                }            }        } catch (Exception ex) {            JOptionPane.showMessageDialog(new JFrame(), ex.toString(),                                          "Exception occured", JOptionPane.ERROR_MESSAGE);        }    }        public static void loadSettings(JFrame frame) throws IOException {        Properties settings = new Properties();        settings.load(new FileInputStream(configFile));        String name = frame.getName();        int x = getInt(settings, name+".x", 100);        int y = getInt(settings, name+".y", 100);        int h = getInt(settings, name+".h", 500);        int w = getInt(settings, name+".w", 500);        frame.setLocation(x,y);        frame.setSize(new Dimension(w,h));        saver.framemap.put(name,frame);        frame.validate();    }        public static int getInt(Properties props, String name, int value) {        String v = props.getProperty(name);        if (v == null) {            return value;        }        return Integer.parseInt(v);    }        public static void saveSettings() throws IOException {        Properties settings = new Properties();        settings.load(new FileInputStream(configFile));                Iterator it = saver.framemap.keySet().iterator();        while (it.hasNext()) {            String name = (String)it.next();            JFrame frame = (JFrame)saver.framemap.get(name);            settings.setProperty(name+".x", ""+frame.getX());            settings.setProperty(name+".y", ""+frame.getY());            settings.setProperty(name+".h", ""+frame.getHeight());            settings.setProperty(name+".w", ""+frame.getWidth());        }        settings.store(new FileOutputStream("configuration.props"), null);    }        public static void main(String[] args) throws Exception {         Toolkit tk = Toolkit.getDefaultToolkit();         tk.addAWTEventListener(WindowSaver.getInstance(), AWTEvent.WINDOW_EVENT_MASK);                  final JFrame frame = new JFrame("WindowSaver");         frame.setName("WSTes.main");         frame.getContentPane().add(new JButton("move my window anywhere\nand make it any size! It'll remember!"));         JMenuBar mb = new JMenuBar();         JMenu menu = new JMenu("File");         menu.add(new AbstractAction("Quit") {             public void actionPerformed(ActionEvent evt) {                 try {                     WindowSaver.saveSettings();                     System.exit(0);                 } catch(Exception ex) {                     System.out.println(ex);                                      }             }         });         mb.add(menu);         frame.setJMenuBar(mb);         frame.pack();         frame.setVisible(true);    }}