001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.settings.support;
017
018 import java.awt.Frame;
019 import java.awt.Window;
020
021 import org.springframework.richclient.settings.Settings;
022 import org.springframework.util.Assert;
023 import org.springframework.util.StringUtils;
024
025 /**
026 * Memento for saving and restoring Window settings.
027 *
028 * @author Peter De Bruycker
029 */
030 public class WindowMemento implements Memento {
031
032 private Window window;
033
034 private String key;
035
036 public WindowMemento(Window window) {
037 this(window, null);
038 }
039
040 public WindowMemento(Window window, String key) {
041 Assert.notNull(window, "Window cannot be null");
042 Assert.isTrue(StringUtils.hasText(key) || StringUtils.hasText(window.getName()),
043 "Key is empty or window has no name");
044
045 if (!StringUtils.hasText(key)) {
046 key = window.getName();
047 }
048
049 this.window = window;
050 this.key = key;
051 }
052
053 public void saveState(Settings settings) {
054 saveLocation(settings);
055 saveSize(settings);
056 saveMaximizedState(settings);
057 }
058
059 void saveMaximizedState(Settings settings) {
060 if (window instanceof Frame) {
061 Frame frame = (Frame) window;
062 settings.setBoolean(key + ".maximized", frame.getExtendedState() == Frame.MAXIMIZED_BOTH);
063 }
064 }
065
066 void saveSize(Settings settings) {
067 settings.setInt(key + ".height", window.getHeight());
068 settings.setInt(key + ".width", window.getWidth());
069 }
070
071 void saveLocation(Settings settings) {
072 settings.setInt(key + ".x", window.getX());
073 settings.setInt(key + ".y", window.getY());
074 }
075
076 public void restoreState(Settings settings) {
077 restoreLocation(settings);
078 restoreSize(settings);
079 restoreMaximizedState(settings);
080 }
081
082 void restoreMaximizedState(Settings settings) {
083 if (window instanceof Frame) {
084 Frame frame = (Frame) window;
085 frame.setExtendedState((settings.getBoolean(key + ".maximized") ? Frame.MAXIMIZED_BOTH : Frame.NORMAL));
086 }
087 }
088
089 void restoreSize(Settings settings) {
090 if (settings.contains(key + ".height") && settings.contains(key + ".width")) {
091 window.setSize(settings.getInt(key + ".width"), settings.getInt(key + ".height"));
092 }
093 }
094
095 void restoreLocation(Settings settings) {
096 if (settings.contains(key + ".x") && settings.contains(key + ".y")) {
097 window.setLocation(settings.getInt(key + ".x"), settings.getInt(key + ".y"));
098 }
099 }
100
101 public Window getWindow() {
102 return window;
103 }
104
105 public String getKey() {
106 return key;
107 }
108 }