You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2008/10/03 11:27:52 UTC
svn commit: r701335 - in /cayenne/main/trunk/framework:
cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/
cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/
cayenne-modeler/src/main/java/org/apache/cayenne/modeler...
Author: aadamchik
Date: Fri Oct 3 02:27:51 2008
New Revision: 701335
URL: http://svn.apache.org/viewvc?rev=701335&view=rev
Log:
CAY-795 Horizontal inheritance
(patch 2 from Dzmitry Rusak - modeler support for attribute overriding. Some more UI cleanup is on the way)
Added:
cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/DuplicatedAttributesDialog.java
cayenne/main/trunk/framework/cayenne-modeler/src/main/resources/org/apache/cayenne/modeler/images/icon-override.gif (with props)
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityAttributeTab.java
cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java?rev=701335&r1=701334&r2=701335&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java Fri Oct 3 02:27:51 2008
@@ -102,7 +102,8 @@
}
// If this obj attribute is mapped to db attribute
- if (getDbAttribute() != null) {
+ if (getDbAttribute() != null ||
+ (((ObjEntity)getEntity()).getIsAbstract() && !Util.isEmptyString(getDbAttributePath()))) {
encoder.print(" db-attribute-path=\"");
encoder.print(Util.encodeXmlAttribute(getDbAttributePath()));
encoder.print('\"');
Added: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/DuplicatedAttributesDialog.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/DuplicatedAttributesDialog.java?rev=701335&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/DuplicatedAttributesDialog.java (added)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/validator/DuplicatedAttributesDialog.java Fri Oct 3 02:27:51 2008
@@ -0,0 +1,312 @@
+package org.apache.cayenne.modeler.dialog.validator;
+
+
+import org.apache.cayenne.modeler.CayenneModelerFrame;
+import org.apache.cayenne.modeler.ProjectController;
+import org.apache.cayenne.modeler.util.CayenneDialog;
+import org.apache.cayenne.modeler.util.CayenneWidgetFactory;
+import org.apache.cayenne.modeler.util.ProjectUtil;
+import org.apache.cayenne.modeler.util.CayenneTableModel;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+
+
+import javax.swing.table.TableColumn;
+import javax.swing.JTable;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JScrollPane;
+import javax.swing.JPanel;
+import javax.swing.JDialog;
+import java.util.List;
+import java.util.LinkedList;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.awt.FlowLayout;
+import java.awt.BorderLayout;
+
+
+import com.jgoodies.forms.layout.CellConstraints;
+import com.jgoodies.forms.layout.FormLayout;
+import com.jgoodies.forms.builder.PanelBuilder;
+
+/**
+ * Dialog for resolving name collision.
+ *
+ * @author Dzmitry Rusak
+ */
+public class DuplicatedAttributesDialog extends CayenneDialog {
+
+ protected static DuplicatedAttributesDialog instance;
+
+ static final String DELETE_ACTION = "delete";
+ static final String RENAME_ACTION = "rename";
+
+ public static final String CANCEL_RESULT = "cancel";
+ public static final String PROCEEDED_RESULT = "proceeded";
+
+ static String result = CANCEL_RESULT;
+
+ protected List<DuplicatedAttributeInfo> duplicatedAttributes;
+
+ protected ObjEntity superEntity;
+ protected ObjEntity entity;
+
+ protected JTable attributesTable;
+ protected JButton cancelButton;
+ protected JButton proceedButton;
+
+ public static synchronized void showDialog(
+ CayenneModelerFrame editor,
+ List<ObjAttribute> duplicatedAttributes,
+ ObjEntity superEntity,
+ ObjEntity entity) {
+
+ if (instance == null) {
+ instance = new DuplicatedAttributesDialog(editor);
+ instance.centerWindow();
+ }
+
+ instance.setSuperEntity(superEntity);
+ instance.setEntity(entity);
+ instance.setDuplicatedAttributes(duplicatedAttributes);
+ instance.updateTable();
+ instance.setVisible(true);
+ }
+
+ protected DuplicatedAttributesDialog(CayenneModelerFrame editor) {
+ super(editor, "Duplicated Attributes", true);
+
+ result = CANCEL_RESULT;
+
+ initView();
+ initController();
+ }
+
+ private void initView() {
+ cancelButton = new JButton("Cancel");
+ proceedButton = new JButton("Continue");
+
+ attributesTable = new JTable();
+
+ // assemble
+ CellConstraints cc = new CellConstraints();
+ PanelBuilder builder = new PanelBuilder(new FormLayout(
+ "fill:200dlu:grow",
+ "pref, 3dlu, top:40dlu:grow"));
+
+ builder.setDefaultDialogBorder();
+
+ builder
+ .addLabel(
+ "Select actions for duplicated attributes:",
+ cc.xy(1, 1));
+ builder.add(new JScrollPane(attributesTable), cc.xy(1, 3));
+
+ JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
+ buttons.add(cancelButton);
+ buttons.add(proceedButton);
+
+
+ getContentPane().setLayout(new BorderLayout());
+ getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
+ getContentPane().add(buttons, BorderLayout.SOUTH);
+
+ // TODO: use preferences
+ setSize(450, 350);
+ }
+
+ private void initController() {
+ setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+ cancelButton.addActionListener(new ActionListener() {
+
+ public void actionPerformed(ActionEvent e) {
+ result = CANCEL_RESULT;
+ setVisible(false);
+ dispose();
+ }
+ });
+
+ proceedButton.addActionListener(new ActionListener() {
+
+ public void actionPerformed(ActionEvent e) {
+ applyChanges();
+ result = PROCEEDED_RESULT;
+ setVisible(false);
+ dispose();
+ }
+ });
+ }
+
+ public static String getResult() {
+ return result;
+ }
+
+ private void updateTable() {
+ TableColumn actionColumn = attributesTable.getColumnModel().getColumn(DuplicatedAttributeTableModel.ACTION);
+ JComboBox actionsCombo = CayenneWidgetFactory.createComboBox(new String[]{DELETE_ACTION, RENAME_ACTION}, false);
+ actionColumn.setCellEditor(CayenneWidgetFactory.createCellEditor(actionsCombo));
+ }
+
+ private void applyChanges() {
+ for (DuplicatedAttributeInfo attributeInfo : duplicatedAttributes) {
+ if (attributeInfo.getAction().equals(DELETE_ACTION)) {
+ entity.removeAttribute(attributeInfo.getName());
+ }
+ if (attributeInfo.getAction().equals(RENAME_ACTION)) {
+ ProjectUtil.setAttributeName(entity.getAttribute(attributeInfo.getName()), attributeInfo.getNewName());
+ }
+ }
+ }
+
+ public void setDuplicatedAttributes(List<ObjAttribute> attributes) {
+ if (duplicatedAttributes == null) {
+ duplicatedAttributes = new LinkedList<DuplicatedAttributeInfo>();
+ }
+
+ duplicatedAttributes.clear();
+
+ for (ObjAttribute attribute : attributes) {
+ DuplicatedAttributeInfo attributeInfo = new DuplicatedAttributeInfo(attribute.getName(), attribute.getType(),
+ ((ObjAttribute) superEntity.getAttribute(attribute.getName())).getType(), DELETE_ACTION);
+ duplicatedAttributes.add(attributeInfo);
+ }
+
+ attributesTable.setModel(new DuplicatedAttributeTableModel(getMediator(), this, duplicatedAttributes));
+
+ }
+
+ public void setSuperEntity(ObjEntity superEntity) {
+ this.superEntity = superEntity;
+ }
+
+ public void setEntity(ObjEntity entity) {
+ this.entity = entity;
+ }
+
+ class DuplicatedAttributeTableModel extends CayenneTableModel {
+
+ static final int ATTRIBUTE_NAME = 0;
+ static final int PARENT_TYPE = 1;
+ static final int TYPE = 2;
+ static final int ACTION = 3;
+
+ /**
+ * Constructor for CayenneTableModel.
+ */
+ public DuplicatedAttributeTableModel(ProjectController mediator, Object eventSource, List objectList) {
+ super(mediator, eventSource, objectList);
+ }
+
+ public void setUpdatedValueAt(Object newValue, int row, int column) {
+ DuplicatedAttributeInfo attributeInfo = duplicatedAttributes.get(row);
+ if(column == ATTRIBUTE_NAME) {
+ attributeInfo.setNewName(newValue.toString());
+ attributeInfo.setAction(RENAME_ACTION);
+ //TODO: add warn if new valuew equals the old one or name equals to another attribute name.
+ this.fireTableDataChanged();
+ }
+
+ if (column == ACTION) {
+ attributeInfo.setAction(newValue.toString());
+ }
+
+ }
+
+ public Class<?> getElementsClass() {
+ return DuplicatedAttributeInfo.class;
+ }
+
+ public int getColumnCount() {
+ return 4;
+ }
+
+ public Object getValueAt(int row, int col) {
+ DuplicatedAttributeInfo attributeInfo = duplicatedAttributes.get(row);
+ switch (col) {
+ case ATTRIBUTE_NAME:
+ return attributeInfo.getNewName();
+ case PARENT_TYPE:
+ return attributeInfo.getParentType();
+ case TYPE:
+ return attributeInfo.getType();
+ case ACTION:
+ return attributeInfo.getAction();
+
+ }
+ return "";
+ }
+
+
+
+ public boolean isCellEditable(int row, int column) {
+ if (column == ACTION || column == ATTRIBUTE_NAME) {
+ return true;
+ }
+ return false;
+ }
+
+ public String getColumnName(int column) {
+ switch (column) {
+ case ATTRIBUTE_NAME:
+ return "Name";
+ case PARENT_TYPE:
+ return "Type in super entity";
+ case TYPE :
+ return "Type";
+ case ACTION:
+ return "Action";
+ }
+ return " ";
+ }
+
+ public Class getColumnClass(int column) {
+ return String.class;
+ }
+ }
+
+ public class DuplicatedAttributeInfo {
+ private String name;
+ private String newName;
+ private String type;
+ private String parentType;
+ private String action;
+
+ DuplicatedAttributeInfo(String name, String type, String parentType, String action) {
+ this.name = name;
+ this.newName = name;
+ this.type = type;
+ this.parentType = parentType;
+ this.action = action;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getParentType() {
+ return parentType;
+ }
+
+ public String getAction() {
+ return action;
+ }
+
+ public void setAction(String action) {
+ this.action = action;
+ }
+
+ public String getNewName() {
+ return newName;
+ }
+
+ public void setNewName(String newName) {
+ this.newName = newName;
+ }
+ }
+
+}
Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java?rev=701335&r1=701334&r2=701335&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java Fri Oct 3 02:27:51 2008
@@ -46,11 +46,12 @@
public class ObjAttributeTableModel extends CayenneTableModel {
// Columns
- static final int OBJ_ATTRIBUTE = 0;
- static final int OBJ_ATTRIBUTE_TYPE = 1;
- static final int DB_ATTRIBUTE = 2;
- static final int DB_ATTRIBUTE_TYPE = 3;
- static final int LOCKING = 4;
+ static final int INHERITED = 0;
+ static final int OBJ_ATTRIBUTE = 1;
+ static final int OBJ_ATTRIBUTE_TYPE = 2;
+ static final int DB_ATTRIBUTE = 3;
+ static final int DB_ATTRIBUTE_TYPE = 4;
+ static final int LOCKING = 5;
protected ObjEntity entity;
protected DbEntity dbEntity;
@@ -119,11 +120,13 @@
}
public int getColumnCount() {
- return 5;
+ return 6;
}
public String getColumnName(int column) {
switch (column) {
+ case INHERITED:
+ return "In";
case OBJ_ATTRIBUTE:
return "ObjAttribute";
case OBJ_ATTRIBUTE_TYPE:
@@ -142,7 +145,10 @@
public Object getValueAt(int row, int column) {
ObjAttribute attribute = getAttribute(row);
- if (column == OBJ_ATTRIBUTE) {
+
+ if (column == INHERITED) {
+ return attribute.isInherited();
+ } else if (column == OBJ_ATTRIBUTE) {
return attribute.getName();
}
else if (column == OBJ_ATTRIBUTE_TYPE) {
@@ -153,13 +159,24 @@
}
else {
DbAttribute dbAttribute = attribute.getDbAttribute();
- if (dbAttribute == null) {
- return null;
- }
- else if (column == DB_ATTRIBUTE)
- return dbAttribute.getName();
- else if (column == DB_ATTRIBUTE_TYPE) {
- return TypesMapping.getSqlNameByType(dbAttribute.getType());
+ if (column == DB_ATTRIBUTE) {
+ if (dbAttribute == null) {
+ if (!attribute.isInherited() && ((ObjEntity)attribute.getEntity()).getIsAbstract()) {
+ return attribute.getDbAttributePath();
+ } else {
+ return null;
+ }
+ }
+ return dbAttribute.getName(); }
+ else
+ if (column == DB_ATTRIBUTE_TYPE) {
+ int type;
+ if (dbAttribute == null) {
+ type = TypesMapping.getSqlTypeByJava(getAttribute(row).getJavaClass());
+ } else {
+ type = dbAttribute.getType();
+ }
+ return TypesMapping.getSqlNameByType(type);
}
else {
return null;
@@ -192,9 +209,12 @@
if (column == DB_ATTRIBUTE) {
// If db attrib exist, associate it with obj attribute
if (value != null) {
- DbAttribute dbAttribute = (DbAttribute) dbEntity.getAttribute(value
- .toString());
- String path = dbAttribute != null ? dbAttribute.getName() : null;
+ String path = value.toString();
+ if (dbEntity != null) {
+ DbAttribute dbAttribute = (DbAttribute) dbEntity.getAttribute(value
+ .toString());
+ path = dbAttribute != null ? dbAttribute.getName() : null;
+ }
attribute.setDbAttributePath(path);
}
// If name is erased, remove db attribute from obj attribute.
@@ -211,10 +231,6 @@
public boolean isCellEditable(int row, int col) {
- if (dbEntity == null) {
- return col != DB_ATTRIBUTE_TYPE && col != DB_ATTRIBUTE;
- }
-
if (getAttribute(row).isInherited()) {
if (col == DB_ATTRIBUTE) {
return true;
@@ -222,7 +238,7 @@
return false;
}
- return col != DB_ATTRIBUTE_TYPE;
+ return col != DB_ATTRIBUTE_TYPE && col != INHERITED;
}
public ObjEntity getEntity() {
Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityAttributeTab.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityAttributeTab.java?rev=701335&r1=701334&r2=701335&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityAttributeTab.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityAttributeTab.java Fri Oct 3 02:27:51 2008
@@ -22,6 +22,7 @@
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
+import java.awt.Font;
import java.util.EventObject;
import java.util.List;
@@ -30,6 +31,7 @@
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JToolBar;
+import javax.swing.ImageIcon;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
@@ -218,6 +220,11 @@
}
protected void setUpTableStructure(ObjAttributeTableModel model) {
+ TableColumn inheritanceColumn = table.getColumnModel().getColumn(
+ ObjAttributeTableModel.INHERITED);
+ inheritanceColumn.setMinWidth(20);
+ inheritanceColumn.setMaxWidth(20);
+
TableColumn nameColumn = table.getColumnModel().getColumn(
ObjAttributeTableModel.OBJ_ATTRIBUTE);
nameColumn.setMinWidth(150);
@@ -302,14 +309,34 @@
ObjAttributeTableModel model = (ObjAttributeTableModel) table.getModel();
ObjAttribute attribute = model.getAttribute(row);
+ if (column != ObjAttributeTableModel.INHERITED) {
- if (attribute != null && attribute.getEntity() != model.getEntity()) {
- setForeground(Color.GRAY);
- }
- else {
- setForeground(isSelected && !hasFocus
- ? table.getSelectionForeground()
- : table.getForeground());
+ if (!model.isCellEditable(row, column)) {
+ setForeground(Color.GRAY);
+ }
+ else {
+ setForeground(isSelected && !hasFocus
+ ? table.getSelectionForeground()
+ : table.getForeground());
+ }
+
+ if (attribute.isInherited()) {
+ Font font = getFont();
+ Font newFont = font.deriveFont(Font.ITALIC);
+ setFont(newFont);
+ }
+ else {
+ setBackground(isSelected && !hasFocus
+ ? table.getSelectionBackground()
+ : table.getBackground());
+ }
+ setIcon(null);
+ } else {
+ if (attribute.isInherited()) {
+ ImageIcon objEntityIcon = ModelerUtil.buildIcon("icon-override.gif");
+ setIcon(objEntityIcon);
+ }
+ setText("");
}
return this;
Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java?rev=701335&r1=701334&r2=701335&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjEntityTab.java Fri Oct 3 02:27:51 2008
@@ -25,6 +25,8 @@
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.EventObject;
+import java.util.List;
+import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
@@ -41,6 +43,7 @@
import org.apache.cayenne.map.DataMap;
import org.apache.cayenne.map.DbEntity;
import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.event.EntityEvent;
import org.apache.cayenne.modeler.Application;
import org.apache.cayenne.modeler.ProjectController;
@@ -48,6 +51,7 @@
import org.apache.cayenne.modeler.action.CreateRelationshipAction;
import org.apache.cayenne.modeler.action.ObjEntitySyncAction;
import org.apache.cayenne.modeler.dialog.objentity.ClassNameUpdater;
+import org.apache.cayenne.modeler.dialog.validator.DuplicatedAttributesDialog;
import org.apache.cayenne.modeler.event.EntityDisplayEvent;
import org.apache.cayenne.modeler.event.ObjEntityDisplayListener;
import org.apache.cayenne.modeler.util.CayenneWidgetFactory;
@@ -263,6 +267,17 @@
ObjEntity entity = mediator.getCurrentObjEntity();
if (!Util.nullSafeEquals(name, entity.getSuperEntityName())) {
+ List<ObjAttribute> duplicateAttributes = getDuplicatedAttributes((ObjEntity) superEntity);
+
+ if (duplicateAttributes.size() > 0) {
+ DuplicatedAttributesDialog.showDialog(Application.getFrame(), duplicateAttributes, (ObjEntity) superEntity, entity);
+ if (DuplicatedAttributesDialog.getResult().equals(DuplicatedAttributesDialog.CANCEL_RESULT)) {
+ superEntityCombo.setSelectedItem(entity.getSuperEntity());
+ superClassName.setText(entity.getSuperClassName());
+ return;
+ }
+
+ }
entity.setSuperEntityName(name);
// if a super-entity selected, disable table selection
@@ -622,5 +637,19 @@
initFromModel(entity);
name.getComponent().requestFocusInWindow();
}
+
+ private List<ObjAttribute> getDuplicatedAttributes(ObjEntity superEntity) {
+ List<ObjAttribute> result = new LinkedList<ObjAttribute>();
+
+ ObjEntity entity = mediator.getCurrentObjEntity();
+
+ for (ObjAttribute attribute : entity.getAttributes()) {
+ if(superEntity.getAttribute(attribute.getName()) != null) {
+ result.add(attribute);
+ }
+ }
+
+ return result;
+ }
}
Added: cayenne/main/trunk/framework/cayenne-modeler/src/main/resources/org/apache/cayenne/modeler/images/icon-override.gif
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/resources/org/apache/cayenne/modeler/images/icon-override.gif?rev=701335&view=auto
==============================================================================
Binary file - no diff available.
Propchange: cayenne/main/trunk/framework/cayenne-modeler/src/main/resources/org/apache/cayenne/modeler/images/icon-override.gif
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream