001package arcommon.persistens; 002 003import java.awt.Dimension; 004import java.awt.Window; 005 006import arcommon.event.ActionList; 007import arcommon.ui.other.GUIUtil; 008 009public abstract class AbstractWindowSettings extends ActionList implements ISettings { 010 011 private static final long serialVersionUID = -4937308873904925436L; 012 013 public static final String EVENT_WIDTH_CHANGED = "Width changed"; 014 public static final String EVENT_HEIGHT_CHANGED = "Height changed"; 015 public static final String EVENT_X_CHANGED = "X changed"; 016 public static final String EVENT_Y_CHANGED = "Y changed"; 017 018 private static final int NOT_INITIALIZED = -1; 019 020 public static final String EVENT_SETTINGS_CHANGED = "Settings changed"; 021 022 private int width; 023 private int height; 024 private int x; 025 private int y; 026 027 private transient boolean changedInProcess; 028 029 public AbstractWindowSettings() { 030 width = NOT_INITIALIZED; 031 height = NOT_INITIALIZED; 032 x = NOT_INITIALIZED; 033 y = NOT_INITIALIZED; 034 } 035 036 public AbstractWindowSettings(int pwidth, int pheight, int px, int py) { 037 width = pwidth; 038 height = pheight; 039 x = px; 040 y = py; 041 } 042 043 public boolean isInitialized() { 044 return getWidth() != NOT_INITIALIZED && getHeight() != NOT_INITIALIZED && getX() != NOT_INITIALIZED && getY() != NOT_INITIALIZED; 045 } 046 047 public void validate() { 048 Dimension screenSize = GUIUtil.getScreenSize(); 049 if (width > screenSize.width) { 050 System.out.println("width > screenSize.width " + this); 051 width = screenSize.width - 50; 052 } 053 if (height > screenSize.height) { 054 System.out.println("height > screenSize.height " + this); 055 height = screenSize.height - 50; 056 } 057 if (x + width > screenSize.width) { 058 System.out.println("x + width > screenSize.width " + this); 059 x = screenSize.width - width - 50; 060 } 061 if (y + height > screenSize.height) { 062 System.out.println("y + height > screenSize.height " + this); 063 y = screenSize.height - height - 50; 064 } 065 if (x < 0) { 066 x = 0; 067 } 068 if (y < 0) { 069 y = 0; 070 } 071 if (width < 10) { 072 width = 10; 073 } 074 if (height < 10) { 075 height = 10; 076 } 077 } 078 079 public void loadWindowSettings(Window window, boolean loadSize) { 080 if (!isInitialized()) { 081 GUIUtil.packAndCenterOnScreen(window); 082 return ; 083 } 084 085 changedInProcess = true; 086 validate(); 087 window.setLocation(getX(), getY()); 088 if (loadSize) { 089 window.setSize(getWidth(), getHeight()); 090 } else { 091 window.pack(); 092 } 093 changedInProcess = false; 094 } 095 096 public boolean saveWindowSettings(Window window, boolean saveSize) { 097 if (changedInProcess) { 098 return false; 099 } 100 101 if (saveSize) { 102 return set(window.getWidth(), window.getHeight(), window.getX(), window.getY()); 103 } 104 105 return set(0, 0, window.getX(), window.getY()); 106 } 107 108 public String toString() { 109 return "width: " + width + " height: " + height + " x: " + x + " y: " + y; 110 } 111 112 public int getWidth() { 113 return width; 114 } 115 116 public void setWidth(int pwidth) { 117 if (width == pwidth) { 118 return ; 119 } 120 121 width = pwidth; 122 fireActionListeners(EVENT_WIDTH_CHANGED); 123 } 124 125 public int getHeight() { 126 return height; 127 } 128 129 public void setHeight(int pheight) { 130 if (height == pheight) { 131 return ; 132 } 133 134 height = pheight; 135 fireActionListeners(EVENT_HEIGHT_CHANGED); 136 } 137 138 public int getX() { 139 return x; 140 } 141 142 public void setX(int px) { 143 if (x == px) { 144 return ; 145 } 146 147 x = px; 148 fireActionListeners(EVENT_X_CHANGED); 149 } 150 151 public int getY() { 152 return y; 153 } 154 155 public void setY(int py) { 156 if (y == py) { 157 return ; 158 } 159 160 y = py; 161 fireActionListeners(EVENT_Y_CHANGED); 162 } 163 164 public void set(AbstractWindowSettings abstractFrameDialogSettings) { 165 set(abstractFrameDialogSettings.width, abstractFrameDialogSettings.height, abstractFrameDialogSettings.x, abstractFrameDialogSettings.y); 166 } 167 168 public boolean set(int pwidth, int pheight, int px, int py) { 169 if (width == pwidth && height == pheight && x == px && y == py) { 170 return false; 171 } 172 173 width = pwidth; 174 height = pheight; 175 x = px; 176 y = py; 177 178 fireActionListeners(EVENT_SETTINGS_CHANGED); 179 180 return true; 181 } 182}