KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.finalist.jag.util.TemplateString;
21 import com.finalist.jaggenerator.JagGenerator;
22 import com.finalist.jaggenerator.SelectTablesDialog;
23 import com.finalist.jaggenerator.Utils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31 import javax.swing.*;
32 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
33 import javax.swing.tree.TreeNode JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * @author hillie
44  */

45 public class Entity extends DefaultMutableTreeNode JavaDoc implements JagBean, ActionListener JavaDoc {
46     private boolean relationsEnabled;
47     static Log log = LogFactory.getLog(JagGenerator.class);
48
49     /**
50      * Creates new form BeanForm
51      */

52     public Entity(String JavaDoc rootPackage, String JavaDoc tableName, String JavaDoc pKey) {
53         initComponents();
54         String JavaDoc tableClassName = Utils.toClassName(tableName);
55         rootPackageText.setText(rootPackage + ".entity." + tableClassName.toLowerCase());
56         tableNameText.setText(tableName);
57         nameText.setText(tableClassName);
58         descriptionText.setText(tableClassName);
59         refNameText.setText(tableClassName);
60         pKeyText.setText(pKey);
61         JagGenerator.addEntity(getRefName(), this);
62         //a little sequencing hack.. the last-created relation can still be initialising itself when
63
//this point is reached - so we pause here a little while: otherwise we'll notify before it begins waiting!
64
synchronized (this) {
65             try {
66                 wait(100);
67             } catch (InterruptedException JavaDoc e) {
68             }
69         }
70         notifyRelationsThatConstructionIsFinished();
71         SelectTablesDialog.getAlreadyselected().add(tableName);
72     }
73
74     public Entity(Element JavaDoc el) {
75         relationsEnabled = !JagGenerator.isRelationsEnabled();
76         initComponents();
77         NodeList JavaDoc nl = el.getChildNodes();
78         for (int i = 0; i < nl.getLength(); i++) {
79             Node JavaDoc n = nl.item(i);
80             if (n.getNodeType() == n.ELEMENT_NODE) {
81                 Element JavaDoc child = (Element JavaDoc) n;
82                 String JavaDoc attName = child.getAttribute("name");
83                 try {
84                     Node JavaDoc first = child.getFirstChild();
85                     if (first == null) continue;
86                     String JavaDoc value = first.getNodeValue();
87                     if (value != null) {
88                         if (attName.equalsIgnoreCase("name")) {
89                             nameText.setText(value);
90                             continue;
91                         }
92                         if (attName.equalsIgnoreCase("display-name")) {
93                             displayNameText.setText(value);
94                             continue;
95                         }
96                         if (attName.equalsIgnoreCase("description")) {
97                             descriptionText.setText(value);
98                             continue;
99                         }
100                         if (attName.equalsIgnoreCase("root-package")) {
101                             rootPackageText.setText(value);
102                             continue;
103                         }
104                         if (attName.equalsIgnoreCase("table-name")) {
105                             tableNameText.setText(value);
106                             SelectTablesDialog.getAlreadyselected().add(value);
107                             continue;
108                         }
109                         if (attName.equalsIgnoreCase("primary-key")) {
110                             pKeyText.setText(value);
111                             continue;
112                         }
113                         if (attName.equalsIgnoreCase("primary-key-type")) {
114                             pKeyTypeText.setText(value);
115                             continue;
116                         }
117                         if (attName.equalsIgnoreCase("is-composite")) {
118                             isCompositeCombo.setSelectedItem(value);
119                             continue;
120                         }
121                         if (attName.equalsIgnoreCase("is-association")) {
122                             isAssociationEntity.setSelectedItem(value);
123                             continue;
124                         }
125                         if (attName.equalsIgnoreCase("field")) {
126                             add(new Field(this, child));
127                             continue;
128                         }
129                         if (attName.equalsIgnoreCase("relation")) {
130                             if (warningNeeded()) {
131                                 JOptionPane.showMessageDialog(getPanel(),
132                                         "The application file you have opened contains relations, but you have disabled relations support. \n" +
133                                                 "If you later save this file or generate an application from this file, the original relations information will be lost.\n" +
134                                                 "\n" +
135                                                 "To avoid this, either enable relations support (Options->Enable relations) and re-open the file, or save the file under a different name.",
136                                         "Container-managed relations disabled!",
137                                         JOptionPane.INFORMATION_MESSAGE);
138                             } else if (relationsEnabled) {
139                                 add(new Relation(this, child));
140                                 continue;
141                             }
142                         }
143                     }
144                 } catch (Exception JavaDoc e) {
145                     e.printStackTrace();
146                     JagGenerator.logToConsole("Error constructing " + this.toString() + ": " + e);
147                 }
148             }
149         }
150         nl = el.getElementsByTagName("ref-name");
151         if (nl.getLength() > 0) {
152             Node JavaDoc node = nl.item(0).getFirstChild();
153             if (node != null) {
154                 refNameText.setText(node.getNodeValue());
155             }
156         }
157
158         JagGenerator.addEntity(getRefName(), this);
159
160         //a little sequencing hack.. the last-created relation can still be initialising itself when
161
//this point is reached - so we pause here a little while: otherwise we'll notify before it begins waiting!
162
synchronized (this) {
163             try {
164                 wait(100);
165             } catch (InterruptedException JavaDoc e) {
166             }
167         }
168         notifyRelationsThatConstructionIsFinished();
169     }
170
171
172     public JPanel getPanel() {
173         return panel;
174     }
175
176     public String JavaDoc getRefName() {
177         return refNameText.getText();
178     }
179
180     public void setRefName(String JavaDoc text) {
181         refNameText.setText(text);
182     }
183
184
185     public void setPKeyType(String JavaDoc pKeyType) {
186         pKeyTypeText.setText(pKeyType);
187     }
188
189     public void getXML(Element JavaDoc el) throws ParserConfigurationException JavaDoc {
190         Document JavaDoc doc = el.getOwnerDocument();
191         Element JavaDoc module = doc.createElement("module");
192         module.setAttribute("name", "entity");
193
194         Element JavaDoc name = doc.createElement("module-data");
195         name.setAttribute("name", "name");
196         if (nameText.getText() != null) {
197             name.appendChild(doc.createTextNode(nameText.getText()));
198         }
199         module.appendChild(name);
200
201         Element JavaDoc displayname = doc.createElement("module-data");
202         displayname.setAttribute("name", "display-name");
203         if (displayNameText.getText() != null) {
204             displayname.appendChild(doc.createTextNode(displayNameText.getText()));
205         }
206         module.appendChild(displayname);
207
208
209         Element JavaDoc description = doc.createElement("module-data");
210         description.setAttribute("name", "description");
211         if (descriptionText.getText() != null) {
212             description.appendChild(doc.createTextNode(descriptionText.getText()));
213         }
214         module.appendChild(description);
215
216         Element JavaDoc rootPackage = doc.createElement("module-data");
217         rootPackage.setAttribute("name", "root-package");
218         if (rootPackageText.getText() != null) {
219             rootPackage.appendChild(doc.createTextNode(rootPackageText.getText()));
220         }
221         module.appendChild(rootPackage);
222
223         Element JavaDoc tableName = doc.createElement("module-data");
224         tableName.setAttribute("name", "table-name");
225         if (tableNameText.getText() != null) {
226             tableName.appendChild(doc.createTextNode(tableNameText.getText()));
227         }
228         module.appendChild(tableName);
229
230         Element JavaDoc pKey = doc.createElement("module-data");
231         pKey.setAttribute("name", "primary-key");
232         pKey.appendChild(doc.createTextNode((isCompositeKey() ? "" : pKeyText.getText())));
233         module.appendChild(pKey);
234
235         Element JavaDoc pKeyType = doc.createElement("module-data");
236         pKeyType.setAttribute("name", "primary-key-type");
237         if (pKeyTypeText.getText() != null) {
238             pKeyType.appendChild(doc.createTextNode(pKeyTypeText.getText()));
239         }
240
241         module.appendChild(pKeyType);
242         Element JavaDoc isComposite = doc.createElement("module-data");
243         isComposite.setAttribute("name", "is-composite");
244         isComposite.appendChild(doc.createTextNode((String JavaDoc) isCompositeCombo.getSelectedItem()));
245         module.appendChild(isComposite);
246
247
248         Element JavaDoc isAssociation = doc.createElement("module-data");
249         isAssociation.setAttribute("name", "is-association");
250         isAssociation.appendChild(doc.createTextNode((String JavaDoc) isAssociationEntity.getSelectedItem()));
251         module.appendChild(isAssociation);
252
253         Element JavaDoc refName = doc.createElement("ref-name");
254         if (refNameText.getText() != null) {
255             refName.appendChild(doc.createTextNode(refNameText.getText()));
256         }
257         module.appendChild(refName);
258
259         //..and finally getXML() from the children: fields and relations
260
Enumeration JavaDoc children = children();
261         while (children.hasMoreElements()) {
262             JagBean child = (JagBean) children.nextElement();
263             child.getXML(module);
264         }
265
266         el.appendChild(module);
267     }
268
269     public TemplateString getName() {
270         return new TemplateString(nameText.getText());
271     }
272
273     public void setName(String JavaDoc text) {
274         nameText.setText(text);
275     }
276
277     /**
278      * Display name is used to determine which field to render in drop downl lists.
279      * if not set, the primary key is used.
280      * @return the name of the field to use to display an entity in a dropdown list.
281      */

282     public TemplateString getDisplayName() {
283         if (displayNameText.getText() == null || "".equals(displayNameText.getText())) {
284             if (getPrimaryKey() == null) {
285                 return new TemplateString("");
286             } else {
287                 return new TemplateString(getPrimaryKey().toString());
288             }
289         }
290         return new TemplateString(displayNameText.getText());
291     }
292
293     public void setDisplayName(String JavaDoc text) {
294         displayNameText.setText(text);
295     }
296
297     public TemplateString getDescription() {
298         return new TemplateString(descriptionText.getText());
299     }
300
301     public void setDescription(String JavaDoc text) {
302         descriptionText.setText(text);
303     }
304
305     public TemplateString getRootPackage() {
306         return new TemplateString(rootPackageText.getText());
307     }
308
309     public void setRootPackage(String JavaDoc text) {
310         rootPackageText.setText(text);
311     }
312
313
314     public Field getPrimaryKey() {
315         return isCompositeKey() ? null : (Field) getPkFields().get(0);
316     }
317
318     /**
319      * Called by a field when it has been selected as primary key in the GUI.
320      *
321      * @param field
322      */

323     public void setPrimaryKey(Field field) {
324         if ("false".equals(isCompositeCombo.getSelectedItem())) {
325             if ("".equals(pKeyText.getText())) {
326                 pKeyText.setText(field.getName().toString());
327                 pKeyTypeText.setText(field.getType().toString());
328
329             } else {
330                 isCompositeCombo.setSelectedItem("true");
331                 pKeyText.setText("");
332                 pKeyTypeText.setText(rootPackageText.getText() + '.' + getName() + "PK");
333             }
334         }
335     }
336
337     /**
338      * Called by a field when it has been de-selected as primary key in the GUI.
339      *
340      * @param field
341      */

342     public void unsetPrimaryKey(Field field) {
343         if ("false".equals(isCompositeCombo.getSelectedItem())) {
344             pKeyText.setText("");
345             pKeyTypeText.setText("");
346
347         } else {
348             List JavaDoc pkFields = getPkFields();
349             if (pkFields.size() == 1) {
350                 isCompositeCombo.setSelectedItem("false");
351                 pKeyText.setText(((Field) pkFields.get(0)).getName().toString());
352                 pKeyTypeText.setText(((Field) pkFields.get(0)).getType().toString());
353             }
354         }
355     }
356
357     public TemplateString getPrimaryKeyType() {
358         return new TemplateString(pKeyTypeText.getText());
359     }
360
361     public TemplateString getPrimaryKeyName() {
362         return new TemplateString(pKeyText.getText());
363     }
364
365     public String JavaDoc getIsComposite() {
366         return (String JavaDoc) isCompositeCombo.getSelectedItem();
367     }
368
369     public void setIsComposite(String JavaDoc composite) {
370         isCompositeCombo.setSelectedItem(composite);
371     }
372
373     public String JavaDoc getIsAssociationEntity() {
374         return (String JavaDoc) isAssociationEntity.getSelectedItem();
375     }
376
377     public void setIsAssociationEntity(String JavaDoc associationEntity) {
378         isAssociationEntity.setSelectedItem(associationEntity);
379     }
380
381     public String JavaDoc getRootPath() {
382         return getRootPackage().toString().replace('.', '/');
383     }
384
385     public List JavaDoc getRelations() {
386         ArrayList JavaDoc relations = new ArrayList JavaDoc();
387         Enumeration JavaDoc children = children();
388         while (children.hasMoreElements()) {
389             JagBean child = (JagBean) children.nextElement();
390             if (child instanceof Relation) {
391                 relations.add(child);
392             }
393         }
394         return relations;
395     }
396
397     /**
398      * Get a list of all Relations to this entity that are not assocation relations.
399      *
400      * @return list with non-assocation relations.
401      */

402     public List JavaDoc getEntitiesRelations() {
403         ArrayList JavaDoc result = new ArrayList JavaDoc();
404         List JavaDoc entities = JagGenerator.getObjectsFromTree(Entity.class);
405         for (int i = 0; i < entities.size(); i++) {
406             Entity relatedEntity = (Entity) entities.get(i);
407             List JavaDoc relations = relatedEntity.getRelations();
408             for (int j = 0; j < relations.size(); j++) {
409                 Relation rel = (Relation) relations.get(j);
410                 Entity en = rel.getRelatedEntity();
411                 // Don't add assocation entities.
412
if ("false".equals(relatedEntity.getIsAssociationEntity()) && en.getName().equals(this.getName())) {
413                     result.add(rel);
414                 }
415             }
416         }
417         return result;
418     }
419
420     /**
421      * Get a list of all assocation Relations to this entity.
422      *
423      * @return list with relations.
424      */

425     public List JavaDoc getEntitiesAssocationRelations() {
426         ArrayList JavaDoc result = new ArrayList JavaDoc();
427         List JavaDoc entities = JagGenerator.getObjectsFromTree(Entity.class);
428         for (int i = 0; i < entities.size(); i++) {
429             Entity relatedEntity = (Entity) entities.get(i);
430             List JavaDoc relations = relatedEntity.getRelations();
431             for (int j = 0; j < relations.size(); j++) {
432                 Relation rel = (Relation) relations.get(j);
433                 // Only add relations that are association entities.
434
Entity en = rel.getRelatedEntity();
435                 if (en.getName().equals(this.getName())) {
436                     if ("true".equals(relatedEntity.getIsAssociationEntity())) {
437                         result.add(rel);
438                     }
439                 }
440             }
441         }
442         return result;
443     }
444
445     /**
446      * Get the Entity that is associated
447      *
448      * @param relationName
449      * @return description
450      */

451     public Entity getAssocationEntity(String JavaDoc relationName) {
452         //
453
return null;
454     }
455
456     public List JavaDoc getRelatedEntities() {
457         ArrayList JavaDoc relatedEntities = new ArrayList JavaDoc();
458         Enumeration JavaDoc children = children();
459         while (children.hasMoreElements()) {
460             JagBean child = (JagBean) children.nextElement();
461             if (child instanceof Relation) {
462                 relatedEntities.add(((Relation) child).getRelatedEntity());
463             }
464         }
465         return relatedEntities;
466     }
467
468     /**
469      * Gets the set of field names within this entity, which are used as relations to other CMR-related entity beans.
470      *
471      * @return a Set of TemplateString objects.
472      */

473     public List JavaDoc getRelationFieldNames() {
474         ArrayList JavaDoc relationFieldNames = new ArrayList JavaDoc();
475         Enumeration JavaDoc children = children();
476         while (children.hasMoreElements()) {
477             JagBean child = (JagBean) children.nextElement();
478             if (child instanceof Relation) {
479                 relationFieldNames.add(((Relation) child).getFieldName());
480             }
481         }
482         return relationFieldNames;
483     }
484
485     public boolean getHasRelations() {
486         return !getRelations().isEmpty();
487     }
488
489     public List JavaDoc getFields() {
490         ArrayList JavaDoc fields = new ArrayList JavaDoc();
491         Enumeration JavaDoc children = children();
492         while (children.hasMoreElements()) {
493             JagBean child = (JagBean) children.nextElement();
494             if (child instanceof Field) {
495                 fields.add(child);
496             }
497         }
498         return fields;
499     }
500
501     public List JavaDoc getNonFkFields() {
502         ArrayList JavaDoc fields = new ArrayList JavaDoc();
503         Enumeration JavaDoc children = children();
504         while (children.hasMoreElements()) {
505             JagBean child = (JagBean) children.nextElement();
506             if (child instanceof Field &&
507                     !((Field) child).isForeignKey()) {
508                 fields.add(child);
509             }
510         }
511         return fields;
512     }
513
514     public List JavaDoc getFkFields() {
515         ArrayList JavaDoc fields = new ArrayList JavaDoc();
516         Enumeration JavaDoc children = children();
517         while (children.hasMoreElements()) {
518             JagBean child = (JagBean) children.nextElement();
519             if (child instanceof Field &&
520                     ((Field) child).isForeignKey()) {
521                 fields.add(child);
522             }
523         }
524         return fields;
525     }
526
527
528     public List JavaDoc getNonRelationFields() {
529         ArrayList JavaDoc fields = new ArrayList JavaDoc();
530         Enumeration JavaDoc children = children();
531         while (children.hasMoreElements()) {
532             JagBean child = (JagBean) children.nextElement();
533             if (child instanceof Field &&
534                     (((Field) child).getRelation() == null)) {
535                 fields.add(child);
536             }
537         }
538         return fields;
539     }
540
541     public List JavaDoc getPkFields() {
542         ArrayList JavaDoc fields = new ArrayList JavaDoc();
543         Enumeration JavaDoc children = children();
544         while (children.hasMoreElements()) {
545             JagBean child = (JagBean) children.nextElement();
546             if (child instanceof Field &&
547                     ((Field) child).isPrimaryKey()) {
548                 fields.add(child);
549             }
550         }
551         return fields;
552     }
553
554     public List JavaDoc getNonPkFields() {
555         List JavaDoc nonPkFields = getFields();
556         nonPkFields.removeAll(getPkFields());
557         return nonPkFields;
558     }
559
560     /**
561      * Gets the fields that are neither primary keys, nor a foreign key involved in a container-managed relation.
562      *
563      * @return List
564      */

565     public List JavaDoc getNonPkRelationFields() {
566         List JavaDoc nonPkRelationFields = getNonRelationFields();
567         nonPkRelationFields.removeAll(getPkFields());
568         return nonPkRelationFields;
569     }
570
571     /**
572      * Iterates over all the table columns and determines whether the table has a single key or a composite key. The
573      * key is said to be composite when more than one column is part of the key.
574      *
575      * @return <code>true</code> only when more the table has a composite key else <code>false</code>
576      */

577     public boolean isCompositeKey() {
578         int countPrimaryKeys = countPrimaryKeyFields();
579         return countPrimaryKeys > 1;
580     }
581
582     /**
583      * Count the number of primary key fields.
584      *
585      * @return <code>int</code> with the number of primary keys
586      */

587     public int countPrimaryKeyFields() {
588         Enumeration JavaDoc children = children();
589         JagBean bean = null;
590         Field field = null;
591         int countPrimaryKeys = 0;
592         while (children.hasMoreElements()) {
593             bean = (JagBean) children.nextElement();
594             if (bean instanceof Field) {
595                 field = (Field) bean;
596                 if (field.isPrimaryKey()) countPrimaryKeys++;
597             }
598         }
599         return countPrimaryKeys;
600     }
601
602     /**
603      * Returns the class that represents the primary key type
604      *
605      * @return the class that represents the primary key type
606      */

607     public String JavaDoc getPrimaryKeyClass() {
608         Enumeration JavaDoc children = children();
609         JagBean bean = null;
610         Field field = null;
611         String JavaDoc getPrimaryKeyClass = null;
612         while (children.hasMoreElements()) {
613             bean = (JagBean) children.nextElement();
614             if (bean instanceof Field) {
615                 field = (Field) bean;
616                 if (field.isPrimaryKey()) getPrimaryKeyClass = field.getType();
617             }
618         }
619         return getPrimaryKeyClass;
620     }
621
622     /**
623      * Returns the First primary key field
624      *
625      * @return the field name of the first primary key field or an empty string if nothing was found.
626      */

627     public String JavaDoc getFirstPrimaryKeyFieldName() {
628         Enumeration JavaDoc children = children();
629         JagBean bean = null;
630         Field field = null;
631         String JavaDoc getPrimaryKeyFieldName = "";
632         while (children.hasMoreElements()) {
633             bean = (JagBean) children.nextElement();
634             if (bean instanceof Field) {
635                 field = (Field) bean;
636                 if (field.isPrimaryKey()) getPrimaryKeyFieldName = field.getName().toString();
637             }
638         }
639         return getPrimaryKeyFieldName;
640     }
641
642     public void actionPerformed(ActionEvent JavaDoc e) {
643         if (e.getActionCommand().equals("GET_PRIMARY_KEY")) {
644             String JavaDoc key = null;
645             if (isCompositeKey())
646                 key = rootPackageText.getText() + "." + nameText.getText() + "PK";
647             else
648                 key = getPrimaryKeyClass();
649             pKeyTypeText.setText(key);
650         }
651     }
652
653     /**
654      * Gets the name of the table represented by this Entity.
655      *
656      * @return the table name.
657      */

658     public TemplateString getLocalTableName() {
659         return new TemplateString(tableNameText.getText());
660     }
661
662     public void setTableName(String JavaDoc table) {
663         tableNameText.setText(table);
664     }
665
666     public String JavaDoc getTableName() {
667         return tableNameText.getText();
668     }
669
670     /**
671      * While an entity is being created, its constituent relations have to wait for the entity to finish being
672      * constructed before they can finish initialising themselves. Call this method when the Entity is ready.
673      */

674     public void notifyRelationsThatConstructionIsFinished() {
675         Iterator JavaDoc relations = getRelations().iterator();
676         while (relations.hasNext()) {
677             Relation relation = (Relation) relations.next();
678             relation.notifyLocalEntityIsComplete();
679         }
680     }
681
682     /**
683      * When a field name is changed, call this to tell all relations to update their lists.
684      *
685      * @param oldName
686      * @param text
687      */

688     public void notifyRelationsThatFieldNameChanged(String JavaDoc oldName, String JavaDoc text) {
689         Iterator JavaDoc relations = getRelations().iterator();
690         while (relations.hasNext()) {
691             Relation relation = (Relation) relations.next();
692             relation.notifyFieldNameChanged(oldName, text);
693         }
694
695     }
696
697     public String JavaDoc toString() {
698         return "Entity - " + getRefName();
699     }
700
701     /**
702      * Inserts a relation in the correct position (relations are always positioned first
703      * amongst an Entity's children, because it looks nicer!).
704      *
705      * @param relation
706      */

707     public void addRelation(Relation relation) {
708         TreeNode JavaDoc lastRelly = null;
709         Enumeration JavaDoc kids = children();
710         while (kids.hasMoreElements()) {
711             TreeNode JavaDoc kid = (TreeNode JavaDoc) kids.nextElement();
712             if (kid instanceof Relation) {
713                 lastRelly = kid;
714             }
715         }
716         int insertPos = 0;
717         if (lastRelly == null) {
718             insertPos = getIndex(getFirstChild());
719         } else {
720             insertPos = getIndex(lastRelly) + 1;
721         }
722         insert(relation, insertPos);
723     }
724
725
726    /**
727     * Check if one of the fields needs a sequence.
728     * If so, return true.
729     * @return true if a sequence is needed.
730     */

731     public boolean isSequenceEntity() {
732        List JavaDoc fields = getFields();
733        for (int i=0; i < getFields().size(); i++) {
734          Field field = (Field) fields.get(i);
735           if (field.isSequenceField()) {
736              return true;
737           }
738        }
739        return false;
740     }
741    /**
742     * Get a session that contains this Entity.
743     * @return Session
744     */

745    public Session getFirstSession() {
746       List JavaDoc services = JagGenerator.getObjectsFromTree(Session.class);
747       for (int i=0; i<services.size(); i++) {
748          Session s = (Session) services.get(i);
749          if (s.getEntities().contains(this)) {
750             return s;
751          }
752       }
753       System.out.println("No session found for entity:" + getName());
754       return null;
755    }
756
757     private boolean warningNeeded() {
758         if (relationsEnabled != JagGenerator.isRelationsEnabled()) {
759             relationsEnabled = JagGenerator.isRelationsEnabled();
760             return !relationsEnabled;
761         }
762         return false;
763     }
764
765     /**
766      * This method is called from within the constructor to
767      * initialize the form.
768      * WARNING: Do NOT modify this code. The content of this method is
769      * always regenerated by the Form Editor.
770      */

771     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
772
private void initComponents() {
773         panel = new javax.swing.JPanel JavaDoc();
774         nameLabel = new javax.swing.JLabel JavaDoc();
775         tableNameLabel = new javax.swing.JLabel JavaDoc();
776         pKeyLabel = new javax.swing.JLabel JavaDoc();
777         pKeyTypeLabel = new javax.swing.JLabel JavaDoc();
778         desciptionLabel = new javax.swing.JLabel JavaDoc();
779         rootPackageLabel = new javax.swing.JLabel JavaDoc();
780         refNameLabel = new javax.swing.JLabel JavaDoc();
781         nameText = new javax.swing.JTextField JavaDoc();
782         tableNameText = new javax.swing.JTextField JavaDoc();
783         pKeyText = new javax.swing.JTextField JavaDoc();
784         pKeyTypeText = new javax.swing.JTextField JavaDoc();
785         descriptionText = new javax.swing.JTextField JavaDoc();
786         rootPackageText = new javax.swing.JTextField JavaDoc();
787         refNameText = new javax.swing.JTextField JavaDoc();
788         isCompositeLabel = new javax.swing.JLabel JavaDoc();
789         isCompositeCombo = new javax.swing.JComboBox JavaDoc();
790         nameLabel1 = new javax.swing.JLabel JavaDoc();
791         displayNameText = new javax.swing.JTextField JavaDoc();
792         isCompositeLabel1 = new javax.swing.JLabel JavaDoc();
793         isAssociationEntity = new javax.swing.JComboBox JavaDoc();
794
795         panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
796
797         nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
798         nameLabel.setText("Name: ");
799         nameLabel.setToolTipText("Name of the entity");
800         panel.add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 90, -1));
801
802         tableNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
803         tableNameLabel.setText("Table name: ");
804         tableNameLabel.setToolTipText("Physical table name the entity is mapped to");
805         panel.add(tableNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 90, -1));
806
807         pKeyLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
808         pKeyLabel.setText("Primary key: ");
809         pKeyLabel.setToolTipText("Primary key field of the entity");
810         panel.add(pKeyLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 90, -1));
811
812         pKeyTypeLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
813         pKeyTypeLabel.setText("Primary key class: ");
814         pKeyTypeLabel.setToolTipText("Primary key class in case of a composite key");
815         panel.add(pKeyTypeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 130, 110, -1));
816
817         desciptionLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
818         desciptionLabel.setText("Description: ");
819         desciptionLabel.setToolTipText("Description of the entity");
820         panel.add(desciptionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 220, 90, -1));
821
822         rootPackageLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
823         rootPackageLabel.setText("Root-package: ");
824         rootPackageLabel.setToolTipText("Root package for the entity");
825         panel.add(rootPackageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 90, -1));
826
827         refNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
828         refNameLabel.setText("Ref-name: ");
829         refNameLabel.setToolTipText("Reference name for the entity");
830         panel.add(refNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 280, 90, -1));
831
832         nameText.setToolTipText("Name of the entity");
833         nameText.addActionListener(new java.awt.event.ActionListener JavaDoc() {
834             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
835                 nameTextActionPerformed(evt);
836             }
837         });
838         nameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
839             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
840                 nameTextFocusLost(evt);
841             }
842         });
843
844         panel.add(nameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 260, -1));
845
846         tableNameText.setToolTipText("Physical table name the entity is mapped to");
847         tableNameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
848             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
849                 tableNameTextFocusLost(evt);
850             }
851         });
852
853         panel.add(tableNameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 260, -1));
854
855         pKeyText.setToolTipText("Primary key field of the entity");
856         pKeyText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
857             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
858                 pKeyTextFocusLost(evt);
859             }
860         });
861
862         panel.add(pKeyText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 260, -1));
863
864         pKeyTypeText.setToolTipText("Primary key class in case of a composite key");
865         pKeyTypeText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
866             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
867                 pKeyTypeTextFocusLost(evt);
868             }
869         });
870
871         panel.add(pKeyTypeText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 260, -1));
872
873         descriptionText.setToolTipText("Description of the entity");
874         descriptionText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
875             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
876                 descriptionTextFocusLost(evt);
877             }
878         });
879
880         panel.add(descriptionText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 220, 260, -1));
881
882         rootPackageText.setToolTipText("Root package for the entity");
883         rootPackageText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
884             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
885                 rootPackageTextFocusLost(evt);
886             }
887         });
888
889         panel.add(rootPackageText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 250, 260, -1));
890
891         refNameText.setToolTipText("Reference name for the entity");
892         refNameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
893             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
894                 refNameTextFocusLost(evt);
895             }
896         });
897         refNameText.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
898             public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
899                 refNameTextMouseReleased(evt);
900             }
901         });
902         refNameText.addHierarchyListener(new java.awt.event.HierarchyListener JavaDoc() {
903             public void hierarchyChanged(java.awt.event.HierarchyEvent JavaDoc evt) {
904                 refNameTextHierarchyChanged(evt);
905             }
906         });
907
908         panel.add(refNameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 280, 260, -1));
909
910         isCompositeLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
911         isCompositeLabel.setText("Composite key:");
912         isCompositeLabel.setToolTipText("Set to true in case of a composite key");
913         panel.add(isCompositeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 190, 110, -1));
914
915         isCompositeCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "false", "true" }));
916         isCompositeCombo.setToolTipText("Set to true in case of a composite key");
917         isCompositeCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
918             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
919                 isCompositeComboActionPerformed(evt);
920             }
921         });
922
923         panel.add(isCompositeCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 190, 260, -1));
924
925         nameLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
926         nameLabel1.setText("Display name: ");
927         nameLabel1.setToolTipText("Display name for the entity. Should be one of the entity fields.");
928         panel.add(nameLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 90, -1));
929
930         displayNameText.setToolTipText("Display name for the entity");
931         panel.add(displayNameText, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 260, -1));
932
933         isCompositeLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
934         isCompositeLabel1.setText("Association entity:");
935         isCompositeLabel1.setToolTipText("Entity used for many to many relations");
936         panel.add(isCompositeLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 160, 110, -1));
937
938         isAssociationEntity.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "false", "true" }));
939         isAssociationEntity.setToolTipText("Entity used for many to many relations");
940         isAssociationEntity.addActionListener(new java.awt.event.ActionListener JavaDoc() {
941             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
942                 isAssociationEntityActionPerformed(evt);
943             }
944         });
945
946         panel.add(isAssociationEntity, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, 260, -1));
947
948     }// </editor-fold>//GEN-END:initComponents
949

950     private void isAssociationEntityActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_isAssociationEntityActionPerformed
951
if ("true".equals(this.isAssociationEntity.getSelectedItem().toString())) {
952             this.pKeyTypeText.setEnabled(false);
953             this.pKeyText.setEnabled(false);
954             this.isCompositeCombo.setEnabled(false);
955         } else {
956             this.pKeyText.setEnabled(true);
957             this.pKeyTypeText.setEnabled(true);
958             this.isCompositeCombo.setEnabled(true);
959         }
960     }//GEN-LAST:event_isAssociationEntityActionPerformed
961

962     private void nameTextActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_nameTextActionPerformed
963
// TODO add your handling code here:
964
}//GEN-LAST:event_nameTextActionPerformed
965

966     private void refNameTextHierarchyChanged(java.awt.event.HierarchyEvent JavaDoc evt) {//GEN-FIRST:event_refNameTextHierarchyChanged
967
// TODO add your handling code here:
968
}//GEN-LAST:event_refNameTextHierarchyChanged
969

970     private void refNameTextMouseReleased(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_refNameTextMouseReleased
971
// TODO add your handling code here:
972
}//GEN-LAST:event_refNameTextMouseReleased
973

974     private void refNameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_refNameTextFocusLost
975
System.out.println("Focus was lost for refname");
976         JagGenerator.stateChanged(true);
977     }//GEN-LAST:event_refNameTextFocusLost
978

979     private void rootPackageTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_rootPackageTextFocusLost
980
JagGenerator.stateChanged(false);
981     }//GEN-LAST:event_rootPackageTextFocusLost
982

983     private void descriptionTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_descriptionTextFocusLost
984
JagGenerator.stateChanged(false);
985     }//GEN-LAST:event_descriptionTextFocusLost
986

987     private void isCompositeComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_isCompositeComboActionPerformed
988
JagGenerator.stateChanged(false);
989     }//GEN-LAST:event_isCompositeComboActionPerformed
990

991     private void pKeyTypeTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_pKeyTypeTextFocusLost
992
JagGenerator.stateChanged(false);
993     }//GEN-LAST:event_pKeyTypeTextFocusLost
994

995     private void pKeyTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_pKeyTextFocusLost
996
JagGenerator.stateChanged(false);
997     }//GEN-LAST:event_pKeyTextFocusLost
998

999     private void tableNameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_tableNameTextFocusLost
1000
JagGenerator.stateChanged(false);
1001        JagGenerator.entityHasupdatedTableName(nameText.getText(), tableNameText.getText());
1002    }//GEN-LAST:event_tableNameTextFocusLost
1003

1004    private void nameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusLost
1005
// Add your handling code here:
1006
evt = null;
1007        if ((nameText.getText() != null) && (nameText.getText().length() > 0)) {
1008            nameText.setText(Utils.initCap(nameText.getText()));
1009            if ((refNameText.getText() == null) || (refNameText.getText().length() == 0)) {
1010                // Only change the refname, if it hasn't been set before.
1011
refNameText.setText(nameText.getText());
1012            }
1013        }
1014        JagGenerator.stateChanged(true);
1015    }//GEN-LAST:event_nameTextFocusLost
1016

1017    // Variables declaration - do not modify//GEN-BEGIN:variables
1018
private javax.swing.JLabel JavaDoc desciptionLabel;
1019    private javax.swing.JTextField JavaDoc descriptionText;
1020    public javax.swing.JTextField JavaDoc displayNameText;
1021    public javax.swing.JComboBox JavaDoc isAssociationEntity;
1022    public javax.swing.JComboBox JavaDoc isCompositeCombo;
1023    private javax.swing.JLabel JavaDoc isCompositeLabel;
1024    private javax.swing.JLabel JavaDoc isCompositeLabel1;
1025    private javax.swing.JLabel JavaDoc nameLabel;
1026    private javax.swing.JLabel JavaDoc nameLabel1;
1027    public javax.swing.JTextField JavaDoc nameText;
1028    private javax.swing.JLabel JavaDoc pKeyLabel;
1029    public javax.swing.JTextField JavaDoc pKeyText;
1030    private javax.swing.JLabel JavaDoc pKeyTypeLabel;
1031    public javax.swing.JTextField JavaDoc pKeyTypeText;
1032    private javax.swing.JPanel JavaDoc panel;
1033    private javax.swing.JLabel JavaDoc refNameLabel;
1034    private javax.swing.JTextField JavaDoc refNameText;
1035    private javax.swing.JLabel JavaDoc rootPackageLabel;
1036    public javax.swing.JTextField JavaDoc rootPackageText;
1037    private javax.swing.JLabel JavaDoc tableNameLabel;
1038    private javax.swing.JTextField JavaDoc tableNameText;
1039    // End of variables declaration//GEN-END:variables
1040
}
1041
1042
Popular Tags