001package arcommon.persistens; 002 003import java.awt.Dialog; 004import java.awt.Frame; 005import java.awt.event.ActionListener; 006import java.util.Arrays; 007import java.util.Collection; 008import java.util.HashMap; 009import java.util.HashSet; 010import java.util.Iterator; 011import java.util.List; 012import java.util.Set; 013 014import javax.swing.JTable; 015 016import arcommon.ReflectionUtil; 017import arcommon.event.ActionList; 018import arcommon.ui.other.GUIUtil; 019import arcommon.ui.window.AbstractSettingsJDialog; 020import arcommon.ui.window.AbstractSettingsJFrame; 021 022public abstract class AbstractSettingWithFrameDialogAndTableSettings implements ISettings { 023 024 private static HashSet<String> validTableSettingsIds; 025 026 public static void setValidTableSettingsIds(String... pvalidTableSettingsIds) { 027 if (validTableSettingsIds != null) { 028 throw new RuntimeException("validTableSettingsIds is already set"); 029 } 030 031 validTableSettingsIds = new HashSet<String>(); 032 List<String> asList = Arrays.asList(pvalidTableSettingsIds); 033 validTableSettingsIds.addAll(asList); 034 } 035 036 public static void addValidTableSettingsId(String pvalidTableSettingsIds) { 037 if (validTableSettingsIds == null) { 038 validTableSettingsIds = new HashSet<String>(); 039 } 040 041 validTableSettingsIds.add(pvalidTableSettingsIds); 042 } 043 044 private static boolean settingsChecked = false; 045 046 private HashMap<String, FrameSettings> frames; 047 private transient ActionList frameListeners; 048 private HashMap<String, DialogSettings> dialogs; 049 private transient ActionList dialogListeners; 050 private HashMap<String, MyJTableSettings> tables; 051 private transient ActionList tableListeners; 052 053 public AbstractSettingWithFrameDialogAndTableSettings() { 054 ; 055 } 056 057 public void loadFrameSettings(String id, Frame frame) { 058 FrameSettings frameSettings = getFrameSettings(id, false); 059 if (frameSettings == null) { 060 GUIUtil.packAndCenterOnScreen(frame); 061 return ; 062 } 063 frameSettings.loadFrameSettings(frame); 064 } 065 066 public void saveFrameSettings(String id, Frame frame) { 067 FrameSettings frameSettings = getFrameSettings(id, true); 068 frameSettings.saveFrameSettings(frame); 069 } 070 071 public FrameSettings getFrameSettings(String id) { 072 if (frames == null) { 073 return null; 074 } 075 return frames.get(id); 076 } 077 078 public FrameSettings getFrameSettings(String id, boolean create) { 079 if (!settingsChecked) { 080 settingsChecked = true; 081 checkSettings(); 082 } 083 if (id == null) { 084 return null; 085 } 086 if (frames == null) { 087 if (!create) { 088 return null; 089 } 090 frames = new HashMap<String, FrameSettings>(); 091 } 092 FrameSettings frameSettings = getFrameSettings(id); 093 if (frameSettings == null) { 094 if (!create) { 095 return null; 096 } 097 frameSettings = new FrameSettings(); 098 frameSettings.addActionListeners(frameListeners); 099 frames.put(id, frameSettings); 100 } 101 return frameSettings; 102 } 103 104 public void removeFrameSettings(String id) { 105 if (frames == null) { 106 return ; 107 } 108 frames.remove(id); 109 } 110 111 public void addFrameActionListener(ActionListener pactionListener) { 112 if (frameListeners == null) { 113 frameListeners = new ActionList(); 114 } 115 frameListeners.addActionListener(pactionListener); 116 if (frames != null) { 117 Collection<FrameSettings> frameSettingsCollection = frames.values(); 118 for (FrameSettings frameSettings : frameSettingsCollection) { 119 frameSettings.addActionListener(pactionListener); 120 } 121 } 122 } 123 124 public boolean removeFrameActionListener(ActionListener pactionListener) { 125 if (frames != null) { 126 Collection<FrameSettings> frameSettingsCollection = frames.values(); 127 for (FrameSettings frameSettings : frameSettingsCollection) { 128 frameSettings.removeActionListener(pactionListener); 129 } 130 } 131 if (frameListeners == null) { 132 return false; 133 } 134 return frameListeners.removeActionListener(pactionListener); 135 } 136 137 public void loadDialogSettings(String id, Dialog dialog) { 138 DialogSettings dialogSettings = getDialogSettings(id, false); 139 if (dialogSettings == null) { 140 GUIUtil.packAndCenterOnScreen(dialog); 141 return ; 142 } 143 144 dialogSettings.loadDialogSettings(dialog); 145 } 146 147 public void saveDialogSettings(String id, Dialog dialog) { 148 DialogSettings dialogSettings = getDialogSettings(id, true); 149 dialogSettings.saveDialogSettings(dialog); 150 } 151 152 public DialogSettings getDialogSettings(String id) { 153 if (dialogs == null) { 154 return null; 155 } 156 return dialogs.get(id); 157 } 158 159 public DialogSettings getDialogSettings(String id, boolean create) { 160 if (!settingsChecked) { 161 settingsChecked = true; 162 checkSettings(); 163 } 164 if (id == null) { 165 return null; 166 } 167 if (dialogs == null) { 168 if (!create) { 169 return null; 170 } 171 dialogs = new HashMap<String, DialogSettings>(); 172 } 173 DialogSettings dialogSettings = getDialogSettings(id); 174 if (dialogSettings == null) { 175 if (!create) { 176 return null; 177 } 178 dialogSettings = new DialogSettings(); 179 dialogSettings.addActionListeners(dialogListeners); 180 dialogs.put(id, dialogSettings); 181 } 182 return dialogSettings; 183 } 184 185 public void removeDialogSettings(String id) { 186 if (dialogs == null) { 187 return ; 188 } 189 dialogs.remove(id); 190 } 191 192 public void addDialogActionListener(ActionListener pactionListener) { 193 if (dialogListeners == null) { 194 dialogListeners = new ActionList(); 195 } 196 dialogListeners.addActionListener(pactionListener); 197 if (dialogs != null) { 198 Collection<DialogSettings> dialogSettingsCollection = dialogs.values(); 199 for (DialogSettings dialogSettings : dialogSettingsCollection) { 200 dialogSettings.addActionListener(pactionListener); 201 } 202 } 203 } 204 205 public boolean removeDialogActionListener(ActionListener pactionListener) { 206 if (dialogs != null) { 207 Collection<DialogSettings> dialogSettingsCollection = dialogs.values(); 208 for (DialogSettings dialogSettings : dialogSettingsCollection) { 209 dialogSettings.removeActionListener(pactionListener); 210 } 211 } 212 if (dialogListeners == null) { 213 return false; 214 } 215 return dialogListeners.removeActionListener(pactionListener); 216 } 217 218 private void validateTableId(String id) { 219 if (validTableSettingsIds == null || !validTableSettingsIds.contains(id)) { 220 throw new RuntimeException("Table id \"" + id + "\" is not valid. Add it with AbstractSettingWithFrameDialogAndTableSettings.setValidTableSettingsIds()."); 221 } 222 } 223 224 public void loadTableSettings(String id, JTable table) { 225 validateTableId(id); 226 MyJTableSettings tableSettings = getTableSettings(id, false, null); 227 if (tableSettings == null) { 228 return ; 229 } 230 tableSettings.loadTableSettings(table); 231 } 232 233 public void saveTableSettings(String id, JTable table) { 234 validateTableId(id); 235 MyJTableSettings tableSettings = getTableSettings(id, true, null); 236 tableSettings.saveTableSettings(table); 237 } 238 239 public MyJTableSettings getTableSettings(String id) { 240 validateTableId(id); 241 if (tables == null) { 242 return null; 243 } 244 return tables.get(id); 245 } 246 247 public MyJTableSettings getTableSettings(String tableSettingsId, boolean create, MyJTableSettings defaultSettings) { 248 validateTableId(tableSettingsId); 249 if (create && defaultSettings != null) { 250 throw new IllegalArgumentException("Create = true && defaultSettings != null"); 251 } 252 if (tables == null) { 253 if (!create && defaultSettings == null) { 254 return null; 255 } 256 tables = new HashMap<String, MyJTableSettings>(); 257 } 258 MyJTableSettings tableSettings = getTableSettings(tableSettingsId); 259 if (tableSettings == null) { 260 if (!create && defaultSettings == null) { 261 return null; 262 } 263 if (defaultSettings != null) { 264 tableSettings = defaultSettings; 265 } else { 266 tableSettings = new MyJTableSettings(); 267 } 268 tableSettings.addActionListeners(tableListeners); 269 tables.put(tableSettingsId, tableSettings); 270 } 271 return tableSettings; 272 } 273 274 public void removeTableSettings(String id) { 275 validateTableId(id); 276 if (tables == null) { 277 return ; 278 } 279 tables.remove(id); 280 } 281 282 public void addTableActionListener(ActionListener pactionListener) { 283 if (tableListeners == null) { 284 tableListeners = new ActionList(); 285 } 286 tableListeners.addActionListener(pactionListener); 287 if (tables != null) { 288 Collection<MyJTableSettings> tableSettingsCollection = tables.values(); 289 for (MyJTableSettings tableSettings : tableSettingsCollection) { 290 tableSettings.addActionListener(pactionListener); 291 } 292 } 293 } 294 295 public boolean removeTableActionListener(ActionListener pactionListener) { 296 if (tables != null) { 297 Collection<MyJTableSettings> tableSettingsCollection = tables.values(); 298 for (MyJTableSettings tableSettings : tableSettingsCollection) { 299 tableSettings.removeActionListener(pactionListener); 300 } 301 } 302 if (tableListeners == null) { 303 return false; 304 } 305 return tableListeners.removeActionListener(pactionListener); 306 } 307 308 private void checkSettings() { 309 if (frames != null) { 310 Set<String> keySet = frames.keySet(); 311 Iterator<String> i = keySet.iterator(); 312 while (i.hasNext()) { 313 String id = i.next(); 314 Class<?> clazz = ReflectionUtil.loadClass(id, false); 315 if (clazz == null) { 316 System.out.println("The class \"" + id + "\" doesn't exist => Frame settings removed"); 317 i.remove(); 318 } else if (!AbstractSettingsJFrame.class.isAssignableFrom(clazz)) { 319 System.out.println("The class \"" + id + "\" is not an AbstractSettingsJFrame => Frame settings removed"); 320 i.remove(); 321 } 322 } 323 } 324 if (dialogs != null) { 325 Set<String> keySet = dialogs.keySet(); 326 Iterator<String> i = keySet.iterator(); 327 while (i.hasNext()) { 328 String id = i.next(); 329 Class<?> clazz = ReflectionUtil.loadClass(id, false); 330 if (clazz == null) { 331 System.out.println("The class \"" + id + "\" doesn't exist => Dialog settings removed"); 332 i.remove(); 333 } else if (!AbstractSettingsJDialog.class.isAssignableFrom(clazz)) { 334 System.out.println("The class \"" + id + "\" is not an AbstractSettingsJDialog => Dialog settings removed"); 335 i.remove(); 336 } 337 } 338 } 339 if (tables != null) { 340 Set<String> keySet = tables.keySet(); 341 Iterator<String> i = keySet.iterator(); 342 while (i.hasNext()) { 343 String id = i.next(); 344 if (validTableSettingsIds == null || !validTableSettingsIds.contains(id)) { 345 System.out.println("Table settings id \"" + id + "\" is not valid => Table settings removed"); 346 i.remove(); 347 } 348 } 349 } 350 } 351 352 private static final long serialVersionUID = 5435784736343522255L; 353}