KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > pref > DataSourcePreferences


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.pref;
57
58 import java.awt.Component JavaDoc;
59 import java.sql.Connection JavaDoc;
60 import java.sql.Driver JavaDoc;
61 import java.sql.SQLException JavaDoc;
62 import java.util.Arrays JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import javax.swing.DefaultComboBoxModel JavaDoc;
66 import javax.swing.JOptionPane JavaDoc;
67
68 import org.objectstyle.cayenne.conn.DriverDataSource;
69 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo;
70 import org.objectstyle.cayenne.modeler.util.CayenneController;
71 import org.objectstyle.cayenne.pref.Domain;
72 import org.objectstyle.cayenne.pref.PreferenceEditor;
73 import org.objectstyle.cayenne.swing.BindingBuilder;
74 import org.objectstyle.cayenne.util.Util;
75
76 /**
77  * Editor for the local DataSources configured in preferences.
78  *
79  * @author Andrei Adamchik
80  */

81 public class DataSourcePreferences extends CayenneController {
82
83     protected DataSourcePreferencesView view;
84     protected PreferenceEditor editor;
85     protected String JavaDoc dataSourceKey;
86     protected Map JavaDoc dataSources;
87
88     public DataSourcePreferences(PreferenceDialog parentController) {
89         super(parentController);
90
91         this.view = new DataSourcePreferencesView(this);
92         this.editor = parentController.getEditor();
93
94         // init view data
95
this.dataSources = getDataSourceDomain().getDetailsMap(DBConnectionInfo.class);
96
97         Object JavaDoc[] keys = dataSources.keySet().toArray();
98         Arrays.sort(keys);
99         DefaultComboBoxModel JavaDoc dataSourceModel = new DefaultComboBoxModel JavaDoc(keys);
100         view.getDataSources().setModel(dataSourceModel);
101
102         initBindings();
103
104         // show first item
105
if (keys.length > 0) {
106             view.getDataSources().setSelectedIndex(0);
107             editDataSourceAction();
108         }
109     }
110
111     public Component JavaDoc getView() {
112         return view;
113     }
114
115     protected void initBindings() {
116         BindingBuilder builder = new BindingBuilder(
117                 getApplication().getBindingFactory(),
118                 this);
119         builder.bindToAction(view.getAddDataSource(), "newDataSourceAction()");
120         builder
121                 .bindToAction(
122                         view.getDuplicateDataSource(),
123                         "duplicateDataSourceAction()");
124         builder.bindToAction(view.getRemoveDataSource(), "removeDataSourceAction()");
125         builder.bindToAction(view.getTestDataSource(), "testDataSourceAction()");
126
127         builder.bindToComboSelection(view.getDataSources(), "dataSourceKey");
128     }
129
130     public Domain getDataSourceDomain() {
131         return editor.editableInstance(getApplication().getPreferenceDomain());
132     }
133
134     public PreferenceEditor getEditor() {
135         return editor;
136     }
137
138     public Map JavaDoc getDataSources() {
139         return dataSources;
140     }
141
142     public String JavaDoc getDataSourceKey() {
143         return dataSourceKey;
144     }
145
146     public void setDataSourceKey(String JavaDoc dataSourceKey) {
147         this.dataSourceKey = dataSourceKey;
148         editDataSourceAction();
149     }
150
151     public DBConnectionInfo getConnectionInfo() {
152         return (DBConnectionInfo) dataSources.get(dataSourceKey);
153     }
154
155     /**
156      * Shows a dialog to create new local DataSource configuration.
157      */

158     public void newDataSourceAction() {
159
160         DataSourceCreator creatorWizard = new DataSourceCreator(this);
161         DBConnectionInfo dataSource = creatorWizard.startupAction();
162
163         if (dataSource != null) {
164             dataSources.put(creatorWizard.getName(), dataSource);
165
166             Object JavaDoc[] keys = dataSources.keySet().toArray();
167             Arrays.sort(keys);
168             view.getDataSources().setModel(new DefaultComboBoxModel JavaDoc(keys));
169             view.getDataSources().setSelectedItem(creatorWizard.getName());
170             editDataSourceAction();
171         }
172     }
173
174     /**
175      * Shows a dialog to duplicate an existing local DataSource configuration.
176      */

177     public void duplicateDataSourceAction() {
178         Object JavaDoc selected = view.getDataSources().getSelectedItem();
179         if (selected != null) {
180             DataSourceDuplicator wizard = new DataSourceDuplicator(this, selected
181                     .toString());
182             DBConnectionInfo dataSource = wizard.startupAction();
183
184             if (dataSource != null) {
185                 dataSources.put(wizard.getName(), dataSource);
186
187                 Object JavaDoc[] keys = dataSources.keySet().toArray();
188                 Arrays.sort(keys);
189                 view.getDataSources().setModel(new DefaultComboBoxModel JavaDoc(keys));
190                 view.getDataSources().setSelectedItem(wizard.getName());
191                 editDataSourceAction();
192             }
193         }
194     }
195
196     /**
197      * Removes current DataSource.
198      */

199     public void removeDataSourceAction() {
200         String JavaDoc key = getDataSourceKey();
201         if (key != null) {
202             editor.deleteDetail(getDataSourceDomain(), key);
203             dataSources.remove(key);
204
205             Object JavaDoc[] keys = dataSources.keySet().toArray();
206             Arrays.sort(keys);
207             view.getDataSources().setModel(new DefaultComboBoxModel JavaDoc(keys));
208             editDataSourceAction(keys.length > 0 ? keys[0] : null);
209         }
210     }
211
212     /**
213      * Opens specified DataSource in the editor.
214      */

215     public void editDataSourceAction(Object JavaDoc dataSourceKey) {
216         view.getDataSources().setSelectedItem(dataSourceKey);
217         editDataSourceAction();
218     }
219
220     /**
221      * Opens current DataSource in the editor.
222      */

223     public void editDataSourceAction() {
224         this.view.getDataSourceEditor().setConnectionInfo(getConnectionInfo());
225     }
226
227     /**
228      * Tries to establish a DB connection, reporting the status of this operation.
229      */

230     public void testDataSourceAction() {
231         DBConnectionInfo currentDataSource = getConnectionInfo();
232         if (currentDataSource == null) {
233             return;
234         }
235
236         if (currentDataSource.getJdbcDriver() == null) {
237             JOptionPane.showMessageDialog(
238                     null,
239                     "No JDBC Driver specified",
240                     "Warning",
241                     JOptionPane.WARNING_MESSAGE);
242             return;
243         }
244
245         if (currentDataSource.getUrl() == null) {
246             JOptionPane.showMessageDialog(
247                     null,
248                     "No Database URL specified",
249                     "Warning",
250                     JOptionPane.WARNING_MESSAGE);
251             return;
252         }
253
254         try {
255             Class JavaDoc driverClass = getApplication().getClassLoadingService().loadClass(
256                     currentDataSource.getJdbcDriver());
257             Driver JavaDoc driver = (Driver JavaDoc) driverClass.newInstance();
258
259             // connect via Cayenne DriverDataSource - it addresses some driver issues...
260
Connection JavaDoc c = new DriverDataSource(
261                     driver,
262                     currentDataSource.getUrl(),
263                     currentDataSource.getUserName(),
264                     currentDataSource.getPassword()).getConnection();
265             try {
266                 c.close();
267             }
268             catch (SQLException JavaDoc e) {
269                 // i guess we can ignore this...
270
}
271
272             JOptionPane.showMessageDialog(
273                     null,
274                     "Connected Successfully",
275                     "Success",
276                     JOptionPane.INFORMATION_MESSAGE);
277         }
278         catch (Throwable JavaDoc th) {
279             th = Util.unwindException(th);
280             JOptionPane.showMessageDialog(null, "Error connecting to DB: "
281                     + th.getLocalizedMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
282             return;
283         }
284     }
285
286 }
Popular Tags