KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > config > AbstractConfigModulePreferencePage


1 /*
2  * Created on Apr 21, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.config;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
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 /**
30  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
31  *
32  */

33 public abstract class AbstractConfigModulePreferencePage
34 extends PreferencePage
35 implements IWorkbenchPreferencePage
36 {
37
38     protected static String JavaDoc[] CONFIG_MODULE_FETCH_GROUPS = new String JavaDoc[] {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     /**
48      *
49      */

50     public AbstractConfigModulePreferencePage() {
51         super();
52     }
53
54     /**
55      * @param title
56      */

57     public AbstractConfigModulePreferencePage(String JavaDoc title) {
58         super(title);
59     }
60
61     /**
62      * @param title
63      * @param image
64      */

65     public AbstractConfigModulePreferencePage(String JavaDoc title, ImageDescriptor image) {
66         super(title, image);
67     }
68     
69     /**
70      * Checks if the user is allowed to changed configuration
71      * for groups or other users.
72      *
73      * @return Weather the user is allowed to change other configurations
74      */

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 JavaDoc e) {
102             throw new RuntimeException JavaDoc(e);
103         }
104         try {
105             return configManager.getUserConfigModule(
106                     getConfigModuleClass(),
107                     getConfigModuleCfModID(),
108                     getConfigModuleFetchGroups()
109             );
110         } catch (Exception JavaDoc e) {
111             throw new RuntimeException JavaDoc(e);
112         }
113     }
114     
115     /**
116      * Default value is true can only be changed by {@link #createContents(Composite, boolean, boolean, boolean)}
117      */

118     private boolean refreshConfigModule = true;
119     /**
120      * Default value is false can only be changed by {@link #createContents(Composite, boolean, boolean, boolean)}
121      */

122     private boolean doCreateConfigGroupHeader = false;
123     /**
124      * Default value is false can only be changed by {@link #createContents(Composite, boolean, boolean, boolean)}
125      */

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     /**
142      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
143      */

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     /**
172      * Called to ask the reciever to create its UI representation.
173      * The parent will be a Composite with a GridLayout.
174      *
175      * @param parent The Composite to wich the Controls should be edited.
176      */

177     protected abstract void createPreferencePage(Composite parent);
178     
179     /**
180      * Will be called when the UI has to be updated with values of
181      * a new ConfigModule.
182      *
183      * @param configModule The currently edited ConfigModule
184      */

185     public abstract void updatePreferencePage(ConfigModule configModule);
186     
187     /**
188      * Will be called to update the ConfigModule from the UI.
189      *
190      * @param configModule
191      */

192     public abstract void updateConfigModule(ConfigModule configModule);
193     
194     /**
195      * Returns the ConfigModule class this PreferencePage does edit.
196      *
197      * @returnm The ConfigModule class this PreferencePage does edit.
198      */

199     public abstract Class JavaDoc getConfigModuleClass();
200     
201     /**
202      * Should return the cfModID of the ConfigModule this preference page
203      * does edit. This method is intended to be overridden. The default
204      * implementation returns null.
205      *
206      * @return null
207      */

208     public String JavaDoc getConfigModuleCfModID() {
209         return null;
210     }
211     
212     /**
213      * Returns fetch-groups containing FetchPlan.ALL. Intented to be overridden
214      * by subclasses to detach ConfigModules with custom fetchgroups.
215      *
216      * @return fetch-groups containing FetchPlan.ALL
217      */

218     public String JavaDoc[] 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     /**
230      * Calls implementors to {@link #updateConfigModule(ConfigModule)} and
231      * stores the updatedConfig module to the server.
232      *
233      */

234     public void storeConfigModule() {
235         ConfigManager configManager = null;
236         try {
237             configManager = ConfigManagerUtil.getHome(Login.getLogin().getInitialContextProperties()).create();
238         } catch (Throwable JavaDoc e) {
239             throw new RuntimeException JavaDoc(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 JavaDoc e) {
250             throw new RuntimeException JavaDoc(e);
251         }
252     }
253     
254     public void setCurrentUserConfigID(ConfigID currentUserConfigID) {
255         this.currentUserConfigID = currentUserConfigID;
256     }
257     
258     /**
259      * When called all widgets created in {@link #createPreferencePage(Composite)}
260      * should be discarded (nulled).
261      */

262     protected abstract void discardPreferencePageWidgets();
263     
264     public void discardWidgets() {
265         wrapper = null;
266         checkBoxAllowOverwriteByUser = null;
267         setControl(null);
268         discardPreferencePageWidgets();
269     }
270
271     /**
272      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
273      */

274     public boolean performOk() {
275         storeConfigModule();
276         return true;
277     }
278
279     protected void updateApplyButton() {
280         super.updateApplyButton();
281     }
282     
283     
284     private List JavaDoc configChangedListeners = new ArrayList JavaDoc();
285     
286     /**
287    * Call this when you modified the entity object.
288    *
289    */

290   public void notifyConfigChangedListeners()
291   {
292     Iterator JavaDoc i = configChangedListeners.iterator();
293     while(i.hasNext())
294         ((ConfigPreferenceChangedListener)i.next()).configPreferenceChanged(this);
295   }
296
297   /**
298    * Listen for modifications of the entity object
299    * @param listener your listener
300    */

301   public void addConfigPreferenceChangedListener(ConfigPreferenceChangedListener listener)
302   {
303     if(!configChangedListeners.contains(listener))
304         configChangedListeners.add(listener);
305   }
306
307   /**
308    * Remove a listener
309    * @param listener the listener
310    */

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