KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > db > DataSourceWizard


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.dialog.db;
57
58 import java.awt.Component JavaDoc;
59 import java.sql.Connection JavaDoc;
60 import java.sql.SQLException JavaDoc;
61 import java.util.Arrays JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import javax.swing.DefaultComboBoxModel JavaDoc;
65 import javax.swing.JDialog JavaDoc;
66
67 import org.objectstyle.cayenne.modeler.ClassLoadingService;
68 import org.objectstyle.cayenne.modeler.dialog.pref.PreferenceDialog;
69 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo;
70 import org.objectstyle.cayenne.modeler.util.CayenneController;
71 import org.objectstyle.cayenne.swing.BindingBuilder;
72 import org.objectstyle.cayenne.swing.ObjectBinding;
73
74 /**
75  * A subclass of ConnectionWizard that tests configured DataSource, but does not keep an
76  * open connection.
77  *
78  * @author Andrei Adamchik
79  */

80 public class DataSourceWizard extends CayenneController {
81
82     protected DataSourceWizardView view;
83
84     protected DBConnectionInfo altDataSource;
85     protected String JavaDoc altDataSourceKey;
86     protected ObjectBinding dataSourceBinding;
87     protected Map JavaDoc dataSources;
88
89     protected String JavaDoc dataSourceKey;
90
91     // this object is a clone of an object selected from the dropdown, as we need to allow
92
// local temporary modifications
93
protected DBConnectionInfo connectionInfo;
94
95     protected boolean canceled;
96
97     public DataSourceWizard(CayenneController parent, String JavaDoc title,
98             String JavaDoc altDataSourceKey, DBConnectionInfo altDataSource) {
99         super(parent);
100
101         this.view = new DataSourceWizardView(this);
102         this.view.setTitle(title);
103         this.altDataSource = altDataSource;
104         this.altDataSourceKey = altDataSourceKey;
105         this.connectionInfo = new DBConnectionInfo();
106
107         initBindings();
108     }
109
110     protected void initBindings() {
111         BindingBuilder builder = new BindingBuilder(
112                 getApplication().getBindingFactory(),
113                 this);
114
115         dataSourceBinding = builder.bindToComboSelection(
116                 view.getDataSources(),
117                 "dataSourceKey");
118
119         builder.bindToAction(view.getCancelButton(), "cancelAction()");
120         builder.bindToAction(view.getOkButton(), "okAction()");
121         builder.bindToAction(view.getConfigButton(), "dataSourceConfigAction()");
122     }
123
124     public String JavaDoc getDataSourceKey() {
125         return dataSourceKey;
126     }
127
128     public void setDataSourceKey(String JavaDoc dataSourceKey) {
129         this.dataSourceKey = dataSourceKey;
130
131         // update a clone object that will be used to obtain connection...
132
DBConnectionInfo currentInfo = (DBConnectionInfo) dataSources.get(dataSourceKey);
133         if (currentInfo != null) {
134             currentInfo.copyTo(connectionInfo);
135         }
136         else {
137             connectionInfo = new DBConnectionInfo();
138         }
139
140         view.getConnectionInfo().setConnectionInfo(connectionInfo);
141     }
142
143     /**
144      * Main action method that pops up a dialog asking for user selection. Returns true if
145      * the selection was confirmed, false - if canceled.
146      */

147     public boolean startupAction() {
148         this.canceled = true;
149
150         refreshDataSources();
151
152         view.pack();
153         view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
154         view.setModal(true);
155         makeCloseableOnEscape();
156         centerView();
157         view.setVisible(true);
158
159         return !canceled;
160     }
161
162     public DBConnectionInfo getConnectionInfo() {
163         return connectionInfo;
164     }
165
166     /**
167      * Tests that the entered information is valid and can be used to open a conneciton.
168      * Does not store the open connection.
169      */

170     public void okAction() {
171         DBConnectionInfo info = getConnectionInfo();
172         ClassLoadingService classLoader = getApplication().getClassLoadingService();
173
174         // try making an adapter...
175
try {
176             info.makeAdapter(classLoader);
177         }
178         catch (Throwable JavaDoc th) {
179             reportError("DbAdapter Error", th);
180             return;
181         }
182
183         // doing connection testing...
184
// attempt opening the connection, and close it right away
185
try {
186             Connection JavaDoc connection = info.makeDataSource(classLoader).getConnection();
187             try {
188                 connection.close();
189             }
190             catch (SQLException JavaDoc ex) {
191                 // ignore close error
192
}
193         }
194         catch (Throwable JavaDoc th) {
195             reportError("Connection Error", th);
196             return;
197         }
198
199         // set success flag, and unblock the caller...
200
canceled = false;
201         view.dispose();
202     }
203
204     public void cancelAction() {
205         canceled = true;
206         view.dispose();
207     }
208
209     /**
210      * Opens preferences panel to allow configuration of DataSource presets.
211      */

212     public void dataSourceConfigAction() {
213         PreferenceDialog prefs = new PreferenceDialog(this);
214         prefs.showDataSourceEditorAction(dataSourceKey);
215         prefs.startupAction();
216
217         refreshDataSources();
218     }
219
220     public Component JavaDoc getView() {
221         return view;
222     }
223
224     protected void refreshDataSources() {
225         this.dataSources = getApplication().getPreferenceDomain().getDetailsMap(
226                 DBConnectionInfo.class);
227
228         if (altDataSourceKey != null
229                 && !dataSources.containsKey(altDataSourceKey)
230                 && altDataSource != null) {
231             dataSources.put(altDataSourceKey, altDataSource);
232         }
233
234         Object JavaDoc[] keys = dataSources.keySet().toArray();
235         Arrays.sort(keys);
236         view.getDataSources().setModel(new DefaultComboBoxModel JavaDoc(keys));
237
238         if (getDataSourceKey() == null) {
239             String JavaDoc key = null;
240
241             if (altDataSourceKey != null) {
242                 key = altDataSourceKey;
243             }
244             else if (keys.length > 0) {
245                 key = keys[0].toString();
246             }
247
248             setDataSourceKey(key);
249             dataSourceBinding.updateView();
250         }
251     }
252 }
Popular Tags