KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > editor > DataDomainView


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
57 package org.objectstyle.cayenne.modeler.editor;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import javax.swing.JButton JavaDoc;
65 import javax.swing.JCheckBox JavaDoc;
66 import javax.swing.JPanel JavaDoc;
67 import javax.swing.JTextField JavaDoc;
68
69 import org.objectstyle.cayenne.access.DataDomain;
70 import org.objectstyle.cayenne.access.DataRowStore;
71 import org.objectstyle.cayenne.conf.Configuration;
72 import org.objectstyle.cayenne.map.event.DomainEvent;
73 import org.objectstyle.cayenne.modeler.Application;
74 import org.objectstyle.cayenne.modeler.ProjectController;
75 import org.objectstyle.cayenne.modeler.dialog.datadomain.CacheSyncConfigController;
76 import org.objectstyle.cayenne.modeler.event.DomainDisplayEvent;
77 import org.objectstyle.cayenne.modeler.event.DomainDisplayListener;
78 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
79 import org.objectstyle.cayenne.modeler.util.TextAdapter;
80 import org.objectstyle.cayenne.pref.Domain;
81 import org.objectstyle.cayenne.project.ApplicationProject;
82 import org.objectstyle.cayenne.util.Util;
83 import org.objectstyle.cayenne.validation.ValidationException;
84
85 import com.jgoodies.forms.builder.DefaultFormBuilder;
86 import com.jgoodies.forms.layout.FormLayout;
87
88 /**
89  * Panel for editing DataDomain.
90  */

91 public class DataDomainView extends JPanel JavaDoc implements DomainDisplayListener {
92
93     protected ProjectController projectController;
94
95     protected TextAdapter name;
96     protected TextAdapter cacheSize;
97     protected JCheckBox JavaDoc objectValidation;
98     protected JCheckBox JavaDoc externalTransactions;
99     protected JCheckBox JavaDoc sharedCache;
100     protected JCheckBox JavaDoc remoteUpdates;
101     protected JButton JavaDoc configRemoteUpdates;
102
103     public DataDomainView(ProjectController projectController) {
104         this.projectController = projectController;
105
106         // Create and layout components
107
initView();
108
109         // hook up listeners to widgets
110
initController();
111     }
112
113     protected void initView() {
114
115         // create widgets
116
this.name = new TextAdapter(new JTextField JavaDoc()) {
117
118             protected void updateModel(String JavaDoc text) {
119                 setDomainName(text);
120             }
121         };
122
123         this.cacheSize = new TextAdapter(new JTextField JavaDoc(10)) {
124
125             protected void updateModel(String JavaDoc text) {
126                 setCacheSize(text);
127             }
128         };
129
130         this.objectValidation = new JCheckBox JavaDoc();
131         this.externalTransactions = new JCheckBox JavaDoc();
132
133         this.sharedCache = new JCheckBox JavaDoc();
134         this.remoteUpdates = new JCheckBox JavaDoc();
135         this.configRemoteUpdates = new JButton JavaDoc("Configure");
136         configRemoteUpdates.setEnabled(false);
137
138         // assemble
139

140         FormLayout layout = new FormLayout(
141                 "right:max(50dlu;pref), 3dlu, left:max(20dlu;pref), 3dlu, left:150",
142                 "");
143         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
144         builder.setDefaultDialogBorder();
145
146         builder.appendSeparator("DataDomain Configuration");
147         builder.append("DataDomain Name:", name.getComponent(), 3);
148         builder.append("Child DataContexts Validate Objects:", objectValidation, 3);
149         builder.append("Container-Managed Transactions:", externalTransactions, 3);
150
151         builder.appendSeparator("Cache Configuration");
152         builder.append("Max. Number of Objects:", cacheSize.getComponent(), 3);
153         builder.append("Use Shared Cache:", sharedCache, 3);
154         builder
155                 .append(
156                         "Remote Change Notifications:",
157                         remoteUpdates,
158                         configRemoteUpdates);
159
160         this.setLayout(new BorderLayout JavaDoc());
161         this.add(builder.getPanel(), BorderLayout.CENTER);
162     }
163
164     protected void initController() {
165         projectController.addDomainDisplayListener(this);
166
167         // add action listener to checkboxes
168
objectValidation.addActionListener(new ActionListener JavaDoc() {
169
170             public void actionPerformed(ActionEvent JavaDoc e) {
171                 String JavaDoc value = objectValidation.isSelected() ? "true" : "false";
172                 setDomainProperty(
173                         DataDomain.VALIDATING_OBJECTS_ON_COMMIT_PROPERTY,
174                         value,
175                         Boolean.toString(DataDomain.VALIDATING_OBJECTS_ON_COMMIT_DEFAULT));
176             }
177         });
178
179         externalTransactions.addActionListener(new ActionListener JavaDoc() {
180
181             public void actionPerformed(ActionEvent JavaDoc e) {
182                 String JavaDoc value = externalTransactions.isSelected() ? "true" : "false";
183                 setDomainProperty(
184                         DataDomain.USING_EXTERNAL_TRANSACTIONS_PROPERTY,
185                         value,
186                         Boolean.toString(DataDomain.USING_EXTERNAL_TRANSACTIONS_DEFAULT));
187             }
188         });
189
190         sharedCache.addActionListener(new ActionListener JavaDoc() {
191
192             public void actionPerformed(ActionEvent JavaDoc e) {
193                 String JavaDoc value = sharedCache.isSelected() ? "true" : "false";
194                 setDomainProperty(
195                         DataDomain.SHARED_CACHE_ENABLED_PROPERTY,
196                         value,
197                         Boolean.toString(DataDomain.SHARED_CACHE_ENABLED_DEFAULT));
198
199                 // turning off shared cache should result in disabling remote events
200

201                 remoteUpdates.setEnabled(sharedCache.isSelected());
202
203                 if (!sharedCache.isSelected()) {
204                     // uncheck remote updates...
205
remoteUpdates.setSelected(false);
206                 }
207
208                 // depending on final remote updates status change button status
209
configRemoteUpdates.setEnabled(remoteUpdates.isSelected());
210             }
211         });
212
213         remoteUpdates.addActionListener(new ActionListener JavaDoc() {
214
215             public void actionPerformed(ActionEvent JavaDoc e) {
216                 String JavaDoc value = remoteUpdates.isSelected() ? "true" : "false";
217
218                 // update config button state
219
configRemoteUpdates.setEnabled(remoteUpdates.isSelected());
220
221                 setDomainProperty(
222                         DataRowStore.REMOTE_NOTIFICATION_PROPERTY,
223                         value,
224                         Boolean.toString(DataRowStore.REMOTE_NOTIFICATION_DEFAULT));
225             }
226         });
227
228         configRemoteUpdates.addActionListener(new ActionListener JavaDoc() {
229
230             public void actionPerformed(ActionEvent JavaDoc e) {
231                 new CacheSyncConfigController(projectController).startup();
232             }
233         });
234     }
235
236     /**
237      * Helper method that updates domain properties. If a value equals to default, null
238      * value is used instead.
239      */

240     protected void setDomainProperty(String JavaDoc property, String JavaDoc value, String JavaDoc defaultValue) {
241
242         DataDomain domain = projectController.getCurrentDataDomain();
243         if (domain == null) {
244             return;
245         }
246
247         // no empty strings
248
if ("".equals(value)) {
249             value = null;
250         }
251
252         // use NULL for defaults
253
if (value != null && value.equals(defaultValue)) {
254             value = null;
255         }
256
257         Map JavaDoc properties = domain.getProperties();
258         Object JavaDoc oldValue = properties.get(property);
259         if (!Util.nullSafeEquals(value, oldValue)) {
260             properties.put(property, value);
261
262             DomainEvent e = new DomainEvent(this, domain);
263             projectController.fireDomainEvent(e);
264         }
265     }
266
267     public String JavaDoc getDomainProperty(String JavaDoc property, String JavaDoc defaultValue) {
268         DataDomain domain = projectController.getCurrentDataDomain();
269         if (domain == null) {
270             return null;
271         }
272
273         String JavaDoc value = (String JavaDoc) domain.getProperties().get(property);
274         return value != null ? value : defaultValue;
275     }
276
277     public boolean getDomainBooleanProperty(String JavaDoc property, String JavaDoc defaultValue) {
278         return "true".equalsIgnoreCase(getDomainProperty(property, defaultValue));
279     }
280
281     /**
282      * Invoked on domain selection event. Updates view with the values from the currently
283      * selected domain.
284      */

285     public void currentDomainChanged(DomainDisplayEvent e) {
286         DataDomain domain = e.getDomain();
287         if (null == domain) {
288             return;
289         }
290
291         // extract values from the new domain object
292
name.setText(domain.getName());
293
294         cacheSize.setText(getDomainProperty(
295                 DataRowStore.SNAPSHOT_CACHE_SIZE_PROPERTY,
296                 Integer.toString(DataRowStore.SNAPSHOT_CACHE_SIZE_DEFAULT)));
297
298         objectValidation.setSelected(getDomainBooleanProperty(
299                 DataDomain.VALIDATING_OBJECTS_ON_COMMIT_PROPERTY,
300                 Boolean.toString(DataDomain.VALIDATING_OBJECTS_ON_COMMIT_DEFAULT)));
301
302         externalTransactions.setSelected(getDomainBooleanProperty(
303                 DataDomain.USING_EXTERNAL_TRANSACTIONS_PROPERTY,
304                 Boolean.toString(DataDomain.USING_EXTERNAL_TRANSACTIONS_DEFAULT)));
305
306         sharedCache.setSelected(getDomainBooleanProperty(
307                 DataDomain.SHARED_CACHE_ENABLED_PROPERTY,
308                 Boolean.toString(DataDomain.SHARED_CACHE_ENABLED_DEFAULT)));
309
310         remoteUpdates.setSelected(getDomainBooleanProperty(
311                 DataRowStore.REMOTE_NOTIFICATION_PROPERTY,
312                 Boolean.toString(DataRowStore.REMOTE_NOTIFICATION_DEFAULT)));
313         remoteUpdates.setEnabled(sharedCache.isSelected());
314         configRemoteUpdates.setEnabled(remoteUpdates.isEnabled()
315                 && remoteUpdates.isSelected());
316     }
317
318     void setDomainName(String JavaDoc newName) {
319         if (newName == null || newName.trim().length() == 0) {
320             throw new ValidationException("Enter name for DataDomain");
321         }
322
323         Configuration configuration = ((ApplicationProject) Application.getProject())
324                 .getConfiguration();
325         DataDomain domain = projectController.getCurrentDataDomain();
326
327         DataDomain matchingDomain = configuration.getDomain(newName);
328
329         if (matchingDomain == null) {
330             Domain prefs = projectController.getPreferenceDomainForDataDomain();
331
332             DomainEvent e = new DomainEvent(this, domain, domain.getName());
333             ProjectUtil.setDataDomainName(configuration, domain, newName);
334             prefs.rename(newName);
335             projectController.fireDomainEvent(e);
336         }
337         else if (matchingDomain != domain) {
338             throw new ValidationException("There is another DataDomain named '"
339                     + newName
340                     + "'. Use a different name.");
341         }
342     }
343
344     void setCacheSize(String JavaDoc text) {
345         if (text.length() > 0) {
346             try {
347                 Integer.parseInt(text);
348             }
349             catch (NumberFormatException JavaDoc ex) {
350                 throw new ValidationException("Cache size must be an integer: " + text);
351             }
352         }
353
354         setDomainProperty(DataRowStore.SNAPSHOT_CACHE_SIZE_PROPERTY, text, Integer
355                 .toString(DataRowStore.SNAPSHOT_CACHE_SIZE_DEFAULT));
356     }
357 }
Popular Tags