KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
62 import java.util.Collections JavaDoc;
63 import java.util.List JavaDoc;
64
65 import javax.swing.DefaultComboBoxModel JavaDoc;
66 import javax.swing.JCheckBox JavaDoc;
67 import javax.swing.JComboBox JavaDoc;
68
69 import org.objectstyle.cayenne.map.DataMap;
70 import org.objectstyle.cayenne.map.ObjEntity;
71 import org.objectstyle.cayenne.modeler.ProjectController;
72 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
73 import org.objectstyle.cayenne.modeler.util.CellRenderers;
74 import org.objectstyle.cayenne.modeler.util.Comparators;
75 import org.objectstyle.cayenne.query.GenericSelectQuery;
76
77 import com.jgoodies.forms.builder.PanelBuilder;
78 import com.jgoodies.forms.layout.CellConstraints;
79 import com.jgoodies.forms.layout.FormLayout;
80
81 /**
82  * A panel that supports editing the properties a query not based on ObjEntity, but still
83  * supporting DataObjects retrieval.
84  *
85  * @author Andrei Adamchik
86  */

87 public abstract class RawQueryPropertiesPanel extends SelectPropertiesPanel {
88
89     protected JCheckBox JavaDoc dataObjects;
90     protected JComboBox JavaDoc entities;
91
92     public RawQueryPropertiesPanel(ProjectController mediator) {
93         super(mediator);
94     }
95
96     protected void initController() {
97         super.initController();
98         dataObjects.addActionListener(new ActionListener JavaDoc() {
99
100             public void actionPerformed(ActionEvent JavaDoc event) {
101                 setFetchingDataObjects(dataObjects.isSelected());
102             }
103         });
104
105         entities.addActionListener(new ActionListener JavaDoc() {
106
107             public void actionPerformed(ActionEvent JavaDoc event) {
108                 ObjEntity entity = (ObjEntity) entities.getModel().getSelectedItem();
109                 setEntity(entity);
110             }
111         });
112     }
113
114     protected void initView() {
115         super.initView();
116
117         // create widgets
118

119         dataObjects = new JCheckBox JavaDoc();
120
121         entities = CayenneWidgetFactory.createComboBox();
122         entities.setRenderer(CellRenderers.listRendererWithIcons());
123
124         // assemble
125
CellConstraints cc = new CellConstraints();
126         FormLayout layout = new FormLayout(
127                 "right:max(80dlu;pref), 3dlu, left:max(10dlu;pref), "
128                         + "3dlu, left:max(37dlu;pref), 3dlu, fill:max(147dlu;pref)",
129                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
130         PanelBuilder builder = new PanelBuilder(layout);
131         builder.setDefaultDialogBorder();
132
133         builder.addSeparator("", cc.xywh(1, 1, 7, 1));
134
135         builder.addLabel("Result Caching:", cc.xy(1, 3));
136         builder.add(cachePolicy, cc.xywh(3, 3, 5, 1));
137         builder.addLabel("Fetch Data Objects:", cc.xy(1, 7));
138         builder.add(dataObjects, cc.xy(3, 7));
139         builder.add(entities, cc.xywh(5, 7, 3, 1));
140         builder.addLabel("Refresh Results:", cc.xy(1, 9));
141         builder.add(refreshesResults, cc.xy(3, 9));
142         builder.addLabel("Fetch Limit, Rows:", cc.xy(1, 11));
143         builder.add(fetchLimit.getComponent(), cc.xywh(3, 11, 3, 1));
144         builder.addLabel("Page Size:", cc.xy(1, 13));
145         builder.add(pageSize.getComponent(), cc.xywh(3, 13, 3, 1));
146
147         this.setLayout(new BorderLayout JavaDoc());
148         this.add(builder.getPanel(), BorderLayout.CENTER);
149     }
150
151     /**
152      * Updates the view from the current model state. Invoked when a currently displayed
153      * query is changed.
154      */

155     public void initFromModel(GenericSelectQuery query) {
156         super.initFromModel(query);
157
158         boolean fetchingDO = !query.isFetchingDataRows();
159         dataObjects.setSelected(fetchingDO);
160
161         // TODO: now we only allow ObjEntities from the current map,
162
// since query root is fully resolved during map loading,
163
// making it impossible to reference other DataMaps.
164

165         DataMap map = mediator.getCurrentDataMap();
166         List JavaDoc objEntities = new ArrayList JavaDoc();
167         objEntities.addAll(map.getObjEntities());
168
169         if (objEntities.size() > 1) {
170             Collections.sort(objEntities, Comparators.getDataMapChildrenComparator());
171         }
172
173         entities.setEnabled(fetchingDO && isEnabled());
174         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(objEntities.toArray());
175         model.setSelectedItem(getEntity(query));
176         entities.setModel(model);
177     }
178
179     protected abstract void setEntity(ObjEntity selectedEntity);
180
181     protected abstract ObjEntity getEntity(GenericSelectQuery query);
182
183     protected void setFetchingDataObjects(boolean dataObjects) {
184         entities.setEnabled(dataObjects && isEnabled());
185
186         if (!dataObjects) {
187             entities.getModel().setSelectedItem(null);
188         }
189
190         setQueryProperty("fetchingDataRows", dataObjects ? Boolean.FALSE : Boolean.TRUE);
191     }
192 }
Popular Tags