KickJava   Java API By Example, From Geeks To Geeks.

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


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.map.DataMap;
70 import org.objectstyle.cayenne.map.ObjEntity;
71 import org.objectstyle.cayenne.map.event.QueryEvent;
72 import org.objectstyle.cayenne.modeler.ProjectController;
73 import org.objectstyle.cayenne.modeler.event.QueryDisplayEvent;
74 import org.objectstyle.cayenne.modeler.event.QueryDisplayListener;
75 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
76 import org.objectstyle.cayenne.modeler.util.CellRenderers;
77 import org.objectstyle.cayenne.modeler.util.Comparators;
78 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
79 import org.objectstyle.cayenne.modeler.util.TextAdapter;
80 import org.objectstyle.cayenne.query.GenericSelectQuery;
81 import org.objectstyle.cayenne.query.ProcedureQuery;
82 import org.objectstyle.cayenne.query.Query;
83 import org.objectstyle.cayenne.util.Util;
84 import org.objectstyle.cayenne.validation.ValidationException;
85
86 import com.jgoodies.forms.builder.PanelBuilder;
87 import com.jgoodies.forms.layout.CellConstraints;
88 import com.jgoodies.forms.layout.FormLayout;
89
90 /**
91  * @author Andrei Adamchik
92  */

93 public class ProcedureQueryView extends JPanel JavaDoc {
94
95     protected ProjectController mediator;
96     protected TextAdapter name;
97     protected JComboBox JavaDoc queryRoot;
98     protected JCheckBox JavaDoc selecting;
99     protected SelectPropertiesPanel properties;
100
101     public ProcedureQueryView(ProjectController mediator) {
102         this.mediator = mediator;
103
104         initView();
105         initController();
106     }
107
108     private void initView() {
109         // create widgets
110
name = new TextAdapter(new JTextField JavaDoc()) {
111
112             protected void updateModel(String JavaDoc text) {
113                 setQueryName(text);
114             }
115         };
116         selecting = new JCheckBox JavaDoc();
117         queryRoot = CayenneWidgetFactory.createComboBox();
118         queryRoot.setRenderer(CellRenderers.listRendererWithIcons());
119         properties = new RawQueryPropertiesPanel(mediator) {
120
121             protected void setEntity(ObjEntity entity) {
122                 ProcedureQueryView.this.setEntity(entity);
123             }
124
125             public ObjEntity getEntity(GenericSelectQuery query) {
126                 if (query instanceof ProcedureQuery) {
127                     return ProcedureQueryView.this.getEntity((ProcedureQuery) query);
128                 }
129
130                 return null;
131             }
132         };
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");
139         PanelBuilder builder = new PanelBuilder(layout);
140         builder.setDefaultDialogBorder();
141
142         builder.addSeparator("ProcedureQuery 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("Procedure:", cc.xy(1, 5));
146         builder.add(queryRoot, cc.xy(3, 5));
147         builder.addLabel("Is Selecting:", cc.xy(1, 7));
148         builder.add(selecting, cc.xy(3, 7));
149
150         this.setLayout(new BorderLayout JavaDoc());
151         this.add(builder.getPanel(), BorderLayout.NORTH);
152         this.add(properties, BorderLayout.CENTER);
153     }
154
155     private void initController() {
156
157         queryRoot.addActionListener(new ActionListener JavaDoc() {
158
159             public void actionPerformed(ActionEvent JavaDoc event) {
160                 Query query = mediator.getCurrentQuery();
161                 if (query != null) {
162                     query.setRoot(queryRoot.getModel().getSelectedItem());
163                     mediator.fireQueryEvent(new QueryEvent(this, query));
164                 }
165             }
166         });
167
168         mediator.addQueryDisplayListener(new QueryDisplayListener() {
169
170             public void currentQueryChanged(QueryDisplayEvent e) {
171                 initFromModel();
172             }
173         });
174
175         selecting.addActionListener(new ActionListener JavaDoc() {
176
177             public void actionPerformed(ActionEvent JavaDoc event) {
178                 properties.setEnabled(selecting.isSelected());
179
180                 Query query = mediator.getCurrentQuery();
181                 if (query instanceof ProcedureQuery) {
182                     ((ProcedureQuery) query).setSelecting(selecting.isSelected());
183                     mediator.fireQueryEvent(new QueryEvent(this, query));
184                 }
185             }
186         });
187     }
188
189     /**
190      * Updates the view from the current model state. Invoked when a currently displayed
191      * query is changed.
192      */

193     void initFromModel() {
194         Query query = mediator.getCurrentQuery();
195
196         if (!(query instanceof ProcedureQuery)) {
197             setVisible(false);
198             return;
199         }
200
201         ProcedureQuery procedureQuery = (ProcedureQuery) query;
202
203         selecting.setSelected(procedureQuery.isSelecting());
204         properties.setEnabled(procedureQuery.isSelecting());
205         name.setText(procedureQuery.getName());
206
207         // init root choices and title label..
208

209         // - ProcedureQuery supports Procedure roots
210

211         // TODO: now we only allow roots from the current map,
212
// since query root is fully resolved during map loading,
213
// making it impossible to reference other DataMaps.
214

215         DataMap map = mediator.getCurrentDataMap();
216         Object JavaDoc[] roots = map.getProcedures().toArray();
217
218         if (roots.length > 1) {
219             Arrays.sort(roots, Comparators.getDataMapChildrenComparator());
220         }
221
222         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(roots);
223         model.setSelectedItem(query.getRoot());
224         queryRoot.setModel(model);
225
226         properties.initFromModel(procedureQuery);
227         setVisible(true);
228     }
229
230     /**
231      * Initializes Query name from string.
232      */

233     void setQueryName(String JavaDoc newName) {
234         if (newName != null && newName.trim().length() == 0) {
235             newName = null;
236         }
237
238         Query query = mediator.getCurrentQuery();
239         if (query == null) {
240             return;
241         }
242
243         if (Util.nullSafeEquals(newName, query.getName())) {
244             return;
245         }
246
247         if (newName == null) {
248             throw new ValidationException("Query name is required.");
249         }
250
251         DataMap map = mediator.getCurrentDataMap();
252
253         if (map.getQuery(newName) == null) {
254             // completely new name, set new name for entity
255
QueryEvent e = new QueryEvent(this, query, query.getName());
256             ProjectUtil.setQueryName(map, query, newName);
257             mediator.fireQueryEvent(e);
258         }
259         else {
260             // there is a query with the same name
261
throw new ValidationException("There is another query named '"
262                     + newName
263                     + "'. Use a different name.");
264         }
265     }
266
267     /**
268      * Returns an entity that maps to a procedure query result class.
269      */

270     ObjEntity getEntity(ProcedureQuery query) {
271         String JavaDoc resultClass = query.getResultClassName();
272         if (resultClass == null) {
273             return null;
274         }
275
276         // currently entity resolver doesn't support lookup by class name
277
// we will have to do manual ObjEntity scan...
278
// TODO: replace this once such indexing is supported
279

280         DataMap map = mediator.getCurrentDataMap();
281         if (map == null) {
282             return null;
283         }
284
285         return map.getObjEntityForJavaClass(resultClass);
286     }
287
288     void setEntity(ObjEntity entity) {
289         Query query = mediator.getCurrentQuery();
290         if (query instanceof ProcedureQuery) {
291             ProcedureQuery procedureQuery = (ProcedureQuery) query;
292             String JavaDoc resultClass = null;
293             
294             if (entity != null) {
295                 resultClass = entity.getClassName();
296             }
297             
298             procedureQuery.setResultClassName(resultClass);
299             mediator.fireQueryEvent(new QueryEvent(this, procedureQuery));
300         }
301     }
302 }
Popular Tags