Source code: javatools/swing/ConnectionWizard.java
1 /*
2 * ScriptExecutorWizard.java
3 *
4 * Created on 14 gennaio 2002, 18.37
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing;
26
27 import javatools.util.Props;
28 import javatools.util.ScriptCatcher;
29 import java.lang.*;
30 import java.io.*;
31
32 /**
33 * The wizard to manage collecting dbms information to save in a properties file.
34 * It is like ScriptExecutorWizard, except in the fact it only saves properties.
35 *
36 * @author Antonio Petrelli
37 * @version 0.2.0
38 */
39 public class ConnectionWizard extends javatools.swing.AbstractWizard {
40
41 /** Creates new ScriptExecutorWizard */
42 public ConnectionWizard() {
43 javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
44 wizPanels = new InputPanel[4];
45 wizPanels[0] = new PresentationPanel();
46 wizPanels[1] = new DBMSSpecificationPanel();
47 wizPanels[2] = new DBMSUserInfoPanel();
48 wizPanels[3] = new PresentationPanel();
49 wizPanels[0].setDescription(javatoolsBundle.getString("Presentation"));
50 wizPanels[1].setDescription(javatoolsBundle.getString("DBMS_information"));
51 wizPanels[2].setDescription(javatoolsBundle.getString("DBMS_access_information"));
52 wizPanels[3].setDescription(javatoolsBundle.getString("Finished"));
53 wizPanels[1].setValue("ListFileName", "availableDBMS.script");
54 wizPanels[1].setValue("PropsFileName", "availableDBMS");
55 wizPanels[1].setValue("DBMSAddress", "localhost");
56 wizPanels[1].setValue("DBMSPort", "-1");
57 wizPanels[2].setValue("UserName", "");
58 wizPanels[2].setValue("Password", "");
59 initPanels();
60 setEnabledFinishButton(false);
61 }
62
63 /** Saves the properties.
64 * @throws WizardException If something goes wrong.
65 */
66 public void execute() throws WizardException {
67 Props dbProps;
68
69 getParams();
70 try {
71 dbProps = Props.singleton(pfn, false);
72 dbProps.setProperty("dbms", selectedDbms);
73 dbProps.setProperty("dbmsAddress", dbmsAddress);
74 dbProps.setProperty("dbDir", dbDir);
75 dbProps.setProperty("dbName", dbName);
76 dbProps.setProperty("driver", dbmsDriver);
77 dbProps.setProperty("port", Integer.toString(port));
78 dbProps.setProperty("userName", userName);
79 dbProps.setProperty("password", password);
80 dbProps.store();
81 }
82 catch (IOException e) {
83 System.out.println(e.getMessage());
84 }
85 }
86
87 /** Sets the file from which this wizard should take the DBMS list.
88 * @param name The needed filename.
89 */
90 public void setScriptListFileName(String name) {
91 scpList = name;
92 }
93
94 /** Sets the file in which properties will be saved.
95 * @param propsFileName The requested filename, without ".properties" extension.
96 */
97 public void setPropsFileName(String propsFileName) {
98 pfn = propsFileName;
99 }
100
101 /** Loads the presentation text from a file.
102 * @param fileName The filename of the file containing the presentation text.
103 * @throws WizardException If file does not exist.
104 */
105 public void loadPresentationText(String fileName) throws WizardException {
106 String tempString;
107
108 try {
109 tempString = ScriptCatcher.textFile2String(fileName);
110 }
111 catch (FileNotFoundException e) {
112 throw new WizardException(e.getMessage());
113 }
114 if (wizPanels != null && wizPanels[0] != null) {
115 wizPanels[0].setValue("PresentationText", tempString);
116 }
117 }
118
119 public void loadPresentationText(InputStream is) throws WizardException {
120 String tempString;
121
122 try {
123 tempString = ScriptCatcher.inputStream2String(is);
124 }
125 catch (IOException e) {
126 throw new WizardException(e.getMessage());
127 }
128 if (wizPanels != null && wizPanels[0] != null) {
129 wizPanels[0].setValue("PresentationText", tempString);
130 }
131 }
132
133 /** Loads the text to be displayed in the last panel.
134 * @param fileName The filename of the file containing the text.
135 * @throws WizardException If the file does not exist
136 */
137 public void loadFinishText(String fileName) throws WizardException {
138 String tempString;
139
140 try {
141 tempString = ScriptCatcher.textFile2String(fileName);
142 }
143 catch (FileNotFoundException e) {
144 throw new WizardException(e.getMessage());
145 }
146 if (wizPanels != null && wizPanels[3] != null) {
147 wizPanels[3].setValue("PresentationText", tempString);
148 }
149 }
150
151 public void loadFinishText(InputStream is) throws WizardException {
152 String tempString;
153
154 try {
155 tempString = ScriptCatcher.inputStream2String(is);
156 }
157 catch (IOException e) {
158 throw new WizardException(e.getMessage());
159 }
160 if (wizPanels != null && wizPanels[3] != null) {
161 wizPanels[4].setValue("PresentationText", tempString);
162 }
163 }
164
165 /** Sets the default database name.
166 * @param defaultName The default database name.
167 */
168 public void setDefaultDatabaseName(String defaultName) {
169 setValue("DatabaseName", defaultName);
170 }
171
172 /** Sets the properties filename containing default values.
173 * @param fileName The filename containing the properties.
174 */
175 public void setDefaultPropertiesFileName(String fileName) {
176 Props tempProps;
177
178 try {
179 tempProps = Props.singleton(fileName);
180 setDefaultDatabaseName(tempProps.getProperty("databaseName"));
181 }
182 catch (IOException e) {
183 System.out.println(e.getMessage());
184 }
185 }
186
187 private void getParams() {
188 Props scpListProps;
189 String driverFileName, localConfigDir;
190
191 if (scpList != null && !scpList.equals("")) {
192 selectedDbms = getValue("SelectedDBMS");
193 scpListProps = null;
194 try {
195 scpListProps = Props.singleton(scpList);
196 localConfigDir = Props.getLocalConfigDir();
197 if (!localConfigDir.equals(""))
198 localConfigDir += "/";
199 driverFileName = scpListProps.getProperty(selectedDbms+".driverFileName");
200 if (driverFileName == null || driverFileName.equals("")) {
201 System.out.println(javatoolsBundle.getString("Invalid_properties_file,_driver_file_name_is_not_present"));
202 System.exit(1);
203 }
204 dbmsDriver = findOneDriver(driverFileName);
205 if (dbmsDriver == null || dbmsDriver.equals("")) {
206 System.out.println(javatoolsBundle.getString("Did_not_find_any_driver_to_perform_operations"));
207 System.exit(1);
208 }
209 }
210 catch (IOException e) {
211 System.out.println(javatoolsBundle.getString("Properties_file_for_getting_script_filenames_does_not_exist"));
212 System.exit(1);
213 }
214 dbmsAddress = getValue("DBMSAddress");
215 dbDir = getValue("DatabaseDir");
216 dbName = getValue("DatabaseName");
217 userName = getValue("UserName");
218 password = getValue("Password");
219 port = -1;
220 try {
221 port = Integer.decode(getValue("DBMSPort")).intValue();
222 }
223 catch (NumberFormatException e) {
224 }
225 }
226 }
227
228 private String findOneDriver(String fileName) {
229 ScriptCatcher driverScript;
230 String tempDriver, localConfigDir;
231 int i, numDrivers;
232 boolean found;
233
234 driverScript = new ScriptCatcher();
235 localConfigDir = Props.getLocalConfigDir();
236 if (!localConfigDir.equals(""))
237 localConfigDir += "/";
238 try {
239 driverScript.loadScript(getClass().getClassLoader().getResourceAsStream("res/" + fileName));
240 }
241 catch (IOException e) {
242 return null;
243 }
244 numDrivers = driverScript.getNumCommands();
245 i=0;
246 found = false;
247 tempDriver = "";
248 while (!found && i < numDrivers) {
249 found = true;
250 tempDriver = driverScript.getCommandText(i);
251 try {
252 Class.forName(tempDriver);
253 }
254 catch (ClassNotFoundException e) {
255 found = false;
256 }
257 i++;
258 }
259 if (found)
260 return tempDriver;
261 else
262 return null;
263 }
264
265 private String selectedDbms, dbmsAddress, dbDir, dbName, userName, password,
266 scpList, pfn, dbmsDriver;
267 private int port;
268 private java.util.ResourceBundle javatoolsBundle;
269 }