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 java.text.SimpleDateFormat;
021:
022: import com.finalist.jag.util.TemplateString;
023: import com.finalist.jaggenerator.JagGenerator;
024: import com.finalist.jaggenerator.Utils;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NodeList;
028:
029: import javax.swing.*;
030: import javax.swing.tree.DefaultMutableTreeNode;
031: import javax.xml.parsers.ParserConfigurationException;
032:
033: /**
034: * @author hillie
035: */
036: public class App extends DefaultMutableTreeNode implements JagBean {
037: private static final String XMLTAG_MODULE_DATA = "module-data";
038:
039: // Check if date is a supported calendar date.
040: public String isCalendarDate() {
041: if (!"".equals(getCalendarDateFormat())) {
042: return "true";
043: }
044: return "false";
045: }
046:
047: public String getCalendarDateFormat() {
048: if ("dd/MM/yyyy".equals(getDateFormat())) {
049: return "%d/%m/%Y";
050: }
051: if ("dd-MM-yyyy".equals(getDateFormat())) {
052: return "%d-%m-%Y";
053: }
054: if ("yyyy/MM/dd".equals(getDateFormat())) {
055: return "%Y/%m/%d";
056: }
057: if ("yyyy-MM-dd".equals(getDateFormat())) {
058: return "%Y-%m-%d";
059: }
060:
061: if ("MM/dd/yyyy".equals(getDateFormat())) {
062: return "%m/%d/%Y";
063: }
064: if ("MM-dd-yyyy".equals(getDateFormat())) {
065: return "%m-%d-%Y";
066: }
067: // Unsupported date format.
068: return "";
069: }
070:
071: // Check if time is a supported calendar date.
072: public String isCalendarTime() {
073: if (!"".equals(getCalendarTimeFormat())) {
074: return "true";
075: }
076: return "false";
077: }
078:
079: public String getCalendarTimeFormat() {
080: if ("dd/MM/yyyy HH:mm:ss".equals(getTimestampFormat())) {
081: return "%d/%m/%Y %H:%M:%S";
082: }
083: if ("dd-MM-yyyy HH:mm:ss".equals(getTimestampFormat())) {
084: return "%d-%m-%Y %H:%M:%S";
085: }
086: if ("yyyy/MM/dd HH:mm:ss".equals(getTimestampFormat())) {
087: return "%Y/%m/%d %H:%M:%S";
088: }
089: if ("yyyy-MM-dd HH:mm:ss".equals(getTimestampFormat())) {
090: return "%Y-%m-%d %H:%M:%S";
091: }
092:
093: if ("MM/dd/yyyy HH:mm:ss".equals(getTimestampFormat())) {
094: return "%m/%d/%Y %H:%M:%S";
095: }
096: if ("MM-dd-yyyy HH:mm:ss".equals(getTimestampFormat())) {
097: return "%m-%d-%Y %H:%M:%S";
098: }
099:
100: // Unsupported date format.
101: return "";
102: }
103:
104: /**
105: * Get the current date formatted using the date format.
106: *
107: * @return formatted current date.
108: */
109: public String getCurrentDate() {
110: SimpleDateFormat format = new SimpleDateFormat(getDateFormat());
111: return format.format(new java.util.Date());
112: }
113:
114: /**
115: * Creates new form BeanForm
116: */
117: public App() {
118: initComponents();
119: nameText.requestFocus();
120: rootPackageText.setText("com.finalist");
121: }
122:
123: public App(Element el) {
124: initComponents();
125: NodeList nl = el.getElementsByTagName(XMLTAG_MODULE_DATA);
126: for (int i = 0; i < nl.getLength(); i++) {
127: Element child = (Element) nl.item(i);
128: String attName = child.getAttribute("name");
129: String value = null;
130: if (child.getFirstChild() == null)
131: value = null;
132: else
133: value = child.getFirstChild().getNodeValue();
134: if (value != null) {
135: if (attName.equalsIgnoreCase("name")) {
136: nameText.setText(value);
137: continue;
138: }
139: if (attName.equalsIgnoreCase("version")) {
140: versionText.setText(value);
141: continue;
142: }
143: if (attName.equalsIgnoreCase("description")) {
144: descriptionText.setText(value);
145: continue;
146: }
147: if (attName.equalsIgnoreCase("root-package")) {
148: rootPackageText.setText(value);
149: continue;
150: }
151: if (attName.equalsIgnoreCase("log-framework")) {
152: loggingFrameworkCombo.setSelectedItem(value);
153: continue;
154: }
155: if (attName.equalsIgnoreCase("date-format")) {
156: dateFormat.setText(value);
157: continue;
158: }
159: if (attName.equalsIgnoreCase("timestamp-format")) {
160: timestampFormat.setText(value);
161: continue;
162: }
163: }
164: }
165: nameText.requestFocus();
166: }
167:
168: public void setName(String text) {
169: this .nameText.setText(text);
170: }
171:
172: public TemplateString getName() {
173: return new TemplateString(nameText.getText());
174: }
175:
176: public String getVersion() {
177: return versionText.getText();
178: }
179:
180: public void setVersion(String text) {
181: this .versionText.setText(text);
182: }
183:
184: public String getDescription() {
185: return descriptionText.getText();
186: }
187:
188: public void setDescription(String text) {
189: descriptionText.setText(text);
190: }
191:
192: public String getRootPackage() {
193: return rootPackageText.getText();
194: }
195:
196: public void setRootPackage(String text) {
197: this .rootPackageText.setText(text);
198: }
199:
200: public String getRootPath() {
201: return rootPackageText.getText().replace('.', '/');
202: }
203:
204: public String getLogFramework() {
205: return (String) loggingFrameworkCombo.getSelectedItem();
206: }
207:
208: public void setLogFramework(String text) {
209: this .loggingFrameworkCombo.setSelectedItem(text);
210: }
211:
212: public String getTimestampFormat() {
213: return timestampFormat.getText();
214: }
215:
216: public void setTimestampFormat(String format) {
217: timestampFormat.setText(format);
218: }
219:
220: public String getDateFormat() {
221: return dateFormat.getText();
222: }
223:
224: public void setDateFormat(String format) {
225: dateFormat.setText(format);
226: }
227:
228: public String toString() {
229: return "Application settings";
230: }
231:
232: public JPanel getPanel() {
233: return panel;
234: }
235:
236: public void getXML(Element el) throws ParserConfigurationException {
237: Document doc = el.getOwnerDocument();
238: Element module = doc.createElement("module");
239: module.setAttribute("name", "app");
240:
241: Element name = doc.createElement(XMLTAG_MODULE_DATA);
242: name.setAttribute("name", "name");
243: if (nameText.getText() != null) {
244: name.appendChild(doc.createTextNode(nameText.getText()));
245: }
246: module.appendChild(name);
247:
248: Element version = doc.createElement(XMLTAG_MODULE_DATA);
249: version.setAttribute("name", "version");
250: if (versionText.getText() != null) {
251: version.appendChild(doc.createTextNode(versionText
252: .getText()));
253: }
254: module.appendChild(version);
255:
256: Element description = doc.createElement(XMLTAG_MODULE_DATA);
257: description.setAttribute("name", "description");
258: if (descriptionText.getText() != null) {
259: description.appendChild(doc.createTextNode(descriptionText
260: .getText()));
261: }
262: module.appendChild(description);
263:
264: Element rootPackage = doc.createElement(XMLTAG_MODULE_DATA);
265: rootPackage.setAttribute("name", "root-package");
266: if (rootPackageText.getText() != null) {
267: rootPackage.appendChild(doc.createTextNode(rootPackageText
268: .getText()));
269: }
270: module.appendChild(rootPackage);
271:
272: Element loggingFramework = doc
273: .createElement(XMLTAG_MODULE_DATA);
274: loggingFramework.setAttribute("name", "log-framework");
275: if (loggingFrameworkCombo.getSelectedItem() != null) {
276: loggingFramework.appendChild(doc
277: .createTextNode((String) loggingFrameworkCombo
278: .getSelectedItem()));
279: }
280: module.appendChild(loggingFramework);
281:
282: Element dateFormat = doc.createElement(XMLTAG_MODULE_DATA);
283: dateFormat.setAttribute("name", "date-format");
284:
285: if (getDateFormat() != null) {
286: dateFormat.appendChild(doc.createTextNode(getDateFormat()));
287: }
288: module.appendChild(dateFormat);
289:
290: Element tsFormat = doc.createElement(XMLTAG_MODULE_DATA);
291: tsFormat.setAttribute("name", "timestamp-format");
292: if (getTimestampFormat() != null) {
293: tsFormat.appendChild(doc
294: .createTextNode(getTimestampFormat()));
295: }
296: module.appendChild(tsFormat);
297:
298: el.appendChild(module);
299: }
300:
301: public String getRefName() {
302: return "app";
303: }
304:
305: /**
306: * This method is called from within the constructor to
307: * initialize the form.
308: * WARNING: Do NOT modify this code. The content of this method is
309: * always regenerated by the Form Editor.
310: */
311: private void initComponents() {//GEN-BEGIN:initComponents
312: panel = new javax.swing.JPanel();
313: nameLabel = new javax.swing.JLabel();
314: versionLabel = new javax.swing.JLabel();
315: desciptionLabel = new javax.swing.JLabel();
316: rootPackageLabel = new javax.swing.JLabel();
317: nameText = new javax.swing.JTextField();
318: versionText = new javax.swing.JTextField();
319: descriptionText = new javax.swing.JTextField();
320: rootPackageText = new javax.swing.JTextField();
321: loggingFrameworkLabel = new javax.swing.JLabel();
322: loggingFrameworkCombo = new javax.swing.JComboBox();
323: dateFormatLabel = new javax.swing.JLabel();
324: dateFormat = new javax.swing.JTextField();
325: timestampFormatLabel = new javax.swing.JLabel();
326: timestampFormat = new javax.swing.JTextField();
327:
328: panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
329:
330: nameLabel
331: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
332: nameLabel.setText("Application Name: ");
333: panel.add(nameLabel,
334: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
335: 10, 110, -1));
336:
337: versionLabel
338: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
339: versionLabel.setText("Version: ");
340: panel.add(versionLabel,
341: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
342: 40, 110, -1));
343:
344: desciptionLabel
345: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
346: desciptionLabel.setText("Description: ");
347: panel.add(desciptionLabel,
348: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
349: 70, 110, -1));
350:
351: rootPackageLabel
352: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
353: rootPackageLabel.setText("Root-package: ");
354: panel.add(rootPackageLabel,
355: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
356: 100, 110, -1));
357:
358: nameText
359: .setToolTipText("Name should be lowercase and characters only!");
360: nameText.addFocusListener(new java.awt.event.FocusAdapter() {
361: public void focusGained(java.awt.event.FocusEvent evt) {
362: nameTextFocusGained(evt);
363: }
364:
365: public void focusLost(java.awt.event.FocusEvent evt) {
366: nameTextFocusLost(evt);
367: }
368: });
369:
370: panel.add(nameText,
371: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
372: 10, 260, -1));
373:
374: versionText.setText("1.0");
375: versionText.setToolTipText("Version number of the application");
376: versionText.addFocusListener(new java.awt.event.FocusAdapter() {
377: public void focusLost(java.awt.event.FocusEvent evt) {
378: versionTextFocusLost(evt);
379: }
380: });
381:
382: panel.add(versionText,
383: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
384: 40, 260, -1));
385:
386: descriptionText
387: .setToolTipText("Description is used for class names, so make sure it starts with a capital and only contains characters");
388: descriptionText
389: .addFocusListener(new java.awt.event.FocusAdapter() {
390: public void focusLost(java.awt.event.FocusEvent evt) {
391: descriptionTextFocusLost(evt);
392: }
393: });
394:
395: panel.add(descriptionText,
396: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
397: 70, 260, -1));
398:
399: rootPackageText.setText("com.finalist.");
400: rootPackageText
401: .setToolTipText("Root package name for your application");
402: rootPackageText
403: .addFocusListener(new java.awt.event.FocusAdapter() {
404: public void focusLost(java.awt.event.FocusEvent evt) {
405: rootPackageTextFocusLost(evt);
406: }
407: });
408:
409: panel.add(rootPackageText,
410: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
411: 100, 260, -1));
412:
413: loggingFrameworkLabel
414: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
415: loggingFrameworkLabel.setText("Logging:");
416: panel.add(loggingFrameworkLabel,
417: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
418: 130, 110, -1));
419:
420: loggingFrameworkCombo
421: .setModel(new javax.swing.DefaultComboBoxModel(
422: new String[] { "log4j", "jdklogging" }));
423: loggingFrameworkCombo.setToolTipText("Select logging method");
424: loggingFrameworkCombo
425: .addActionListener(new java.awt.event.ActionListener() {
426: public void actionPerformed(
427: java.awt.event.ActionEvent evt) {
428: loggingFrameworkComboActionPerformed(evt);
429: }
430: });
431:
432: panel.add(loggingFrameworkCombo,
433: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
434: 130, 260, -1));
435:
436: dateFormatLabel
437: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
438: dateFormatLabel.setText("Date format:");
439: panel.add(dateFormatLabel,
440: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
441: 160, 110, -1));
442:
443: dateFormat.setText("dd/MM/yyyy");
444: dateFormat
445: .setToolTipText("Date format used for displaying dates");
446: panel.add(dateFormat,
447: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
448: 160, 260, -1));
449:
450: timestampFormatLabel
451: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
452: timestampFormatLabel.setText("Timestamp format:");
453: panel.add(timestampFormatLabel,
454: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
455: 190, 110, -1));
456:
457: timestampFormat.setText("dd/MM/yyyy HH:mm:ss");
458: timestampFormat
459: .setToolTipText("Timestamp format used for rendering timestamps");
460: panel.add(timestampFormat,
461: new org.netbeans.lib.awtextra.AbsoluteConstraints(140,
462: 190, 260, -1));
463:
464: }//GEN-END:initComponents
465:
466: private void nameTextFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusGained
467: // TODO add your handling code here:
468: }//GEN-LAST:event_nameTextFocusGained
469:
470: private void descriptionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFocusLost
471: // Make sure we only use characters and all in lowercase..
472: String name = descriptionText.getText();
473: if ((name == null) || (name.length() == 0))
474: return;
475: String formattedName = Utils.formatLowerAndUpperCase(name);
476: descriptionText.setText(formattedName);
477: JagGenerator.stateChanged(false);
478: }//GEN-LAST:event_descriptionTextFocusLost
479:
480: private void versionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_versionTextFocusLost
481: JagGenerator.stateChanged(false);
482: }//GEN-LAST:event_versionTextFocusLost
483:
484: private void loggingFrameworkComboActionPerformed(
485: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loggingFrameworkComboActionPerformed
486: JagGenerator.stateChanged(false);
487: }//GEN-LAST:event_loggingFrameworkComboActionPerformed
488:
489: private void rootPackageTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rootPackageTextFocusLost
490: Root root = (Root) getParent();
491: root.setRootPackage(rootPackageText.getText());
492: JagGenerator.stateChanged(false);
493: }//GEN-LAST:event_rootPackageTextFocusLost
494:
495: private void nameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusLost
496: // Make sure we only use characters and all in lowercase..
497: String name = nameText.getText();
498: if ((name == null) || (name.length() == 0))
499: return;
500: String formattedName = Utils.formatLowercase(name);
501: nameText.setText(formattedName);
502: JagGenerator.stateChanged(false);
503: }//GEN-LAST:event_nameTextFocusLost
504:
505: // Variables declaration - do not modify//GEN-BEGIN:variables
506: public javax.swing.JTextField dateFormat;
507: private javax.swing.JLabel dateFormatLabel;
508: private javax.swing.JLabel desciptionLabel;
509: public javax.swing.JTextField descriptionText;
510: public javax.swing.JComboBox loggingFrameworkCombo;
511: private javax.swing.JLabel loggingFrameworkLabel;
512: private javax.swing.JLabel nameLabel;
513: public javax.swing.JTextField nameText;
514: public javax.swing.JPanel panel;
515: private javax.swing.JLabel rootPackageLabel;
516: public javax.swing.JTextField rootPackageText;
517: public javax.swing.JTextField timestampFormat;
518: private javax.swing.JLabel timestampFormatLabel;
519: private javax.swing.JLabel versionLabel;
520: private javax.swing.JTextField versionText;
521: // End of variables declaration//GEN-END:variables
522: }
|