Source code: org/objectstyle/cayenne/modeler/datamap/ChooseSchemaDialog.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002-2003 The ObjectStyle Group
6 * and individual authors of the software. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution, if
21 * any, must include the following acknowlegement:
22 * "This product includes software developed by the
23 * ObjectStyle Group (http://objectstyle.org/)."
24 * Alternately, this acknowlegement may appear in the software itself,
25 * if and wherever such third-party acknowlegements normally appear.
26 *
27 * 4. The names "ObjectStyle Group" and "Cayenne"
28 * must not be used to endorse or promote products derived
29 * from this software without prior written permission. For written
30 * permission, please contact andrus@objectstyle.org.
31 *
32 * 5. Products derived from this software may not be called "ObjectStyle"
33 * nor may "ObjectStyle" appear in their names without prior written
34 * permission of the ObjectStyle Group.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the ObjectStyle Group. For more
52 * information on the ObjectStyle Group, please see
53 * <http://objectstyle.org/>.
54 *
55 */
56 package org.objectstyle.cayenne.modeler.datamap;
57
58 import java.awt.BorderLayout;
59 import java.awt.Component;
60 import java.awt.event.ActionEvent;
61 import java.awt.event.ActionListener;
62 import java.util.Iterator;
63 import java.util.List;
64
65 import javax.swing.DefaultComboBoxModel;
66 import javax.swing.JButton;
67 import javax.swing.JComboBox;
68 import javax.swing.JLabel;
69 import javax.swing.JPanel;
70 import javax.swing.JTextField;
71
72 import org.objectstyle.cayenne.access.DbLoader;
73 import org.objectstyle.cayenne.conn.DataSourceInfo;
74 import org.objectstyle.cayenne.modeler.CayenneDialog;
75 import org.objectstyle.cayenne.modeler.Editor;
76 import org.objectstyle.cayenne.modeler.PanelFactory;
77 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory;
78
79 /**
80 * Dialog that allows to select schema of the database.
81 *
82 * @author Misha Shengaout
83 * @author Andrei Adamchik
84 */
85 public class ChooseSchemaDialog extends CayenneDialog implements ActionListener {
86 public static final int SELECT = 0;
87 public static final int CANCEL = 1;
88
89 protected List schemaList;
90
91 protected JComboBox schemaSelect;
92 protected JTextField tabeNamePatternField;
93 protected JButton select = new JButton("Continue");
94 protected JButton cancel = new JButton("Cancel");
95 protected int choice = CANCEL;
96
97 /**
98 * Creates and initializes new ChooseSchemaDialog.
99 */
100 public ChooseSchemaDialog(List schemaList, DataSourceInfo dsi) {
101 super(Editor.getFrame(), "Schema Selector", true);
102 setResizable(false);
103
104 this.schemaList = schemaList;
105
106 init(dsi.getUserName());
107
108 select.addActionListener(this);
109 cancel.addActionListener(this);
110
111 this.pack();
112
113 // display dialog in the center
114 this.centerWindow();
115 }
116
117 /** Sets up the graphical components. */
118 protected void init(String userName) {
119 getContentPane().setLayout(new BorderLayout());
120
121 tabeNamePatternField = CayenneWidgetFactory.createTextField();
122 tabeNamePatternField.setText(DbLoader.WILDCARD);
123
124 Component[] left = null;
125 Component[] right = null;
126 JPanel buttons = PanelFactory.createButtonPanel(new JButton[] { select, cancel });
127
128 // optionally create schema selector
129 if (schemaList != null && schemaList.size() > 0) {
130 schemaSelect = CayenneWidgetFactory.createComboBox();
131 schemaSelect.setModel(new DefaultComboBoxModel(schemaList.toArray()));
132
133 // select schema belonging to the user
134 if (userName != null) {
135 Iterator schemas = schemaList.iterator();
136 while (schemas.hasNext()) {
137 String schema = (String) schemas.next();
138 if (userName.equalsIgnoreCase(schema)) {
139 schemaSelect.setSelectedItem(schema);
140 break;
141 }
142 }
143 }
144
145 left =
146 new Component[] {
147 CayenneWidgetFactory.createLabel("Table Name Pattern: "),
148 CayenneWidgetFactory.createLabel("Schemas: "),
149 new JLabel()};
150
151 right = new Component[] { tabeNamePatternField, schemaSelect, buttons };
152 }
153 else {
154 left = new Component[] { CayenneWidgetFactory.createLabel("Table Name Pattern: "), new JLabel()};
155
156 right = new Component[] { tabeNamePatternField, buttons };
157 }
158
159 JPanel panel = PanelFactory.createForm(left, right, 5, 5, 5, 5);
160 getContentPane().add(panel, BorderLayout.CENTER);
161 }
162
163 public void actionPerformed(ActionEvent e) {
164 Object src = e.getSource();
165 if (src == select) {
166 processSelect();
167 }
168 else if (src == cancel) {
169 processCancel();
170 }
171 }
172
173 public int getChoice() {
174 return choice;
175 }
176
177 private void processSelect() {
178 choice = SELECT;
179 hide();
180 }
181
182 private void processCancel() {
183 choice = CANCEL;
184 hide();
185 }
186
187 public String getSchemaName() {
188 if (getChoice() != SELECT || schemaSelect == null) {
189 return null;
190 }
191
192 String schema = (String) schemaSelect.getSelectedItem();
193 if ("".equals(schema)) {
194 schema = null;
195 }
196
197 return schema;
198 }
199
200 /**
201 * Returns the tableNamePattern.
202 */
203 public String getTableNamePattern() {
204 if (getChoice() != SELECT) {
205 return null;
206 }
207
208 String pattern = tabeNamePatternField.getText();
209 if ("".equals(pattern)) {
210 pattern = null;
211 }
212
213 return pattern;
214 }
215 }