KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61 import java.util.Arrays JavaDoc;
62
63 import javax.swing.DefaultComboBoxModel JavaDoc;
64 import javax.swing.JCheckBox JavaDoc;
65 import javax.swing.JComboBox JavaDoc;
66 import javax.swing.JPanel JavaDoc;
67 import javax.swing.JTextField JavaDoc;
68
69 import org.objectstyle.cayenne.exp.Expression;
70 import org.objectstyle.cayenne.map.DataMap;
71 import org.objectstyle.cayenne.map.event.QueryEvent;
72 import org.objectstyle.cayenne.modeler.ProjectController;
73 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
74 import org.objectstyle.cayenne.modeler.util.CellRenderers;
75 import org.objectstyle.cayenne.modeler.util.Comparators;
76 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
77 import org.objectstyle.cayenne.modeler.util.TextAdapter;
78 import org.objectstyle.cayenne.query.Query;
79 import org.objectstyle.cayenne.query.SelectQuery;
80 import org.objectstyle.cayenne.util.Util;
81 import org.objectstyle.cayenne.validation.ValidationException;
82 import org.scopemvc.util.convertor.StringConvertor;
83 import org.scopemvc.util.convertor.StringConvertors;
84
85 import com.jgoodies.forms.builder.PanelBuilder;
86 import com.jgoodies.forms.layout.CellConstraints;
87 import com.jgoodies.forms.layout.FormLayout;
88
89 /**
90  * A tabbed pane that contains editors for various SelectQuery parts.
91  *
92  * @author Andrei Adamchik
93  */

94 public class SelectQueryMainTab extends JPanel JavaDoc {
95
96     protected ProjectController mediator;
97
98     protected TextAdapter name;
99     protected JComboBox JavaDoc queryRoot;
100     protected TextAdapter qualifier;
101     protected JCheckBox JavaDoc distinct;
102     protected ObjectQueryPropertiesPanel properties;
103
104     public SelectQueryMainTab(ProjectController mediator) {
105         this.mediator = mediator;
106
107         initView();
108         initController();
109     }
110
111     private void initView() {
112         // create widgets
113
name = new TextAdapter(new JTextField JavaDoc()) {
114
115             protected void updateModel(String JavaDoc text) {
116                 setQueryName(text);
117             }
118         };
119
120         queryRoot = CayenneWidgetFactory.createComboBox();
121         queryRoot.setRenderer(CellRenderers.listRendererWithIcons());
122
123         qualifier = new TextAdapter(new JTextField JavaDoc()) {
124
125             protected void updateModel(String JavaDoc text) {
126                 setQueryQualifier(text);
127             }
128         };
129
130         distinct = new JCheckBox JavaDoc();
131
132         properties = new ObjectQueryPropertiesPanel(mediator);
133
134         // assemble
135
CellConstraints cc = new CellConstraints();
136         FormLayout layout = new FormLayout(
137                 "right:max(80dlu;pref), 3dlu, fill:max(200dlu;pref)",
138                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
139         PanelBuilder builder = new PanelBuilder(layout);
140         builder.setDefaultDialogBorder();
141
142         builder.addSeparator("SelectQuery Settings", cc.xywh(1, 1, 3, 1));
143         builder.addLabel("Query Name:", cc.xy(1, 3));
144         builder.add(name.getComponent(), cc.xy(3, 3));
145         builder.addLabel("Query Root:", cc.xy(1, 5));
146         builder.add(queryRoot, cc.xy(3, 5));
147         builder.addLabel("Qualifier:", cc.xy(1, 7));
148         builder.add(qualifier.getComponent(), cc.xy(3, 7));
149         builder.addLabel("Distinct:", cc.xy(1, 9));
150         builder.add(distinct, cc.xy(3, 9));
151
152         this.setLayout(new BorderLayout JavaDoc());
153         this.add(builder.getPanel(), BorderLayout.NORTH);
154         this.add(properties, BorderLayout.CENTER);
155     }
156
157     private void initController() {
158
159         queryRoot.addActionListener(new ActionListener JavaDoc() {
160
161             public void actionPerformed(ActionEvent JavaDoc event) {
162                 Query query = getQuery();
163                 if (query != null) {
164                     query.setRoot(queryRoot.getModel().getSelectedItem());
165                     mediator.fireQueryEvent(new QueryEvent(this, query));
166                 }
167             }
168         });
169
170         distinct.addActionListener(new ActionListener JavaDoc() {
171
172             public void actionPerformed(ActionEvent JavaDoc event) {
173                 SelectQuery query = getQuery();
174                 if (query != null) {
175                     query.setDistinct(distinct.isSelected());
176                     mediator.fireQueryEvent(new QueryEvent(this, query));
177                 }
178             }
179         });
180
181     }
182
183     /**
184      * Updates the view from the current model state. Invoked when a currently displayed
185      * query is changed.
186      */

187     void initFromModel() {
188         Query query = mediator.getCurrentQuery();
189
190         if (!(query instanceof SelectQuery)) {
191             setVisible(false);
192             return;
193         }
194
195         SelectQuery selectQuery = (SelectQuery) query;
196
197         name.setText(query.getName());
198         distinct.setSelected(selectQuery.isDistinct());
199         qualifier.setText(selectQuery.getQualifier() != null ? selectQuery
200                 .getQualifier()
201                 .toString() : null);
202
203         // init root choices and title label..
204

205         // - SelectQuery supports ObjEntity roots
206

207         // TODO: now we only allow roots from the current map,
208
// since query root is fully resolved during map loading,
209
// making it impossible to reference other DataMaps.
210

211         DataMap map = mediator.getCurrentDataMap();
212         Object JavaDoc[] roots = map.getObjEntities().toArray();
213
214         if (roots.length > 1) {
215             Arrays.sort(roots, Comparators.getDataMapChildrenComparator());
216         }
217
218         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(roots);
219         model.setSelectedItem(query.getRoot());
220         queryRoot.setModel(model);
221
222         properties.initFromModel(selectQuery);
223
224         setVisible(true);
225     }
226
227     protected SelectQuery getQuery() {
228         return (SelectQuery) mediator.getCurrentQuery();
229     }
230
231     /**
232      * Initializes Query qualifier from string.
233      */

234     void setQueryQualifier(String JavaDoc text) {
235         if (text != null && text.trim().length() == 0) {
236             text = null;
237         }
238
239         SelectQuery query = getQuery();
240         if(query == null) {
241             return;
242         }
243         
244         StringConvertor convertor = StringConvertors.forClass(Expression.class);
245         try {
246             String JavaDoc oldQualifier = convertor.valueAsString(query.getQualifier());
247             if (!Util.nullSafeEquals(oldQualifier, text)) {
248                 Expression exp = (Expression) convertor.stringAsValue(text);
249                 query.setQualifier(exp);
250                 mediator.fireQueryEvent(new QueryEvent(this, query));
251             }
252         }
253         catch (IllegalArgumentException JavaDoc ex) {
254             // unparsable qualifier
255
throw new ValidationException(ex.getMessage());
256         }
257     }
258
259     /**
260      * Initializes Query name from string.
261      */

262     void setQueryName(String JavaDoc newName) {
263         if (newName != null && newName.trim().length() == 0) {
264             newName = null;
265         }
266
267         Query query = getQuery();
268  
269         if(query == null) {
270             return;
271         }
272         
273         if (Util.nullSafeEquals(newName, query.getName())) {
274             return;
275         }
276
277         if (newName == null) {
278             throw new ValidationException("SelectQuery name is required.");
279         }
280
281         DataMap map = mediator.getCurrentDataMap();
282         Query matchingQuery = map.getQuery(newName);
283
284         if (matchingQuery == null) {
285             // completely new name, set new name for entity
286
QueryEvent e = new QueryEvent(this, query, query.getName());
287             ProjectUtil.setQueryName(map, query, newName);
288             mediator.fireQueryEvent(e);
289         }
290         else if (matchingQuery != query) {
291             // there is a query with the same name
292
throw new ValidationException("There is another query named '"
293                     + newName
294                     + "'. Use a different name.");
295         }
296     }
297 }
Popular Tags