forked from LupusNobilis/java-org.hwo.ui
WindowStateManager
parent
1963bb40e0
commit
a87d15c980
|
@ -0,0 +1,10 @@
|
|||
package org.hwo.ui.states;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public interface IWindowState {
|
||||
|
||||
void getWindowState(Properties properties);
|
||||
void setWindowState(Properties properties);
|
||||
|
||||
}
|
|
@ -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()) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue