1 17 package com.finalist.jaggenerator.modules; 18 19 import java.io.File ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.swing.*; 26 import javax.swing.event.ListSelectionEvent ; 27 import javax.swing.event.ListSelectionListener ; 28 import javax.swing.border.TitledBorder ; 29 import javax.swing.tree.DefaultMutableTreeNode ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 import com.finalist.jaggenerator.JagGenerator; 38 import com.finalist.jaggenerator.template.Template; 39 import com.finalist.jaggenerator.template.TemplateConfigException; 40 import com.finalist.jaggenerator.template.TemplateConfigPanel; 41 42 45 public class Config extends DefaultMutableTreeNode implements JagBean, ListSelectionListener 46 { 47 private DefaultListModel listModel = new DefaultListModel(); 48 private static final File DEFAULT_TEMPLATE = new File ("../templates/java5_2_tier"); 49 private Template template; 50 public Template selectedTemplate = null; 51 private TemplateConfigPanel templatePanel; 52 private static final String XMLTAG_CONFIG_PARAM = "config-param"; 53 private static final String NAME_ATTRIBUTE = "name"; 54 private static final String VALUE_ATTRIBUTE = "value"; 55 56 57 60 public Config() { 61 initComponents(); 62 setTemplate(DEFAULT_TEMPLATE); 63 templateList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); 64 templateList.setModel(listModel); 65 templateList.addListSelectionListener(this); 66 } 67 68 69 public Config(Element el) { 70 initComponents(); 71 templateList.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); 72 templateList.setModel(listModel); 73 templateList.addListSelectionListener(this); 74 NodeList nl = el.getChildNodes(); 75 for (int i = 0; i < nl.getLength(); i++) { 76 if (nl.item(i) instanceof Element ) { 77 Element child = (Element ) nl.item(i); 78 String nodeName = child.getNodeName(); 79 if (nodeName.equalsIgnoreCase("author")) { 80 Node node = child.getFirstChild(); 81 if (node != null) { 82 authorText.setText(node.getNodeValue()); 83 } 84 continue; 85 } 86 if (nodeName.equalsIgnoreCase("version")) { 87 Node node = child.getFirstChild(); 88 if (node != null) { 89 versionText.setText(node.getNodeValue()); 90 } 91 continue; 92 } 93 if (nodeName.equalsIgnoreCase("company")) { 94 Node node = child.getFirstChild(); 95 if (node != null) { 96 companyText.setText(node.getNodeValue()); 97 } 98 continue; 99 } 100 if (nodeName.equalsIgnoreCase("templates")) { 101 NodeList templates = child.getElementsByTagName("template-root"); 102 for (int j = 0; j < templates.getLength(); j++) { 103 Node templateRootText = templates.item(j).getFirstChild(); 104 setTemplate(new File (templateRootText.getNodeValue())); 105 } 106 107 NodeList params = child.getElementsByTagName(XMLTAG_CONFIG_PARAM); 108 for (int j = 0; j < params.getLength(); j++) { 109 Node paramNode = params.item(j); 110 String paramName = paramNode.getAttributes().getNamedItem(NAME_ATTRIBUTE).getNodeValue(); 111 String paramValue = paramNode.getAttributes().getNamedItem(VALUE_ATTRIBUTE).getNodeValue(); 112 Map components = templatePanel.getConfigComponents(); 113 JComponent component = (JComponent) components.get(paramName); 114 if (component == null) { 115 JagGenerator 116 .logToConsole("Application file contains an unrecognised template config-param: " + paramName); 117 } 118 else { 119 if (component instanceof JTextField) { 120 ((JTextField) component).setText(paramValue); 121 } 122 else if (component instanceof JCheckBox) { 123 ((JCheckBox) component).setSelected(new Boolean (paramValue).booleanValue()); 124 } 125 else if (component instanceof JComboBox) { 126 ((JComboBox) component).setSelectedItem(paramValue); 127 } 128 } 129 } 130 } 131 } 132 } 133 } 134 135 136 public void setAuthor(String text) { 137 this.authorText.setText(text); 138 } 139 140 141 public void setVersion(String text) { 142 this.versionText.setText(text); 143 } 144 145 146 public void setCompany(String text) { 147 this.companyText.setText(text); 148 } 149 150 151 155 private void initComponents() { 156 panel = new javax.swing.JPanel (); 157 authorLabel = new javax.swing.JLabel (); 158 versionLabel = new javax.swing.JLabel (); 159 companyLabel = new javax.swing.JLabel (); 160 rootPackageLabel = new javax.swing.JLabel (); 161 authorText = new javax.swing.JTextField (); 162 versionText = new javax.swing.JTextField (); 163 companyText = new javax.swing.JTextField (); 164 editButton = new javax.swing.JButton (); 165 scrollPane = new javax.swing.JScrollPane (); 166 templateList = new javax.swing.JList (); 167 templateSettingsPanel = new javax.swing.JPanel (); 168 templateSettingsScrollPane = new javax.swing.JScrollPane (); 169 170 panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); 171 172 authorLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 173 authorLabel.setText("Author: "); 174 panel.add(authorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 90, -1)); 175 176 versionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 177 versionLabel.setText("Version: "); 178 panel.add(versionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 90, -1)); 179 180 companyLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 181 companyLabel.setText("Company: "); 182 panel.add(companyLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 90, -1)); 183 184 rootPackageLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 185 rootPackageLabel.setText("Template: "); 186 panel.add(rootPackageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 90, -1)); 187 188 authorText.addFocusListener(new java.awt.event.FocusAdapter () { 189 public void focusLost(java.awt.event.FocusEvent evt) { 190 authorTextFocusLost(evt); 191 } 192 }); 193 194 panel.add(authorText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 320, -1)); 195 196 versionText.setText("1.0"); 197 versionText.addFocusListener(new java.awt.event.FocusAdapter () { 198 public void focusLost(java.awt.event.FocusEvent evt) { 199 versionTextFocusLost(evt); 200 } 201 }); 202 203 panel.add(versionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 320, -1)); 204 205 companyText.setText("Finalist IT Group"); 206 companyText.addFocusListener(new java.awt.event.FocusAdapter () { 207 public void focusLost(java.awt.event.FocusEvent evt) { 208 companyTextFocusLost(evt); 209 } 210 }); 211 212 panel.add(companyText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 320, -1)); 213 214 editButton.setText("Select generation template"); 215 editButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 216 editButton.setMaximumSize(new java.awt.Dimension (400, 26)); 217 editButton.setMinimumSize(new java.awt.Dimension (400, 26)); 218 editButton.setPreferredSize(new java.awt.Dimension (400, 26)); 219 editButton.addActionListener(new java.awt.event.ActionListener () { 220 public void actionPerformed(java.awt.event.ActionEvent evt) { 221 editButtonActionPerformed(evt); 222 } 223 }); 224 225 panel.add(editButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 180, 190, 20)); 226 227 templateList.setBorder(new javax.swing.border.EtchedBorder ()); 228 scrollPane.setViewportView(templateList); 229 230 panel.add(scrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 320, 60)); 231 232 templateSettingsPanel.setLayout(new java.awt.BorderLayout ()); 233 234 templateSettingsPanel.setBorder(new javax.swing.border.TitledBorder ("Template settings:")); 235 236 templateSettingsPanel.add(templateSettingsScrollPane, java.awt.BorderLayout.CENTER); 237 238 panel.add(templateSettingsPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 220, 400, 250)); 239 240 } 241 242 243 private void companyTextFocusLost(java.awt.event.FocusEvent evt) { 244 JagGenerator.stateChanged(false); 245 } 246 247 248 private void versionTextFocusLost(java.awt.event.FocusEvent evt) { 249 JagGenerator.stateChanged(false); 250 } 251 252 253 private void authorTextFocusLost(java.awt.event.FocusEvent evt) { 254 JagGenerator.stateChanged(false); 255 } 256 257 258 private void editButtonActionPerformed(java.awt.event.ActionEvent evt) { 259 int fileChooserStatus; 260 261 String path = ((Template) listModel.get(0)).getTemplateDir().getAbsolutePath(); 262 JFileChooser fileChooser = new JFileChooser(path); 263 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 264 fileChooser.setDialogTitle("Select a generation template (directory).."); 265 fileChooserStatus = fileChooser.showDialog(null, "Select"); 266 if (fileChooserStatus == JFileChooser.APPROVE_OPTION) { 267 File file = fileChooser.getSelectedFile(); 268 setTemplate(file); 269 } 270 JagGenerator.stateChanged(false); 271 } 272 273 public void valueChanged( ListSelectionEvent event ) 275 { 276 if( event.getSource() == templateList 279 && !event.getValueIsAdjusting() ) 280 { 281 Template template = (Template)templateList.getSelectedValue(); 284 if( template != null ) { 285 selectedTemplate = template; 286 setTemplate(selectedTemplate.getTemplateDir()); 287 } 288 } 289 } 290 291 private void setTemplate(File path) { 292 try { 293 listModel.clear(); 294 File parent = path.getParentFile(); 295 File [] fileList = parent.listFiles(); 296 int index = 0; 297 for (int i = 0; i < fileList.length; i++) { 298 if (fileList[i].isDirectory()) { 299 File templateFile = new File (fileList[i] + File.separator + "template.xml"); 301 if (templateFile.exists()) { 302 template = new Template(fileList[i]); 303 if (fileList[i].equals(path)) { 304 selectedTemplate = template; 305 } 306 listModel.add(index, template); 307 index++; 308 } 309 } 310 } 311 if (selectedTemplate == null) { 312 System.out.println("No templates found!"); 313 return; 314 } 315 templatePanel = new TemplateConfigPanel(selectedTemplate.getConfigParams(), selectedTemplate.getName()); 316 TitledBorder border = (TitledBorder ) templateSettingsPanel.getBorder(); 318 templateSettingsScrollPane.setViewportView(templatePanel); 320 321 } 322 catch (TemplateConfigException tce) { 323 JOptionPane.showMessageDialog(JagGenerator.jagGenerator, 324 tce.getMessage(), "Invalid template!", javax.swing.JOptionPane.ERROR_MESSAGE); 325 } 326 } 327 328 329 public String toString() { 330 return "Configuration"; 331 } 332 333 334 public JPanel getPanel() { 335 return panel; 336 } 337 338 339 public void getXML(Element el) throws ParserConfigurationException { 340 Document doc = el.getOwnerDocument(); 341 Element config = doc.createElement("config"); 342 343 Element author = doc.createElement("author"); 344 if (authorText.getText() != null) { 345 author.appendChild(doc.createTextNode(authorText.getText())); 346 } 347 config.appendChild(author); 348 349 Element version = doc.createElement("version"); 350 if (versionText.getText() != null) { 351 version.appendChild(doc.createTextNode(versionText.getText())); 352 } 353 config.appendChild(version); 354 355 Element company = doc.createElement("company"); 356 if (companyText.getText() != null) { 357 company.appendChild(doc.createTextNode(companyText.getText())); 358 } 359 config.appendChild(company); 360 361 Element templates = doc.createElement("templates"); 362 Enumeration children = listModel.elements(); 363 while (children.hasMoreElements()) { 364 String templatePath = "."; 365 Element templateRoot = doc.createElement("template-root"); 366 367 if (selectedTemplate != null) { 368 templatePath = selectedTemplate.getTemplateDir().getPath(); 369 } else { 370 templatePath = ((Template) children.nextElement()).getTemplateDir().getPath(); 371 } 372 templatePath = templatePath.replace('\\', '/'); 373 templateRoot.appendChild(doc.createTextNode(templatePath)); 375 templates.appendChild(templateRoot); 376 break; 377 } 378 Map configSettings = getTemplateSettings(); 379 Iterator i = configSettings.entrySet().iterator(); 380 while (i.hasNext()) { 381 Map.Entry entry = (Map.Entry ) i.next(); 382 Element param = doc.createElement(XMLTAG_CONFIG_PARAM); 383 param.setAttribute(NAME_ATTRIBUTE, (String ) entry.getKey()); 384 param.setAttribute(VALUE_ATTRIBUTE, (String ) entry.getValue()); 385 templates.appendChild(param); 386 } 387 388 config.appendChild(templates); 389 390 el.appendChild(config); 391 } 392 393 394 398 public Template getTemplate() { 399 return template; 400 } 401 402 403 public String getRefName() { 404 return null; 405 } 406 407 408 412 public Boolean useRelations() { 413 String templateValue = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_RELATIONS); 414 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 415 return new Boolean (false); 416 } 417 else { 418 return new Boolean (true); 419 } 420 } 421 422 423 427 public Boolean useMock() { 428 String templateValue = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_MOCK); 429 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 430 return new Boolean (false); 431 } 432 else { 433 return new Boolean (true); 434 } 435 } 436 437 438 442 public Boolean useJava5() { 443 String templateValue = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_JAVA5); 444 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 445 return new Boolean (false); 446 } 447 else { 448 return new Boolean (true); 449 } 450 } 451 452 453 457 public Boolean useWebService() { 458 String templateValue = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_WEB_SERVICE); 459 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 460 return new Boolean (false); 461 } 462 else { 463 return new Boolean (true); 464 } 465 } 466 467 468 472 public Boolean useSecurity() { 473 String templateValue = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_USE_SECURITY); 474 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 475 return new Boolean (false); 476 } 477 else { 478 return new Boolean (true); 479 } 480 } 481 482 483 487 public Boolean useRss() { 488 return check("rssEnabled"); 489 } 490 491 492 496 public Boolean check(String checkBoxField) { 497 String templateValue = (String ) getTemplateSettings().get(checkBoxField); 498 if (templateValue == null || "false".equalsIgnoreCase(templateValue)) { 499 return new Boolean (false); 500 } 501 else { 502 return new Boolean (true); 503 } 504 } 505 506 507 511 public Map getTemplateSettings() { 512 String value = null; 513 HashMap settings = new HashMap (); 514 Map componentsMap = templatePanel.getConfigComponents(); 515 Iterator components = componentsMap.entrySet().iterator(); 516 517 while (components.hasNext()) { 518 Map.Entry entry = (Map.Entry ) components.next(); 519 JComponent component = (JComponent) entry.getValue(); 520 if (component instanceof JTextField) { 521 value = ((JTextField) component).getText(); 522 } 523 else if (component instanceof JCheckBox) { 524 value = "" + ((JCheckBox) component).isSelected(); 525 } 526 else if (component instanceof JComboBox) { 527 value = "" + ((JComboBox) component).getSelectedItem(); 528 } 529 settings.put(entry.getKey(), value); 530 } 531 return settings; 532 } 533 534 535 538 public void setTemplateSettings(Map templateSettings) { 539 540 Map componentsMap = templatePanel.getConfigComponents(); 541 Iterator components = componentsMap.entrySet().iterator(); 542 543 while (components.hasNext()) { 544 Map.Entry entry = (Map.Entry ) components.next(); 545 JComponent component = (JComponent) entry.getValue(); 546 String name = component.getName(); 547 String value = (String ) templateSettings.get(name); 548 if (value != null) { 549 if (component instanceof JTextField) { 550 ((JTextField) component).setText(value); 551 } 552 else if (component instanceof JCheckBox) { 553 if ("true".equalsIgnoreCase(value)) { 554 ((JCheckBox) component).setSelected(true); 555 } 556 else { 557 ((JCheckBox) component).setSelected(false); 558 } 559 } 560 else if (component instanceof JComboBox) { 561 ((JComboBox) component).setSelectedItem(value); 562 } 563 } 564 } 565 566 } 567 568 569 576 public Boolean matchAppserver(String value) { 577 String selectedAppserver = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_APPLICATION_SERVER); 578 selectedAppserver = selectedAppserver.toLowerCase(); 579 value = value.toLowerCase(); 580 if (selectedAppserver == null || "".equals(selectedAppserver) || value == null || "".equals(value)) { 581 return new Boolean (false); 582 } 583 if (selectedAppserver.equals(value) || selectedAppserver.startsWith(value)) { 584 return new Boolean (true); 585 } 586 else { 587 return new Boolean (false); 588 } 589 } 590 591 592 599 public Boolean matchBusinessTier(String value) { 600 String selectedBusinessTier = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_BUSINESS_TIER); 601 selectedBusinessTier = selectedBusinessTier.toLowerCase(); 602 value = value.toLowerCase(); 603 if (selectedBusinessTier == null || "".equals(selectedBusinessTier) || value == null || "".equals(value)) { 604 return new Boolean (false); 605 } 606 if (selectedBusinessTier.equals(value) || selectedBusinessTier.startsWith(value)) { 607 return new Boolean (true); 608 } 609 else { 610 return new Boolean (false); 611 } 612 } 613 614 615 622 public Boolean matchServiceTier(String value) { 623 String selectedServiceTier = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_SERVICE_TIER); 624 selectedServiceTier = selectedServiceTier.toLowerCase(); 625 value = value.toLowerCase(); 626 if (selectedServiceTier == null || "".equals(selectedServiceTier) || value == null || "".equals(value)) { 627 return new Boolean (false); 628 } 629 if (selectedServiceTier.equals(value) || selectedServiceTier.startsWith(value)) { 630 return new Boolean (true); 631 } 632 else { 633 return new Boolean (false); 634 } 635 } 636 637 638 645 public Boolean matchWebTier(String value) { 646 String selectedWebTier = (String ) getTemplateSettings().get(JagGenerator.TEMPLATE_WEB_TIER); 647 selectedWebTier = selectedWebTier.toLowerCase(); 648 value = value.toLowerCase(); 649 if (selectedWebTier == null || "".equals(selectedWebTier) || value == null || "".equals(value)) { 650 return new Boolean (false); 651 } 652 if (selectedWebTier.equals(value) || selectedWebTier.startsWith(value)) { 653 return new Boolean (true); 654 } 655 else { 656 return new Boolean (false); 657 } 658 } 659 660 661 public String getAuthorText() { 662 return authorText.getText().toString(); 663 } 664 665 666 public String getVersionText() { 667 return versionText.getText().toString(); 668 } 669 670 671 public String getCompanyText() { 672 return companyText.getText().toString(); 673 } 674 675 676 private javax.swing.JLabel authorLabel; 678 private javax.swing.JTextField authorText; 679 private javax.swing.JLabel companyLabel; 680 private javax.swing.JTextField companyText; 681 private javax.swing.JButton editButton; 682 private javax.swing.JPanel panel; 683 private javax.swing.JLabel rootPackageLabel; 684 private javax.swing.JScrollPane scrollPane; 685 public javax.swing.JList templateList; 686 private javax.swing.JPanel templateSettingsPanel; 687 private javax.swing.JScrollPane templateSettingsScrollPane; 688 private javax.swing.JLabel versionLabel; 689 private javax.swing.JTextField versionText; 690 } 692 693 | Popular Tags |