001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator.modules;
019:
020: import com.finalist.jag.util.TemplateString;
021: import com.finalist.jaggenerator.Database;
022: import com.finalist.jaggenerator.DatabaseManager;
023: import com.finalist.jaggenerator.JagGenerator;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.NodeList;
027:
028: import javax.swing.*;
029: import javax.swing.tree.DefaultMutableTreeNode;
030: import javax.xml.parsers.ParserConfigurationException;
031:
032: /**
033: * The 'JagBean' for handling datasource configuration.
034: *
035: * @author Michael O'Connor - Finalist IT Group
036: */
037: public class Datasource extends DefaultMutableTreeNode implements
038: JagBean {
039: private static final String[] URL_TEMPLATES = new String[] {
040: "",
041: "jdbc:oracle:thin:@<host>:<port>:<sid>",
042: "jdbc:mysql://<host>/<database>",
043: "jdbc:postgresql://<host>:<port>/<database>",
044: "jdbc:postgresql:net",
045: "jdbc:hsqldb:mem:.",
046: "jdbc:hsqldb:hsql://<host>",
047: "jdbc:hsqldb:<database>",
048: "jdbc:hsqldb:hsql://<host>",
049: "jdbc:sybase:Tds:<host>:<port>/<database>",
050: "jdbc:db2://<host>:<port>/<database>",
051: "jdbc:db2:<database>",
052: "jdbc:microsoft:sqlserver://<host>:<port>;DatabaseName=<database>",
053: "jdbc:idb:<propertyFile>",
054: "jdbc:mckoi://<host>/",
055: "jdbc:Cache://<host>:<port>/<namespace>",
056: "jdbc:informix-sqli://<host>:<port>/<database>:informixserver=<dbservername>",
057: "jdbc:pointbase:server://<host>/<database>" };
058:
059: private boolean constructing = true;
060:
061: /** Creates new form BeanForm */
062: public Datasource() {
063: init();
064: constructing = false;
065: }
066:
067: public Datasource(Element el) {
068: init();
069:
070: NodeList nl = el.getElementsByTagName("module-data");
071: for (int i = 0; i < nl.getLength(); i++) {
072: Element child = (Element) nl.item(i);
073: String attName = child.getAttribute("name");
074: String value = null;
075: if (child.getFirstChild() == null)
076: value = null;
077: else
078: value = child.getFirstChild().getNodeValue();
079: if (value != null) {
080: if (attName.equalsIgnoreCase("jndi-name")) {
081: jndiText.setText(value);
082: continue;
083: }
084: if (attName.equalsIgnoreCase("jdbc-url")) {
085: jdbcURLCombo.setSelectedItem(value);
086: continue;
087: }
088: if (attName.equalsIgnoreCase("user-name")) {
089: userNameText.setText(value);
090: continue;
091: }
092: if (attName.equalsIgnoreCase("password")) {
093: passwordText.setText(value);
094: continue;
095: }
096:
097: if (attName.equalsIgnoreCase("mapping")) {
098: for (int j = 0; j < mappingCombo.getItemCount(); j++) {
099: String item = ((Database) mappingCombo
100: .getItemAt(j)).getDbName();
101: if (value.equalsIgnoreCase(item)) {
102: mappingCombo.setSelectedIndex(j);
103: break;
104: }
105: }
106: }
107: }
108: }
109: constructing = false;
110: }
111:
112: public String toString() {
113: return "Datasource";
114: }
115:
116: public JPanel getPanel() {
117: return panel;
118: }
119:
120: public void getXML(Element el) throws ParserConfigurationException {
121:
122: Document doc = el.getOwnerDocument();
123: Element module = doc.createElement("module");
124: module.setAttribute("name", "datasource");
125:
126: Element jndi = doc.createElement("module-data");
127: jndi.setAttribute("name", "jndi-name");
128: if (jndiText.getText() != null) {
129: jndi.appendChild(doc.createTextNode(jndiText.getText()));
130: }
131: module.appendChild(jndi);
132:
133: Element mapping = doc.createElement("module-data");
134: mapping.setAttribute("name", "mapping");
135:
136: mapping.appendChild(doc.createTextNode(mappingCombo.getModel()
137: .getSelectedItem().toString()));
138: module.appendChild(mapping);
139:
140: Element jdbcUrl = doc.createElement("module-data");
141: jdbcUrl.setAttribute("name", "jdbc-url");
142: jdbcUrl.appendChild(doc.createTextNode(jdbcURLCombo.getEditor()
143: .getItem().toString()));
144: module.appendChild(jdbcUrl);
145:
146: Element userName = doc.createElement("module-data");
147: userName.setAttribute("name", "user-name");
148: if (userNameText.getText() != null) {
149: userName.appendChild(doc.createTextNode(userNameText
150: .getText()));
151: }
152: module.appendChild(userName);
153:
154: Element password = doc.createElement("module-data");
155: password.setAttribute("name", "password");
156: if (passwordText.getText() != null) {
157: password.appendChild(doc.createTextNode(passwordText
158: .getText()));
159: }
160: module.appendChild(password);
161:
162: el.appendChild(module);
163: }
164:
165: public TemplateString getJndiName() {
166: return new TemplateString(jndiText.getText());
167: }
168:
169: public void setMapping(String text) {
170: mappingCombo.setSelectedItem(text);
171: for (int j = 0; j < mappingCombo.getItemCount(); j++) {
172: String item = ((Database) mappingCombo.getItemAt(j))
173: .getDbName();
174: if (text.equalsIgnoreCase(item)) {
175: mappingCombo.setSelectedIndex(j);
176: break;
177: }
178: }
179: }
180:
181: public void setJdbcUrl(String jdbcUrlText) {
182: jdbcURLCombo.setSelectedItem(jdbcUrlText);
183: }
184:
185: public void setJndi(String jndiText) {
186: this .jndiText.setText(jndiText);
187: }
188:
189: public void setPassword(String passwordText) {
190: this .passwordText.setText(passwordText);
191: }
192:
193: public void setUserName(String userNameText) {
194: this .userNameText.setText(userNameText);
195: }
196:
197: /**
198: * Gets the database currently selected for the application.
199: * @return the Database bean.
200: */
201: public Database getDatabase() {
202: return (Database) mappingCombo.getModel().getSelectedItem();
203: }
204:
205: /**
206: * Convenience method: gets the database's appserver type mapping as a TemplateString.
207: * @return
208: */
209: public TemplateString getTypeMapping() {
210: return new TemplateString(getDatabase().getTypeMapping());
211: }
212:
213: public TemplateString getJdbcUrl() {
214: return new TemplateString((String) jdbcURLCombo.getEditor()
215: .getItem());
216: }
217:
218: public TemplateString getUserName() {
219: return new TemplateString(userNameText.getText());
220: }
221:
222: public TemplateString getPassword() {
223: return new TemplateString(passwordText.getText());
224: }
225:
226: public String getRefName() {
227: return "datasource";
228: }
229:
230: private void init() {
231: initComponents();
232: Database[] dbs = DatabaseManager.getInstance()
233: .getSupportedDatabases();
234: setSupportedDatabases(dbs);
235: for (int i = 0; i < URL_TEMPLATES.length; i++) {
236: jdbcURLCombo.addItem(URL_TEMPLATES[i]);
237: }
238:
239: }
240:
241: /**
242: * Resets the dropdown list of supported databases.
243: * @param dbs
244: */
245: public void setSupportedDatabases(Database[] dbs) {
246: mappingCombo.removeAllItems();
247: for (int i = 0; i < dbs.length; i++) {
248: mappingCombo.addItem(dbs[i]);
249: }
250: }
251:
252: /** This method is called from within the constructor to
253: * initialize the form.
254: * WARNING: Do NOT modify this code. The content of this method is
255: * always regenerated by the Form Editor.
256: */
257: private void initComponents() {//GEN-BEGIN:initComponents
258: panel = new javax.swing.JPanel();
259: jndiLabel = new javax.swing.JLabel();
260: mappingLabel = new javax.swing.JLabel();
261: jndiText = new javax.swing.JTextField();
262: mappingCombo = new javax.swing.JComboBox();
263: jdbcUrlLabel = new javax.swing.JLabel();
264: userNameLabel = new javax.swing.JLabel();
265: passwordLabel = new javax.swing.JLabel();
266: userNameText = new javax.swing.JTextField();
267: passwordText = new javax.swing.JTextField();
268: driverManagerButton = new javax.swing.JButton();
269: jdbcURLCombo = new javax.swing.JComboBox();
270:
271: panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
272:
273: jndiLabel
274: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
275: jndiLabel.setText("JNDI name: ");
276: panel.add(jndiLabel,
277: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
278: 10, 90, -1));
279:
280: mappingLabel
281: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
282: mappingLabel.setText("Database type:");
283: panel.add(mappingLabel,
284: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
285: 40, 90, -1));
286:
287: jndiText.setText("jdbc/");
288: jndiText
289: .setToolTipText("JNDI name for the datasource that can be used to lookup from the application");
290: jndiText.addFocusListener(new java.awt.event.FocusAdapter() {
291: public void focusLost(java.awt.event.FocusEvent evt) {
292: jndiTextFocusLost(evt);
293: }
294: });
295:
296: panel.add(jndiText,
297: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
298: 10, 260, -1));
299:
300: mappingCombo
301: .setToolTipText("Select a database type. To add a new type, use the Database Driver Manager");
302: mappingCombo
303: .addActionListener(new java.awt.event.ActionListener() {
304: public void actionPerformed(
305: java.awt.event.ActionEvent evt) {
306: mappingComboActionPerformed(evt);
307: }
308: });
309:
310: panel.add(mappingCombo,
311: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
312: 40, 260, 20));
313:
314: jdbcUrlLabel
315: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
316: jdbcUrlLabel.setText("JDBC url: ");
317: panel.add(jdbcUrlLabel,
318: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
319: 70, 90, -1));
320:
321: userNameLabel
322: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
323: userNameLabel.setText("User name:");
324: panel.add(userNameLabel,
325: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
326: 100, 90, -1));
327:
328: passwordLabel
329: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
330: passwordLabel.setText("Password: ");
331: panel.add(passwordLabel,
332: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
333: 130, 90, -1));
334:
335: userNameText
336: .setToolTipText("Set a user name to connect to the database");
337: userNameText
338: .addFocusListener(new java.awt.event.FocusAdapter() {
339: public void focusLost(java.awt.event.FocusEvent evt) {
340: userNameTextFocusLost(evt);
341: }
342: });
343:
344: panel.add(userNameText,
345: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
346: 100, 260, 20));
347:
348: passwordText
349: .setToolTipText("Set the password to connect to the database");
350: passwordText
351: .addFocusListener(new java.awt.event.FocusAdapter() {
352: public void focusLost(java.awt.event.FocusEvent evt) {
353: passwordTextFocusLost(evt);
354: }
355: });
356:
357: panel.add(passwordText,
358: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
359: 130, 260, -1));
360:
361: driverManagerButton.setText("Database Driver Manager..");
362: driverManagerButton
363: .addActionListener(new java.awt.event.ActionListener() {
364: public void actionPerformed(
365: java.awt.event.ActionEvent evt) {
366: driverManagerButtonActionPerformed(evt);
367: }
368: });
369:
370: panel.add(driverManagerButton,
371: new org.netbeans.lib.awtextra.AbsoluteConstraints(160,
372: 180, -1, -1));
373:
374: jdbcURLCombo.setEditable(true);
375: jdbcURLCombo.setToolTipText("Configure the JDBC url");
376: jdbcURLCombo
377: .addFocusListener(new java.awt.event.FocusAdapter() {
378: public void focusLost(java.awt.event.FocusEvent evt) {
379: jdbcURLComboFocusLost(evt);
380: }
381: });
382:
383: panel.add(jdbcURLCombo,
384: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
385: 70, 260, 20));
386:
387: }//GEN-END:initComponents
388:
389: private void jdbcURLComboFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jdbcURLComboFocusLost
390: // Add your handling code here:
391: }//GEN-LAST:event_jdbcURLComboFocusLost
392:
393: private void driverManagerButtonActionPerformed(
394: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_driverManagerButtonActionPerformed
395: DatabaseManagerFrame.getInstance().show();
396: }//GEN-LAST:event_driverManagerButtonActionPerformed
397:
398: private void passwordTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordTextFocusLost
399: JagGenerator.stateChanged(false);
400: }//GEN-LAST:event_passwordTextFocusLost
401:
402: private void userNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userNameTextFocusLost
403: JagGenerator.stateChanged(false);
404: }//GEN-LAST:event_userNameTextFocusLost
405:
406: private void mappingComboActionPerformed(
407: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mappingComboActionPerformed
408: JagGenerator.stateChanged(false);
409: if (!constructing) {
410: JagGenerator.normaliseSQLTypesWithChosenDatabase();
411: }
412: }//GEN-LAST:event_mappingComboActionPerformed
413:
414: private void jndiTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jndiTextFocusLost
415: JagGenerator.stateChanged(false);
416: }//GEN-LAST:event_jndiTextFocusLost
417:
418: // Variables declaration - do not modify//GEN-BEGIN:variables
419: private javax.swing.JButton driverManagerButton;
420: public javax.swing.JComboBox jdbcURLCombo;
421: private javax.swing.JLabel jdbcUrlLabel;
422: private javax.swing.JLabel jndiLabel;
423: public javax.swing.JTextField jndiText;
424: public javax.swing.JComboBox mappingCombo;
425: private javax.swing.JLabel mappingLabel;
426: public javax.swing.JPanel panel;
427: private javax.swing.JLabel passwordLabel;
428: public javax.swing.JTextField passwordText;
429: private javax.swing.JLabel userNameLabel;
430: public javax.swing.JTextField userNameText;
431: // End of variables declaration//GEN-END:variables
432: }
|