KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
59 import java.awt.FlowLayout JavaDoc;
60 import java.awt.event.ActionEvent JavaDoc;
61 import java.awt.event.ActionListener JavaDoc;
62 import java.util.Collection JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import javax.swing.DefaultComboBoxModel JavaDoc;
66 import javax.swing.JButton JavaDoc;
67 import javax.swing.JCheckBox JavaDoc;
68 import javax.swing.JComboBox JavaDoc;
69 import javax.swing.JDialog JavaDoc;
70 import javax.swing.JLabel JavaDoc;
71 import javax.swing.JPanel JavaDoc;
72 import javax.swing.JTextField JavaDoc;
73 import javax.swing.event.ChangeEvent JavaDoc;
74 import javax.swing.event.ChangeListener JavaDoc;
75
76 import org.objectstyle.cayenne.access.DbLoader;
77 import org.objectstyle.cayenne.modeler.Application;
78 import org.objectstyle.cayenne.modeler.util.CayenneDialog;
79
80 import com.jgoodies.forms.builder.DefaultFormBuilder;
81 import com.jgoodies.forms.layout.FormLayout;
82
83 /**
84  * Dialog for selecting database reverse-engineering parameters.
85  */

86 public class DbLoaderOptionsDialog extends CayenneDialog {
87
88     public static final int CANCEL = 0;
89     public static final int SELECT = 1;
90
91     protected JLabel JavaDoc schemaLabel;
92     protected JComboBox JavaDoc schemaSelector;
93     protected JTextField JavaDoc tableNamePatternField;
94     protected JCheckBox JavaDoc loadProcedures;
95     protected JTextField JavaDoc procNamePatternField;
96     protected JLabel JavaDoc procedureLabel;
97     protected JButton JavaDoc selectButton;
98     protected JButton JavaDoc cancelButton;
99     protected int choice;
100
101     /**
102      * Creates and initializes new ChooseSchemaDialog.
103      */

104     public DbLoaderOptionsDialog(Collection JavaDoc schemas, String JavaDoc dbUserName,
105             boolean loadProcedures) {
106         super(Application.getFrame(), "Reengineer DB Schema: Select Options");
107
108         init();
109         initController();
110         initFromModel(schemas, dbUserName, loadProcedures);
111
112         pack();
113         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
114         setModal(true);
115         centerWindow();
116     }
117
118     /** Sets up the graphical components. */
119     protected void init() {
120
121         // create widgets...
122
selectButton = new JButton JavaDoc("Continue");
123         cancelButton = new JButton JavaDoc("Cancel");
124         schemaSelector = new JComboBox JavaDoc();
125         tableNamePatternField = new JTextField JavaDoc();
126         procNamePatternField = new JTextField JavaDoc();
127         loadProcedures = new JCheckBox JavaDoc();
128
129         // assemble
130
FormLayout layout = new FormLayout(
131                 "right:pref, 3dlu, fill:max(170dlu;pref):grow",
132                 "");
133         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
134         builder.setDefaultDialogBorder();
135
136         schemaLabel = builder.append("Select Schema:", schemaSelector);
137         builder.append("Table Name Pattern:", tableNamePatternField);
138         builder.append("Load Procedures:", loadProcedures);
139         procedureLabel = builder.append("Procedure Name Pattern:", procNamePatternField);
140
141         JPanel JavaDoc buttons = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
142         buttons.add(cancelButton);
143         buttons.add(selectButton);
144
145         getContentPane().setLayout(new BorderLayout JavaDoc());
146         getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
147         getContentPane().add(buttons, BorderLayout.SOUTH);
148     }
149
150     protected void initController() {
151         selectButton.addActionListener(new ActionListener JavaDoc() {
152
153             public void actionPerformed(ActionEvent JavaDoc e) {
154                 processSelect();
155             }
156         });
157
158         cancelButton.addActionListener(new ActionListener JavaDoc() {
159
160             public void actionPerformed(ActionEvent JavaDoc e) {
161                 processCancel();
162             }
163         });
164
165         loadProcedures.addChangeListener(new ChangeListener JavaDoc() {
166
167             public void stateChanged(ChangeEvent JavaDoc e) {
168                 procNamePatternField.setEnabled(loadProcedures.isSelected());
169                 procedureLabel.setEnabled(loadProcedures.isSelected());
170             }
171         });
172     }
173
174     protected void initFromModel(
175             Collection JavaDoc schemas,
176             String JavaDoc dbUserName,
177             boolean shouldLoadProcedures) {
178
179         this.choice = CANCEL;
180         this.tableNamePatternField.setText(DbLoader.WILDCARD);
181         this.loadProcedures.setSelected(shouldLoadProcedures);
182         this.procNamePatternField.setText(DbLoader.WILDCARD);
183         this.procNamePatternField.setEnabled(shouldLoadProcedures);
184         this.procedureLabel.setEnabled(shouldLoadProcedures);
185
186         boolean showSchemaSelector = schemas != null && !schemas.isEmpty();
187         schemaSelector.setVisible(showSchemaSelector);
188         schemaLabel.setVisible(showSchemaSelector);
189
190         if (showSchemaSelector) {
191
192             schemaSelector.setModel(new DefaultComboBoxModel JavaDoc(schemas.toArray()));
193
194             // select schema belonging to the user
195
if (dbUserName != null) {
196                 Iterator JavaDoc it = schemas.iterator();
197                 while (it.hasNext()) {
198                     String JavaDoc schema = (String JavaDoc) it.next();
199                     if (dbUserName.equalsIgnoreCase(schema)) {
200                         schemaSelector.setSelectedItem(schema);
201                         break;
202                     }
203                 }
204             }
205         }
206     }
207
208     public int getChoice() {
209         return choice;
210     }
211
212     private void processSelect() {
213         choice = SELECT;
214         setVisible(false);
215     }
216
217     private void processCancel() {
218         choice = CANCEL;
219         setVisible(false);
220     }
221
222     /**
223      * Returns selected schema.
224      */

225     public String JavaDoc getSelectedSchema() {
226         String JavaDoc schema = (String JavaDoc) schemaSelector.getSelectedItem();
227         return "".equals(schema) ? null : schema;
228     }
229
230     /**
231      * Returns the tableNamePattern.
232      */

233     public String JavaDoc getTableNamePattern() {
234         return "".equals(tableNamePatternField.getText()) ? null : tableNamePatternField
235                 .getText();
236     }
237
238     public boolean isLoadingProcedures() {
239         return loadProcedures.isSelected();
240     }
241
242     /**
243      * Returns the procedure name pattern.
244      */

245     public String JavaDoc getProcedureNamePattern() {
246         return "".equals(procNamePatternField.getText()) ? null : procNamePatternField
247                 .getText();
248     }
249 }
Popular Tags