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.Column;
0022: import com.finalist.jaggenerator.JagGenerator;
0023: import com.finalist.jaggenerator.Utils;
0024: import com.finalist.jaggenerator.validation.StrutsValidation;
0025: import org.w3c.dom.Document;
0026: import org.w3c.dom.Element;
0027: import org.w3c.dom.NodeList;
0028:
0029: import javax.swing.*;
0030: import javax.swing.tree.DefaultMutableTreeNode;
0031: import java.util.ArrayList;
0032: import java.util.HashSet;
0033: import java.util.Iterator;
0034: import java.util.List;
0035:
0036: /**
0037: * @author hillie
0038: */
0039: public class Field extends DefaultMutableTreeNode implements JagBean {
0040: private StrutsValidation validations = new StrutsValidation();
0041: private Relation relation;
0042: private Entity parentEntity;
0043: private boolean previousRequiredState;
0044: private String oldName;
0045: private static final ArrayList jdbcTypes = new ArrayList();
0046: private boolean nullable = true;
0047:
0048: /**
0049: * Static set of classes for which the UniqueIdGenerator is able to generate primary keys.
0050: */
0051: private final static HashSet autogeneratablePrimaryKeyClasses = new HashSet();
0052:
0053: static {
0054: autogeneratablePrimaryKeyClasses.add("java.lang.Byte");
0055: autogeneratablePrimaryKeyClasses.add("java.lang.Double");
0056: autogeneratablePrimaryKeyClasses.add("java.lang.Integer");
0057: autogeneratablePrimaryKeyClasses.add("java.lang.Long");
0058: autogeneratablePrimaryKeyClasses.add("java.lang.Short");
0059: autogeneratablePrimaryKeyClasses.add("java.lang.String");
0060:
0061: java.lang.reflect.Field[] fields = java.sql.Types.class
0062: .getDeclaredFields();
0063: for (int i = 0; i < fields.length; i++) {
0064: jdbcTypes.add(fields[i].getName());
0065: }
0066: }
0067:
0068: /**
0069: * Creates new form BeanForm
0070: */
0071: public Field(Entity parent, Column column) {
0072: parentEntity = parent;
0073: try {
0074: init();
0075: String name = Utils.format(column.getName());
0076: nullable = column.isNullable();
0077: nameText.setText(name);
0078: oldName = name;
0079: typeText.setText(getType(column));
0080: columnNameText.setText(column.getName());
0081: sqlTypeText.setText(getSqlType(column));
0082: jdbcTypeComboBox.setSelectedItem(getJdbcType(column));
0083: if (column.isPrimaryKey()) {
0084: setPrimaryKey(column.isPrimaryKey());
0085: }
0086: if (primaryKeyCheckBox.isSelected()) {
0087: autoGeneratedCheckBox
0088: .setSelected(isPkClassIsAutogeneratable());
0089: }
0090: requiredCheckBox.setSelected(!nullable);
0091: regenerateValidations();
0092:
0093: } catch (Exception e) {
0094: e.printStackTrace();
0095: }
0096: }
0097:
0098: /**
0099: * Use for building up the entity field gui component
0100: */
0101: public Field(Entity parent, Element el) {
0102: parentEntity = parent;
0103: try {
0104: init();
0105: NodeList nl = el.getElementsByTagName("module-data");
0106:
0107: for (int i = 0; i < nl.getLength(); i++) {
0108: Element child = (Element) nl.item(i);
0109: String attName = child.getAttribute("name");
0110: String value = null;
0111: if (child.getFirstChild() != null)
0112: value = child.getFirstChild().getNodeValue();
0113: if (value != null) {
0114: if (attName.equalsIgnoreCase("name")) {
0115: nameText.setText(value);
0116: oldName = value;
0117: continue;
0118: }
0119: if (attName.equalsIgnoreCase("type")) {
0120: typeText.setText(value);
0121: continue;
0122: }
0123: if (attName.equalsIgnoreCase("column-name")) {
0124: columnNameText.setText(value);
0125: continue;
0126: }
0127: if (attName.equalsIgnoreCase("required")) {
0128: boolean required = "true"
0129: .equalsIgnoreCase(value);
0130: nullable = !required;
0131: requiredCheckBox.setSelected(required);
0132: continue;
0133: }
0134: if (attName.equalsIgnoreCase("sql-type")) {
0135: sqlTypeText.setText(value);
0136: continue;
0137: }
0138: if (attName.equalsIgnoreCase("jdbc-type")) {
0139: jdbcTypeComboBox.setSelectedItem(value);
0140: continue;
0141: }
0142: if (attName.equalsIgnoreCase("primary-key")) {
0143: // Don't use the setPrimaryKey method for non-primary keys,
0144: // since it will overrule the required and auto-primary key settings.
0145: if ("true".equalsIgnoreCase(value)) {
0146: setPrimaryKey(true);
0147: } else {
0148: primaryKeyCheckBox.setSelected(false);
0149: }
0150: continue;
0151: }
0152: if (attName.equalsIgnoreCase("auto-primary-key")) {
0153: autoGeneratedCheckBox.setSelected("true"
0154: .equalsIgnoreCase(value)
0155: && isPkClassIsAutogeneratable());
0156: continue;
0157: }
0158: if (attName.equalsIgnoreCase("foreign-key")) {
0159: setForeignKey("true".equalsIgnoreCase(value
0160: .trim()));
0161: continue;
0162: }
0163: if (attName.equalsIgnoreCase("validation-depends")) {
0164: if (value != null) {
0165: validations.setDependsList(value);
0166: validationDependsText.setText(value);
0167: } else {
0168: validationDependsText.setText("");
0169: }
0170: continue;
0171: }
0172: if (attName.equalsIgnoreCase("validation-xml")) {
0173: if (value != null) {
0174: validations.setXml(value);
0175: validationXMLTextArea.setText(value);
0176: } else {
0177: validationXMLTextArea.setText("");
0178: }
0179: continue;
0180: }
0181: }
0182: }
0183:
0184: } catch (Exception e) {
0185: e.printStackTrace();
0186: }
0187: }
0188:
0189: public String toString() {
0190: return nameText.getText();
0191: }
0192:
0193: public JPanel getPanel() {
0194: return panel;
0195: }
0196:
0197: public void getXML(Element el) {
0198: Document doc = el.getOwnerDocument();
0199: Element module = doc.createElement("module-data");
0200: module.setAttribute("name", "field");
0201:
0202: Element name = doc.createElement("module-data");
0203: name.setAttribute("name", "name");
0204: if (nameText.getText() != null) {
0205: if (nameText.getText() != null) {
0206: name
0207: .appendChild(doc.createTextNode(nameText
0208: .getText()));
0209: }
0210: }
0211: module.appendChild(name);
0212:
0213: Element type = doc.createElement("module-data");
0214: type.setAttribute("name", "type");
0215: if (typeText.getText() != null) {
0216: type.appendChild(doc.createTextNode(typeText.getText()));
0217: }
0218: module.appendChild(type);
0219:
0220: Element columnName = doc.createElement("module-data");
0221: columnName.setAttribute("name", "column-name");
0222: if (columnNameText.getText() != null) {
0223: columnName.appendChild(doc.createTextNode(columnNameText
0224: .getText()));
0225: }
0226: module.appendChild(columnName);
0227:
0228: Element required = doc.createElement("module-data");
0229: required.setAttribute("name", "required");
0230: required.appendChild(doc.createTextNode(Boolean
0231: .toString(requiredCheckBox.isSelected())));
0232: module.appendChild(required);
0233:
0234: Element sqlType = doc.createElement("module-data");
0235: sqlType.setAttribute("name", "sql-type");
0236: if (sqlTypeText.getText() != null) {
0237: sqlType.appendChild(doc.createTextNode(sqlTypeText
0238: .getText()));
0239: }
0240: module.appendChild(sqlType);
0241:
0242: Element jdbcType = doc.createElement("module-data");
0243: jdbcType.setAttribute("name", "jdbc-type");
0244: jdbcType.appendChild(doc.createTextNode(jdbcTypeComboBox
0245: .getSelectedItem().toString()));
0246: module.appendChild(jdbcType);
0247:
0248: Element primaryKey = doc.createElement("module-data");
0249: primaryKey.setAttribute("name", "primary-key");
0250: primaryKey.appendChild(doc.createTextNode(Boolean
0251: .toString(primaryKeyCheckBox.isSelected())));
0252: module.appendChild(primaryKey);
0253:
0254: Element autoPrimary = doc.createElement("module-data");
0255: autoPrimary.setAttribute("name", "auto-primary-key");
0256: autoPrimary.appendChild(doc.createTextNode(Boolean
0257: .toString(autoGeneratedCheckBox.isSelected())));
0258: module.appendChild(autoPrimary);
0259:
0260: Element foreignKey = doc.createElement("module-data");
0261: foreignKey.setAttribute("name", "foreign-key");
0262: foreignKey.appendChild(doc.createTextNode("" + isForeignKey()));
0263: module.appendChild(foreignKey);
0264:
0265: Element validationDepends = doc.createElement("module-data");
0266: validationDepends.setAttribute("name", "validation-depends");
0267: if (validationDependsText.getText() != null) {
0268: validationDepends.appendChild(doc
0269: .createTextNode(validationDependsText.getText()));
0270: }
0271: module.appendChild(validationDepends);
0272:
0273: Element validationXml = doc.createElement("module-data");
0274: validationXml.setAttribute("name", "validation-xml");
0275: if (validationXMLTextArea.getText() != null) {
0276: validationXml.appendChild(doc
0277: .createTextNode(validationXMLTextArea.getText()));
0278: }
0279: module.appendChild(validationXml);
0280:
0281: el.appendChild(module);
0282: }
0283:
0284: public TemplateString getSqlType() {
0285: return new TemplateString(sqlTypeText.getText());
0286: }
0287:
0288: public void setSqlType(String text) {
0289: sqlTypeText.setText(text);
0290: }
0291:
0292: public TemplateString getJdbcType() {
0293: return new TemplateString(jdbcTypeComboBox.getSelectedItem()
0294: .toString());
0295: }
0296:
0297: public void setJdbcType(String text) {
0298: jdbcTypeComboBox.setSelectedItem(text);
0299: }
0300:
0301: public TemplateString getPrimaryKey() {
0302: return new TemplateString(Boolean.toString(primaryKeyCheckBox
0303: .isSelected()));
0304: }
0305:
0306: public TemplateString getForeignKey() {
0307: return new TemplateString(Boolean.toString(isForeignKey()));
0308: }
0309:
0310: public String getValidationDepends() {
0311: if (validationDependsText.getText() == null) {
0312: return "";
0313: } else {
0314: return validationDependsText.getText();
0315: }
0316: }
0317:
0318: public String getValidationXml() {
0319: return validationXMLTextArea.getText();
0320: }
0321:
0322: /**
0323: * Check if the field is a date field.
0324: *
0325: * @return true if the field is a date field
0326: */
0327: public String isDate() {
0328: if ("java.sql.Date".equals(getType())) {
0329: return "true";
0330: }
0331: if ("java.util.Date".equals(getType())) {
0332: return "true";
0333: }
0334: return "false";
0335: }
0336:
0337: /**
0338: * Check if the field is a time field.
0339: *
0340: * @return true if the field is a time field
0341: */
0342: public String isTime() {
0343: if ("java.sql.Time".equals(getType())) {
0344: return "true";
0345: }
0346: if ("java.sql.Timestamp".equals(getType())) {
0347: return "true";
0348: }
0349: return "false";
0350: }
0351:
0352: public String getType(Column column) {
0353: if (column.getSqlType() == null) {
0354: return "";
0355: }
0356: String sqlType = column.getSqlType().toUpperCase();
0357: int scale = column.getScale();
0358: int precision = column.getPrecision();
0359: if (sqlType == null)
0360: return "";
0361: if (sqlType.equals("DATE"))
0362: return "java.sql.Date";
0363: if (sqlType.equals("BOOL"))
0364: return "java.lang.Boolean";
0365: if (sqlType.equals("FLOAT"))
0366: return "java.lang.Float";
0367: if (sqlType.equals("DOUBLE"))
0368: return "java.lang.Double";
0369: if (sqlType.equals("FLOAT(7)"))
0370: return "java.lang.Float";
0371: if (sqlType.equals("FLOAT8"))
0372: return "java.lang.Double";
0373: if (contains(sqlType, "NUMERIC")
0374: || contains(sqlType, "NUMERIC"))
0375: return "java.math.BigDecimal";
0376: if (sqlType.equals("BYTEA")) {
0377: System.out
0378: .println("Mapping the BYTEA binary type to java.sql.Blob. JAG has no support for binary fields.");
0379: return "java.sql.Blob";
0380: }
0381: if ((sqlType.indexOf("TIMESTAMP") != -1)
0382: || (sqlType.indexOf("DATETIME") != -1))
0383: return "java.sql.Timestamp";
0384: if (sqlType.equals("TIME"))
0385: return "java.sql.Time";
0386: if (contains(sqlType, "TINYINT"))
0387: return "java.lang.Byte";
0388: if (contains(sqlType, "SMALLINT"))
0389: return "java.lang.Short";
0390: if (contains(sqlType, "BIGINT"))
0391: return "java.lang.Long";
0392: if (contains(sqlType, "DECIMAL"))
0393: return "java.math.BigDecimal";
0394: if (contains(sqlType, "BLOB"))
0395: return "java.sql.Blob";
0396: if (contains(sqlType, "SERIAL"))
0397: return "java.lang.Long";
0398: if (contains(sqlType, "IDENTITY"))
0399: return "java.lang.Long";
0400: if (sqlType.equals("NUMBER") || sqlType.equals("INT")
0401: || sqlType.equals("YEAR")
0402: || sqlType.indexOf("INT") > -1) {
0403: if (scale == 0) {
0404: if (precision == 0) {
0405: // this is the case for pseudo columns
0406: // like sequences, count(*) etc.
0407: // by convention, let's convert them to Integer
0408: return "java.lang.Integer";
0409: }
0410: if (precision <= 2) {
0411: //let it be a byte
0412: //
0413: return "java.lang.Integer";
0414: }
0415: if (precision <= 5) {
0416: return "java.lang.Integer";
0417: }
0418: if (precision <= 9) {
0419: //let it be an int
0420: return "java.lang.Integer";
0421: }
0422: if (precision <= 18) {
0423: if (sqlType.indexOf("INT") != -1) {
0424: return "java.lang.Integer";
0425: }
0426: //let it be a long
0427: return "java.lang.Long";
0428: } else {
0429: return "java.math.BigDecimal";
0430: }
0431: }
0432: if (precision + scale <= 12) {
0433: //return "java.lang.Float";
0434: return "java.math.BigDecimal";
0435: }
0436: if (precision + scale <= 64) {
0437: return "java.lang.Double";
0438: } else {
0439: return "java.math.BigDecimal";
0440: }
0441: }
0442:
0443: if (sqlType.indexOf("CHAR") > -1) {
0444: return "java.lang.String";
0445: }
0446: if (sqlType.indexOf("TEXT") > -1) {
0447: return "java.lang.String";
0448: }
0449: System.out.println("unknown sql type: " + sqlType
0450: + " Map it to a String.");
0451: return "java.lang.String";
0452: }
0453:
0454: public String getJdbcType(Column column) {
0455: if (column.getSqlType() == null) {
0456: return "";
0457: }
0458: String sqlType = column.getSqlType().toUpperCase();
0459: int scale = column.getScale();
0460: int precision = column.getPrecision();
0461: if (sqlType == null)
0462: return "";
0463:
0464: if (sqlType.equals("NUMERIC") || sqlType.equals("TINYINT")
0465: || sqlType.equals("SMALLINT")
0466: || sqlType.equals("DOUBLE")
0467: || sqlType.equals("TIMESTAMP")
0468: || sqlType.equals("FLOAT") || sqlType.equals("DATE")
0469: || sqlType.equals("TIME")
0470: || contains(sqlType, "BIGINT")
0471: || sqlType.equals("DECIMAL"))
0472: return sqlType;
0473:
0474: if (contains(sqlType, "CHAR")) {
0475: return "VARCHAR";
0476: }
0477: if (contains(sqlType, "TEXT")) {
0478: return "VARCHAR";
0479: }
0480: if (sqlType.equals("DATETIME"))
0481: return "TIMESTAMP";
0482: // Postgress specific:
0483: if (sqlType.equals("FLOAT(7)"))
0484: return "FLOAT";
0485: // Postgress specific:
0486: if (sqlType.equals("FLOAT8"))
0487: return "DOUBLE";
0488: if (sqlType.equals("BYTEA"))
0489: return "VARBINARY";
0490: if (contains(sqlType, "BLOB")) {
0491: return "BLOB";
0492: }
0493: if (contains(sqlType, "SERIAL"))
0494: return "INTEGER";
0495: if (contains(sqlType, "IDENTITY"))
0496: return "INTEGER";
0497: if (sqlType.equals("NUMBER") || contains(sqlType, "INT")
0498: || sqlType.equals("YEAR")) {
0499: if (scale == 0) {
0500: if (precision == 0) {
0501: // this is the case for pseudo columns
0502: // like sequences, count(*) etc.
0503: // by convention, let's convert them to Integer
0504: return "INTEGER";
0505: }
0506: if (precision <= 5) {
0507: //let it be a byte
0508: return "INTEGER";
0509: }
0510: if (precision <= 9) {
0511: //let it be an int
0512: return "INTEGER";
0513: }
0514: if (precision <= 18) {
0515: if (sqlType.indexOf("INT") != -1) {
0516: return "INTEGER";
0517: }
0518: //let it be a long
0519: return "BIGINT";
0520: }
0521: }
0522: if (precision + scale <= 12) {
0523: return "DECIMAL";
0524: }
0525: if (precision + scale <= 64) {
0526: return "DOUBLE";
0527: }
0528: }
0529:
0530: return "JAVA_OBJECT";
0531: }
0532:
0533: public String getSqlType(Column column) {
0534: if (column.getSqlType() == null) {
0535: return "";
0536: }
0537: String sqlType = column.getSqlType().toUpperCase();
0538: int scale = column.getScale();
0539: int precision = column.getPrecision();
0540: int length = column.getLength();
0541: if (sqlType == null)
0542: return "";
0543: if (sqlType.equals("NUMBER") || sqlType.equals("DOUBLE")
0544: || contains(sqlType, "INT") || sqlType.equals("YEAR")
0545: || sqlType.equals("FLOAT") || sqlType.equals("DECIMAL")) {
0546: if (precision > 0) {
0547: sqlType = sqlType + "(" + precision;
0548: if (scale != 0) {
0549: sqlType = sqlType + ", " + scale;
0550: }
0551: sqlType = sqlType + ")";
0552: }
0553: return sqlType;
0554: }
0555: if (sqlType.indexOf("CHAR") > -1) {
0556: if (length > 0) {
0557: return sqlType + "(" + length + ")";
0558: }
0559: }
0560: return sqlType;
0561: }
0562:
0563: /**
0564: * Helper method to determine the max size of a field.
0565: * -1 will be returned fields without a size specification.
0566: *
0567: * @return max size of a field.
0568: */
0569: public int getMaxSize() {
0570: String jdbcType = getJdbcType().toString();
0571: int size;
0572: if ("VARCHAR".equals(jdbcType) || "CHAR".equals(jdbcType)
0573: || "LONGVARCHAR".equals(jdbcType)) {
0574: size = -1;
0575: } else {
0576: return -1;
0577: }
0578: String sqlType = getSqlType().toString();
0579: if (sqlType == null) {
0580: return size;
0581: }
0582: int beginIndex = sqlType.indexOf("(");
0583: int endIndex = sqlType.indexOf(",");
0584: if (endIndex == -1) {
0585: endIndex = sqlType.indexOf(")");
0586: }
0587: if (beginIndex == -1 || endIndex == -1) {
0588: return size;
0589: }
0590: String strSize = sqlType.substring(beginIndex + 1, endIndex);
0591: try {
0592: size = Integer.parseInt(strSize);
0593: return size;
0594: } catch (Exception e) {
0595: return size;
0596: }
0597: }
0598:
0599: public String getRefName() {
0600: return null;
0601: }
0602:
0603: /**
0604: * Return true if the field is a numeric field,
0605: * like Integer, Short etc..
0606: *
0607: * @return true if numeric
0608: */
0609: public boolean isNumeric() {
0610: return ("java.lang.Integer".equals(getType()))
0611: || ("java.lang.Byte".equals(getType()))
0612: || ("java.lang.Long".equals(getType()))
0613: || ("java.lang.Short".equals(getType()));
0614: }
0615:
0616: /**
0617: * Determine if the field can be used with a sequence.
0618: * For this the following must apply:
0619: * - the field needs to be a primary key
0620: * - the autogenerate primary key checkbox should be on.
0621: * - the database should be oracle or postgresql with a Serial field.
0622: *
0623: * @return true if sequence is allowed
0624: */
0625: public boolean isSequenceField() {
0626: if (isPrimaryKey() && getHasAutoGenPrimaryKey()) {
0627: Datasource d = getDatasource();
0628: String mapping = d.getTypeMapping().getLower();
0629: if (mapping.startsWith("mysql")) {
0630: return false;
0631: }
0632: // For Oracle there is no autoincrement field. Always use true.
0633: if (mapping.startsWith("oracle")) {
0634: return true;
0635: }
0636: if (mapping.startsWith("postgresql")) {
0637: // Postgresql supporst sequence, but if the field is a Serial field, we don't want to use them.
0638: if (getSqlType().getLower().startsWith("serial")) {
0639: return false;
0640: } else {
0641: return true;
0642: }
0643: }
0644: if (mapping.startsWith("hypersonic")) {
0645: if (getSqlType().getLower().startsWith("identity")) {
0646: return false;
0647: } else {
0648: return true;
0649: }
0650: }
0651: }
0652: // By default, return false.
0653: return false;
0654: }
0655:
0656: private Config getConfig() {
0657: List services = JagGenerator.getObjectsFromTree(Config.class);
0658: for (int i = 0; i < services.size(); i++) {
0659: Config c = (Config) services.get(i);
0660: return c;
0661: }
0662: return null;
0663: }
0664:
0665: private Datasource getDatasource() {
0666: List services = JagGenerator
0667: .getObjectsFromTree(Datasource.class);
0668: for (int i = 0; i < services.size(); i++) {
0669: Datasource d = (Datasource) services.get(i);
0670: return d;
0671: }
0672: return null;
0673: }
0674:
0675: /**
0676: * Indicates whether the field is part of the primary key.
0677: *
0678: * @return <code>true</code> if the field is part of the primary key
0679: */
0680: public boolean isPrimaryKey() {
0681: return primaryKeyCheckBox.isSelected();
0682: }
0683:
0684: public boolean getHasAutoGenPrimaryKey() {
0685: return autoGeneratedCheckBox.isSelected();
0686: }
0687:
0688: public void setHasAutoGenPrimaryKey(boolean value) {
0689: autoGeneratedCheckBox.setSelected(value);
0690: }
0691:
0692: /**
0693: * Setter for this field, which specifies whether or not this Field is a foreign key field within its table.
0694: *
0695: * @param foreignKey
0696: */
0697: public void setForeignKey(boolean foreignKey) {
0698: foreignKeyCheckBox.setSelected(foreignKey);
0699: }
0700:
0701: public boolean isForeignKey() {
0702: return foreignKeyCheckBox.isSelected();
0703: }
0704:
0705: /**
0706: * Returns the class that represents the primary key type
0707: *
0708: * @return the class that represents the primary key type
0709: */
0710: public String getType() {
0711: return typeText.getText();
0712: }
0713:
0714: /**
0715: * Set the field type
0716: *
0717: * @param text type.
0718: */
0719: public void setType(String text) {
0720: typeText.setText(text);
0721: }
0722:
0723: /**
0724: * Returns the name of the field
0725: *
0726: * @return the name of the field.
0727: */
0728: public TemplateString getName() {
0729: return new TemplateString(nameText.getText());
0730: }
0731:
0732: /**
0733: * Sets the name of the field
0734: *
0735: * @param name the name of the field.
0736: */
0737: public void setName(String name) {
0738: oldName = nameText.getText();
0739: nameText.setText(name);
0740: }
0741:
0742: public String getColumnName() {
0743: return columnNameText.getText();
0744: }
0745:
0746: /**
0747: * Gets the relation, if this field represents a container-managed relation field in its parent entity bean.
0748: *
0749: * @return the Relation object, or <code>null</code> if this field doesn't represent a relation.
0750: */
0751: public Relation getRelation() {
0752: return relation;
0753: }
0754:
0755: public void setRelation(Relation relation) {
0756: this .relation = relation;
0757: }
0758:
0759: public boolean isRelation() {
0760: return relation != null;
0761: }
0762:
0763: /**
0764: * Make the nullable field available.
0765: *
0766: * @return true if the field is nullable.
0767: */
0768: public boolean isNullable() {
0769: return nullable;
0770: }
0771:
0772: /**
0773: * Return true if the field is required, so nullable is true.
0774: *
0775: * @return true if the field is required.
0776: */
0777: public boolean isRequired() {
0778: return !nullable;
0779: }
0780:
0781: /**
0782: * Return the size of a sql type in case it is a string type. Otherwise return null.
0783: *
0784: * @return get the size.
0785: */
0786: public String getSize() {
0787: TemplateString theType = getSqlType();
0788: if (theType == null) {
0789: return null;
0790: }
0791: List parameters = StrutsValidation.getParams(getSqlType()
0792: .toString());
0793: if (getSqlType().toString().indexOf("CHAR") != -1
0794: && !parameters.isEmpty()) {
0795: return (String) parameters.get(0);
0796: }
0797: return null;
0798: }
0799:
0800: /**
0801: * Regenerates the Struts validations for this field.
0802: */
0803: public void regenerateValidations() {
0804: validations = new StrutsValidation(sqlTypeText.getText(),
0805: jdbcTypeComboBox.getSelectedItem().toString(),
0806: enforceRequiredValidation());
0807: if (validations.getDependsList() != null) {
0808: validationDependsText.setText(validations.getDependsList());
0809: } else {
0810: validationDependsText.setText("");
0811: }
0812: if (validations.getXml() != null) {
0813: validationXMLTextArea.setText(validations.getXml());
0814: } else {
0815: validationXMLTextArea.setText("");
0816: }
0817: }
0818:
0819: private boolean enforceRequiredValidation() {
0820: //primary key fields are always 'required' (can't be null), but in the webapp we only validate this
0821: //if the pk field isn't auto-generated.
0822: return getHasAutoGenPrimaryKey() ? false : requiredCheckBox
0823: .isSelected();
0824: }
0825:
0826: /**
0827: * A heap of things happen when you set a field as primary key - keep them all here!
0828: *
0829: * @param value
0830: */
0831: public void setPrimaryKey(boolean value) {
0832: primaryKeyCheckBox.setSelected(value);
0833: autoGeneratedCheckBox.setEnabled(value);
0834: requiredCheckBox.setEnabled(!value);
0835: if (value) {
0836: previousRequiredState = requiredCheckBox.isSelected();
0837: requiredCheckBox.setSelected(true);
0838: } else {
0839: requiredCheckBox.setSelected(previousRequiredState);
0840: }
0841: }
0842:
0843: public boolean isPkClassIsAutogeneratable() {
0844: String primaryKeyClass = typeText.getText();
0845: return autogeneratablePrimaryKeyClasses
0846: .contains(primaryKeyClass);
0847: }
0848:
0849: private void init() {
0850: initComponents();
0851: Iterator types = jdbcTypes.iterator();
0852: while (types.hasNext()) {
0853: String jdbcType = (String) types.next();
0854: jdbcTypeComboBox.addItem(jdbcType);
0855: }
0856: }
0857:
0858: /**
0859: * This method is called from within the constructor to
0860: * initialize the form.
0861: * WARNING: Do NOT modify this code. The content of this method is
0862: * always regenerated by the Form Editor.
0863: */
0864: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
0865: private void initComponents() {
0866: panel = new javax.swing.JPanel();
0867: nameLabel = new javax.swing.JLabel();
0868: typeLabel = new javax.swing.JLabel();
0869: columnNameLabel = new javax.swing.JLabel();
0870: sqlTypeLabel = new javax.swing.JLabel();
0871: jdbcTypeLabel = new javax.swing.JLabel();
0872: nameText = new javax.swing.JTextField();
0873: typeText = new javax.swing.JTextField();
0874: columnNameText = new javax.swing.JTextField();
0875: sqlTypeText = new javax.swing.JTextField();
0876: jPanel1 = new javax.swing.JPanel();
0877: validationDependsText = new javax.swing.JTextField();
0878: validationXMLScrollPane = new javax.swing.JScrollPane();
0879: validationXMLTextArea = new javax.swing.JTextArea();
0880: validationXMLLabel = new javax.swing.JLabel();
0881: validationDependsLabel = new javax.swing.JLabel();
0882: regenerateButton = new javax.swing.JButton();
0883: jdbcTypeComboBox = new javax.swing.JComboBox();
0884: checkboxPanel2 = new javax.swing.JPanel();
0885: autoGeneratedCheckBox = new javax.swing.JCheckBox();
0886: requiredCheckBox = new javax.swing.JCheckBox();
0887: checkboxPanel1 = new javax.swing.JPanel();
0888: primaryKeyCheckBox = new javax.swing.JCheckBox();
0889: foreignKeyCheckBox = new javax.swing.JCheckBox();
0890:
0891: panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
0892:
0893: nameLabel
0894: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0895: nameLabel.setText("Name: ");
0896: panel.add(nameLabel,
0897: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0898: 10, 90, -1));
0899:
0900: typeLabel
0901: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0902: typeLabel.setText("Type: ");
0903: panel.add(typeLabel,
0904: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0905: 40, 90, -1));
0906:
0907: columnNameLabel
0908: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0909: columnNameLabel.setText("Column name: ");
0910: panel.add(columnNameLabel,
0911: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0912: 70, 90, -1));
0913:
0914: sqlTypeLabel
0915: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0916: sqlTypeLabel.setText("SQL-type: ");
0917: panel.add(sqlTypeLabel,
0918: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0919: 100, 90, -1));
0920:
0921: jdbcTypeLabel
0922: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
0923: jdbcTypeLabel.setText("JDBC-type: ");
0924: panel.add(jdbcTypeLabel,
0925: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
0926: 130, 90, -1));
0927:
0928: nameText.addFocusListener(new java.awt.event.FocusAdapter() {
0929: public void focusLost(java.awt.event.FocusEvent evt) {
0930: nameTextFocusLost(evt);
0931: }
0932: });
0933:
0934: panel.add(nameText,
0935: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0936: 10, 260, -1));
0937:
0938: typeText.addFocusListener(new java.awt.event.FocusAdapter() {
0939: public void focusLost(java.awt.event.FocusEvent evt) {
0940: typeTextFocusLost(evt);
0941: }
0942: });
0943:
0944: panel.add(typeText,
0945: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0946: 40, 260, -1));
0947:
0948: columnNameText
0949: .addFocusListener(new java.awt.event.FocusAdapter() {
0950: public void focusLost(java.awt.event.FocusEvent evt) {
0951: columnNameTextFocusLost(evt);
0952: }
0953: });
0954:
0955: panel.add(columnNameText,
0956: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0957: 70, 260, -1));
0958:
0959: sqlTypeText.addFocusListener(new java.awt.event.FocusAdapter() {
0960: public void focusLost(java.awt.event.FocusEvent evt) {
0961: sqlTypeTextFocusLost(evt);
0962: }
0963: });
0964:
0965: panel.add(sqlTypeText,
0966: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
0967: 100, 260, -1));
0968:
0969: jPanel1
0970: .setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
0971:
0972: jPanel1.setBorder(new javax.swing.border.TitledBorder(
0973: "Struts validations:"));
0974: validationDependsText.setColumns(46);
0975: validationDependsText
0976: .addFocusListener(new java.awt.event.FocusAdapter() {
0977: public void focusLost(java.awt.event.FocusEvent evt) {
0978: validationDependsTextFocusLost(evt);
0979: }
0980: });
0981:
0982: jPanel1.add(validationDependsText,
0983: new org.netbeans.lib.awtextra.AbsoluteConstraints(90,
0984: 30, 290, -1));
0985:
0986: validationXMLTextArea.setColumns(48);
0987: validationXMLTextArea.setFont(new java.awt.Font(
0988: "Lucida Console", 0, 10));
0989: validationXMLTextArea.setRows(6);
0990: validationXMLTextArea
0991: .addFocusListener(new java.awt.event.FocusAdapter() {
0992: public void focusLost(java.awt.event.FocusEvent evt) {
0993: validationXMLTextAreaFocusLost(evt);
0994: }
0995: });
0996:
0997: validationXMLScrollPane.setViewportView(validationXMLTextArea);
0998:
0999: jPanel1.add(validationXMLScrollPane,
1000: new org.netbeans.lib.awtextra.AbsoluteConstraints(90,
1001: 59, -1, 90));
1002:
1003: validationXMLLabel
1004: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1005: validationXMLLabel.setText("Validation XML: ");
1006: jPanel1.add(validationXMLLabel,
1007: new org.netbeans.lib.awtextra.AbsoluteConstraints(-20,
1008: 60, 110, 20));
1009:
1010: validationDependsLabel
1011: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1012: validationDependsLabel.setText("Validations: ");
1013: jPanel1.add(validationDependsLabel,
1014: new org.netbeans.lib.awtextra.AbsoluteConstraints(-20,
1015: 30, 110, -1));
1016:
1017: regenerateButton.setText("Regenerate validations");
1018: regenerateButton.setEnabled(false);
1019: regenerateButton
1020: .addActionListener(new java.awt.event.ActionListener() {
1021: public void actionPerformed(
1022: java.awt.event.ActionEvent evt) {
1023: regenerateButtonActionPerformed(evt);
1024: }
1025: });
1026:
1027: jPanel1.add(regenerateButton,
1028: new org.netbeans.lib.awtextra.AbsoluteConstraints(240,
1029: 170, -1, -1));
1030:
1031: panel.add(jPanel1,
1032: new org.netbeans.lib.awtextra.AbsoluteConstraints(30,
1033: 240, 400, 200));
1034:
1035: jdbcTypeComboBox
1036: .addActionListener(new java.awt.event.ActionListener() {
1037: public void actionPerformed(
1038: java.awt.event.ActionEvent evt) {
1039: jdbcTypeComboBoxActionPerformed(evt);
1040: }
1041: });
1042:
1043: panel.add(jdbcTypeComboBox,
1044: new org.netbeans.lib.awtextra.AbsoluteConstraints(120,
1045: 130, 260, -1));
1046:
1047: checkboxPanel2.setLayout(new java.awt.BorderLayout());
1048:
1049: autoGeneratedCheckBox.setText("Auto-generated primary key:");
1050: autoGeneratedCheckBox.setEnabled(false);
1051: autoGeneratedCheckBox
1052: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1053: autoGeneratedCheckBox
1054: .setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
1055: autoGeneratedCheckBox
1056: .addActionListener(new java.awt.event.ActionListener() {
1057: public void actionPerformed(
1058: java.awt.event.ActionEvent evt) {
1059: autoGeneratedCheckBoxActionPerformed(evt);
1060: }
1061: });
1062:
1063: checkboxPanel2.add(autoGeneratedCheckBox,
1064: java.awt.BorderLayout.SOUTH);
1065:
1066: requiredCheckBox.setText("Required (not nullable):");
1067: requiredCheckBox
1068: .setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
1069: requiredCheckBox
1070: .setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
1071: requiredCheckBox
1072: .setMaximumSize(new java.awt.Dimension(186, 24));
1073: requiredCheckBox
1074: .setMinimumSize(new java.awt.Dimension(186, 24));
1075: requiredCheckBox.setPreferredSize(new java.awt.Dimension(186,
1076: 24));
1077: requiredCheckBox
1078: .addActionListener(new java.awt.event.ActionListener() {
1079: public void actionPerformed(
1080: java.awt.event.ActionEvent evt) {
1081: requiredCheckBoxActionPerformed(evt);
1082: }
1083: });
1084:
1085: checkboxPanel2.add(requiredCheckBox,
1086: java.awt.BorderLayout.NORTH);
1087:
1088: panel.add(checkboxPanel2,
1089: new org.netbeans.lib.awtextra.AbsoluteConstraints(220,
1090: 170, 190, 50));
1091:
1092: checkboxPanel1.setLayout(new java.awt.BorderLayout());
1093:
1094: primaryKeyCheckBox.setText("Primary key:");
1095: primaryKeyCheckBox
1096: .setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
1097: primaryKeyCheckBox
1098: .addActionListener(new java.awt.event.ActionListener() {
1099: public void actionPerformed(
1100: java.awt.event.ActionEvent evt) {
1101: primaryKeyCheckBoxActionPerformed(evt);
1102: }
1103: });
1104:
1105: checkboxPanel1.add(primaryKeyCheckBox,
1106: java.awt.BorderLayout.NORTH);
1107:
1108: foreignKeyCheckBox.setText("Foreign key:");
1109: foreignKeyCheckBox
1110: .setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
1111: foreignKeyCheckBox
1112: .addActionListener(new java.awt.event.ActionListener() {
1113: public void actionPerformed(
1114: java.awt.event.ActionEvent evt) {
1115: foreignKeyCheckBoxActionPerformed(evt);
1116: }
1117: });
1118:
1119: checkboxPanel1.add(foreignKeyCheckBox,
1120: java.awt.BorderLayout.SOUTH);
1121:
1122: panel.add(checkboxPanel1,
1123: new org.netbeans.lib.awtextra.AbsoluteConstraints(60,
1124: 170, 160, 50));
1125:
1126: }
1127:
1128: // </editor-fold>//GEN-END:initComponents
1129:
1130: private void jdbcTypeComboBoxActionPerformed(
1131: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jdbcTypeComboBoxActionPerformed
1132: JagGenerator.stateChanged(false);
1133: regenerateButton.setEnabled(true);
1134: }//GEN-LAST:event_jdbcTypeComboBoxActionPerformed
1135:
1136: private void autoGeneratedCheckBoxActionPerformed(
1137: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoGeneratedCheckBoxActionPerformed
1138: if (autoGeneratedCheckBox.isSelected()
1139: && !isPkClassIsAutogeneratable()) {
1140: JagGenerator
1141: .logToConsole("Can't autogenerate primary keys with class type '"
1142: + typeText.getText() + "'!");
1143: autoGeneratedCheckBox.setSelected(false);
1144: }
1145: }//GEN-LAST:event_autoGeneratedCheckBoxActionPerformed
1146:
1147: private void regenerateButtonActionPerformed(
1148: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regenerateButtonActionPerformed
1149: regenerateValidations();
1150: regenerateButton.setEnabled(false);
1151: }//GEN-LAST:event_regenerateButtonActionPerformed
1152:
1153: private void requiredCheckBoxActionPerformed(
1154: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_requiredCheckBoxActionPerformed
1155: JagGenerator.stateChanged(false);
1156: regenerateButton.setEnabled(true);
1157: }//GEN-LAST:event_requiredCheckBoxActionPerformed
1158:
1159: private void validationXMLTextAreaFocusLost(
1160: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_validationXMLTextAreaFocusLost
1161: JagGenerator.stateChanged(false);
1162: }//GEN-LAST:event_validationXMLTextAreaFocusLost
1163:
1164: private void validationDependsTextFocusLost(
1165: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_validationDependsTextFocusLost
1166: JagGenerator.stateChanged(false);
1167: }//GEN-LAST:event_validationDependsTextFocusLost
1168:
1169: private void foreignKeyCheckBoxActionPerformed(
1170: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foreignKeyCheckBoxActionPerformed
1171: JagGenerator.stateChanged(false);
1172: regenerateButton.setEnabled(true);
1173: }//GEN-LAST:event_foreignKeyCheckBoxActionPerformed
1174:
1175: private void primaryKeyCheckBoxActionPerformed(
1176: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_primaryKeyCheckBoxActionPerformed
1177: boolean checked = primaryKeyCheckBox.isSelected();
1178: setPrimaryKey(checked);
1179:
1180: //tell the parent entity bean that its primary key status has changed..
1181: if (checked) {
1182: parentEntity.setPrimaryKey(this );
1183: } else {
1184: parentEntity.unsetPrimaryKey(this );
1185: }
1186:
1187: JagGenerator.stateChanged(false);
1188: regenerateButton.setEnabled(true);
1189: }//GEN-LAST:event_primaryKeyCheckBoxActionPerformed
1190:
1191: private void sqlTypeTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_sqlTypeTextFocusLost
1192: JagGenerator.stateChanged(false);
1193: regenerateButton.setEnabled(true);
1194: }//GEN-LAST:event_sqlTypeTextFocusLost
1195:
1196: private void columnNameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_columnNameTextFocusLost
1197: JagGenerator.stateChanged(false);
1198: }//GEN-LAST:event_columnNameTextFocusLost
1199:
1200: private void typeTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_typeTextFocusLost
1201: JagGenerator.stateChanged(false);
1202: }//GEN-LAST:event_typeTextFocusLost
1203:
1204: private void nameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFocusLost
1205: if (!nameText.getText().equals(oldName)) {
1206: parentEntity.notifyRelationsThatFieldNameChanged(oldName,
1207: nameText.getText());
1208: oldName = nameText.getText();
1209: }
1210: JagGenerator.stateChanged(true);
1211: }//GEN-LAST:event_nameTextFocusLost
1212:
1213: private boolean contains(String s, String sub) {
1214: return s.indexOf(sub) > -1;
1215: }
1216:
1217: // Variables declaration - do not modify//GEN-BEGIN:variables
1218: public javax.swing.JCheckBox autoGeneratedCheckBox;
1219: private javax.swing.JPanel checkboxPanel1;
1220: private javax.swing.JPanel checkboxPanel2;
1221: private javax.swing.JLabel columnNameLabel;
1222: private javax.swing.JTextField columnNameText;
1223: public javax.swing.JCheckBox foreignKeyCheckBox;
1224: private javax.swing.JPanel jPanel1;
1225: private javax.swing.JComboBox jdbcTypeComboBox;
1226: private javax.swing.JLabel jdbcTypeLabel;
1227: private javax.swing.JLabel nameLabel;
1228: private javax.swing.JTextField nameText;
1229: private javax.swing.JPanel panel;
1230: public javax.swing.JCheckBox primaryKeyCheckBox;
1231: private javax.swing.JButton regenerateButton;
1232: public javax.swing.JCheckBox requiredCheckBox;
1233: private javax.swing.JLabel sqlTypeLabel;
1234: private javax.swing.JTextField sqlTypeText;
1235: private javax.swing.JLabel typeLabel;
1236: private javax.swing.JTextField typeText;
1237: private javax.swing.JLabel validationDependsLabel;
1238: public javax.swing.JTextField validationDependsText;
1239: private javax.swing.JLabel validationXMLLabel;
1240: private javax.swing.JScrollPane validationXMLScrollPane;
1241: private javax.swing.JTextArea validationXMLTextArea;
1242: // End of variables declaration//GEN-END:variables
1243: }
|