View Javadoc

1   /*   Copyright (C) 2003 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.jaggenerator.modules;
19  
20  import com.finalist.jag.util.TemplateString;
21  import com.finalist.jaggenerator.Database;
22  import com.finalist.jaggenerator.DatabaseManager;
23  import com.finalist.jaggenerator.JagGenerator;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.NodeList;
27  
28  import javax.swing.*;
29  import javax.swing.tree.DefaultMutableTreeNode;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  /***
33   * The 'JagBean' for handling datasource configuration.
34   *
35   * @author  Michael O'Connor - Finalist IT Group
36   */
37  public class Datasource extends DefaultMutableTreeNode implements JagBean {
38     private static final String[] URL_TEMPLATES = new String[] {
39        "",
40        "jdbc:oracle:thin:@<host>:<port>:<sid>",
41        "jdbc:mysql://<host>/<database>",
42        "jdbc:postgresql://<host>:<port>/<database>",
43        "jdbc:postgresql:net",
44        "jdbc:hsqldb:mem:.",
45        "jdbc:hsqldb:hsql://<host>",
46        "jdbc:hsqldb:<database>",
47        "jdbc:hsqldb:hsql://<host>",
48        "jdbc:sybase:Tds:<host>:<port>/<database>",
49        "jdbc:db2://<host>:<port>/<database>",
50        "jdbc:db2:<database>",
51        "jdbc:microsoft:sqlserver://<host>:<port>;DatabaseName=<database>",
52        "jdbc:idb:<propertyFile>",
53        "jdbc:mckoi://<host>/",
54        "jdbc:Cache://<host>:<port>/<namespace>",
55        "jdbc:informix-sqli://<host>:<port>/<database>:informixserver=<dbservername>",
56        "jdbc:pointbase:server://<host>/<database>"
57     };
58  
59     private boolean constructing = true;
60  
61  
62     /*** Creates new form BeanForm */
63     public Datasource() {
64        init();
65        constructing = false;
66     }
67  
68     public Datasource(Element el) {
69        init();
70  
71        NodeList nl = el.getElementsByTagName("module-data");
72        for (int i = 0; i < nl.getLength(); i++) {
73           Element child = (Element) nl.item(i);
74           String attName = child.getAttribute("name");
75           String value = null;
76           if (child.getFirstChild() == null)
77              value = null;
78           else
79              value = child.getFirstChild().getNodeValue();
80           if (value != null) {
81              if (attName.equalsIgnoreCase("jndi-name")) {
82                 jndiText.setText(value);
83                 continue;
84              }
85              if (attName.equalsIgnoreCase("jdbc-url")) {
86                 jdbcURLCombo.setSelectedItem(value);
87                 continue;
88              }
89              if (attName.equalsIgnoreCase("user-name")) {
90                 userNameText.setText(value);
91                 continue;
92              }
93              if (attName.equalsIgnoreCase("password")) {
94                 passwordText.setText(value);
95                 continue;
96              }
97  
98              if (attName.equalsIgnoreCase("mapping")) {
99                 for (int j = 0; j < mappingCombo.getItemCount(); j++) {
100                   String item = ((Database) mappingCombo.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 
113    public String toString() {
114       return "Datasource";
115    }
116 
117    public JPanel getPanel() {
118       return panel;
119    }
120 
121    public void getXML(Element el) throws ParserConfigurationException {
122 
123       Document doc = el.getOwnerDocument();
124       Element module = doc.createElement("module");
125       module.setAttribute("name", "datasource");
126 
127       Element jndi = doc.createElement("module-data");
128       jndi.setAttribute("name", "jndi-name");
129       if (jndiText.getText() != null) {
130         jndi.appendChild(doc.createTextNode(jndiText.getText()));
131       }
132       module.appendChild(jndi);
133 
134       Element mapping = doc.createElement("module-data");
135       mapping.setAttribute("name", "mapping");
136 
137       mapping.appendChild(doc.createTextNode(mappingCombo.getModel().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().getItem().toString()));
143       module.appendChild(jdbcUrl);
144 
145       Element userName = doc.createElement("module-data");
146       userName.setAttribute("name", "user-name");
147       if (userNameText.getText() != null) {
148         userName.appendChild(doc.createTextNode(userNameText.getText()));
149       }
150       module.appendChild(userName);
151 
152       Element password = doc.createElement("module-data");
153       password.setAttribute("name", "password");
154       if (passwordText.getText() != null) {
155         password.appendChild(doc.createTextNode(passwordText.getText()));
156       }
157       module.appendChild(password);
158 
159       el.appendChild(module);
160    }
161 
162    public TemplateString getJndiName() {
163       return new TemplateString(jndiText.getText());
164    }
165 
166    public void setMapping(String text) {
167       mappingCombo.setSelectedItem(text);
168       for (int j = 0; j < mappingCombo.getItemCount(); j++) {
169          String item = ((Database) mappingCombo.getItemAt(j)).getDbName();
170          if (text.equalsIgnoreCase(item)) {
171             mappingCombo.setSelectedIndex(j);
172             break;
173          }
174       }
175    }
176 
177    public void setJdbcUrl(String jdbcUrlText) {
178       jdbcURLCombo.setSelectedItem(jdbcUrlText);
179    }
180 
181    public void setJndi(String jndiText) {
182       this.jndiText.setText(jndiText);
183    }
184 
185    public void setPassword(String passwordText) {
186       this.passwordText.setText(passwordText);
187    }
188 
189    public void setUserName(String userNameText) {
190       this.userNameText.setText(userNameText);
191    }
192 
193    /***
194     * Gets the database currently selected for the application.
195     * @return the Database bean.
196     */
197    public Database getDatabase() {
198       return (Database) mappingCombo.getModel().getSelectedItem();
199    }
200 
201    /***
202     * Convenience method: gets the database's appserver type mapping as a TemplateString.
203     * @return
204     */
205    public TemplateString getTypeMapping() {
206       return new TemplateString(getDatabase().getTypeMapping());
207    }
208 
209    public TemplateString getJdbcUrl() {
210       return new TemplateString((String) jdbcURLCombo.getEditor().getItem());
211    }
212 
213    public TemplateString getUserName() {
214       return new TemplateString(userNameText.getText());
215    }
216 
217    public TemplateString getPassword() {
218       return new TemplateString(passwordText.getText());
219    }
220 
221    public String getRefName() {
222       return "datasource";
223    }
224 
225 
226    private void init() {
227       initComponents();
228       Database[] dbs = DatabaseManager.getInstance().getSupportedDatabases();
229       setSupportedDatabases(dbs);
230       for (int i = 0; i < URL_TEMPLATES.length; i++) {
231          jdbcURLCombo.addItem(URL_TEMPLATES[i]);
232       }
233 
234    }
235 
236    /***
237     * Resets the dropdown list of supported databases.
238     * @param dbs
239     */
240    public void setSupportedDatabases(Database[] dbs) {
241       mappingCombo.removeAllItems();
242       for (int i = 0; i < dbs.length; i++) {
243          mappingCombo.addItem(dbs[i]);
244       }
245    }
246 
247    /*** This method is called from within the constructor to
248     * initialize the form.
249     * WARNING: Do NOT modify this code. The content of this method is
250     * always regenerated by the Form Editor.
251     */
252     private void initComponents() {//GEN-BEGIN:initComponents
253         panel = new javax.swing.JPanel();
254         jndiLabel = new javax.swing.JLabel();
255         mappingLabel = new javax.swing.JLabel();
256         jndiText = new javax.swing.JTextField();
257         mappingCombo = new javax.swing.JComboBox();
258         jdbcUrlLabel = new javax.swing.JLabel();
259         userNameLabel = new javax.swing.JLabel();
260         passwordLabel = new javax.swing.JLabel();
261         userNameText = new javax.swing.JTextField();
262         passwordText = new javax.swing.JTextField();
263         driverManagerButton = new javax.swing.JButton();
264         jdbcURLCombo = new javax.swing.JComboBox();
265 
266         panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
267 
268         jndiLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
269         jndiLabel.setText("JNDI name: ");
270         panel.add(jndiLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 90, -1));
271 
272         mappingLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
273         mappingLabel.setText("Database type:");
274         panel.add(mappingLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 90, -1));
275 
276         jndiText.setText("jdbc/");
277         jndiText.setToolTipText("JNDI name for the datasource that can be used to lookup from the application");
278         jndiText.addFocusListener(new java.awt.event.FocusAdapter() {
279             public void focusLost(java.awt.event.FocusEvent evt) {
280                 jndiTextFocusLost(evt);
281             }
282         });
283 
284         panel.add(jndiText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 260, -1));
285 
286         mappingCombo.setToolTipText("Select a database type. To add a new type, use the Database Driver Manager");
287         mappingCombo.addActionListener(new java.awt.event.ActionListener() {
288             public void actionPerformed(java.awt.event.ActionEvent evt) {
289                 mappingComboActionPerformed(evt);
290             }
291         });
292 
293         panel.add(mappingCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 260, 20));
294 
295         jdbcUrlLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
296         jdbcUrlLabel.setText("JDBC url: ");
297         panel.add(jdbcUrlLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 90, -1));
298 
299         userNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
300         userNameLabel.setText("User name:");
301         panel.add(userNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 90, -1));
302 
303         passwordLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
304         passwordLabel.setText("Password: ");
305         panel.add(passwordLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 90, -1));
306 
307         userNameText.setToolTipText("Set a user name to connect to the database");
308         userNameText.addFocusListener(new java.awt.event.FocusAdapter() {
309             public void focusLost(java.awt.event.FocusEvent evt) {
310                 userNameTextFocusLost(evt);
311             }
312         });
313 
314         panel.add(userNameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 260, 20));
315 
316         passwordText.setToolTipText("Set the password to connect to the database");
317         passwordText.addFocusListener(new java.awt.event.FocusAdapter() {
318             public void focusLost(java.awt.event.FocusEvent evt) {
319                 passwordTextFocusLost(evt);
320             }
321         });
322 
323         panel.add(passwordText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 260, -1));
324 
325         driverManagerButton.setText("Database Driver Manager..");
326         driverManagerButton.addActionListener(new java.awt.event.ActionListener() {
327             public void actionPerformed(java.awt.event.ActionEvent evt) {
328                 driverManagerButtonActionPerformed(evt);
329             }
330         });
331 
332         panel.add(driverManagerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 180, -1, -1));
333 
334         jdbcURLCombo.setEditable(true);
335         jdbcURLCombo.setToolTipText("Configure the JDBC url");
336         jdbcURLCombo.addFocusListener(new java.awt.event.FocusAdapter() {
337             public void focusLost(java.awt.event.FocusEvent evt) {
338                 jdbcURLComboFocusLost(evt);
339             }
340         });
341 
342         panel.add(jdbcURLCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 260, 20));
343 
344     }//GEN-END:initComponents
345 
346     private void jdbcURLComboFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jdbcURLComboFocusLost
347         // Add your handling code here:
348     }//GEN-LAST:event_jdbcURLComboFocusLost
349 
350     private void driverManagerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_driverManagerButtonActionPerformed
351        DatabaseManagerFrame.getInstance().show();
352     }//GEN-LAST:event_driverManagerButtonActionPerformed
353 
354    private void passwordTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordTextFocusLost
355       JagGenerator.stateChanged(false);
356    }//GEN-LAST:event_passwordTextFocusLost
357 
358    private void userNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_userNameTextFocusLost
359       JagGenerator.stateChanged(false);
360    }//GEN-LAST:event_userNameTextFocusLost
361 
362    private void mappingComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mappingComboActionPerformed
363       JagGenerator.stateChanged(false);
364       if (!constructing) {
365          JagGenerator.normaliseSQLTypesWithChosenDatabase();
366       }
367    }//GEN-LAST:event_mappingComboActionPerformed
368 
369    private void jndiTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jndiTextFocusLost
370       JagGenerator.stateChanged(false);
371    }//GEN-LAST:event_jndiTextFocusLost
372 
373     // Variables declaration - do not modify//GEN-BEGIN:variables
374     private javax.swing.JButton driverManagerButton;
375     public javax.swing.JComboBox jdbcURLCombo;
376     private javax.swing.JLabel jdbcUrlLabel;
377     private javax.swing.JLabel jndiLabel;
378     public javax.swing.JTextField jndiText;
379     public javax.swing.JComboBox mappingCombo;
380     private javax.swing.JLabel mappingLabel;
381     public javax.swing.JPanel panel;
382     private javax.swing.JLabel passwordLabel;
383     public javax.swing.JTextField passwordText;
384     private javax.swing.JLabel userNameLabel;
385     public javax.swing.JTextField userNameText;
386     // End of variables declaration//GEN-END:variables
387 }
388