0001: /* Copyright (C) 2003 Finalist IT Group
0002: *
0003: * This file is part of JAG - the Java J2EE Application Generator
0004: *
0005: * JAG is free software; you can redistribute it and/or modify
0006: * it under the terms of the GNU General Public License as published by
0007: * the Free Software Foundation; either version 2 of the License, or
0008: * (at your option) any later version.
0009: * JAG is distributed in the hope that it will be useful,
0010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012: * GNU General Public License for more details.
0013: * You should have received a copy of the GNU General Public License
0014: * along with JAG; if not, write to the Free Software
0015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0016: */
0017:
0018: package com.finalist.jaggenerator.modules;
0019:
0020: import com.finalist.jag.util.TemplateString;
0021: import com.finalist.jaggenerator.JagGenerator;
0022: import com.finalist.jaggenerator.SelectTablesDialog;
0023: import com.finalist.jaggenerator.Utils;
0024: import org.apache.commons.logging.Log;
0025: import org.apache.commons.logging.LogFactory;
0026: import org.w3c.dom.Document;
0027: import org.w3c.dom.Element;
0028: import org.w3c.dom.Node;
0029: import org.w3c.dom.NodeList;
0030:
0031: import javax.swing.*;
0032: import javax.swing.tree.DefaultMutableTreeNode;
0033: import javax.swing.tree.TreeNode;
0034: import javax.xml.parsers.ParserConfigurationException;
0035: import java.awt.event.ActionEvent;
0036: import java.awt.event.ActionListener;
0037: import java.util.ArrayList;
0038: import java.util.Enumeration;
0039: import java.util.Iterator;
0040: import java.util.List;
0041:
0042: /**
0043: * @author hillie
0044: */
0045: public class Entity extends DefaultMutableTreeNode implements JagBean,
0046: ActionListener {
0047: private boolean relationsEnabled;
0048: static Log log = LogFactory.getLog(JagGenerator.class);
0049:
0050: /**
0051: * Creates new form BeanForm
0052: */
0053: public Entity(String rootPackage, String tableName, String pKey) {
0054: initComponents();
0055: String tableClassName = Utils.toClassName(tableName);
0056: rootPackageText.setText(rootPackage + ".entity."
0057: + tableClassName.toLowerCase());
0058: tableNameText.setText(tableName);
0059: nameText.setText(tableClassName);
0060: descriptionText.setText(tableClassName);
0061: refNameText.setText(tableClassName);
0062: pKeyText.setText(pKey);
0063: JagGenerator.addEntity(getRefName(), this );
0064: //a little sequencing hack.. the last-created relation can still be initialising itself when
0065: //this point is reached - so we pause here a little while: otherwise we'll notify before it begins waiting!
0066: synchronized (this ) {
0067: try {
0068: wait(100);
0069: } catch (InterruptedException e) {
0070: }
0071: }
0072: notifyRelationsThatConstructionIsFinished();
0073: SelectTablesDialog.getAlreadyselected().add(tableName);
0074: }
0075:
0076: public Entity(Element el) {
0077: relationsEnabled = !JagGenerator.isRelationsEnabled();
0078: initComponents();
0079: NodeList nl = el.getChildNodes();
0080: for (int i = 0; i < nl.getLength(); i++) {
0081: Node n = nl.item(i);
0082: if (n.getNodeType() == n.ELEMENT_NODE) {
0083: Element child = (Element) n;
0084: String attName = child.getAttribute("name");
0085: try {
0086: Node first = child.getFirstChild();
0087: if (first == null)
0088: continue;
0089: String value = first.getNodeValue();
0090: if (value != null) {
0091: if (attName.equalsIgnoreCase("name")) {
0092: nameText.setText(value);
0093: continue;
0094: }
0095: if (attName.equalsIgnoreCase("display-name")) {
0096: displayNameText.setText(value);
0097: continue;
0098: }
0099: if (attName.equalsIgnoreCase("description")) {
0100: descriptionText.setText(value);
0101: continue;
0102: }
0103: if (attName.equalsIgnoreCase("root-package")) {
0104: rootPackageText.setText(value);
0105: continue;
0106: }
0107: if (attName.equalsIgnoreCase("table-name")) {
0108: tableNameText.setText(value);
0109: SelectTablesDialog.getAlreadyselected()
0110: .add(value);
0111: continue;
0112: }
0113: if (attName.equalsIgnoreCase("primary-key")) {
0114: pKeyText.setText(value);
0115: continue;
0116: }
0117: if (attName
0118: .equalsIgnoreCase("primary-key-type")) {
0119: pKeyTypeText.setText(value);
0120: continue;
0121: }
0122: if (attName.equalsIgnoreCase("is-composite")) {
0123: isCompositeCombo.setSelectedItem(value);
0124: continue;
0125: }
0126: if (attName.equalsIgnoreCase("is-association")) {
0127: isAssociationEntity.setSelectedItem(value);
0128: continue;
0129: }
0130: if (attName.equalsIgnoreCase("field")) {
0131: add(new Field(this , child));
0132: continue;
0133: }
0134: if (attName.equalsIgnoreCase("relation")) {
0135: if (warningNeeded()) {
0136: JOptionPane
0137: .showMessageDialog(
0138: getPanel(),
0139: "The application file you have opened contains relations, but you have disabled relations support. \n"
0140: + "If you later save this file or generate an application from this file, the original relations information will be lost.\n"
0141: + "\n"
0142: + "To avoid this, either enable relations support (Options->Enable relations) and re-open the file, or save the file under a different name.",
0143: "Container-managed relations disabled!",
0144: JOptionPane.INFORMATION_MESSAGE);
0145: } else if (relationsEnabled) {
0146: add(new Relation(this , child));
0147: continue;
0148: }
0149: }
0150: }
0151: } catch (Exception e) {
0152: e.printStackTrace();
0153: JagGenerator.logToConsole("Error constructing "
0154: + this .toString() + ": " + e);
0155: }
0156: }
0157: }
0158: nl = el.getElementsByTagName("ref-name");
0159: if (nl.getLength() > 0) {
0160: Node node = nl.item(0).getFirstChild();
0161: if (node != null) {
0162: refNameText.setText(node.getNodeValue());
0163: }
0164: }
0165:
0166: JagGenerator.addEntity(getRefName(), this );
0167:
0168: //a little sequencing hack.. the last-created relation can still be initialising itself when
0169: //this point is reached - so we pause here a little while: otherwise we'll notify before it begins waiting!
0170: synchronized (this ) {
0171: try {
0172: wait(100);
0173: } catch (InterruptedException e) {
0174: }
0175: }
0176: notifyRelationsThatConstructionIsFinished();
0177: }
0178:
0179: public JPanel getPanel() {
0180: return panel;
0181: }
0182:
0183: public String getRefName() {
0184: return refNameText.getText();
0185: }
0186:
0187: public void setRefName(String text) {
0188: refNameText.setText(text);
0189: }
0190:
0191: public void setPKeyType(String pKeyType) {
0192: pKeyTypeText.setText(pKeyType);
0193: }
0194:
0195: public void getXML(Element el) throws ParserConfigurationException {
0196: Document doc = el.getOwnerDocument();
0197: Element module = doc.createElement("module");
0198: module.setAttribute("name", "entity");
0199:
0200: Element name = doc.createElement("module-data");
0201: name.setAttribute("name", "name");
0202: if (nameText.getText() != null) {
0203: name.appendChild(doc.createTextNode(nameText.getText()));
0204: }
0205: module.appendChild(name);
0206:
0207: Element displayname = doc.createElement("module-data");
0208: displayname.setAttribute("name", "display-name");
0209: if (displayNameText.getText() != null) {
0210: displayname.appendChild(doc.createTextNode(displayNameText
0211: .getText()));
0212: }
0213: module.appendChild(displayname);
0214:
0215: Element description = doc.createElement("module-data");
0216: description.setAttribute("name", "description");
0217: if (descriptionText.getText() != null) {
0218: description.appendChild(doc.createTextNode(descriptionText
0219: .getText()));
0220: }
0221: module.appendChild(description);
0222:
0223: Element rootPackage = doc.createElement("module-data");
0224: rootPackage.setAttribute("name", "root-package");
0225: if (rootPackageText.getText() != null) {
0226: rootPackage.appendChild(doc.createTextNode(rootPackageText
0227: .getText()));
0228: }
0229: module.appendChild(rootPackage);
0230:
0231: Element tableName = doc.createElement("module-data");
0232: tableName.setAttribute("name", "table-name");
0233: if (tableNameText.getText() != null) {
0234: tableName.appendChild(doc.createTextNode(tableNameText
0235: .getText()));
0236: }
0237: module.appendChild(tableName);
0238:
0239: Element pKey = doc.createElement("module-data");
0240: pKey.setAttribute("name", "primary-key");
0241: pKey.appendChild(doc.createTextNode((isCompositeKey() ? ""
0242: : pKeyText.getText())));
0243: module.appendChild(pKey);
0244:
0245: Element pKeyType = doc.createElement("module-data");
0246: pKeyType.setAttribute("name", "primary-key-type");
0247: if (pKeyTypeText.getText() != null) {
0248: pKeyType.appendChild(doc.createTextNode(pKeyTypeText
0249: .getText()));
0250: }
0251:
0252: module.appendChild(pKeyType);
0253: Element isComposite = doc.createElement("module-data");
0254: isComposite.setAttribute("name", "is-composite");
0255: isComposite.appendChild(doc
0256: .createTextNode((String) isCompositeCombo
0257: .getSelectedItem()));
0258: module.appendChild(isComposite);
0259:
0260: Element isAssociation = doc.createElement("module-data");
0261: isAssociation.setAttribute("name", "is-association");
0262: isAssociation.appendChild(doc
0263: .createTextNode((String) isAssociationEntity
0264: .getSelectedItem()));
0265: module.appendChild(isAssociation);
0266:
0267: Element refName = doc.createElement("ref-name");
0268: if (refNameText.getText() != null) {
0269: refName.appendChild(doc.createTextNode(refNameText
0270: .getText()));
0271: }
0272: module.appendChild(refName);
0273:
0274: //..and finally getXML() from the children: fields and relations
0275: Enumeration children = children();
0276: while (children.hasMoreElements()) {
0277: JagBean child = (JagBean) children.nextElement();
0278: child.getXML(module);
0279: }
0280:
0281: el.appendChild(module);
0282: }
0283:
0284: public TemplateString getName() {
0285: return new TemplateString(nameText.getText());
0286: }
0287:
0288: public void setName(String text) {
0289: nameText.setText(text);
0290: }
0291:
0292: /**
0293: * Display name is used to determine which field to render in drop downl lists.
0294: * if not set, the primary key is used.
0295: * @return the name of the field to use to display an entity in a dropdown list.
0296: */
0297: public TemplateString getDisplayName() {
0298: if (displayNameText.getText() == null
0299: || "".equals(displayNameText.getText())) {
0300: if (getPrimaryKey() == null) {
0301: return new TemplateString("");
0302: } else {
0303: return new TemplateString(getPrimaryKey().toString());
0304: }
0305: }
0306: return new TemplateString(displayNameText.getText());
0307: }
0308:
0309: public void setDisplayName(String text) {
0310: displayNameText.setText(text);
0311: }
0312:
0313: public TemplateString getDescription() {
0314: return new TemplateString(descriptionText.getText());
0315: }
0316:
0317: public void setDescription(String text) {
0318: descriptionText.setText(text);
0319: }
0320:
0321: public TemplateString getRootPackage() {
0322: return new TemplateString(rootPackageText.getText());
0323: }
0324:
0325: public void setRootPackage(String text) {
0326: rootPackageText.setText(text);
0327: }
0328:
0329: public Field getPrimaryKey() {
0330: return isCompositeKey() ? null : (Field) getPkFields().get(0);
0331: }
0332:
0333: /**
0334: * Called by a field when it has been selected as primary key in the GUI.
0335: *
0336: * @param field
0337: */
0338: public void setPrimaryKey(Field field) {
0339: if ("false".equals(isCompositeCombo.getSelectedItem())) {
0340: if ("".equals(pKeyText.getText())) {
0341: pKeyText.setText(field.getName().toString());
0342: pKeyTypeText.setText(field.getType().toString());
0343:
0344: } else {
0345: isCompositeCombo.setSelectedItem("true");
0346: pKeyText.setText("");
0347: pKeyTypeText.setText(rootPackageText.getText() + '.'
0348: + getName() + "PK");
0349: }
0350: }
0351: }
0352:
0353: /**
0354: * Called by a field when it has been de-selected as primary key in the GUI.
0355: *
0356: * @param field
0357: */
0358: public void unsetPrimaryKey(Field field) {
0359: if ("false".equals(isCompositeCombo.getSelectedItem())) {
0360: pKeyText.setText("");
0361: pKeyTypeText.setText("");
0362:
0363: } else {
0364: List pkFields = getPkFields();
0365: if (pkFields.size() == 1) {
0366: isCompositeCombo.setSelectedItem("false");
0367: pKeyText.setText(((Field) pkFields.get(0)).getName()
0368: .toString());
0369: pKeyTypeText.setText(((Field) pkFields.get(0))
0370: .getType().toString());
0371: }
0372: }
0373: }
0374:
0375: public TemplateString getPrimaryKeyType() {
0376: return new TemplateString(pKeyTypeText.getText());
0377: }
0378:
0379: public TemplateString getPrimaryKeyName() {
0380: return new TemplateString(pKeyText.getText());
0381: }
0382:
0383: public String getIsComposite() {
0384: return (String) isCompositeCombo.getSelectedItem();
0385: }
0386:
0387: public void setIsComposite(String composite) {
0388: isCompositeCombo.setSelectedItem(composite);
0389: }
0390:
0391: public String getIsAssociationEntity() {
0392: return (String) isAssociationEntity.getSelectedItem();
0393: }
0394:
0395: public void setIsAssociationEntity(String associationEntity) {
0396: isAssociationEntity.setSelectedItem(associationEntity);
0397: }
0398:
0399: public String getRootPath() {
0400: return getRootPackage().toString().replace('.', '/');
0401: }
0402:
0403: public List getRelations() {
0404: ArrayList relations = new ArrayList();
0405: Enumeration children = children();
0406: while (children.hasMoreElements()) {
0407: JagBean child = (JagBean) children.nextElement();
0408: if (child instanceof Relation) {
0409: relations.add(child);
0410: }
0411: }
0412: return relations;
0413: }
0414:
0415: /**
0416: * Get a list of all Relations to this entity that are not assocation relations.
0417: *
0418: * @return list with non-assocation relations.
0419: */
0420: public List getEntitiesRelations() {
0421: ArrayList result = new ArrayList();
0422: List entities = JagGenerator.getObjectsFromTree(Entity.class);
0423: for (int i = 0; i < entities.size(); i++) {
0424: Entity relatedEntity = (Entity) entities.get(i);
0425: List relations = relatedEntity.getRelations();
0426: for (int j = 0; j < relations.size(); j++) {
0427: Relation rel = (Relation) relations.get(j);
0428: Entity en = rel.getRelatedEntity();
0429: // Don't add assocation entities.
0430: if ("false".equals(relatedEntity
0431: .getIsAssociationEntity())
0432: && en.getName().equals(this .getName())) {
0433: result.add(rel);
0434: }
0435: }
0436: }
0437: return result;
0438: }
0439:
0440: /**
0441: * Get a list of all assocation Relations to this entity.
0442: *
0443: * @return list with relations.
0444: */
0445: public List getEntitiesAssocationRelations() {
0446: ArrayList result = new ArrayList();
0447: List entities = JagGenerator.getObjectsFromTree(Entity.class);
0448: for (int i = 0; i < entities.size(); i++) {
0449: Entity relatedEntity = (Entity) entities.get(i);
0450: List relations = relatedEntity.getRelations();
0451: for (int j = 0; j < relations.size(); j++) {
0452: Relation rel = (Relation) relations.get(j);
0453: // Only add relations that are association entities.
0454: Entity en = rel.getRelatedEntity();
0455: if (en.getName().equals(this .getName())) {
0456: if ("true".equals(relatedEntity
0457: .getIsAssociationEntity())) {
0458: result.add(rel);
0459: }
0460: }
0461: }
0462: }
0463: return result;
0464: }
0465:
0466: /**
0467: * Get the Entity that is associated
0468: *
0469: * @param relationName
0470: * @return description
0471: */
0472: public Entity getAssocationEntity(String relationName) {
0473: //
0474: return null;
0475: }
0476:
0477: public List getRelatedEntities() {
0478: ArrayList relatedEntities = new ArrayList();
0479: Enumeration children = children();
0480: while (children.hasMoreElements()) {
0481: JagBean child = (JagBean) children.nextElement();
0482: if (child instanceof Relation) {
0483: relatedEntities.add(((Relation) child)
0484: .getRelatedEntity());
0485: }
0486: }
0487: return relatedEntities;
0488: }
0489:
0490: /**
0491: * Gets the set of field names within this entity, which are used as relations to other CMR-related entity beans.
0492: *
0493: * @return a Set of TemplateString objects.
0494: */
0495: public List getRelationFieldNames() {
0496: ArrayList relationFieldNames = new ArrayList();
0497: Enumeration children = children();
0498: while (children.hasMoreElements()) {
0499: JagBean child = (JagBean) children.nextElement();
0500: if (child instanceof Relation) {
0501: relationFieldNames.add(((Relation) child)
0502: .getFieldName());
0503: }
0504: }
0505: return relationFieldNames;
0506: }
0507:
0508: public boolean getHasRelations() {
0509: return !getRelations().isEmpty();
0510: }
0511:
0512: public List getFields() {
0513: ArrayList fields = new ArrayList();
0514: Enumeration children = children();
0515: while (children.hasMoreElements()) {
0516: JagBean child = (JagBean) children.nextElement();
0517: if (child instanceof Field) {
0518: fields.add(child);
0519: }
0520: }
0521: return fields;
0522: }
0523:
0524: public List getNonFkFields() {
0525: ArrayList fields = new ArrayList();
0526: Enumeration children = children();
0527: while (children.hasMoreElements()) {
0528: JagBean child = (JagBean) children.nextElement();
0529: if (child instanceof Field
0530: && !((Field) child).isForeignKey()) {
0531: fields.add(child);
0532: }
0533: }
0534: return fields;
0535: }
0536:
0537: public List getFkFields() {
0538: ArrayList fields = new ArrayList();
0539: Enumeration children = children();
0540: while (children.hasMoreElements()) {
0541: JagBean child = (JagBean) children.nextElement();
0542: if (child instanceof Field
0543: && ((Field) child).isForeignKey()) {
0544: fields.add(child);
0545: }
0546: }
0547: return fields;
0548: }
0549:
0550: public List getNonRelationFields() {
0551: ArrayList fields = new ArrayList();
0552: Enumeration children = children();
0553: while (children.hasMoreElements()) {
0554: JagBean child = (JagBean) children.nextElement();
0555: if (child instanceof Field
0556: && (((Field) child).getRelation() == null)) {
0557: fields.add(child);
0558: }
0559: }
0560: return fields;
0561: }
0562:
0563: public List getPkFields() {
0564: ArrayList fields = new ArrayList();
0565: Enumeration children = children();
0566: while (children.hasMoreElements()) {
0567: JagBean child = (JagBean) children.nextElement();
0568: if (child instanceof Field
0569: && ((Field) child).isPrimaryKey()) {
0570: fields.add(child);
0571: }
0572: }
0573: return fields;
0574: }
0575:
0576: public List getNonPkFields() {
0577: List nonPkFields = getFields();
0578: nonPkFields.removeAll(getPkFields());
0579: return nonPkFields;
0580: }
0581:
0582: /**
0583: * Gets the fields that are neither primary keys, nor a foreign key involved in a container-managed relation.
0584: *
0585: * @return List
0586: */
0587: public List getNonPkRelationFields() {
0588: List nonPkRelationFields = getNonRelationFields();
0589: nonPkRelationFields.removeAll(getPkFields());
0590: return nonPkRelationFields;
0591: }
0592:
0593: /**
0594: * Iterates over all the table columns and determines whether the table has a single key or a composite key. The
0595: * key is said to be composite when more than one column is part of the key.
0596: *
0597: * @return <code>true</code> only when more the table has a composite key else <code>false</code>
0598: */
0599: public boolean isCompositeKey() {
0600: int countPrimaryKeys = countPrimaryKeyFields();
0601: return countPrimaryKeys > 1;
0602: }
0603:
0604: /**
0605: * Count the number of primary key fields.
0606: *
0607: * @return <code>int</code> with the number of primary keys
0608: */
0609: public int countPrimaryKeyFields() {
0610: Enumeration children = children();
0611: JagBean bean = null;
0612: Field field = null;
0613: int countPrimaryKeys = 0;
0614: while (children.hasMoreElements()) {
0615: bean = (JagBean) children.nextElement();
0616: if (bean instanceof Field) {
0617: field = (Field) bean;
0618: if (field.isPrimaryKey())
0619: countPrimaryKeys++;
0620: }
0621: }
0622: return countPrimaryKeys;
0623: }
0624:
0625: /**
0626: * Returns the class that represents the primary key type
0627: *
0628: * @return the class that represents the primary key type
0629: */
0630: public String getPrimaryKeyClass() {
0631: Enumeration children = children();
0632: JagBean bean = null;
0633: Field field = null;
0634: String getPrimaryKeyClass = null;
0635: while (children.hasMoreElements()) {
0636: bean = (JagBean) children.nextElement();
0637: if (bean instanceof Field) {
0638: field = (Field) bean;
0639: if (field.isPrimaryKey())
0640: getPrimaryKeyClass = field.getType();
0641: }
0642: }
0643: return getPrimaryKeyClass;
0644: }
0645:
0646: /**
0647: * Returns the First primary key field
0648: *
0649: * @return the field name of the first primary key field or an empty string if nothing was found.
0650: */
0651: public String getFirstPrimaryKeyFieldName() {
0652: Enumeration children = children();
0653: JagBean bean = null;
0654: Field field = null;
0655: String getPrimaryKeyFieldName = "";
0656: while (children.hasMoreElements()) {
0657: bean = (JagBean) children.nextElement();
0658: if (bean instanceof Field) {
0659: field = (Field) bean;
0660: if (field.isPrimaryKey())
0661: getPrimaryKeyFieldName = field.getName().toString();
0662: }
0663: }
0664: return getPrimaryKeyFieldName;
0665: }
0666:
0667: public void actionPerformed(ActionEvent e) {
0668: if (e.getActionCommand().equals("GET_PRIMARY_KEY")) {
0669: String key = null;
0670: if (isCompositeKey())
0671: key = rootPackageText.getText() + "."
0672: + nameText.getText() + "PK";
0673: else
0674: key = getPrimaryKeyClass();
0675: pKeyTypeText.setText(key);
0676: }
0677: }
0678:
0679: /**
0680: * Gets the name of the table represented by this Entity.
0681: *
0682: * @return the table name.
0683: */
0684: public TemplateString getLocalTableName() {
0685: return new TemplateString(tableNameText.getText());
0686: }
0687:
0688: public void setTableName(String table) {
0689: tableNameText.setText(table);
0690: }
0691:
0692: public String getTableName() {
0693: return tableNameText.getText();
0694: }
0695:
0696: /**
0697: * While an entity is being created, its constituent relations have to wait for the entity to finish being
0698: * constructed before they can finish initialising themselves. Call this method when the Entity is ready.
0699: */
0700: public void notifyRelationsThatConstructionIsFinished() {
0701: Iterator relations = getRelations().iterator();
0702: while (relations.hasNext()) {
0703: Relation relation = (Relation) relations.next();
0704: relation.notifyLocalEntityIsComplete();
0705: }
0706: }
0707:
0708: /**
0709: * When a field name is changed, call this to tell all relations to update their lists.
0710: *
0711: * @param oldName
0712: * @param text
0713: */
0714: public void notifyRelationsThatFieldNameChanged(String oldName,
0715: String text) {
0716: Iterator relations = getRelations().iterator();
0717: while (relations.hasNext()) {
0718: Relation relation = (Relation) relations.next();
0719: relation.notifyFieldNameChanged(oldName, text);
0720: }
0721:
0722: }
0723:
0724: public String toString() {
0725: return "Entity - " + getRefName();
0726: }
0727:
0728: /**
0729: * Inserts a relation in the correct position (relations are always positioned first
0730: * amongst an Entity's children, because it looks nicer!).
0731: *
0732: * @param relation
0733: */
0734: public void addRelation(Relation relation) {
0735: TreeNode lastRelly = null;
0736: Enumeration kids = children();
0737: while (kids.hasMoreElements()) {
0738: TreeNode kid = (TreeNode) kids.nextElement();
0739: if (kid instanceof Relation) {
0740: lastRelly = kid;
0741: }
0742: }
0743: int insertPos = 0;
0744: if (lastRelly == null) {
0745: insertPos = getIndex(getFirstChild());
0746: } else {
0747: insertPos = getIndex(lastRelly) + 1;
0748: }
0749: insert(relation, insertPos);
0750: }
0751:
0752: /**
0753: * Check if one of the fields needs a sequence.
0754: * If so, return true.
0755: * @return true if a sequence is needed.
0756: */
0757: public boolean isSequenceEntity() {
0758: List fields = getFields();
0759: for (int i = 0; i < getFields().size(); i++) {
0760: Field field = (Field) fields.get(i);
0761: if (field.isSequenceField()) {
0762: return true;
0763: }
0764: }
0765: return false;
0766: }
0767:
0768: /**
0769: * Get a session that contains this Entity.
0770: * @return Session
0771: */
0772: public Session getFirstSession() {
0773: List services = JagGenerator.getObjectsFromTree(Session.class);
0774: for (int i = 0; i < services.size(); i++) {
0775: Session s = (Session) services.get(i);
0776: if (s.getEntities().contains(this )) {
0777: return s;
0778: }
0779: }
0780: System.out.println("No session found for entity:" + getName());
0781: return null;
0782: }
0783:
0784: private boolean warningNeeded() {
0785: if (relationsEnabled != JagGenerator.isRelationsEnabled()) {
0786: relationsEnabled = JagGenerator.isRelationsEnabled();
0787: return !relationsEnabled;
0788: }
0789: return false;
0790: }
0791:
0792: /**
0793: * This method is called from within the constructor to
0794: * initialize the form.
0795: * WARNING: Do NOT modify this code. The content of this method is
0796: * always regenerated by the Form Editor.
0797: */
0798: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
0799: private void initComponents() {
0800: panel = new javax.swing.JPanel();
0801: nameLabel = new javax.swing.JLabel();
0802: tableNameLabel = new javax.swing.JLabel();
0803: pKeyLabel = new javax.swing.JLabel();
0804: pKeyTypeLabel = new javax.swing.JLabel();
0805: desciptionLabel = new javax.swing.JLabel();
0806: rootPackageLabel = new javax.swing.JLabel();
0807: refNameLabel = new javax.swing.JLabel();
0808: nameText = new javax.swing.JTextField();
0809: tableNameText = new javax.swing.JTextField();
0810: pKeyText = new javax.swing.JTextField();
0811: pKeyTypeText = new javax.swing.JTextField();
0812: descriptionText = new javax.swing.JTextField();
0813: rootPackageText = new javax.swing.JTextField();
0814: refNameText = new javax.swing.JTextField();
0815: isCompositeLabel = new javax.swing.JLabel();
0816: isCompositeCombo = new javax.swing.JComboBox();
0817: nameLabel1 = new javax.swing.JLabel();
0818: displayNameText = new javax.swing.JTextField();
0819: isCompositeLabel1 = new javax.swing.JLabel();
0820: isAssociationEntity = new javax.swing.JComboBox();
0821:
0822: panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
0823:
0824: nameLabel
0825: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0826: nameLabel.setText("Name: ");
0827: nameLabel.setToolTipText("Name of the entity");
0828: panel.add(nameLabel,
0829: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0830: 10, 90, -1));
0831:
0832: tableNameLabel
0833: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0834: tableNameLabel.setText("Table name: ");
0835: tableNameLabel
0836: .setToolTipText("Physical table name the entity is mapped to");
0837: panel.add(tableNameLabel,
0838: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0839: 70, 90, -1));
0840:
0841: pKeyLabel
0842: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0843: pKeyLabel.setText("Primary key: ");
0844: pKeyLabel.setToolTipText("Primary key field of the entity");
0845: panel.add(pKeyLabel,
0846: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0847: 100, 90, -1));
0848:
0849: pKeyTypeLabel
0850: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0851: pKeyTypeLabel.setText("Primary key class: ");
0852: pKeyTypeLabel
0853: .setToolTipText("Primary key class in case of a composite key");
0854: panel.add(pKeyTypeLabel,
0855: new org.netbeans.lib.awtextra.AbsoluteConstraints(0,
0856: 130, 110, -1));
0857:
0858: desciptionLabel
0859: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0860: desciptionLabel.setText("Description: ");
0861: desciptionLabel.setToolTipText("Description of the entity");
0862: panel.add(desciptionLabel,
0863: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0864: 220, 90, -1));
0865:
0866: rootPackageLabel
0867: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0868: rootPackageLabel.setText("Root-package: ");
0869: rootPackageLabel.setToolTipText("Root package for the entity");
0870: panel.add(rootPackageLabel,
0871: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0872: 250, 90, -1));
0873:
0874: refNameLabel
0875: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0876: refNameLabel.setText("Ref-name: ");
0877: refNameLabel.setToolTipText("Reference name for the entity");
0878: panel.add(refNameLabel,
0879: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0880: 280, 90, -1));
0881:
0882: nameText.setToolTipText("Name of the entity");
0883: nameText.addActionListener(new java.awt.event.ActionListener() {
0884: public void actionPerformed(java.awt.event.ActionEvent evt) {
0885: nameTextActionPerformed(evt);
0886: }
0887: });
0888: nameText.addFocusListener(new java.awt.event.FocusAdapter() {
0889: public void focusLost(java.awt.event.FocusEvent evt) {
0890: nameTextFocusLost(evt);
0891: }
0892: });
0893:
0894: panel.add(nameText,
0895: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0896: 10, 260, -1));
0897:
0898: tableNameText
0899: .setToolTipText("Physical table name the entity is mapped to");
0900: tableNameText
0901: .addFocusListener(new java.awt.event.FocusAdapter() {
0902: public void focusLost(java.awt.event.FocusEvent evt) {
0903: tableNameTextFocusLost(evt);
0904: }
0905: });
0906:
0907: panel.add(tableNameText,
0908: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0909: 70, 260, -1));
0910:
0911: pKeyText.setToolTipText("Primary key field of the entity");
0912: pKeyText.addFocusListener(new java.awt.event.FocusAdapter() {
0913: public void focusLost(java.awt.event.FocusEvent evt) {
0914: pKeyTextFocusLost(evt);
0915: }
0916: });
0917:
0918: panel.add(pKeyText,
0919: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0920: 100, 260, -1));
0921:
0922: pKeyTypeText
0923: .setToolTipText("Primary key class in case of a composite key");
0924: pKeyTypeText
0925: .addFocusListener(new java.awt.event.FocusAdapter() {
0926: public void focusLost(java.awt.event.FocusEvent evt) {
0927: pKeyTypeTextFocusLost(evt);
0928: }
0929: });
0930:
0931: panel.add(pKeyTypeText,
0932: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0933: 130, 260, -1));
0934:
0935: descriptionText.setToolTipText("Description of the entity");
0936: descriptionText
0937: .addFocusListener(new java.awt.event.FocusAdapter() {
0938: public void focusLost(java.awt.event.FocusEvent evt) {
0939: descriptionTextFocusLost(evt);
0940: }
0941: });
0942:
0943: panel.add(descriptionText,
0944: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0945: 220, 260, -1));
0946:
0947: rootPackageText.setToolTipText("Root package for the entity");
0948: rootPackageText
0949: .addFocusListener(new java.awt.event.FocusAdapter() {
0950: public void focusLost(java.awt.event.FocusEvent evt) {
0951: rootPackageTextFocusLost(evt);
0952: }
0953: });
0954:
0955: panel.add(rootPackageText,
0956: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0957: 250, 260, -1));
0958:
0959: refNameText.setToolTipText("Reference name for the entity");
0960: refNameText.addFocusListener(new java.awt.event.FocusAdapter() {
0961: public void focusLost(java.awt.event.FocusEvent evt) {
0962: refNameTextFocusLost(evt);
0963: }
0964: });
0965: refNameText.addMouseListener(new java.awt.event.MouseAdapter() {
0966: public void mouseReleased(java.awt.event.MouseEvent evt) {
0967: refNameTextMouseReleased(evt);
0968: }
0969: });
0970: refNameText
0971: .addHierarchyListener(new java.awt.event.HierarchyListener() {
0972: public void hierarchyChanged(
0973: java.awt.event.HierarchyEvent evt) {
0974: refNameTextHierarchyChanged(evt);
0975: }
0976: });
0977:
0978: panel.add(refNameText,
0979: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0980: 280, 260, -1));
0981:
0982: isCompositeLabel
0983: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0984: isCompositeLabel.setText("Composite key:");
0985: isCompositeLabel
0986: .setToolTipText("Set to true in case of a composite key");
0987: panel.add(isCompositeLabel,
0988: new org.netbeans.lib.awtextra.AbsoluteConstraints(0,
0989: 190, 110, -1));
0990:
0991: isCompositeCombo.setModel(new javax.swing.DefaultComboBoxModel(
0992: new String[] { "false", "true" }));
0993: isCompositeCombo
0994: .setToolTipText("Set to true in case of a composite key");
0995: isCompositeCombo
0996: .addActionListener(new java.awt.event.ActionListener() {
0997: public void actionPerformed(
0998: java.awt.event.ActionEvent evt) {
0999: isCompositeComboActionPerformed(evt);
1000: }
1001: });
1002:
1003: panel.add(isCompositeCombo,
1004: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
1005: 190, 260, -1));
1006:
1007: nameLabel1
1008: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1009: nameLabel1.setText("Display name: ");
1010: nameLabel1
1011: .setToolTipText("Display name for the entity. Should be one of the entity fields.");
1012: panel.add(nameLabel1,
1013: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
1014: 40, 90, -1));
1015:
1016: displayNameText.setToolTipText("Display name for the entity");
1017: panel.add(displayNameText,
1018: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
1019: 40, 260, -1));
1020:
1021: isCompositeLabel1
1022: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1023: isCompositeLabel1.setText("Association entity:");
1024: isCompositeLabel1
1025: .setToolTipText("Entity used for many to many relations");
1026: panel.add(isCompositeLabel1,
1027: new org.netbeans.lib.awtextra.AbsoluteConstraints(0,
1028: 160, 110, -1));
1029:
1030: isAssociationEntity
1031: .setModel(new javax.swing.DefaultComboBoxModel(
1032: new String[] { "false", "true" }));
1033: isAssociationEntity
1034: .setToolTipText("Entity used for many to many relations");
1035: isAssociationEntity
1036: .addActionListener(new java.awt.event.ActionListener() {
1037: public void actionPerformed(
1038: java.awt.event.ActionEvent evt) {
1039: isAssociationEntityActionPerformed(evt);
1040: }
1041: });
1042:
1043: panel.add(isAssociationEntity,
1044: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
1045: 160, 260, -1));
1046:
1047: }// </editor-fold>//GEN-END:initComponents
1048:
1049: private void isAssociationEntityActionPerformed(
1050: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isAssociationEntityActionPerformed
1051: if ("true".equals(this .isAssociationEntity.getSelectedItem()
1052: .toString())) {
1053: this .pKeyTypeText.setEnabled(false);
1054: this .pKeyText.setEnabled(false);
1055: this .isCompositeCombo.setEnabled(false);
1056: } else {
1057: this .pKeyText.setEnabled(true);
1058: this .pKeyTypeText.setEnabled(true);
1059: this .isCompositeCombo.setEnabled(true);
1060: }
1061: }//GEN-LAST:event_isAssociationEntityActionPerformed
1062:
1063: private void nameTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextActionPerformed
1064: // TODO add your handling code here:
1065: }//GEN-LAST:event_nameTextActionPerformed
1066:
1067: private void refNameTextHierarchyChanged(
1068: java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_refNameTextHierarchyChanged
1069: // TODO add your handling code here:
1070: }//GEN-LAST:event_refNameTextHierarchyChanged
1071:
1072: private void refNameTextMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_refNameTextMouseReleased
1073: // TODO add your handling code here:
1074: }//GEN-LAST:event_refNameTextMouseReleased
1075:
1076: private void refNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_refNameTextFocusLost
1077: System.out.println("Focus was lost for refname");
1078: JagGenerator.stateChanged(true);
1079: }//GEN-LAST:event_refNameTextFocusLost
1080:
1081: private void rootPackageTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rootPackageTextFocusLost
1082: JagGenerator.stateChanged(false);
1083: }//GEN-LAST:event_rootPackageTextFocusLost
1084:
1085: private void descriptionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFocusLost
1086: JagGenerator.stateChanged(false);
1087: }//GEN-LAST:event_descriptionTextFocusLost
1088:
1089: private void isCompositeComboActionPerformed(
1090: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isCompositeComboActionPerformed
1091: JagGenerator.stateChanged(false);
1092: }//GEN-LAST:event_isCompositeComboActionPerformed
1093:
1094: private void pKeyTypeTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_pKeyTypeTextFocusLost
1095: JagGenerator.stateChanged(false);
1096: }//GEN-LAST:event_pKeyTypeTextFocusLost
1097:
1098: private void pKeyTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_pKeyTextFocusLost
1099: JagGenerator.stateChanged(false);
1100: }//GEN-LAST:event_pKeyTextFocusLost
1101:
1102: private void tableNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tableNameTextFocusLost
1103: JagGenerator.stateChanged(false);
1104: JagGenerator.entityHasupdatedTableName(nameText.getText(),
1105: tableNameText.getText());
1106: }//GEN-LAST:event_tableNameTextFocusLost
1107:
1108: private void nameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusLost
1109: // Add your handling code here:
1110: evt = null;
1111: if ((nameText.getText() != null)
1112: && (nameText.getText().length() > 0)) {
1113: nameText.setText(Utils.initCap(nameText.getText()));
1114: if ((refNameText.getText() == null)
1115: || (refNameText.getText().length() == 0)) {
1116: // Only change the refname, if it hasn't been set before.
1117: refNameText.setText(nameText.getText());
1118: }
1119: }
1120: JagGenerator.stateChanged(true);
1121: }//GEN-LAST:event_nameTextFocusLost
1122:
1123: // Variables declaration - do not modify//GEN-BEGIN:variables
1124: private javax.swing.JLabel desciptionLabel;
1125: private javax.swing.JTextField descriptionText;
1126: public javax.swing.JTextField displayNameText;
1127: public javax.swing.JComboBox isAssociationEntity;
1128: public javax.swing.JComboBox isCompositeCombo;
1129: private javax.swing.JLabel isCompositeLabel;
1130: private javax.swing.JLabel isCompositeLabel1;
1131: private javax.swing.JLabel nameLabel;
1132: private javax.swing.JLabel nameLabel1;
1133: public javax.swing.JTextField nameText;
1134: private javax.swing.JLabel pKeyLabel;
1135: public javax.swing.JTextField pKeyText;
1136: private javax.swing.JLabel pKeyTypeLabel;
1137: public javax.swing.JTextField pKeyTypeText;
1138: private javax.swing.JPanel panel;
1139: private javax.swing.JLabel refNameLabel;
1140: private javax.swing.JTextField refNameText;
1141: private javax.swing.JLabel rootPackageLabel;
1142: public javax.swing.JTextField rootPackageText;
1143: private javax.swing.JLabel tableNameLabel;
1144: private javax.swing.JTextField tableNameText;
1145: // End of variables declaration//GEN-END:variables
1146: }
|