1 6 package com.nightlabs.ipanema.config; 7 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import javax.jdo.FetchPlan; 13 14 import org.eclipse.jface.preference.PreferencePage; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.ui.IWorkbench; 23 import org.eclipse.ui.IWorkbenchPreferencePage; 24 25 import com.nightlabs.ipanema.base.login.Login; 26 import com.nightlabs.ipanema.config.id.ConfigID; 27 import com.nightlabs.rcp.composite.OrdinaryWrapperComposite; 28 29 33 public abstract class AbstractConfigModulePreferencePage 34 extends PreferencePage 35 implements IWorkbenchPreferencePage 36 { 37 38 protected static String [] CONFIG_MODULE_FETCH_GROUPS = new String [] {FetchPlan.DEFAULT, FetchPlan.VALUES, FetchPlan.ALL}; 39 40 private OrdinaryWrapperComposite wrapper; 41 private Button checkBoxAllowOverwriteByUser; 42 43 protected UserConfigModule currentConfigModule; 44 protected ConfigID currentUserConfigID = null; 45 protected boolean configChanged = false; 46 47 50 public AbstractConfigModulePreferencePage() { 51 super(); 52 } 53 54 57 public AbstractConfigModulePreferencePage(String title) { 58 super(title); 59 } 60 61 65 public AbstractConfigModulePreferencePage(String title, ImageDescriptor image) { 66 super(title, image); 67 } 68 69 75 protected boolean isUserConfigSelectionAllowed() { 76 return true; 77 } 78 79 protected UserConfigModule getCurrentConfigModule() { 80 return currentConfigModule; 81 } 82 83 public void setCurrentConfigModule(UserConfigModule configModule) { 84 this.currentConfigModule = configModule; 85 } 86 87 88 public boolean isConfigChanged() { 89 return configChanged; 90 } 91 public void setConfigChanged(boolean configChanged) { 92 this.configChanged = configChanged; 93 if (configChanged) 94 notifyConfigChangedListeners(); 95 } 96 97 protected UserConfigModule getCurrentUsersConfigModule() { 98 ConfigManager configManager = null; 99 try { 100 configManager = ConfigManagerUtil.getHome(Login.getLogin().getInitialContextProperties()).create(); 101 } catch (Throwable e) { 102 throw new RuntimeException (e); 103 } 104 try { 105 return configManager.getUserConfigModule( 106 getConfigModuleClass(), 107 getConfigModuleCfModID(), 108 getConfigModuleFetchGroups() 109 ); 110 } catch (Exception e) { 111 throw new RuntimeException (e); 112 } 113 } 114 115 118 private boolean refreshConfigModule = true; 119 122 private boolean doCreateConfigGroupHeader = false; 123 126 private boolean doSetControl = false; 127 128 public Control createContents(Composite parent, boolean refreshConfigModule, boolean doCreateConfigGroupHeader, boolean doSetControl) { 129 this.refreshConfigModule = refreshConfigModule; 130 this.doCreateConfigGroupHeader = doCreateConfigGroupHeader; 131 this.doSetControl = doSetControl; 132 try { 133 return createContents(parent); 134 } 135 finally { 136 refreshConfigModule = true; 137 doCreateConfigGroupHeader = false; 138 doSetControl = false; 139 } 140 } 141 144 protected Control createContents(Composite parent) { 145 wrapper = new OrdinaryWrapperComposite(parent, SWT.NONE, true); 146 147 if (doCreateConfigGroupHeader) 148 createConfigGroupHeader(wrapper); 149 150 createPreferencePage(wrapper); 151 152 if (refreshConfigModule) { 153 if (getCurrentConfigModule() == null) 154 setCurrentConfigModule(getCurrentUsersConfigModule()); 155 updatePreferencePage(getCurrentConfigModule()); 156 } 157 if (doSetControl) 158 setControl(wrapper); 159 return wrapper; 160 } 161 162 protected void createConfigGroupHeader(Composite parent) { 163 checkBoxAllowOverwriteByUser = new Button(wrapper, SWT.CHECK); 164 checkBoxAllowOverwriteByUser.setText("Allow users to overwrite configuration."); 165 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 166 gd.horizontalSpan = 2; 167 checkBoxAllowOverwriteByUser.setLayoutData(gd); 168 (new Label(wrapper, SWT.SEPARATOR | SWT.HORIZONTAL)).setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 169 } 170 171 177 protected abstract void createPreferencePage(Composite parent); 178 179 185 public abstract void updatePreferencePage(ConfigModule configModule); 186 187 192 public abstract void updateConfigModule(ConfigModule configModule); 193 194 199 public abstract Class getConfigModuleClass(); 200 201 208 public String getConfigModuleCfModID() { 209 return null; 210 } 211 212 218 public String [] getConfigModuleFetchGroups() { 219 return CONFIG_MODULE_FETCH_GROUPS; 220 } 221 222 public void init(IWorkbench workbench) { 223 } 224 225 public boolean okToLeave() { 226 return super.okToLeave(); 227 } 228 229 234 public void storeConfigModule() { 235 ConfigManager configManager = null; 236 try { 237 configManager = ConfigManagerUtil.getHome(Login.getLogin().getInitialContextProperties()).create(); 238 } catch (Throwable e) { 239 throw new RuntimeException (e); 240 } 241 if (isConfigChanged()) 242 updateConfigModule(getCurrentConfigModule()); 243 244 try { 245 if (currentUserConfigID == null) 246 currentConfigModule = configManager.storeUserConfigModule(getCurrentConfigModule(), true, getConfigModuleFetchGroups()); 247 else 248 currentConfigModule = configManager.storeUserConfigModule(currentUserConfigID, getCurrentConfigModule(), true, getConfigModuleFetchGroups()); 249 } catch (Exception e) { 250 throw new RuntimeException (e); 251 } 252 } 253 254 public void setCurrentUserConfigID(ConfigID currentUserConfigID) { 255 this.currentUserConfigID = currentUserConfigID; 256 } 257 258 262 protected abstract void discardPreferencePageWidgets(); 263 264 public void discardWidgets() { 265 wrapper = null; 266 checkBoxAllowOverwriteByUser = null; 267 setControl(null); 268 discardPreferencePageWidgets(); 269 } 270 271 274 public boolean performOk() { 275 storeConfigModule(); 276 return true; 277 } 278 279 protected void updateApplyButton() { 280 super.updateApplyButton(); 281 } 282 283 284 private List configChangedListeners = new ArrayList (); 285 286 290 public void notifyConfigChangedListeners() 291 { 292 Iterator i = configChangedListeners.iterator(); 293 while(i.hasNext()) 294 ((ConfigPreferenceChangedListener)i.next()).configPreferenceChanged(this); 295 } 296 297 301 public void addConfigPreferenceChangedListener(ConfigPreferenceChangedListener listener) 302 { 303 if(!configChangedListeners.contains(listener)) 304 configChangedListeners.add(listener); 305 } 306 307 311 public void removeDataChangedListener(ConfigPreferenceChangedListener listener) 312 { 313 if(configChangedListeners.contains(listener)) 314 configChangedListeners.remove(listener); 315 } 316 317 public void dispose() { 318 configChangedListeners.clear(); 319 super.dispose(); 320 } 321 322 323 324 325 } 326
| Popular Tags
|