KickJava   Java API By Example, From Geeks To Geeks.

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


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
62 import javax.swing.JCheckBox JavaDoc;
63 import javax.swing.JPanel JavaDoc;
64 import javax.swing.JTextField JavaDoc;
65
66 import org.objectstyle.cayenne.map.DataMap;
67 import org.objectstyle.cayenne.map.ObjEntity;
68 import org.objectstyle.cayenne.map.event.QueryEvent;
69 import org.objectstyle.cayenne.modeler.ProjectController;
70 import org.objectstyle.cayenne.modeler.util.ProjectUtil;
71 import org.objectstyle.cayenne.modeler.util.TextAdapter;
72 import org.objectstyle.cayenne.query.GenericSelectQuery;
73 import org.objectstyle.cayenne.query.Query;
74 import org.objectstyle.cayenne.query.SQLTemplate;
75 import org.objectstyle.cayenne.util.Util;
76 import org.objectstyle.cayenne.validation.ValidationException;
77
78 import com.jgoodies.forms.builder.PanelBuilder;
79 import com.jgoodies.forms.layout.CellConstraints;
80 import com.jgoodies.forms.layout.FormLayout;
81
82 /**
83  * A main panel for editing a SQLTemplate.
84  *
85  * @author Andrei Adamchik
86  */

87 public class SQLTemplateMainTab extends JPanel JavaDoc {
88
89     protected ProjectController mediator;
90     protected TextAdapter name;
91     protected JCheckBox JavaDoc selecting;
92     protected SelectPropertiesPanel properties;
93
94     public SQLTemplateMainTab(ProjectController mediator) {
95         this.mediator = mediator;
96
97         initView();
98         initController();
99     }
100
101     private void initView() {
102         // create widgets
103
name = new TextAdapter(new JTextField JavaDoc()) {
104
105             protected void updateModel(String JavaDoc text) {
106                 setQueryName(text);
107             }
108         };
109
110         selecting = new JCheckBox JavaDoc();
111
112         properties = new RawQueryPropertiesPanel(mediator) {
113
114             protected void setEntity(ObjEntity entity) {
115                 SQLTemplateMainTab.this.setEntity(entity);
116             }
117
118             public ObjEntity getEntity(GenericSelectQuery query) {
119                 if (query instanceof SQLTemplate) {
120                     return SQLTemplateMainTab.this.getEntity((SQLTemplate) query);
121                 }
122
123                 return null;
124             }
125         };
126
127         // assemble
128
CellConstraints cc = new CellConstraints();
129         FormLayout layout = new FormLayout(
130                 "right:max(80dlu;pref), 3dlu, fill:max(200dlu;pref)",
131                 "p, 3dlu, p, 3dlu, p");
132         PanelBuilder builder = new PanelBuilder(layout);
133         builder.setDefaultDialogBorder();
134
135         builder.addSeparator("SQLTemplate Settings", cc.xywh(1, 1, 3, 1));
136         builder.addLabel("Query Name:", cc.xy(1, 3));
137         builder.add(name.getComponent(), cc.xy(3, 3));
138         builder.addLabel("Is Selecting:", cc.xy(1, 5));
139         builder.add(selecting, cc.xy(3, 5));
140
141         this.setLayout(new BorderLayout JavaDoc());
142         this.add(builder.getPanel(), BorderLayout.NORTH);
143         this.add(properties, BorderLayout.CENTER);
144     }
145
146     private void initController() {
147
148         selecting.addActionListener(new ActionListener JavaDoc() {
149
150             public void actionPerformed(ActionEvent JavaDoc event) {
151                 properties.setEnabled(selecting.isSelected());
152
153                 SQLTemplate query = getQuery();
154                 if (query != null) {
155                     query.setSelecting(selecting.isSelected());
156                     mediator.fireQueryEvent(new QueryEvent(this, query));
157                 }
158             }
159         });
160
161     }
162
163     /**
164      * Updates the view from the current model state. Invoked when a currently displayed
165      * query is changed.
166      */

167     void initFromModel() {
168         Query query = mediator.getCurrentQuery();
169
170         if (!(query instanceof SQLTemplate)) {
171             setVisible(false);
172             return;
173         }
174
175         SQLTemplate sqlQuery = (SQLTemplate) query;
176
177         name.setText(sqlQuery.getName());
178         selecting.setSelected(sqlQuery.isSelecting());
179         properties.setEnabled(sqlQuery.isSelecting());
180         properties.initFromModel(sqlQuery);
181
182         setVisible(true);
183     }
184
185     protected SQLTemplate getQuery() {
186         Query query = mediator.getCurrentQuery();
187         return (query instanceof SQLTemplate) ? (SQLTemplate) query : null;
188     }
189
190     /**
191      * Initializes Query name from string.
192      */

193     void setQueryName(String JavaDoc newName) {
194         if (newName != null && newName.trim().length() == 0) {
195             newName = null;
196         }
197
198         Query query = getQuery();
199         
200         if(query == null) {
201             return;
202         }
203
204         if (Util.nullSafeEquals(newName, query.getName())) {
205             return;
206         }
207
208         if (newName == null) {
209             throw new ValidationException("Query name is required.");
210         }
211
212         DataMap map = mediator.getCurrentDataMap();
213
214         if (map.getQuery(newName) == null) {
215             // completely new name, set new name for entity
216
QueryEvent e = new QueryEvent(this, query, query.getName());
217             ProjectUtil.setQueryName(map, query, newName);
218             mediator.fireQueryEvent(e);
219         }
220         else {
221             // there is a query with the same name
222
throw new ValidationException("There is another query named '"
223                     + newName
224                     + "'. Use a different name.");
225         }
226     }
227
228     /**
229      * Returns an entity that maps to a procedure query result class.
230      */

231     ObjEntity getEntity(SQLTemplate query) {
232         return query != null && query.getRoot() instanceof ObjEntity ? (ObjEntity) query
233                 .getRoot() : null;
234     }
235
236     void setEntity(ObjEntity entity) {
237         SQLTemplate template = getQuery();
238         if (template != null) {
239             // in case of null entity, set root to DataMap
240
Object JavaDoc root = entity != null ? (Object JavaDoc) entity : mediator.getCurrentDataMap();
241             template.setRoot(root);
242
243             mediator.fireQueryEvent(new QueryEvent(this, template));
244         }
245     }
246 }
Popular Tags