From a87d15c980a1ede652c0c21fbeaa1feb626c0830 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Thu, 15 Sep 2016 10:30:09 +0200 Subject: [PATCH] WindowStateManager --- src/org/hwo/ui/states/IWindowState.java | 10 ++ src/org/hwo/ui/states/WindowStateManager.java | 163 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/org/hwo/ui/states/IWindowState.java create mode 100644 src/org/hwo/ui/states/WindowStateManager.java diff --git a/src/org/hwo/ui/states/IWindowState.java b/src/org/hwo/ui/states/IWindowState.java new file mode 100644 index 0000000..da4bbee --- /dev/null +++ b/src/org/hwo/ui/states/IWindowState.java @@ -0,0 +1,10 @@ +package org.hwo.ui.states; + +import java.util.Properties; + +public interface IWindowState { + + void getWindowState(Properties properties); + void setWindowState(Properties properties); + +} diff --git a/src/org/hwo/ui/states/WindowStateManager.java b/src/org/hwo/ui/states/WindowStateManager.java new file mode 100644 index 0000000..122e301 --- /dev/null +++ b/src/org/hwo/ui/states/WindowStateManager.java @@ -0,0 +1,163 @@ +package org.hwo.ui.states; + +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Point; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Properties; + +public class WindowStateManager { + + + public String saveState(Component component){ + WindowState ws = createWindowState(component); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + try { + ws.properties.storeToXML(os, "WindowState"); + } catch (IOException e) { + e.printStackTrace(); + } + + return new String(os.toByteArray()); + } + + public void applyState(Component c,String state){ + WindowState ws = createWindowState(state); + applyState(c,ws); + } + + private void applyState(Component c,WindowState ws){ + + if (ws.dimension != null){ + c.setSize(ws.dimension); + } + if (ws.location != null){ + c.setLocation(ws.location); + } + + if (IWindowState.class.isInstance(c)){ + IWindowState iws = (IWindowState)c; + iws.setWindowState(ws.properties); + } + } + + public Component createComponent(String state){ + return createComponent( createWindowState(state)); + } + + private WindowState createWindowState(String state){ + ByteArrayInputStream is = new ByteArrayInputStream(state.getBytes()); + Properties properties = new Properties(); + + try { + properties.loadFromXML(is); + } catch (IOException e) { + e.printStackTrace(); + } + + return new WindowState(properties); + } + + private WindowState createWindowState(Component c){ + WindowState ws = new WindowState(); + + ws.setClazz(c.getClass()); + ws.setLocation(c.getLocation()); + ws.setDimension(c.getSize()); + + if (IWindowState.class.isInstance(c)){ + IWindowState iws = (IWindowState)c; + iws.getWindowState(ws.properties); + } + + return ws; + } + + private WindowState createWindowState(Properties properties){ + return new WindowState(properties); + } + + private Component createComponent(WindowState ws){ + Component c = null; + + try { + c = (Component) ws.clazz.newInstance(); + + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + return c; + } + + + + class WindowState { + + Class clazz; + Point location; + Dimension dimension; + + Properties properties; + + + public WindowState(){ + this.properties = new Properties(); + } + public WindowState(Properties properties){ + this.properties = properties; + + if (properties.containsKey("WindowState.clazz")){ + try { + this.clazz = this.getClass().getClassLoader().loadClass(properties.getProperty("WindowState.clazz")); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + if (properties.containsKey("WindowState.dimension.width") && properties.containsKey("WindowState.dimension.height")){ + this.dimension = new Dimension(); + this.dimension.setSize( Double.parseDouble( this.properties.getProperty("WindowState.dimension.width") ), Double.parseDouble( this.properties.getProperty("WindowState.dimension.height") )); + } + + if (properties.containsKey("WindowState.location.x") && properties.containsKey("WindowState.location.y")){ + this.location = new Point(); + this.location.setLocation( Double.parseDouble( this.properties.getProperty("WindowState.location.x") ), Double.parseDouble( this.properties.getProperty("WindowState.location.y") )); + } + } + + public Class getClazz() { + return clazz; + } + public void setClazz(Class clazz) { + this.clazz = clazz; + + this.properties.setProperty("WindowState.clazz", clazz.getCanonicalName()); + } + public Dimension getDimension() { + return dimension; + } + public void setDimension(Dimension dimension) { + this.dimension = dimension; + + this.properties.setProperty("WindowState.dimension.width", Double.toString(dimension.getWidth()) ); + this.properties.setProperty("WindowState.dimension.height", Double.toString(dimension.getHeight()) ); + } + public Point getLocation() { + return location; + } + public void setLocation(Point location) { + this.location = location; + this.properties.setProperty("WindowState.location.x", Double.toString(location.getX()) ); + this.properties.setProperty("WindowState.location.y", Double.toString(location.getY()) ); + } + + + } + +}