KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
60 import java.awt.Component JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Arrays JavaDoc;
63 import java.util.Collections JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import javax.swing.DefaultComboBoxModel JavaDoc;
68 import javax.swing.DefaultListCellRenderer JavaDoc;
69 import javax.swing.JList JavaDoc;
70 import javax.swing.JPanel JavaDoc;
71 import javax.swing.JScrollPane JavaDoc;
72 import javax.swing.JTextArea JavaDoc;
73 import javax.swing.ListSelectionModel JavaDoc;
74 import javax.swing.event.DocumentEvent JavaDoc;
75 import javax.swing.event.ListSelectionEvent JavaDoc;
76 import javax.swing.event.ListSelectionListener JavaDoc;
77 import javax.swing.text.BadLocationException JavaDoc;
78 import javax.swing.text.Document JavaDoc;
79
80 import org.objectstyle.cayenne.map.event.QueryEvent;
81 import org.objectstyle.cayenne.modeler.ProjectController;
82 import org.objectstyle.cayenne.modeler.util.DbAdapterInfo;
83 import org.objectstyle.cayenne.modeler.util.TextAdapter;
84 import org.objectstyle.cayenne.query.Query;
85 import org.objectstyle.cayenne.query.SQLTemplate;
86 import org.objectstyle.cayenne.util.Util;
87
88 import com.jgoodies.forms.builder.PanelBuilder;
89 import com.jgoodies.forms.layout.CellConstraints;
90 import com.jgoodies.forms.layout.FormLayout;
91
92 /**
93  * A panel for configuring SQL scripts of a SQL template.
94  *
95  * @author Andrei Adamchik
96  */

97 public class SQLTemplateScriptsTab extends JPanel JavaDoc {
98
99     private static final String JavaDoc DEFAULT_LABEL = "Default";
100
101     protected ProjectController mediator;
102
103     protected JList JavaDoc scripts;
104     protected TextAdapter script;
105     protected ListSelectionListener JavaDoc scriptRefreshHandler;
106
107     public SQLTemplateScriptsTab(ProjectController mediator) {
108         this.mediator = mediator;
109
110         initView();
111     }
112
113     protected void initView() {
114         // create widgets, etc.
115

116         scriptRefreshHandler = new ListSelectionListener JavaDoc() {
117
118             public void valueChanged(ListSelectionEvent JavaDoc e) {
119                 if (!e.getValueIsAdjusting()) {
120                     displayScript();
121                 }
122             }
123         };
124
125         scripts = new JList JavaDoc();
126         scripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
127         scripts.setCellRenderer(new DbAdapterListRenderer(DbAdapterInfo
128                 .getStandardAdapterLabels()));
129
130         List JavaDoc keys = new ArrayList JavaDoc(DbAdapterInfo.getStandardAdapters().length + 1);
131         keys.addAll(Arrays.asList(DbAdapterInfo.getStandardAdapters()));
132         Collections.sort(keys);
133         keys.add(0, DEFAULT_LABEL);
134         scripts.setModel(new DefaultComboBoxModel JavaDoc(keys.toArray()));
135
136         script = new TextAdapter(new JTextArea JavaDoc(15, 30)) {
137
138             protected void updateModel(String JavaDoc text) {
139                 setSQL(text);
140             }
141         };
142
143         // assemble
144
CellConstraints cc = new CellConstraints();
145         PanelBuilder builder = new PanelBuilder(new FormLayout(
146                 "fill:100dlu, 3dlu, fill:100dlu:grow",
147                 "3dlu, fill:p:grow"));
148
149         // orderings table must grow as the panel is resized
150
builder.add(new JScrollPane JavaDoc(
151                 scripts,
152                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
153                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), cc.xy(1, 2));
154         builder.add(new JScrollPane JavaDoc(script.getComponent()), cc.xy(3, 2));
155
156         setLayout(new BorderLayout JavaDoc());
157         add(builder.getPanel(), BorderLayout.CENTER);
158     }
159
160     void initFromModel() {
161         Query query = mediator.getCurrentQuery();
162
163         if (!(query instanceof SQLTemplate)) {
164             setVisible(false);
165             return;
166         }
167
168         // select default script.. display it bypassing the listener...
169
scripts.removeListSelectionListener(scriptRefreshHandler);
170         scripts.setSelectedIndex(0);
171         displayScript();
172         scripts.addListSelectionListener(scriptRefreshHandler);
173
174         script.getComponent().setEnabled(true);
175         setVisible(true);
176     }
177
178     /**
179      * Returns SQLTemplate text for current selection.
180      */

181     String JavaDoc getSQLTemplate(String JavaDoc key) {
182         if (key == null) {
183             return null;
184         }
185
186         SQLTemplate query = getQuery();
187         if (query == null) {
188             return null;
189         }
190
191         return (key.equals(DEFAULT_LABEL)) ? query.getDefaultTemplate() : query
192                 .getCustomTemplate(key);
193     }
194
195     SQLTemplate getQuery() {
196         Query query = mediator.getCurrentQuery();
197         return (query instanceof SQLTemplate) ? (SQLTemplate) query : null;
198     }
199
200     /**
201      * Shows selected script in the editor.
202      */

203     void displayScript() {
204
205         SQLTemplate query = getQuery();
206         if (query == null) {
207             disableEditor();
208             return;
209         }
210
211         String JavaDoc key = (String JavaDoc) scripts.getSelectedValue();
212         if (key == null) {
213             disableEditor();
214             return;
215         }
216
217         enableEditor();
218
219         String JavaDoc text = (key.equals(DEFAULT_LABEL)) ? query.getDefaultTemplate() : query
220                 .getCustomTemplate(key);
221
222         script.setText(text);
223     }
224
225     void disableEditor() {
226         script.setText(null);
227         script.getComponent().setEnabled(false);
228         script.getComponent().setEditable(false);
229         script.getComponent().setBackground(getBackground());
230     }
231
232     void enableEditor() {
233         script.getComponent().setEnabled(true);
234         script.getComponent().setEditable(true);
235         script.getComponent().setBackground(Color.WHITE);
236     }
237
238     void setSQL(DocumentEvent JavaDoc e) {
239         Document JavaDoc doc = e.getDocument();
240
241         try {
242             setSQL(doc.getText(0, doc.getLength()));
243         }
244         catch (BadLocationException JavaDoc e1) {
245             e1.printStackTrace();
246         }
247
248     }
249
250     /**
251      * Sets the value of SQL template for the currently selected script.
252      */

253     void setSQL(String JavaDoc text) {
254         SQLTemplate query = getQuery();
255         if (query == null) {
256             return;
257         }
258
259         String JavaDoc key = (String JavaDoc) scripts.getSelectedValue();
260         if (key == null) {
261             return;
262         }
263
264         if (text != null) {
265             text = text.trim();
266             if (text.length() == 0) {
267                 text = null;
268             }
269         }
270
271         // Compare the value before modifying the query - text area
272
// will call "verify" even if no changes have occured....
273
if (key.equals(DEFAULT_LABEL)) {
274             if (!Util.nullSafeEquals(text, query.getDefaultTemplate())) {
275                 query.setDefaultTemplate(text);
276                 mediator.fireQueryEvent(new QueryEvent(this, query));
277             }
278         }
279         else {
280             if (!Util.nullSafeEquals(text, query.getTemplate(key))) {
281                 query.setTemplate(key, text);
282                 mediator.fireQueryEvent(new QueryEvent(this, query));
283             }
284         }
285     }
286
287     final class DbAdapterListRenderer extends DefaultListCellRenderer JavaDoc {
288
289         Map JavaDoc adapterLabels;
290
291         DbAdapterListRenderer(Map JavaDoc adapterLabels) {
292             this.adapterLabels = (adapterLabels != null)
293                     ? adapterLabels
294                     : Collections.EMPTY_MAP;
295         }
296
297         public Component JavaDoc getListCellRendererComponent(
298                 JList JavaDoc list,
299                 Object JavaDoc object,
300                 int index,
301                 boolean selected,
302                 boolean hasFocus) {
303
304             if (object instanceof Class JavaDoc) {
305                 object = ((Class JavaDoc) object).getName();
306             }
307
308             Object JavaDoc label = adapterLabels.get(object);
309             if (label == null) {
310                 label = object;
311             }
312
313             Component JavaDoc c = super.getListCellRendererComponent(
314                     list,
315                     label,
316                     index,
317                     selected,
318                     hasFocus);
319
320             // grey out keys that have no SQL
321
setForeground(selected || getSQLTemplate(object.toString()) != null
322                     ? Color.BLACK
323                     : Color.LIGHT_GRAY);
324
325             return c;
326         }
327     }
328 }
Popular Tags