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  package com.finalist.jaggenerator.modules;
18  
19  import com.finalist.jaggenerator.JagGenerator;
20  import com.finalist.jaggenerator.template.Template;
21  import com.finalist.jaggenerator.template.TemplateConfigException;
22  import com.finalist.jaggenerator.template.TemplateConfigPanel;
23  import com.finalist.jag.uml.JagUMLProfile;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  import org.w3c.dom.NodeList;
28  
29  import javax.swing.*;
30  import javax.swing.tree.DefaultMutableTreeNode;
31  import javax.xml.parsers.ParserConfigurationException;
32  import java.io.File;
33  import java.util.Enumeration;
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.Map;
37  
38  /***
39   * @author hillie
40   */
41  public class Config extends DefaultMutableTreeNode implements JagBean {
42     private DefaultListModel listModel = new DefaultListModel();
43     private static final File DEFAULT_TEMPLATE = new File("../templates/entity_facade_jboss_jsk_2_0");
44     private Template template;
45     private TemplateConfigPanel templatePanel;
46     private static final String XMLTAG_CONFIG_PARAM = "config-param";
47     private static final String NAME_ATTRIBUTE = "name";
48     private static final String VALUE_ATTRIBUTE = "value";
49  
50  
51     /***
52      * Creates new form Config
53      */
54     public Config() {
55        initComponents();
56        setTemplate(DEFAULT_TEMPLATE);
57        templateList.setModel(listModel);
58     }
59  
60     public Config(Element el) {
61        initComponents();
62        templateList.setModel(listModel);
63        NodeList nl = el.getChildNodes();
64        for (int i = 0; i < nl.getLength(); i++) {
65           if (nl.item(i) instanceof Element) {
66              Element child = (Element) nl.item(i);
67              String nodeName = child.getNodeName();
68              if (nodeName.equalsIgnoreCase("author")) {
69                 Node node = child.getFirstChild();
70                 if (node != null) {
71                    authorText.setText(node.getNodeValue());
72                 }
73                 continue;
74              }
75              if (nodeName.equalsIgnoreCase("version")) {
76                 Node node = child.getFirstChild();
77                 if (node != null) {
78                    versionText.setText(node.getNodeValue());
79                 }
80                 continue;
81              }
82              if (nodeName.equalsIgnoreCase("company")) {
83                 Node node = child.getFirstChild();
84                 if (node != null) {
85                    companyText.setText(node.getNodeValue());
86                 }
87                 continue;
88              }
89              if (nodeName.equalsIgnoreCase("templates")) {
90                 NodeList templates = child.getElementsByTagName("template-root");
91                 for (int j = 0; j < templates.getLength(); j++) {
92                    Node templateRootText = templates.item(j).getFirstChild();
93                    setTemplate(new File(templateRootText.getNodeValue()));
94                 }
95  
96                 NodeList params = child.getElementsByTagName(XMLTAG_CONFIG_PARAM);
97                 for (int j = 0; j < params.getLength(); j++) {
98                    Node paramNode = params.item(j);
99                    String paramName = paramNode.getAttributes().getNamedItem(NAME_ATTRIBUTE).getNodeValue();
100                   String paramValue = paramNode.getAttributes().getNamedItem(VALUE_ATTRIBUTE).getNodeValue();
101                   Map components = templatePanel.getConfigComponents();
102                   JComponent component = (JComponent) components.get(paramName);
103                   if (component == null) {
104                      JagGenerator.logToConsole("Application file contains an unrecognised template config-param: " + paramName);
105                   } else {
106                      if (component instanceof JTextField) {
107                         ((JTextField) component).setText(paramValue);
108                      } else if (component instanceof JCheckBox) {
109                         ((JCheckBox) component).setSelected(new Boolean(paramValue).booleanValue());
110                      } else if (component instanceof JComboBox) {
111                         ((JComboBox) component).setSelectedItem(paramValue);
112                      }
113                   }
114                }
115             }
116          }
117       }
118    }
119 
120    public void setAuthor(String text) {
121       this.authorText.setText(text);
122    }
123 
124    public void setVersion(String text) {
125       this.versionText.setText(text);
126    }
127 
128    public void setCompany(String text) {
129       this.companyText.setText(text);
130    }
131 
132    /***
133     * This method is called from within the constructor to
134     * initialize the form.
135     * WARNING: Do NOT modify this code. The content of this method is
136     * always regenerated by the Form Editor.
137     */
138    private void initComponents() {
139       panel = new javax.swing.JPanel();
140       authorLabel = new javax.swing.JLabel();
141       versionLabel = new javax.swing.JLabel();
142       companyLabel = new javax.swing.JLabel();
143       rootPackageLabel = new javax.swing.JLabel();
144       authorText = new javax.swing.JTextField();
145       versionText = new javax.swing.JTextField();
146       companyText = new javax.swing.JTextField();
147       editButton = new javax.swing.JButton();
148       scrollPane = new javax.swing.JScrollPane();
149       templateList = new javax.swing.JList();
150       templateSettingsPanel = new javax.swing.JPanel();
151       templateSettingsScrollPane = new javax.swing.JScrollPane();
152 
153       panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
154 
155       authorLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
156       authorLabel.setText("Author: ");
157       panel.add(authorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 90, -1));
158 
159       versionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
160       versionLabel.setText("Version: ");
161       panel.add(versionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 90, -1));
162 
163       companyLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
164       companyLabel.setText("Company: ");
165       panel.add(companyLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 90, -1));
166 
167       rootPackageLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
168       rootPackageLabel.setText("Template: ");
169       panel.add(rootPackageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 90, -1));
170 
171       authorText.addFocusListener(new java.awt.event.FocusAdapter() {
172          public void focusLost(java.awt.event.FocusEvent evt) {
173             authorTextFocusLost(evt);
174          }
175       });
176 
177       panel.add(authorText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 320, -1));
178 
179       versionText.setText("1.0");
180       versionText.addFocusListener(new java.awt.event.FocusAdapter() {
181          public void focusLost(java.awt.event.FocusEvent evt) {
182             versionTextFocusLost(evt);
183          }
184       });
185 
186       panel.add(versionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 320, -1));
187 
188       companyText.setText("Finalist IT Group");
189       companyText.addFocusListener(new java.awt.event.FocusAdapter() {
190          public void focusLost(java.awt.event.FocusEvent evt) {
191             companyTextFocusLost(evt);
192          }
193       });
194 
195       panel.add(companyText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 320, -1));
196 
197       editButton.setText("Select generation template");
198       editButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
199       editButton.setMaximumSize(new java.awt.Dimension(400, 26));
200       editButton.setMinimumSize(new java.awt.Dimension(400, 26));
201       editButton.setPreferredSize(new java.awt.Dimension(400, 26));
202       editButton.addActionListener(new java.awt.event.ActionListener() {
203          public void actionPerformed(java.awt.event.ActionEvent evt) {
204             editButtonActionPerformed(evt);
205          }
206       });
207 
208       panel.add(editButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 180, 190, 20));
209 
210       templateList.setBorder(new javax.swing.border.EtchedBorder());
211       scrollPane.setViewportView(templateList);
212 
213       panel.add(scrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 320, 60));
214 
215       templateSettingsPanel.setLayout(new java.awt.BorderLayout());
216 
217       templateSettingsPanel.setBorder(new javax.swing.border.TitledBorder("Template settings:"));
218 
219       templateSettingsPanel.add(templateSettingsScrollPane, java.awt.BorderLayout.CENTER);
220 
221       panel.add(templateSettingsPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 220, 400, 250));
222 
223    }
224 
225    private void companyTextFocusLost(java.awt.event.FocusEvent evt) {
226       JagGenerator.stateChanged(false);
227    }
228 
229    private void versionTextFocusLost(java.awt.event.FocusEvent evt) {
230       JagGenerator.stateChanged(false);
231    }
232 
233    private void authorTextFocusLost(java.awt.event.FocusEvent evt) {
234       JagGenerator.stateChanged(false);
235    }
236 
237 
238    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {
239       int fileChooserStatus;
240 
241       String path = ((Template) listModel.get(0)).getTemplateDir().getAbsolutePath();
242       JFileChooser fileChooser = new JFileChooser(path);
243       fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
244       fileChooser.setDialogTitle("Select a generation template (directory)..");
245       fileChooserStatus = fileChooser.showDialog(null, "Select");
246       if (fileChooserStatus == JFileChooser.APPROVE_OPTION) {
247          File file = fileChooser.getSelectedFile();
248          setTemplate(file);
249       }
250       JagGenerator.stateChanged(false);
251    }
252 
253    private void setTemplate(File path) {
254       try {
255          template = new Template(path);
256          listModel.clear();
257          listModel.add(0, template);
258          templatePanel = new TemplateConfigPanel(template.getConfigParams());
259          templateSettingsScrollPane.setViewportView(templatePanel);
260 
261       } catch (TemplateConfigException tce) {
262          JOptionPane.showMessageDialog(JagGenerator.jagGenerator,
263                tce.getMessage(), "Invalid template!", javax.swing.JOptionPane.ERROR_MESSAGE);
264       }
265    }
266 
267    public String toString() {
268       return "Configuration";
269    }
270 
271    public JPanel getPanel() {
272       return panel;
273    }
274 
275    public void getXML(Element el) throws ParserConfigurationException {
276       Document doc = el.getOwnerDocument();
277       Element config = doc.createElement("config");
278 
279       Element author = doc.createElement("author");
280       if (authorText.getText() != null) {
281          author.appendChild(doc.createTextNode(authorText.getText()));
282       }
283       config.appendChild(author);
284 
285       Element version = doc.createElement("version");
286       if (versionText.getText() != null) {
287          version.appendChild(doc.createTextNode(versionText.getText()));
288       }
289       config.appendChild(version);
290 
291       Element company = doc.createElement("company");
292       if (companyText.getText() != null) {
293          company.appendChild(doc.createTextNode(companyText.getText()));
294       }
295       config.appendChild(company);
296 
297       Element templates = doc.createElement("templates");
298       Enumeration children = listModel.elements();
299       while (children.hasMoreElements()) {
300          Element templateRoot = doc.createElement("template-root");
301 
302          String templatePath = ((Template) children.nextElement()).getTemplateDir().getPath();
303          templatePath = templatePath.replace('//', '/');
304          // Make sure that paths is stored os independent
305          templateRoot.appendChild(doc.createTextNode(templatePath));
306          templates.appendChild(templateRoot);
307       }
308       Map configSettings = getTemplateSettings();
309       Iterator i = configSettings.entrySet().iterator();
310       while (i.hasNext()) {
311          Map.Entry entry = (Map.Entry) i.next();
312          Element param = doc.createElement(XMLTAG_CONFIG_PARAM);
313          param.setAttribute(NAME_ATTRIBUTE, (String) entry.getKey());
314          param.setAttribute(VALUE_ATTRIBUTE, (String) entry.getValue());
315          templates.appendChild(param);
316       }
317 
318       config.appendChild(templates);
319 
320       el.appendChild(config);
321    }
322 
323    /***
324     * Gets the chosen application generaton template.
325     *
326     * @return
327     */
328    public Template getTemplate() {
329       return template;
330    }
331 
332    public String getRefName() {
333       return null;
334    }
335 
336    /***
337     * check if the Container-managed relations checkbox was checked.
338     * @return
339     */
340    public Boolean useRelations() {
341       String templateValue = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_RELATIONS);
342       if ("false".equalsIgnoreCase(templateValue)) {
343          return new Boolean(false);
344       } else {
345          return new Boolean(true);
346       }
347    }
348 
349    /***
350     * check if the useMock checkbox was checked to generate a mock implementation.
351     * @return
352     */
353    public Boolean useMock() {
354       String templateValue = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_MOCK);
355       if ("false".equalsIgnoreCase(templateValue)) {
356          return new Boolean(false);
357       } else {
358          return new Boolean(true);
359       }
360    }
361 
362     /***
363      * check if the useJava5 checkbox was checked to generate java5 support.
364      * @return
365      */
366     public Boolean useJava5() {
367        String templateValue = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_JAVA5);
368        if ("false".equalsIgnoreCase(templateValue)) {
369           return new Boolean(false);
370        } else {
371           return new Boolean(true);
372        }
373     }
374 
375     /***
376      * check if the useWebService checkbox was checked to generate a webservice.
377      * @return
378      */
379     public Boolean useWebService() {
380        String templateValue = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_WEB_SERVICE);
381        if ("false".equalsIgnoreCase(templateValue)) {
382           return new Boolean(false);
383        } else {
384           return new Boolean(true);
385        }
386     }
387 
388    /***
389     * Gets the configuration settings as set by the user in the GUI.
390     *
391     * @return a Map of (String) configuration parameter id --> (String) value.
392     */
393    public Map getTemplateSettings() {
394       String value = null;
395       HashMap settings = new HashMap();
396       Map componentsMap = templatePanel.getConfigComponents();
397       Iterator components = componentsMap.entrySet().iterator();
398 
399       while (components.hasNext()) {
400          Map.Entry entry = (Map.Entry) components.next();
401          JComponent component = (JComponent) entry.getValue();
402          if (component instanceof JTextField) {
403             value = ((JTextField) component).getText();
404          } else if (component instanceof JCheckBox) {
405             value = "" + ((JCheckBox) component).isSelected();
406          } else if (component instanceof JComboBox) {
407             value = "" + ((JComboBox) component).getSelectedItem();
408          }
409          settings.put(entry.getKey(), value);
410       }
411       return settings;
412    }
413 
414    /***
415     * Set the template settings.
416     */
417    public void setTemplateSettings(Map templateSettings) {
418 
419       Map componentsMap = templatePanel.getConfigComponents();
420       Iterator components = componentsMap.entrySet().iterator();
421 
422       while (components.hasNext()) {
423          Map.Entry entry = (Map.Entry) components.next();
424          JComponent component = (JComponent) entry.getValue();
425          String name = component.getName();
426          String value = (String) templateSettings.get(name);
427          if (value != null) {
428             if (component instanceof JTextField) {
429                ((JTextField) component).setText(value);
430             } else if (component instanceof JCheckBox) {
431                if ("true".equalsIgnoreCase(value)) {
432                   ((JCheckBox) component).setSelected(true);
433                } else {
434                   ((JCheckBox) component).setSelected(false);
435                }
436             } else if (component instanceof JComboBox) {
437                ((JComboBox) component).setSelectedItem(value);
438             }
439          }
440       }
441 
442    }
443 
444    /***
445     * Helper method to determine if the selected appserver matches the passed string.
446     * A match is made in the selected appserver equals or starts with the passed value ignoring cases.
447     * This method can be used to ignore version numbers.
448     * So if you want to know that a JBoss appserver was selected, just match with "jboss".
449     *
450     * @param value
451     * @return Boolean true if there is a match.
452     */
453    public Boolean matchAppserver(String value) {
454       String selectedAppserver = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_APPLICATION_SERVER);
455       selectedAppserver = selectedAppserver.toLowerCase();
456       value = value.toLowerCase();
457       if (selectedAppserver == null || "".equals(selectedAppserver) || value == null || "".equals(value)) {
458          return new Boolean(false);
459       }
460       if (selectedAppserver.equals(value) || selectedAppserver.startsWith(value)) {
461          return new Boolean(true);
462       } else {
463          return new Boolean(false);
464       }
465    }
466 
467    /***
468     * Helper method to determine if the selected business tier matches the passed string.
469     * A match is made in the selected appserver equals or starts with the passed value ignoring cases.
470     * This method can be used to ignore version numbers.
471     *
472     * @param value
473     * @return Boolean true if there is a match.
474     */
475    public Boolean matchBusinessTier(String value) {
476       String selectedBusinessTier = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_BUSINESS_TIER);
477       selectedBusinessTier = selectedBusinessTier.toLowerCase();
478       value = value.toLowerCase();
479       if (selectedBusinessTier == null || "".equals(selectedBusinessTier) || value == null || "".equals(value)) {
480          return new Boolean(false);
481       }
482       if (selectedBusinessTier.equals(value) || selectedBusinessTier.startsWith(value)) {
483          return new Boolean(true);
484       } else {
485          return new Boolean(false);
486       }
487    }
488 
489    /***
490     * Helper method to determine if the selected service tier matches the passed string.
491     * A match is made if the selected service tier equals or starts with the passed value ignoring cases.
492     * This method can be used to ignore version numbers.
493     *
494     * @param value
495     * @return Boolean true if there is a match.
496     */
497    public Boolean matchServiceTier(String value) {
498       String selectedServiceTier = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_SERVICE_TIER);
499       selectedServiceTier = selectedServiceTier.toLowerCase();
500       value = value.toLowerCase();
501       if (selectedServiceTier == null || "".equals(selectedServiceTier) || value == null || "".equals(value)) {
502          return new Boolean(false);
503       }
504       if (selectedServiceTier.equals(value) || selectedServiceTier.startsWith(value)) {
505          return new Boolean(true);
506       } else {
507          return new Boolean(false);
508       }
509    }
510    
511    
512    /***
513     * Helper method to determine if the selected web tier matches the passed string.
514     * A match is made in the selected appserver equals or starts with the passed value ignoring cases.
515     * This method can be used to ignore version numbers.
516     *
517     * @param value
518     * @return Boolean true if there is a match.
519     */
520    public Boolean matchWebTier(String value) {
521       String selectedWebTier = (String) getTemplateSettings().get(JagGenerator.TEMPLATE_WEB_TIER);
522       selectedWebTier = selectedWebTier.toLowerCase();
523       value = value.toLowerCase();
524       if (selectedWebTier == null || "".equals(selectedWebTier) || value == null || "".equals(value)) {
525          return new Boolean(false);
526       }
527       if (selectedWebTier.equals(value) || selectedWebTier.startsWith(value)) {
528          return new Boolean(true);
529       } else {
530          return new Boolean(false);
531       }
532    }
533 
534 
535    public String getAuthorText() {
536       return authorText.getText().toString();
537    }
538 
539    public String getVersionText() {
540       return versionText.getText().toString();
541    }
542 
543    public String getCompanyText() {
544       return companyText.getText().toString();
545    }
546 
547    // Variables declaration - do not modify
548    private javax.swing.JLabel authorLabel;
549    private javax.swing.JTextField authorText;
550    private javax.swing.JLabel companyLabel;
551    private javax.swing.JTextField companyText;
552    private javax.swing.JButton editButton;
553    private javax.swing.JPanel panel;
554    private javax.swing.JLabel rootPackageLabel;
555    private javax.swing.JScrollPane scrollPane;
556    public javax.swing.JList templateList;
557    private javax.swing.JPanel templateSettingsPanel;
558    private javax.swing.JScrollPane templateSettingsScrollPane;
559    private javax.swing.JLabel versionLabel;
560    private javax.swing.JTextField versionText;
561    // End of variables declaration
562 }
563