KickJava   Java API By Example, From Geeks To Geeks.

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


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.editor;
57
58 import java.awt.Component JavaDoc;
59 import java.awt.Container JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.Map JavaDoc;
63 import java.util.TreeMap JavaDoc;
64
65 import javax.swing.DefaultComboBoxModel JavaDoc;
66 import javax.swing.DefaultListCellRenderer JavaDoc;
67 import javax.swing.JCheckBox JavaDoc;
68 import javax.swing.JComboBox JavaDoc;
69 import javax.swing.JList JavaDoc;
70 import javax.swing.JPanel JavaDoc;
71 import javax.swing.JTextField JavaDoc;
72
73 import org.apache.commons.beanutils.PropertyUtils;
74 import org.apache.log4j.Logger;
75 import org.objectstyle.cayenne.map.event.QueryEvent;
76 import org.objectstyle.cayenne.modeler.ProjectController;
77 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
78 import org.objectstyle.cayenne.modeler.util.TextAdapter;
79 import org.objectstyle.cayenne.query.GenericSelectQuery;
80 import org.objectstyle.cayenne.query.Query;
81 import org.objectstyle.cayenne.validation.ValidationException;
82
83 /**
84  * A panel that supports editing the properties of a GenericSelectQuery.
85  *
86  * @author Andrei Adamchik
87  */

88 public abstract class SelectPropertiesPanel extends JPanel JavaDoc {
89
90     private static final Logger logObj = Logger.getLogger(SelectPropertiesPanel.class);
91
92     private static final Integer JavaDoc ZERO = new Integer JavaDoc(0);
93
94     private static final String JavaDoc NO_CACHE_LABEL = "No Result Caching";
95     private static final String JavaDoc LOCAL_CACHE_LABEL = "DataContext Cache";
96     private static final String JavaDoc SHARED_CACHE_LABEL = "Shared Cache";
97
98     private static final Object JavaDoc[] CACHE_POLICIES = new Object JavaDoc[] {
99             GenericSelectQuery.NO_CACHE, GenericSelectQuery.LOCAL_CACHE,
100             GenericSelectQuery.SHARED_CACHE
101     };
102
103     private static final Map JavaDoc cachePolicyLabels = new TreeMap JavaDoc();
104
105     static {
106         cachePolicyLabels.put(GenericSelectQuery.NO_CACHE, NO_CACHE_LABEL);
107         cachePolicyLabels.put(GenericSelectQuery.LOCAL_CACHE, LOCAL_CACHE_LABEL);
108         cachePolicyLabels.put(GenericSelectQuery.SHARED_CACHE, SHARED_CACHE_LABEL);
109     }
110
111     protected TextAdapter fetchLimit;
112     protected TextAdapter pageSize;
113     protected JComboBox JavaDoc cachePolicy;
114     protected JCheckBox JavaDoc refreshesResults;
115
116     protected ProjectController mediator;
117
118     public SelectPropertiesPanel(ProjectController mediator) {
119         this.mediator = mediator;
120         initView();
121         initController();
122     }
123
124     protected void initView() {
125         fetchLimit = new TextAdapter(new JTextField JavaDoc(7)) {
126
127             protected void updateModel(String JavaDoc text) {
128                 setFetchLimit(text);
129             }
130         };
131
132         pageSize = new TextAdapter(new JTextField JavaDoc(7)) {
133
134             protected void updateModel(String JavaDoc text) {
135                 setPageSize(text);
136             }
137         };
138
139         cachePolicy = CayenneWidgetFactory.createComboBox();
140         cachePolicy.setRenderer(new CachePolicyRenderer());
141         refreshesResults = new JCheckBox JavaDoc();
142     }
143
144     protected void initController() {
145         cachePolicy.addActionListener(new ActionListener JavaDoc() {
146
147             public void actionPerformed(ActionEvent JavaDoc event) {
148                 Object JavaDoc policy = cachePolicy.getModel().getSelectedItem();
149                 setQueryProperty("cachePolicy", policy);
150             }
151         });
152
153         refreshesResults.addActionListener(new ActionListener JavaDoc() {
154
155             public void actionPerformed(ActionEvent JavaDoc event) {
156                 Boolean JavaDoc b = refreshesResults.isSelected() ? Boolean.TRUE : Boolean.FALSE;
157                 setQueryProperty("refreshingObjects", b);
158             }
159         });
160     }
161
162     /**
163      * Updates the view from the current model state. Invoked when a currently displayed
164      * query is changed.
165      */

166     public void initFromModel(GenericSelectQuery query) {
167         DefaultComboBoxModel JavaDoc cacheModel = new DefaultComboBoxModel JavaDoc(CACHE_POLICIES);
168         cacheModel.setSelectedItem(query.getCachePolicy());
169         cachePolicy.setModel(cacheModel);
170
171         fetchLimit.setText(String.valueOf(query.getFetchLimit()));
172         pageSize.setText(String.valueOf(query.getPageSize()));
173         refreshesResults.setSelected(query.isRefreshingObjects());
174     }
175
176     void setFetchLimit(String JavaDoc string) {
177         string = (string == null) ? "" : string.trim();
178
179         if (string.length() == 0) {
180             setQueryProperty("fetchLimit", ZERO);
181         }
182         else {
183             try {
184                 setQueryProperty("fetchLimit", new Integer JavaDoc(string));
185             }
186             catch (NumberFormatException JavaDoc nfex) {
187                 throw new ValidationException("Fetch limit must be an integer: " + string);
188             }
189         }
190     }
191
192     void setPageSize(String JavaDoc string) {
193         string = (string == null) ? "" : string.trim();
194
195         if (string.length() == 0) {
196             setQueryProperty("pageSize", ZERO);
197         }
198         else {
199             try {
200                 setQueryProperty("pageSize", new Integer JavaDoc(string));
201             }
202             catch (NumberFormatException JavaDoc nfex) {
203                 throw new ValidationException("Page size must be an integer: " + string);
204             }
205         }
206     }
207
208     Query getQuery() {
209         return mediator.getCurrentQuery();
210     }
211
212     public void setEnabled(boolean flag) {
213         super.setEnabled(flag);
214
215         // propagate to children
216

217         Container JavaDoc mainPanel = (Container JavaDoc) getComponent(0);
218         Component JavaDoc[] children = mainPanel.getComponents();
219         for (int i = 0; i < children.length; i++) {
220             children[i].setEnabled(flag);
221         }
222     }
223
224     void setQueryProperty(String JavaDoc property, Object JavaDoc value) {
225         Query query = getQuery();
226         if (query != null) {
227             try {
228                 PropertyUtils.setProperty(query, property, value);
229                 mediator.fireQueryEvent(new QueryEvent(this, query));
230             }
231             catch (Exception JavaDoc ex) {
232                 logObj.warn("Error setting property: " + property, ex);
233             }
234         }
235     }
236
237     final class CachePolicyRenderer extends DefaultListCellRenderer JavaDoc {
238
239         public Component JavaDoc getListCellRendererComponent(
240                 JList JavaDoc list,
241                 Object JavaDoc object,
242                 int arg2,
243                 boolean arg3,
244                 boolean arg4) {
245
246             if (object != null) {
247                 object = cachePolicyLabels.get(object);
248             }
249
250             if (object == null) {
251                 object = NO_CACHE_LABEL;
252             }
253
254             return super.getListCellRendererComponent(list, object, arg2, arg3, arg4);
255         }
256     }
257
258 }
Popular Tags