KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > datadomain > CacheSyncConfigController


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.dialog.datadomain;
57
58 import java.util.HashMap JavaDoc;
59 import java.util.Map JavaDoc;
60
61 import org.apache.log4j.Logger;
62 import org.objectstyle.cayenne.access.DataDomain;
63 import org.objectstyle.cayenne.access.DataRowStore;
64 import org.objectstyle.cayenne.map.event.DomainEvent;
65 import org.objectstyle.cayenne.modeler.ProjectController;
66 import org.scopemvc.controller.basic.BasicController;
67 import org.scopemvc.core.Control;
68 import org.scopemvc.core.ControlException;
69 import org.scopemvc.core.ModelChangeEvent;
70 import org.scopemvc.core.ModelChangeListener;
71 import org.scopemvc.core.Selector;
72 import org.scopemvc.view.swing.SPanel;
73
74 /**
75  * A controller for CacheSyncConfigDialog and its subviews. Builds a model out of a
76  * DataDomain properties map, and on save updates DataDomain properties with configuration
77  * changes made by the user. This controller manages one main dialog view, and its
78  * subviews organized using CardLayout. Each subview as well as the main dialog have their
79  * own independent models.
80  *
81  * @author Andrei Adamchik
82  */

83 public class CacheSyncConfigController extends BasicController implements
84         ModelChangeListener {
85
86     private static Logger logObj = Logger.getLogger(CacheSyncConfigController.class);
87
88     // using strings instead of the actioal factory classes, since we
89
// JMS and JavaGroups libraries may not be around, and Modeler
90
// may throw CNFE
91
private static final String JavaDoc JGROUPS_FACTORY_CLASS = "org.objectstyle.cayenne.event.JavaGroupsBridgeFactory";
92     private static final String JavaDoc JMS_FACTORY_CLASS = "org.objectstyle.cayenne.event.JMSBridgeFactory";
93
94     public static final String JavaDoc SAVE_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.save.button";
95     public static final String JavaDoc CANCEL_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.cancel.button";
96
97     public static final String JavaDoc JGROUPS_DEFAULT_CONTROL = "cayenne.modeler.jgroupConfig.radio1";
98
99     public static final String JavaDoc JGROUPS_URL_CONTROL = "cayenne.modeler.jgroupConfig.radio2";
100
101     protected Map JavaDoc existingCards;
102     protected boolean modified;
103     protected ProjectController eventController;
104
105     public CacheSyncConfigController(ProjectController eventController) {
106         this.eventController = eventController;
107     }
108
109     /**
110      * Creates and shows a new modal dialog window. Registers as a listener for its own
111      * model to update subviews on model changes.
112      */

113     public void startup() {
114         DataDomain domain = eventController.getCurrentDataDomain();
115         String JavaDoc factory = (String JavaDoc) domain.getProperties().get(
116                 DataRowStore.EVENT_BRIDGE_FACTORY_PROPERTY);
117
118         CacheSyncTypesModel topModel = buildTypesModel(factory);
119         setModel(topModel);
120         setView(new CacheSyncConfigDialog());
121
122         // build cards, showing the one corresponding to DataDomain state
123
prepareChildren(factory);
124         super.startup();
125     }
126
127     /**
128      * ModelChangeListener implementation that updates "modified" status and changes
129      * dialog subview on model changes.
130      */

131     public void modelChanged(ModelChangeEvent inEvent) {
132         logObj.info("ModelChangeEvent: " + inEvent.getSelector());
133
134         Selector selector = inEvent.getSelector();
135
136         if (selector.startsWith(CacheSyncTypesModel.FACTORY_LABEL_SELECTOR)) {
137             changeConfigView();
138             modified = true;
139             logObj.info("** Factory selection modified..");
140         }
141         else {
142             modified = true;
143             logObj.info("** Property modified modified..");
144         }
145     }
146
147     /**
148      * Overrides super implementation to process controls from this controller's view and
149      * its subviews.
150      */

151     protected void doHandleControl(Control control) throws ControlException {
152         logObj.info("Control: " + control);
153
154         if (control.matchesID(CANCEL_CONFIG_CONTROL)) {
155             shutdown();
156         }
157         else if (control.matchesID(SAVE_CONFIG_CONTROL)) {
158             commitChanges();
159         }
160         else if (control.matchesID(JGROUPS_DEFAULT_CONTROL)) {
161             jgroupsDefaultConfig();
162         }
163         else if (control.matchesID(JGROUPS_URL_CONTROL)) {
164             jgroupsURLConfig();
165         }
166     }
167
168     protected void jgroupsDefaultConfig() {
169         JGroupsConfigPanel view = (JGroupsConfigPanel) existingCards
170                 .get(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL);
171         if (view != null) {
172             view.showDefaultConfig();
173         }
174     }
175
176     protected void jgroupsURLConfig() {
177         JGroupsConfigPanel view = (JGroupsConfigPanel) existingCards
178                 .get(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL);
179         if (view != null) {
180             view.showCustomConfig();
181         }
182     }
183
184     /**
185      * Stores configuration changes in the data domain properties.
186      */

187     protected void commitChanges() {
188         logObj.info("Has changes?: " + modified);
189
190         if (modified) {
191             // extract model from current card
192
CacheSyncTypesModel topModel = (CacheSyncTypesModel) getModel();
193             SPanel card = (SPanel) existingCards.get(topModel.getFactoryLabel());
194             CacheSyncConfigModel model = (CacheSyncConfigModel) card.getShownModel();
195
196             DataDomain domain = eventController.getCurrentDataDomain();
197
198             logObj.warn("domain properties BEFORE: " + domain.getProperties());
199             model.storeProperties(domain.getProperties());
200
201             logObj.warn("domain properties: " + domain.getProperties());
202
203             eventController.fireDomainEvent(new DomainEvent(this, domain));
204         }
205
206         shutdown();
207     }
208
209     /**
210      * Changes a subview to a panel specific for the currently selected configuration
211      * type.
212      */

213     protected void changeConfigView() {
214         CacheSyncTypesModel topModel = (CacheSyncTypesModel) getModel();
215         CacheSyncConfigModel newModel = buildModel(topModel);
216
217         // NOTE: card doesn't have a controller, since it does not need it
218
String JavaDoc label = topModel.getFactoryLabel();
219         SPanel card = (SPanel) existingCards.get(label);
220         card.setBoundModel(newModel);
221         ((CacheSyncConfigDialog) getView()).showCard(label);
222     }
223
224     protected CacheSyncTypesModel buildTypesModel(String JavaDoc factory) {
225
226         if (factory == null) {
227             factory = DataRowStore.EVENT_BRIDGE_FACTORY_DEFAULT;
228         }
229
230         String JavaDoc label;
231
232         if (JGROUPS_FACTORY_CLASS.equals(factory)) {
233             label = CacheSyncTypesModel.JGROUPS_FACTORY_LABEL;
234         }
235         else if (JMS_FACTORY_CLASS.equals(factory)) {
236             label = CacheSyncTypesModel.JMS_FACTORY_LABEL;
237         }
238         else {
239             label = CacheSyncTypesModel.CUSTOM_FACTORY_LABEL;
240         }
241
242         CacheSyncTypesModel model = new CacheSyncTypesModel();
243         model.setFactoryLabel(label);
244         model.addModelChangeListener(this);
245         return model;
246     }
247
248     protected CacheSyncConfigModel buildModel(CacheSyncTypesModel topModel) {
249         String JavaDoc label = topModel.getFactoryLabel();
250         String JavaDoc factory;
251
252         if (label.equals(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL)) {
253             factory = JGROUPS_FACTORY_CLASS;
254         }
255         else if (label.equals(CacheSyncTypesModel.JMS_FACTORY_LABEL)) {
256             factory = JMS_FACTORY_CLASS;
257         }
258         else {
259             // reset factory
260
factory = null;
261         }
262
263         return buildModel(factory);
264     }
265
266     protected CacheSyncConfigModel buildModel(String JavaDoc factory) {
267
268         CacheSyncConfigModel model;
269
270         if (JGROUPS_FACTORY_CLASS.equals(factory)) {
271             model = new JGroupsConfigModel();
272         }
273         else if (JMS_FACTORY_CLASS.equals(factory)) {
274             model = new JMSConfigModel();
275         }
276         else {
277             model = new CacheSyncConfigModel();
278         }
279
280         model.setMap(new HashMap JavaDoc(eventController.getCurrentDataDomain().getProperties()));
281         model.setFactoryClass(factory);
282         model.addModelChangeListener(this);
283
284         return model;
285     }
286
287     protected void prepareChildren(String JavaDoc factory) {
288         existingCards = new HashMap JavaDoc();
289         CacheSyncConfigDialog topView = (CacheSyncConfigDialog) getView();
290
291         // note that none of the panels need a controller
292
// if they issue controls, they will use this object taken from parent
293

294         JGroupsConfigPanel jgroupsPanel = new JGroupsConfigPanel();
295         existingCards.put(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL, jgroupsPanel);
296         topView.addCard(jgroupsPanel, CacheSyncTypesModel.JGROUPS_FACTORY_LABEL);
297
298         JMSConfigPanel jmsPanel = new JMSConfigPanel();
299         existingCards.put(CacheSyncTypesModel.JMS_FACTORY_LABEL, jmsPanel);
300         topView.addCard(jmsPanel, CacheSyncTypesModel.JMS_FACTORY_LABEL);
301
302         CustomRemoteEventsConfigPanel customFactoryPanel = new CustomRemoteEventsConfigPanel();
303         existingCards.put(CacheSyncTypesModel.CUSTOM_FACTORY_LABEL, customFactoryPanel);
304         topView.addCard(customFactoryPanel, CacheSyncTypesModel.CUSTOM_FACTORY_LABEL);
305
306         if (factory == null) {
307             factory = DataRowStore.EVENT_BRIDGE_FACTORY_DEFAULT;
308         }
309
310         // display the right initial card
311
// can't call "changeConfigView", since it will reset custom factories..
312
Object JavaDoc model = buildModel(factory);
313
314         if (JGROUPS_FACTORY_CLASS.equals(factory)) {
315             jgroupsPanel.setBoundModel(model);
316             ((CacheSyncConfigDialog) getView())
317                     .showCard(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL);
318         }
319         else if (JMS_FACTORY_CLASS.equals(factory)) {
320             jmsPanel.setBoundModel(model);
321             ((CacheSyncConfigDialog) getView())
322                     .showCard(CacheSyncTypesModel.JMS_FACTORY_LABEL);
323         }
324         else {
325             customFactoryPanel.setBoundModel(model);
326             ((CacheSyncConfigDialog) getView())
327                     .showCard(CacheSyncTypesModel.CUSTOM_FACTORY_LABEL);
328         }
329     }
330 }
Popular Tags