KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > modules > App


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 java.text.SimpleDateFormat JavaDoc;
21
22 import com.finalist.jag.util.TemplateString;
23 import com.finalist.jaggenerator.JagGenerator;
24 import com.finalist.jaggenerator.Utils;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 import javax.swing.*;
30 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32
33 /**
34  * @author hillie
35  */

36 public class App extends DefaultMutableTreeNode JavaDoc implements JagBean {
37     private static final String JavaDoc XMLTAG_MODULE_DATA = "module-data";
38
39     // Check if date is a supported calendar date.
40
public String JavaDoc isCalendarDate() {
41         if (!"".equals(getCalendarDateFormat())) {
42             return "true";
43         }
44         return "false";
45     }
46
47     public String JavaDoc getCalendarDateFormat() {
48         if ("dd/MM/yyyy".equals(getDateFormat()))
49         {
50             return "%d/%m/%Y";
51         }
52         if ("dd-MM-yyyy".equals(getDateFormat()))
53         {
54             return "%d-%m-%Y";
55         }
56         if ("yyyy/MM/dd".equals(getDateFormat()))
57         {
58             return "%Y/%m/%d";
59         }
60         if ("yyyy-MM-dd".equals(getDateFormat()))
61         {
62             return "%Y-%m-%d";
63         }
64
65         if ("MM/dd/yyyy".equals(getDateFormat()))
66         {
67             return "%m/%d/%Y";
68         }
69         if ("MM-dd-yyyy".equals(getDateFormat()))
70         {
71             return "%m-%d-%Y";
72         }
73         // Unsupported date format.
74
return "";
75     }
76
77     // Check if time is a supported calendar date.
78
public String JavaDoc isCalendarTime() {
79         if (!"".equals(getCalendarTimeFormat())) {
80             return "true";
81         }
82         return "false";
83     }
84
85     public String JavaDoc getCalendarTimeFormat() {
86         if ("dd/MM/yyyy HH:mm:ss".equals(getTimestampFormat()))
87         {
88             return "%d/%m/%Y %H:%M:%S";
89         }
90         if ("dd-MM-yyyy HH:mm:ss".equals(getTimestampFormat()))
91         {
92             return "%d-%m-%Y %H:%M:%S";
93         }
94         if ("yyyy/MM/dd HH:mm:ss".equals(getTimestampFormat()))
95         {
96             return "%Y/%m/%d %H:%M:%S";
97         }
98         if ("yyyy-MM-dd HH:mm:ss".equals(getTimestampFormat()))
99         {
100             return "%Y-%m-%d %H:%M:%S";
101         }
102
103         if ("MM/dd/yyyy HH:mm:ss".equals(getTimestampFormat()))
104         {
105             return "%m/%d/%Y %H:%M:%S";
106         }
107         if ("MM-dd-yyyy HH:mm:ss".equals(getTimestampFormat()))
108         {
109             return "%m-%d-%Y %H:%M:%S";
110         }
111
112         // Unsupported date format.
113
return "";
114     }
115
116
117    /**
118     * Get the current date formatted using the date format.
119     *
120     * @return formatted current date.
121     */

122    public String JavaDoc getCurrentDate() {
123       SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(getDateFormat());
124       return format.format(new java.util.Date JavaDoc());
125    }
126     /**
127      * Creates new form BeanForm
128      */

129     public App() {
130         initComponents();
131         nameText.requestFocus();
132         rootPackageText.setText("com.finalist");
133     }
134
135     public App(Element JavaDoc el) {
136         initComponents();
137         NodeList JavaDoc nl = el.getElementsByTagName(XMLTAG_MODULE_DATA);
138         for (int i = 0; i < nl.getLength(); i++) {
139             Element JavaDoc child = (Element JavaDoc) nl.item(i);
140             String JavaDoc attName = child.getAttribute("name");
141             String JavaDoc value = null;
142             if (child.getFirstChild() == null)
143                 value = null;
144             else
145                 value = child.getFirstChild().getNodeValue();
146             if (value != null) {
147                 if (attName.equalsIgnoreCase("name")) {
148                     nameText.setText(value);
149                     continue;
150                 }
151                 if (attName.equalsIgnoreCase("version")) {
152                     versionText.setText(value);
153                     continue;
154                 }
155                 if (attName.equalsIgnoreCase("description")) {
156                     descriptionText.setText(value);
157                     continue;
158                 }
159                 if (attName.equalsIgnoreCase("root-package")) {
160                     rootPackageText.setText(value);
161                     continue;
162                 }
163                 if (attName.equalsIgnoreCase("log-framework")) {
164                     loggingFrameworkCombo.setSelectedItem(value);
165                     continue;
166                 }
167                 if (attName.equalsIgnoreCase("date-format")) {
168                     dateFormat.setText(value);
169                     continue;
170                 }
171                 if (attName.equalsIgnoreCase("timestamp-format")) {
172                     timestampFormat.setText(value);
173                     continue;
174                 }
175             }
176         }
177         nameText.requestFocus();
178     }
179
180
181     public void setName(String JavaDoc text) {
182         this.nameText.setText(text);
183     }
184
185     public TemplateString getName() {
186         return new TemplateString(nameText.getText());
187     }
188
189     public String JavaDoc getVersion() {
190         return versionText.getText();
191     }
192
193     public void setVersion(String JavaDoc text) {
194         this.versionText.setText(text);
195     }
196
197     public String JavaDoc getDescription() {
198         return descriptionText.getText();
199     }
200
201     public void setDescription(String JavaDoc text) {
202         descriptionText.setText(text);
203     }
204
205     public String JavaDoc getRootPackage() {
206         return rootPackageText.getText();
207     }
208
209     public void setRootPackage(String JavaDoc text) {
210         this.rootPackageText.setText(text);
211     }
212
213     public String JavaDoc getRootPath() {
214         return rootPackageText.getText().replace('.', '/');
215     }
216
217     public String JavaDoc getLogFramework() {
218         return (String JavaDoc) loggingFrameworkCombo.getSelectedItem();
219     }
220
221     public void setLogFramework(String JavaDoc text) {
222         this.loggingFrameworkCombo.setSelectedItem(text);
223     }
224
225     public String JavaDoc getTimestampFormat() {
226         return timestampFormat.getText();
227     }
228
229     public void setTimestampFormat(String JavaDoc format) {
230         timestampFormat.setText(format);
231     }
232
233     public String JavaDoc getDateFormat() {
234         return dateFormat.getText();
235     }
236
237     public void setDateFormat(String JavaDoc format) {
238         dateFormat.setText(format);
239     }
240
241     public String JavaDoc toString() {
242         return "Application settings";
243     }
244
245     public JPanel getPanel() {
246         return panel;
247     }
248
249     public void getXML(Element JavaDoc el) throws ParserConfigurationException JavaDoc {
250         Document JavaDoc doc = el.getOwnerDocument();
251         Element JavaDoc module = doc.createElement("module");
252         module.setAttribute("name", "app");
253
254         Element JavaDoc name = doc.createElement(XMLTAG_MODULE_DATA);
255         name.setAttribute("name", "name");
256         if (nameText.getText() != null) {
257             name.appendChild(doc.createTextNode(nameText.getText()));
258         }
259         module.appendChild(name);
260
261         Element JavaDoc version = doc.createElement(XMLTAG_MODULE_DATA);
262         version.setAttribute("name", "version");
263         if (versionText.getText() != null) {
264             version.appendChild(doc.createTextNode(versionText.getText()));
265         }
266         module.appendChild(version);
267
268         Element JavaDoc description = doc.createElement(XMLTAG_MODULE_DATA);
269         description.setAttribute("name", "description");
270         if (descriptionText.getText() != null) {
271             description.appendChild(doc.createTextNode(descriptionText.getText()));
272         }
273         module.appendChild(description);
274
275         Element JavaDoc rootPackage = doc.createElement(XMLTAG_MODULE_DATA);
276         rootPackage.setAttribute("name", "root-package");
277         if (rootPackageText.getText() != null) {
278             rootPackage.appendChild(doc.createTextNode(rootPackageText.getText()));
279         }
280         module.appendChild(rootPackage);
281
282         Element JavaDoc loggingFramework = doc.createElement(XMLTAG_MODULE_DATA);
283         loggingFramework.setAttribute("name", "log-framework");
284         if (loggingFrameworkCombo.getSelectedItem() != null) {
285             loggingFramework.appendChild(doc.createTextNode((String JavaDoc) loggingFrameworkCombo.getSelectedItem()));
286         }
287         module.appendChild(loggingFramework);
288
289         Element JavaDoc dateFormat = doc.createElement(XMLTAG_MODULE_DATA);
290         dateFormat.setAttribute("name", "date-format");
291
292         if (getDateFormat() != null) {
293             dateFormat.appendChild(doc.createTextNode(getDateFormat()));
294         }
295         module.appendChild(dateFormat);
296
297         Element JavaDoc tsFormat = doc.createElement(XMLTAG_MODULE_DATA);
298         tsFormat.setAttribute("name", "timestamp-format");
299         if (getTimestampFormat() != null) {
300             tsFormat.appendChild(doc.createTextNode(getTimestampFormat()));
301         }
302         module.appendChild(tsFormat);
303
304         el.appendChild(module);
305     }
306
307     public String JavaDoc getRefName() {
308         return "app";
309     }
310
311
312     /**
313      * This method is called from within the constructor to
314      * initialize the form.
315      * WARNING: Do NOT modify this code. The content of this method is
316      * always regenerated by the Form Editor.
317      */

318     private void initComponents() {//GEN-BEGIN:initComponents
319
panel = new javax.swing.JPanel JavaDoc();
320         nameLabel = new javax.swing.JLabel JavaDoc();
321         versionLabel = new javax.swing.JLabel JavaDoc();
322         desciptionLabel = new javax.swing.JLabel JavaDoc();
323         rootPackageLabel = new javax.swing.JLabel JavaDoc();
324         nameText = new javax.swing.JTextField JavaDoc();
325         versionText = new javax.swing.JTextField JavaDoc();
326         descriptionText = new javax.swing.JTextField JavaDoc();
327         rootPackageText = new javax.swing.JTextField JavaDoc();
328         loggingFrameworkLabel = new javax.swing.JLabel JavaDoc();
329         loggingFrameworkCombo = new javax.swing.JComboBox JavaDoc();
330         dateFormatLabel = new javax.swing.JLabel JavaDoc();
331         dateFormat = new javax.swing.JTextField JavaDoc();
332         timestampFormatLabel = new javax.swing.JLabel JavaDoc();
333         timestampFormat = new javax.swing.JTextField JavaDoc();
334
335         panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
336
337         nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
338         nameLabel.setText("Application Name: ");
339         panel.add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 110, -1));
340
341         versionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
342         versionLabel.setText("Version: ");
343         panel.add(versionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 110, -1));
344
345         desciptionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
346         desciptionLabel.setText("Description: ");
347         panel.add(desciptionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 110, -1));
348
349         rootPackageLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
350         rootPackageLabel.setText("Root-package: ");
351         panel.add(rootPackageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 110, -1));
352
353         nameText.setToolTipText("Name should be lowercase and characters only!");
354         nameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
355             public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
356                 nameTextFocusGained(evt);
357             }
358
359             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
360                 nameTextFocusLost(evt);
361             }
362         });
363
364         panel.add(nameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, 260, -1));
365
366         versionText.setText("1.0");
367         versionText.setToolTipText("Version number of the application");
368         versionText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
369             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
370                 versionTextFocusLost(evt);
371             }
372         });
373
374         panel.add(versionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 260, -1));
375
376         descriptionText.setToolTipText("Description is used for class names, so make sure it starts with a capital and only contains characters");
377         descriptionText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
378             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
379                 descriptionTextFocusLost(evt);
380             }
381         });
382
383         panel.add(descriptionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 70, 260, -1));
384
385         rootPackageText.setText("com.finalist.");
386         rootPackageText.setToolTipText("Root package name for your application");
387         rootPackageText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
388             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
389                 rootPackageTextFocusLost(evt);
390             }
391         });
392
393         panel.add(rootPackageText, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 100, 260, -1));
394
395         loggingFrameworkLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
396         loggingFrameworkLabel.setText("Logging:");
397         panel.add(loggingFrameworkLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 110, -1));
398
399         loggingFrameworkCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[]{"log4j", "jdklogging"}));
400         loggingFrameworkCombo.setToolTipText("Select logging method");
401         loggingFrameworkCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
402             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
403                 loggingFrameworkComboActionPerformed(evt);
404             }
405         });
406
407         panel.add(loggingFrameworkCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 260, -1));
408
409         dateFormatLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
410         dateFormatLabel.setText("Date format:");
411         panel.add(dateFormatLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, 110, -1));
412
413         dateFormat.setText("dd/MM/yyyy");
414         dateFormat.setToolTipText("Date format used for displaying dates");
415         panel.add(dateFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 260, -1));
416
417         timestampFormatLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
418         timestampFormatLabel.setText("Timestamp format:");
419         panel.add(timestampFormatLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, 110, -1));
420
421         timestampFormat.setText("dd/MM/yyyy HH:mm:ss");
422         timestampFormat.setToolTipText("Timestamp format used for rendering timestamps");
423         panel.add(timestampFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 190, 260, -1));
424
425     }//GEN-END:initComponents
426

427     private void nameTextFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusGained
428
// TODO add your handling code here:
429
}//GEN-LAST:event_nameTextFocusGained
430

431     private void descriptionTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_descriptionTextFocusLost
432
// Make sure we only use characters and all in lowercase..
433
String JavaDoc name = descriptionText.getText();
434         if ((name == null) || (name.length() == 0))
435             return;
436         String JavaDoc formattedName = Utils.formatLowerAndUpperCase(name);
437         descriptionText.setText(formattedName);
438         JagGenerator.stateChanged(false);
439     }//GEN-LAST:event_descriptionTextFocusLost
440

441     private void versionTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_versionTextFocusLost
442
JagGenerator.stateChanged(false);
443     }//GEN-LAST:event_versionTextFocusLost
444

445     private void loggingFrameworkComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_loggingFrameworkComboActionPerformed
446
JagGenerator.stateChanged(false);
447     }//GEN-LAST:event_loggingFrameworkComboActionPerformed
448

449     private void rootPackageTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_rootPackageTextFocusLost
450
Root root = (Root) getParent();
451         root.setRootPackage(rootPackageText.getText());
452         JagGenerator.stateChanged(false);
453     }//GEN-LAST:event_rootPackageTextFocusLost
454

455
456     private void nameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusLost
457
// Make sure we only use characters and all in lowercase..
458
String JavaDoc name = nameText.getText();
459         if ((name == null) || (name.length() == 0))
460             return;
461         String JavaDoc formattedName = Utils.formatLowercase(name);
462         nameText.setText(formattedName);
463         JagGenerator.stateChanged(false);
464     }//GEN-LAST:event_nameTextFocusLost
465

466     // Variables declaration - do not modify//GEN-BEGIN:variables
467
public javax.swing.JTextField JavaDoc dateFormat;
468     private javax.swing.JLabel JavaDoc dateFormatLabel;
469     private javax.swing.JLabel JavaDoc desciptionLabel;
470     public javax.swing.JTextField JavaDoc descriptionText;
471     public javax.swing.JComboBox JavaDoc loggingFrameworkCombo;
472     private javax.swing.JLabel JavaDoc loggingFrameworkLabel;
473     private javax.swing.JLabel JavaDoc nameLabel;
474     public javax.swing.JTextField JavaDoc nameText;
475     public javax.swing.JPanel JavaDoc panel;
476     private javax.swing.JLabel JavaDoc rootPackageLabel;
477     public javax.swing.JTextField JavaDoc rootPackageText;
478     public javax.swing.JTextField JavaDoc timestampFormat;
479     private javax.swing.JLabel JavaDoc timestampFormatLabel;
480     private javax.swing.JLabel JavaDoc versionLabel;
481     private javax.swing.JTextField JavaDoc versionText;
482     // End of variables declaration//GEN-END:variables
483
}
484
485
Popular Tags