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.*;
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.NodeList;
25  
26  import javax.swing.*;
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  /***
31   *
32   * @author  hillie
33   */
34  public class App extends DefaultMutableTreeNode implements JagBean {
35     private static final String XMLTAG_MODULE_DATA = "module-data";
36  
37  
38     /*** Creates new form BeanForm */
39     public App() {
40        initComponents();
41        nameText.requestFocus();
42        rootPackageText.setText("com.finalist");
43     }
44  
45     public App(Element el) {
46        initComponents();
47        NodeList nl = el.getElementsByTagName(XMLTAG_MODULE_DATA);
48        for (int i = 0; i < nl.getLength(); i++) {
49           Element child = (Element) nl.item(i);
50           String attName = child.getAttribute("name");
51           String value = null;
52           if (child.getFirstChild() == null)
53              value = null;
54           else
55              value = child.getFirstChild().getNodeValue();
56           if (value != null) {
57              if (attName.equalsIgnoreCase("name")) {
58                 nameText.setText(value);
59                 continue;
60              }
61              if (attName.equalsIgnoreCase("version")) {
62                 versionText.setText(value);
63                 continue;
64              }
65              if (attName.equalsIgnoreCase("description")) {
66                 descriptionText.setText(value);
67                 continue;
68              }
69              if (attName.equalsIgnoreCase("root-package")) {
70                 rootPackageText.setText(value);
71                 continue;
72              }
73              if (attName.equalsIgnoreCase("log-framework")) {
74                 loggingFrameworkCombo.setSelectedItem(value);
75                 continue;
76              }
77              if (attName.equalsIgnoreCase("date-format")) {
78                 dateFormat.setText(value);
79                 continue;
80              }
81              if (attName.equalsIgnoreCase("timestamp-format")) {
82                 timestampFormat.setText(value);
83                 continue;
84              }
85           }
86        }
87        nameText.requestFocus();
88     }
89  
90  
91     public void setName(String text) {
92        this.nameText.setText(text);
93     }
94  
95     public TemplateString getName() {
96        return new TemplateString(nameText.getText());
97     }
98  
99     public String getVersion() {
100       return versionText.getText();
101    }
102    public void setVersion(String text) {
103       this.versionText.setText(text);
104    }
105 
106    public String getDescription() {
107       return descriptionText.getText();
108    }
109 
110    public void setDescription(String text) {
111       descriptionText.setText(text);
112    }
113 
114    public String getRootPackage() {
115       return rootPackageText.getText();
116    }
117    public void setRootPackage(String text) {
118       this.rootPackageText.setText(text);
119    }
120 
121    public String getRootPath() {
122       return rootPackageText.getText().replace('.', '/');
123    }
124 
125    public String getLogFramework() {
126       return (String) loggingFrameworkCombo.getSelectedItem();
127    }
128 
129    public void setLogFramework(String text) {
130       this.loggingFrameworkCombo.setSelectedItem(text);
131    }
132 
133    public String getTimestampFormat() {
134       return timestampFormat.getText();
135    }
136 
137     public void setTimestampFormat(String format) {
138        timestampFormat.setText(format);
139     }
140 
141    public String getDateFormat() {
142       return dateFormat.getText();
143    }
144 
145     public void setDateFormat(String format) {
146        dateFormat.setText(format);
147     }
148 
149    public String toString() {
150       return "Application settings";
151    }
152 
153    public JPanel getPanel() {
154       return panel;
155    }
156 
157    public void getXML(Element el) throws ParserConfigurationException {
158       Document doc = el.getOwnerDocument();
159       Element module = doc.createElement("module");
160       module.setAttribute("name", "app");
161 
162       Element name = doc.createElement(XMLTAG_MODULE_DATA);
163       name.setAttribute("name", "name");
164       if (nameText.getText() != null) {
165         name.appendChild(doc.createTextNode(nameText.getText()));
166       }
167       module.appendChild(name);
168 
169       Element version = doc.createElement(XMLTAG_MODULE_DATA);
170       version.setAttribute("name", "version");
171       if (versionText.getText() != null) {
172         version.appendChild(doc.createTextNode(versionText.getText()));
173       }
174       module.appendChild(version);
175 
176       Element description = doc.createElement(XMLTAG_MODULE_DATA);
177       description.setAttribute("name", "description");
178       if (descriptionText.getText() != null) {
179         description.appendChild(doc.createTextNode(descriptionText.getText()));
180       }
181       module.appendChild(description);
182 
183       Element rootPackage = doc.createElement(XMLTAG_MODULE_DATA);
184       rootPackage.setAttribute("name", "root-package");
185       if (rootPackageText.getText() != null) {
186         rootPackage.appendChild(doc.createTextNode(rootPackageText.getText()));
187       }
188       module.appendChild(rootPackage);
189 
190       Element loggingFramework = doc.createElement(XMLTAG_MODULE_DATA);
191       loggingFramework.setAttribute("name", "log-framework");
192       if (loggingFrameworkCombo.getSelectedItem() != null) {
193          loggingFramework.appendChild(doc.createTextNode((String) loggingFrameworkCombo.getSelectedItem()));
194       }
195       module.appendChild(loggingFramework);
196 
197       Element dateFormat = doc.createElement(XMLTAG_MODULE_DATA);
198       dateFormat.setAttribute("name", "date-format");
199 
200       if (getDateFormat() != null) {
201         dateFormat.appendChild(doc.createTextNode(getDateFormat()));
202       }
203       module.appendChild(dateFormat);
204 
205       Element tsFormat = doc.createElement(XMLTAG_MODULE_DATA);
206       tsFormat.setAttribute("name", "timestamp-format");
207       if (getTimestampFormat() != null) {
208         tsFormat.appendChild(doc.createTextNode(getTimestampFormat()));
209       }
210       module.appendChild(tsFormat);
211 
212       el.appendChild(module);
213    }
214 
215    public String getRefName() {
216       return "app";
217    }
218 
219 
220    /*** This method is called from within the constructor to
221     * initialize the form.
222     * WARNING: Do NOT modify this code. The content of this method is
223     * always regenerated by the Form Editor.
224     */
225     private void initComponents() {//GEN-BEGIN:initComponents
226         panel = new javax.swing.JPanel();
227         nameLabel = new javax.swing.JLabel();
228         versionLabel = new javax.swing.JLabel();
229         desciptionLabel = new javax.swing.JLabel();
230         rootPackageLabel = new javax.swing.JLabel();
231         nameText = new javax.swing.JTextField();
232         versionText = new javax.swing.JTextField();
233         descriptionText = new javax.swing.JTextField();
234         rootPackageText = new javax.swing.JTextField();
235         loggingFrameworkLabel = new javax.swing.JLabel();
236         loggingFrameworkCombo = new javax.swing.JComboBox();
237         dateFormatLabel = new javax.swing.JLabel();
238         dateFormat = new javax.swing.JTextField();
239         timestampFormatLabel = new javax.swing.JLabel();
240         timestampFormat = new javax.swing.JTextField();
241 
242         panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
243 
244         nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
245         nameLabel.setText("Application Name: ");
246         panel.add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 110, -1));
247 
248         versionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
249         versionLabel.setText("Version: ");
250         panel.add(versionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 110, -1));
251 
252         desciptionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
253         desciptionLabel.setText("Description: ");
254         panel.add(desciptionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 110, -1));
255 
256         rootPackageLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
257         rootPackageLabel.setText("Root-package: ");
258         panel.add(rootPackageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 110, -1));
259 
260         nameText.setToolTipText("Name should be lowercase and characters only!");
261         nameText.addFocusListener(new java.awt.event.FocusAdapter() {
262             public void focusGained(java.awt.event.FocusEvent evt) {
263                 nameTextFocusGained(evt);
264             }
265             public void focusLost(java.awt.event.FocusEvent evt) {
266                 nameTextFocusLost(evt);
267             }
268         });
269 
270         panel.add(nameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, 260, -1));
271 
272         versionText.setText("1.0");
273         versionText.setToolTipText("Version number of the application");
274         versionText.addFocusListener(new java.awt.event.FocusAdapter() {
275             public void focusLost(java.awt.event.FocusEvent evt) {
276                 versionTextFocusLost(evt);
277             }
278         });
279 
280         panel.add(versionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 260, -1));
281 
282         descriptionText.setToolTipText("Description is used for class names, so make sure it starts with a capital and only contains characters");
283         descriptionText.addFocusListener(new java.awt.event.FocusAdapter() {
284             public void focusLost(java.awt.event.FocusEvent evt) {
285                 descriptionTextFocusLost(evt);
286             }
287         });
288 
289         panel.add(descriptionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 70, 260, -1));
290 
291         rootPackageText.setText("com.finalist.");
292         rootPackageText.setToolTipText("Root package name for your application");
293         rootPackageText.addFocusListener(new java.awt.event.FocusAdapter() {
294             public void focusLost(java.awt.event.FocusEvent evt) {
295                 rootPackageTextFocusLost(evt);
296             }
297         });
298 
299         panel.add(rootPackageText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 100, 260, -1));
300 
301         loggingFrameworkLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
302         loggingFrameworkLabel.setText("Logging:");
303         panel.add(loggingFrameworkLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 110, -1));
304 
305         loggingFrameworkCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "log4j", "jdklogging" }));
306         loggingFrameworkCombo.setToolTipText("Select logging method");
307         loggingFrameworkCombo.addActionListener(new java.awt.event.ActionListener() {
308             public void actionPerformed(java.awt.event.ActionEvent evt) {
309                 loggingFrameworkComboActionPerformed(evt);
310             }
311         });
312 
313         panel.add(loggingFrameworkCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 260, -1));
314 
315         dateFormatLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
316         dateFormatLabel.setText("Date format:");
317         panel.add(dateFormatLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, 110, -1));
318 
319         dateFormat.setText("dd/MM/yyyy");
320         dateFormat.setToolTipText("Date format used for displaying dates");
321         panel.add(dateFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 260, -1));
322 
323         timestampFormatLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
324         timestampFormatLabel.setText("Timestamp format:");
325         panel.add(timestampFormatLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, 110, -1));
326 
327         timestampFormat.setText("dd/MM/yyyy HH:mm:ss");
328         timestampFormat.setToolTipText("Timestamp format used for rendering timestamps");
329         panel.add(timestampFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 190, 260, -1));
330 
331     }//GEN-END:initComponents
332 
333     private void nameTextFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusGained
334         // TODO add your handling code here:
335     }//GEN-LAST:event_nameTextFocusGained
336 
337    private void descriptionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFocusLost
338        // Make sure we only use characters and all in lowercase..
339        String name = descriptionText.getText();
340        if ((name == null) || (name.length() == 0))
341           return;
342        String formattedName = Utils.formatLowerAndUpperCase(name);
343        descriptionText.setText(formattedName);       
344        JagGenerator.stateChanged(false);
345    }//GEN-LAST:event_descriptionTextFocusLost
346 
347    private void versionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_versionTextFocusLost
348       JagGenerator.stateChanged(false);
349    }//GEN-LAST:event_versionTextFocusLost
350 
351    private void loggingFrameworkComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loggingFrameworkComboActionPerformed
352       JagGenerator.stateChanged(false);
353    }//GEN-LAST:event_loggingFrameworkComboActionPerformed
354 
355    private void rootPackageTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rootPackageTextFocusLost
356       Root root = (Root) getParent();
357       root.setRootPackage(rootPackageText.getText());
358       JagGenerator.stateChanged(false);
359    }//GEN-LAST:event_rootPackageTextFocusLost
360 
361 
362    private void nameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusLost
363      // Make sure we only use characters and all in lowercase..
364        String name = nameText.getText();
365        if ((name == null) || (name.length() == 0))
366           return;
367        String formattedName = Utils.formatLowercase(name);
368        nameText.setText(formattedName);       
369        JagGenerator.stateChanged(false);
370    }//GEN-LAST:event_nameTextFocusLost
371 
372     // Variables declaration - do not modify//GEN-BEGIN:variables
373     public javax.swing.JTextField dateFormat;
374     private javax.swing.JLabel dateFormatLabel;
375     private javax.swing.JLabel desciptionLabel;
376     public javax.swing.JTextField descriptionText;
377     public javax.swing.JComboBox loggingFrameworkCombo;
378     private javax.swing.JLabel loggingFrameworkLabel;
379     private javax.swing.JLabel nameLabel;
380     public javax.swing.JTextField nameText;
381     public javax.swing.JPanel panel;
382     private javax.swing.JLabel rootPackageLabel;
383     public javax.swing.JTextField rootPackageText;
384     public javax.swing.JTextField timestampFormat;
385     private javax.swing.JLabel timestampFormatLabel;
386     private javax.swing.JLabel versionLabel;
387     private javax.swing.JTextField versionText;
388     // End of variables declaration//GEN-END:variables
389 }
390