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 com.finalist.jaggenerator.*;
021: import com.finalist.jag.util.TemplateString;
022: import java.awt.event.ActionListener;
023:
024: import java.util.*;
025: import javax.swing.*;
026: import javax.swing.tree.*;
027: import javax.xml.parsers.*;
028:
029: import org.w3c.dom.*;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: *
035: * @author hillie
036: */
037: public class Session extends DefaultMutableTreeNode implements JagBean {
038:
039: private DefaultListModel listModel = new DefaultListModel();
040: private ArrayList relationRefs = new ArrayList();
041: // private ArrayList businessMethods = new ArrayList();
042: static Log log = LogFactory.getLog(JagGenerator.class);
043:
044: /** Creates new form BeanForm */
045: public Session(String rootPackage) {
046: initComponents();
047: refList.setModel(listModel);
048: rootPackageText.setText(rootPackage + ".session");
049: }
050:
051: public Session(Element el) {
052: initComponents();
053: refList.setModel(listModel);
054: NodeList nl = el.getElementsByTagName("module-data");
055: for (int i = 0; i < nl.getLength(); i++) {
056: Element child = (Element) nl.item(i);
057: String attName = child.getAttribute("name");
058: try {
059: String value = child.getFirstChild().getNodeValue();
060: if (value != null) {
061: if (attName.equalsIgnoreCase("name")) {
062: nameText.setText(value);
063: } else if (attName.equalsIgnoreCase("root-package")) {
064: rootPackageText.setText(value);
065: } else if (attName.equalsIgnoreCase("description")) {
066: descriptionText.setText(value);
067: } else if (attName.equalsIgnoreCase("relation-ref")) {
068: NodeList subList = child
069: .getElementsByTagName("module-data");
070: for (int j = 0; j < subList.getLength(); j++) {
071: String entityName = (subList.item(j))
072: .getFirstChild().getNodeValue();
073: relationRefs.add(entityName);
074: }
075: }
076: }
077: } catch (NullPointerException e) {
078: }
079: }
080: nl = el.getElementsByTagName("ref-name");
081: if (nl.getLength() > 0) {
082: Node node = nl.item(0).getFirstChild();
083: if (node != null)
084: refNameText.setText(node.getNodeValue());
085: }
086: nl = el.getElementsByTagName("ref");
087: for (int i = 0; i < nl.getLength(); i++) {
088: Node node = nl.item(i).getFirstChild();
089: addRef(node.getNodeValue());
090: }
091:
092: nl = el.getElementsByTagName("business-methods");
093:
094: for (int i = 0; i < nl.getLength(); i++) {
095: Element child = (Element) nl.item(i);
096: NodeList bm = child.getElementsByTagName("business-method");
097: // Now, we got the list with business methods..
098: log.debug("Number of business methods: " + bm.getLength());
099: for (int j = 0; j < bm.getLength(); j++) {
100: Element bmNode = (Element) bm.item(j);
101: BusinessMethod newBusinessMethod = new BusinessMethod(
102: this , bmNode);
103: add(newBusinessMethod);
104: }
105: }
106: }
107:
108: public ArrayList getBusinessMethods() {
109: ArrayList refs = new ArrayList();
110: for (int i = 0; i < getChildCount(); i++) {
111: JagBean child = (JagBean) getChildAt(i);
112: if (child instanceof BusinessMethod) {
113: refs.add(child);
114: }
115: }
116: return refs;
117: }
118:
119: /** set Business methods. */
120: public void setBusinessMethods(ArrayList businessMethods) {
121: for (int i = 0; i < businessMethods.size(); i++) {
122: BusinessMethod child = (BusinessMethod) businessMethods
123: .get(i);
124: add(child);
125: }
126: }
127:
128: /**
129: * check if there are any business methods defines in this Session.
130: * @return true if this session has any business methods.
131: */
132: public Boolean hasBusinessMethods() {
133: if (getBusinessMethods().size() > 0) {
134: return new Boolean(true);
135: } else {
136: return new Boolean(false);
137: }
138: }
139:
140: /**
141: * Gets all entities 'covered' by this session bean.
142: * @return a list of the entity beans' reference names (String).
143: */
144: public ArrayList getEntityRefs() {
145: ArrayList entityRefs = new ArrayList();
146: if (listModel != null) {
147: for (int i = 0; i < listModel.size(); i++) {
148: entityRefs.add(listModel.get(i));
149: }
150: }
151: return entityRefs;
152: }
153:
154: /**
155: * Sets all entities 'covered' by this session bean.
156: */
157: public void setEntityRefs(ArrayList refs) {
158: if (refs != null) {
159: for (int i = 0; i < refs.size(); i++) {
160: addRef((String) refs.get(i));
161: }
162: }
163: }
164:
165: /**
166: * Gets all entities 'covered' by this session bean, excluding those that
167: * were only added to support a container-managed relation.
168: * @return a list of the entity beans' reference names (String).
169: */
170: public ArrayList getNonRelationEntityRefs() {
171: ArrayList entityRefs = new ArrayList();
172: if (listModel != null) {
173: for (int i = 0; i < listModel.size(); i++) {
174: String entity = (String) listModel.get(i);
175: if (!relationRefs.contains(entity)) {
176: entityRefs.add(entity);
177: }
178: }
179: }
180: return entityRefs;
181: }
182:
183: public String toString() {
184: return "Service - " + getRefName();
185: }
186:
187: public JPanel getPanel() {
188: return panel;
189: }
190:
191: public void getXML(Element el) throws ParserConfigurationException {
192: Document doc = el.getOwnerDocument();
193: Element module = doc.createElement("module");
194: module.setAttribute("name", "session");
195:
196: Element name = doc.createElement("module-data");
197: name.setAttribute("name", "name");
198: if (nameText.getText() != null) {
199: name.appendChild(doc.createTextNode(nameText.getText()));
200: }
201: module.appendChild(name);
202:
203: Element description = doc.createElement("module-data");
204: description.setAttribute("name", "description");
205: if (descriptionText.getText() != null) {
206: description.appendChild(doc.createTextNode(descriptionText
207: .getText()));
208: }
209: module.appendChild(description);
210:
211: Element rootPackage = doc.createElement("module-data");
212: rootPackage.setAttribute("name", "root-package");
213: if (rootPackageText.getText() != null) {
214: rootPackage.appendChild(doc.createTextNode(rootPackageText
215: .getText()));
216: }
217: module.appendChild(rootPackage);
218:
219: Element refName = doc.createElement("ref-name");
220: if (refNameText.getText() != null) {
221: refName.appendChild(doc.createTextNode(refNameText
222: .getText()));
223: module.appendChild(refName);
224:
225: } else {
226: // Only add in case of references..
227: }
228:
229: Enumeration refs = listModel.elements();
230: if (refs != null) {
231: while (refs.hasMoreElements()) {
232: Element refNode = doc.createElement("ref");
233: String ref = (String) refs.nextElement();
234: if (ref != null) {
235: refNode.appendChild(doc.createTextNode(ref));
236: }
237: module.appendChild(refNode);
238: }
239: }
240:
241: // Create the business methods.
242: if (getChildCount() > 0) {
243: Element businessMethods = doc
244: .createElement("business-methods");
245: for (int i = 0; i < getChildCount(); i++) {
246: BusinessMethod bm = (BusinessMethod) getChildAt(i);
247: bm.getXML(businessMethods);
248: }
249: module.appendChild(businessMethods);
250: }
251:
252: if (!relationRefs.isEmpty()) {
253: Iterator relRefs = relationRefs.iterator();
254: while (relRefs.hasNext()) {
255: Element refNode = doc.createElement("module-data");
256: refNode.setAttribute("name", "relation-ref");
257: Element child = doc.createElement("module-data");
258: child.setAttribute("name", "entity-name");
259: child.appendChild(doc.createTextNode((String) relRefs
260: .next()));
261: refNode.appendChild(child);
262: module.appendChild(refNode);
263: }
264: }
265:
266: el.appendChild(module);
267: }
268:
269: public String getRootPath() {
270: return getRootPackage().toString().replace('.', '/');
271: }
272:
273: public TemplateString getName() {
274: return new TemplateString(nameText.getText());
275: }
276:
277: public TemplateString getUpperCaseName() {
278: return new TemplateString(nameText.getText().toUpperCase());
279: }
280:
281: public void setName(String text) {
282: nameText.setText(text);
283: }
284:
285: public TemplateString getDescription() {
286: return new TemplateString(descriptionText.getText());
287: }
288:
289: public void setDescription(String text) {
290: descriptionText.setText(text);
291: }
292:
293: public TemplateString getRootPackage() {
294: return new TemplateString(rootPackageText.getText());
295: }
296:
297: public void setRootPackage(String text) {
298: rootPackageText.setText(text);
299: }
300:
301: /**
302: * Returns a List of all Entities in this Session, but not those that are only
303: * a 'relation reference'.
304: * @return
305: */
306: public List getEntities() {
307: List entities = getEntitiesAndReferences();
308: entities.removeAll(getReferencedEntities());
309: return entities;
310: }
311:
312: public List getReferencedEntities() {
313: ArrayList entities = new ArrayList();
314: Iterator relRefs = relationRefs.iterator();
315: while (relRefs.hasNext()) {
316: String ref = (String) relRefs.next();
317: Entity entity = JagGenerator.getEntityByRefName(ref);
318: entities.add(entity);
319: }
320: return entities;
321: }
322:
323: public List getEntitiesAndReferences() {
324: ArrayList entities = new ArrayList();
325: Enumeration refs = listModel.elements();
326: while (refs.hasMoreElements()) {
327: String ref = (String) refs.nextElement();
328: Entity entity = JagGenerator.getEntityByRefName(ref);
329: entities.add(entity);
330: }
331: return entities;
332: }
333:
334: /**
335: *
336: * @return
337: */
338: public String getRefName() {
339: return refNameText.getText();
340: }
341:
342: /**
343: *
344: * @param text
345: */
346: public void setRefName(String text) {
347: refNameText.setText(text);
348: }
349:
350: /**
351: *
352: * @param ref
353: */
354: public void addRef(String ref) {
355: listModel.addElement(ref);
356: }
357:
358: public void addRelationRef(String entity) {
359: addRef(entity);
360: relationRefs.add(entity);
361: }
362:
363: /** This method is called from within the constructor to
364: * initialize the form.
365: * WARNING: Do NOT modify this code. The content of this method is
366: * always regenerated by the Form Editor.
367: */
368: private void initComponents() {//GEN-BEGIN:initComponents
369: panel = new javax.swing.JPanel();
370: nameLabel = new javax.swing.JLabel();
371: desciptionLabel = new javax.swing.JLabel();
372: rootPackageLabel = new javax.swing.JLabel();
373: refNameLabel = new javax.swing.JLabel();
374: nameText = new javax.swing.JTextField();
375: descriptionText = new javax.swing.JTextField();
376: rootPackageText = new javax.swing.JTextField();
377: refNameText = new javax.swing.JTextField();
378: refsLabel = new javax.swing.JLabel();
379: jScrollPane1 = new javax.swing.JScrollPane();
380: refList = new javax.swing.JList();
381: buttonPanel = new javax.swing.JPanel();
382: removeButton = new javax.swing.JButton();
383: addButton = new javax.swing.JButton();
384:
385: panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
386:
387: nameLabel
388: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
389: nameLabel.setText("Name: ");
390: panel.add(nameLabel,
391: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
392: 10, 90, -1));
393:
394: desciptionLabel
395: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
396: desciptionLabel.setText("Description: ");
397: panel.add(desciptionLabel,
398: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
399: 40, 90, -1));
400:
401: rootPackageLabel
402: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
403: rootPackageLabel.setText("Root-package: ");
404: panel.add(rootPackageLabel,
405: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
406: 70, 90, -1));
407:
408: refNameLabel
409: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
410: refNameLabel.setText("Ref-name: ");
411: panel.add(refNameLabel,
412: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
413: 100, 90, -1));
414:
415: nameText.setAutoscrolls(false);
416: nameText.addFocusListener(new java.awt.event.FocusAdapter() {
417: public void focusLost(java.awt.event.FocusEvent evt) {
418: nameTextFocusLost(evt);
419: }
420: });
421:
422: panel.add(nameText,
423: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
424: 10, 260, -1));
425:
426: descriptionText
427: .addFocusListener(new java.awt.event.FocusAdapter() {
428: public void focusLost(java.awt.event.FocusEvent evt) {
429: descriptionTextFocusLost(evt);
430: }
431: });
432:
433: panel.add(descriptionText,
434: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
435: 40, 260, -1));
436:
437: rootPackageText
438: .addFocusListener(new java.awt.event.FocusAdapter() {
439: public void focusLost(java.awt.event.FocusEvent evt) {
440: rootPackageTextFocusLost(evt);
441: }
442: });
443:
444: panel.add(rootPackageText,
445: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
446: 70, 260, -1));
447:
448: refNameText.addFocusListener(new java.awt.event.FocusAdapter() {
449: public void focusLost(java.awt.event.FocusEvent evt) {
450: refNameTextFocusLost(evt);
451: }
452: });
453:
454: panel.add(refNameText,
455: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
456: 100, 260, -1));
457:
458: refsLabel
459: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
460: refsLabel.setText("Refs: ");
461: panel.add(refsLabel,
462: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
463: 130, 90, -1));
464:
465: refList.setBorder(new javax.swing.border.EtchedBorder());
466: jScrollPane1.setViewportView(refList);
467:
468: panel.add(jScrollPane1,
469: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
470: 130, 180, 170));
471:
472: buttonPanel.setLayout(new java.awt.BorderLayout());
473:
474: removeButton.setText("Remove");
475: removeButton.setMaximumSize(new java.awt.Dimension(56, 26));
476: removeButton.setMinimumSize(new java.awt.Dimension(56, 26));
477: removeButton
478: .addActionListener(new java.awt.event.ActionListener() {
479: public void actionPerformed(
480: java.awt.event.ActionEvent evt) {
481: removeButtonActionPerformed(evt);
482: }
483: });
484:
485: buttonPanel.add(removeButton, java.awt.BorderLayout.SOUTH);
486:
487: addButton.setText("Add");
488: addButton
489: .addActionListener(new java.awt.event.ActionListener() {
490: public void actionPerformed(
491: java.awt.event.ActionEvent evt) {
492: addButtonActionPerformed(evt);
493: }
494: });
495:
496: buttonPanel.add(addButton, java.awt.BorderLayout.CENTER);
497:
498: panel.add(buttonPanel,
499: new org.netbeans.lib.awtextra.AbsoluteConstraints(310,
500: 160, -1, -1));
501:
502: }//GEN-END:initComponents
503:
504: private void refNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_refNameTextFocusLost
505: JagGenerator.stateChanged(false);
506: }//GEN-LAST:event_refNameTextFocusLost
507:
508: private void rootPackageTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rootPackageTextFocusLost
509: JagGenerator.stateChanged(false);
510: }//GEN-LAST:event_rootPackageTextFocusLost
511:
512: private void descriptionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFocusLost
513: JagGenerator.stateChanged(false);
514: }//GEN-LAST:event_descriptionTextFocusLost
515:
516: private void nameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusLost
517: // Add your handling code here:
518: if ((nameText.getText() != null)
519: && (nameText.getText().length() > 0)) {
520: nameText.setText(Utils.initCap(nameText.getText()));
521: this .descriptionText.setText(Utils.initCap(nameText
522: .getText()));
523: this .refNameText.setText(Utils.initCap(nameText.getText()));
524: }
525: JagGenerator.stateChanged(true);
526: }//GEN-LAST:event_nameTextFocusLost
527:
528: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
529: if (((Root) getRoot()).getRefs().isEmpty()) {
530: JOptionPane
531: .showMessageDialog(
532: getPanel(),
533: "There are no entity beans specified in the current application, \n"
534: + "so it is not possible to add anything to this service bean yet.",
535: "No entity beans!",
536: javax.swing.JOptionPane.ERROR_MESSAGE);
537: return;
538: }
539: ArrayList refs = new ArrayList();
540: JFrame tmpFrame = new JFrame();
541: new SelectRefs(tmpFrame, this , refs).show();
542: if (refs == null)
543: return;
544:
545: for (int i = 0; i < refs.size(); i++) {
546: listModel.addElement(refs.get(i));
547: }
548:
549: refList.setModel(listModel);
550: JagGenerator.stateChanged(false);
551: }//GEN-LAST:event_addButtonActionPerformed
552:
553: private void removeButtonActionPerformed(
554: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
555: int[] selection = refList.getSelectedIndices();
556: if (selection != null) {
557: for (int i = selection.length - 1; i >= 0; i--) {
558: relationRefs.remove(listModel.elementAt(selection[i]));
559: listModel.removeElementAt(selection[i]);
560: }
561: refList.setModel(listModel);
562: JagGenerator.stateChanged(false);
563: }
564: }//GEN-LAST:event_removeButtonActionPerformed
565:
566: // Variables declaration - do not modify//GEN-BEGIN:variables
567: private javax.swing.JButton addButton;
568: private javax.swing.JPanel buttonPanel;
569: private javax.swing.JLabel desciptionLabel;
570: public javax.swing.JTextField descriptionText;
571: private javax.swing.JScrollPane jScrollPane1;
572: private javax.swing.JLabel nameLabel;
573: public javax.swing.JTextField nameText;
574: private javax.swing.JPanel panel;
575: private javax.swing.JList refList;
576: private javax.swing.JLabel refNameLabel;
577: public javax.swing.JTextField refNameText;
578: private javax.swing.JLabel refsLabel;
579: private javax.swing.JButton removeButton;
580: private javax.swing.JLabel rootPackageLabel;
581: private javax.swing.JTextField rootPackageText;
582: // End of variables declaration//GEN-END:variables
583: }
|