cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r568119 - in /cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler: dialog/ dialog/objentity/ editor/
Date Tue, 21 Aug 2007 13:31:05 GMT
Author: aadamchik
Date: Tue Aug 21 06:31:04 2007
New Revision: 568119

URL: http://svn.apache.org/viewvc?rev=568119&view=rev
Log:
CAY-848 Support for mapping to-many as Maps and Sets and Collections
modeler support

Modified:
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoController.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoDialog.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java?rev=568119&r1=568118&r2=568119&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
Tue Aug 21 06:31:04 2007
@@ -21,6 +21,7 @@
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
+import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
@@ -33,6 +34,7 @@
 import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JOptionPane;
+import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextField;
 import javax.swing.ListSelectionModel;
@@ -112,7 +114,7 @@
         CellConstraints cc = new CellConstraints();
         PanelBuilder builder = new PanelBuilder(
                 new FormLayout(
-                        "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(150dlu;pref)",
+                        "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(50dlu;pref)",
                         "p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, top:14dlu, 3dlu, top:p:grow"));
         builder.setDefaultDialogBorder();
 
@@ -124,8 +126,12 @@
 
         builder.addSeparator("Joins", cc.xywh(1, 7, 5, 1));
         builder.add(new JScrollPane(table), cc.xywh(1, 9, 3, 3, "fill, fill"));
-        builder.add(addButton, cc.xywh(5, 9, 1, 1));
-        builder.add(removeButton, cc.xywh(5, 11, 1, 1));
+
+        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEADING));
+        buttons.add(addButton);
+        buttons.add(removeButton);
+
+        builder.add(buttons, cc.xywh(5, 9, 1, 3));
 
         getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
         getContentPane().add(PanelFactory.createButtonPanel(new JButton[] {
@@ -148,9 +154,9 @@
                     + aRelationship.getSourceEntity());
         }
 
-        // Once assigned, can reference relationship directly.  Would it be
+        // Once assigned, can reference relationship directly. Would it be
         // OK to assign relationship at the very top of this method?
-        relationship        = aRelationship;
+        relationship = aRelationship;
         reverseRelationship = relationship.getReverseRelationship();
 
         // init UI components
@@ -243,9 +249,9 @@
         }
 
         if (sourceEntityName == null) {
-            sourceEntityName =
-                NamedObjectFactory.createName(DbRelationship.class,
-                                             relationship.getSourceEntity());
+            sourceEntityName = NamedObjectFactory.createName(
+                    DbRelationship.class,
+                    relationship.getSourceEntity());
         }
 
         if (!validateName(relationship.getSourceEntity(), relationship, sourceEntityName))
{
@@ -258,9 +264,9 @@
         }
 
         if (targetEntityName == null) {
-            targetEntityName =
-                NamedObjectFactory.createName(DbRelationship.class,
-                                              relationship.getTargetEntity());
+            targetEntityName = NamedObjectFactory.createName(
+                    DbRelationship.class,
+                    relationship.getTargetEntity());
         }
 
         // check if reverse name is valid
@@ -319,7 +325,8 @@
                                     MapEvent.ADD));
                 }
             }
-            else if (!Util.nullSafeEquals(targetEntityName, reverseRelationship.getName()))
{
+            else if (!Util
+                    .nullSafeEquals(targetEntityName, reverseRelationship.getName())) {
                 String oldName = reverseRelationship.getName();
                 ProjectUtil.setRelationshipName(
                         reverseRelationship.getSourceEntity(),

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoController.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoController.java?rev=568119&r1=568118&r2=568119&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoController.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoController.java
Tue Aug 21 06:31:04 2007
@@ -61,7 +61,9 @@
      * Creates and runs the classpath dialog.
      */
     public void startup() {
-        setView(new ObjRelationshipInfoDialog());
+        ObjRelationshipInfoDialog view = new ObjRelationshipInfoDialog();
+        setView(view);
+        view.initFromModel();
         super.startup();
     }
 

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoDialog.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoDialog.java?rev=568119&r1=568118&r2=568119&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoDialog.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoDialog.java
Tue Aug 21 06:31:04 2007
@@ -22,11 +22,15 @@
 import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 
 import javax.swing.DefaultCellEditor;
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.JButton;
 import javax.swing.JComboBox;
+import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
 import javax.swing.table.TableCellEditor;
@@ -49,8 +53,7 @@
 import com.jgoodies.forms.layout.FormLayout;
 
 /**
- * A view of the dialog for mapping an ObjRelationship to one or more
- * DbRelationships.
+ * A view of the dialog for mapping an ObjRelationship to one or more DbRelationships.
  * 
  * @since 1.1
  * @author Andrus Adamchik
@@ -59,33 +62,38 @@
 
     protected STable pathTable;
 
+    protected Component collectionTypeLabel;
+    protected SComboBox collectionTypeCombo;
+    protected Component mapKeysLabel;
+    protected SComboBox mapKeysCombo;
+
     public ObjRelationshipInfoDialog() {
         init();
     }
 
     protected void init() {
-        // create widgets 
-        SButton saveButton =
-            new SButton(new SAction(ObjRelationshipInfoController.SAVE_CONTROL));
+        // create widgets
+        SButton saveButton = new SButton(new SAction(
+                ObjRelationshipInfoController.SAVE_CONTROL));
         saveButton.setEnabled(true);
 
-        SButton cancelButton =
-            new SButton(new SAction(ObjRelationshipInfoController.CANCEL_CONTROL));
+        SButton cancelButton = new SButton(new SAction(
+                ObjRelationshipInfoController.CANCEL_CONTROL));
         cancelButton.setEnabled(true);
 
-        SButton newToOneButton =
-            new SButton(new SAction(ObjRelationshipInfoController.NEW_TOONE_CONTROL));
+        SButton newToOneButton = new SButton(new SAction(
+                ObjRelationshipInfoController.NEW_TOONE_CONTROL));
         newToOneButton.setEnabled(true);
-        SButton newToManyButton =
-            new SButton(new SAction(ObjRelationshipInfoController.NEW_TOMANY_CONTROL));
+        SButton newToManyButton = new SButton(new SAction(
+                ObjRelationshipInfoController.NEW_TOMANY_CONTROL));
         newToManyButton.setEnabled(true);
 
         STextField relationshipName = new STextField(25);
         relationshipName.setSelector(ObjRelationshipInfoModel.RELATIONSHIP_NAME_SELECTOR);
 
         SLabel sourceEntityLabel = new SLabel();
-        sourceEntityLabel.setSelector(
-            ObjRelationshipInfoModel.SOURCE_ENTITY_NAME_SELECTOR);
+        sourceEntityLabel
+                .setSelector(ObjRelationshipInfoModel.SOURCE_ENTITY_NAME_SELECTOR);
 
         SComboBox targetCombo = new SComboBox();
         targetCombo.setSelector(ObjRelationshipInfoModel.OBJECT_TARGETS_SELECTOR);
@@ -93,20 +101,39 @@
         SListCellRenderer renderer = (SListCellRenderer) targetCombo.getRenderer();
         renderer.setTextSelector("name");
 
+        collectionTypeCombo = new SComboBox();
+        collectionTypeCombo
+                .setSelector(ObjRelationshipInfoModel.TARGET_COLLECTIONS_SELECTOR);
+        collectionTypeCombo
+                .setSelectionSelector(ObjRelationshipInfoModel.TARGET_COLLECTION_SELECTOR);
+
+        mapKeysCombo = new SComboBox();
+        mapKeysCombo.setSelector(ObjRelationshipInfoModel.MAP_KEYS_SELECTOR);
+        mapKeysCombo.setSelectionSelector(ObjRelationshipInfoModel.MAP_KEY_SELECTOR);
+
         pathTable = new ObjRelationshipPathTable();
         STableModel pathTableModel = new STableModel(pathTable);
-        pathTableModel.setSelector(
-            ObjRelationshipInfoModel.DB_RELATIONSHIP_PATH_SELECTOR);
-        pathTableModel.setColumnNames(new String[] { "DbRelationships" });
-        pathTableModel.setColumnSelectors(
-            new Selector[] {
-                 EntityRelationshipsModel.RELATIONSHIP_DISPLAY_NAME_SELECTOR });
+        pathTableModel
+                .setSelector(ObjRelationshipInfoModel.DB_RELATIONSHIP_PATH_SELECTOR);
+        pathTableModel.setColumnNames(new String[] {
+            "DbRelationships"
+        });
+        pathTableModel.setColumnSelectors(new Selector[] {
+            EntityRelationshipsModel.RELATIONSHIP_DISPLAY_NAME_SELECTOR
+        });
 
         pathTable.setModel(pathTableModel);
-        pathTable.setSelectionSelector(
-            ObjRelationshipInfoModel.SELECTED_PATH_COMPONENT_SELECTOR);
-        pathTable.getColumn("DbRelationships").setCellEditor(
-            RelationshipPicker.createEditor(this));
+        pathTable
+                .setSelectionSelector(ObjRelationshipInfoModel.SELECTED_PATH_COMPONENT_SELECTOR);
+        pathTable.getColumn("DbRelationships").setCellEditor(createRelationshipEditor());
+
+        // enable/disable map keys for collection type selection
+        collectionTypeCombo.addActionListener(new ActionListener() {
+
+            public void actionPerformed(ActionEvent action) {
+                initFromModel();
+            }
+        });
 
         // assemble
         setDisplayMode(SwingView.MODAL_DIALOG);
@@ -114,11 +141,10 @@
         setLayout(new BorderLayout());
 
         CellConstraints cc = new CellConstraints();
-        PanelBuilder builder =
-            new PanelBuilder(
+        PanelBuilder builder = new PanelBuilder(
                 new FormLayout(
-                    "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(150dlu;pref)",
-                    "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, top:14dlu, 3dlu, top:p:grow"));
+                        "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(120dlu;pref)",
+                        "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, top:14dlu,
3dlu, top:p:grow"));
         builder.setDefaultDialogBorder();
 
         builder.addSeparator("ObjRelationship Information", cc.xywh(1, 1, 5, 1));
@@ -128,16 +154,24 @@
         builder.add(sourceEntityLabel, cc.xywh(3, 5, 1, 1));
         builder.addLabel("Target:", cc.xy(1, 7));
         builder.add(targetCombo, cc.xywh(3, 7, 1, 1));
+        collectionTypeLabel = builder.addLabel("Collection Type:", cc.xy(1, 9));
+        builder.add(collectionTypeCombo, cc.xywh(3, 9, 1, 1));
+        mapKeysLabel = builder.addLabel("Map Key:", cc.xy(1, 11));
+        builder.add(mapKeysCombo, cc.xywh(3, 11, 1, 1));
+
+        builder.addSeparator("Mapping to DbRelationships", cc.xywh(1, 13, 5, 1));
+        builder.add(new JScrollPane(pathTable), cc.xywh(1, 15, 3, 3));
+
+        JPanel newRelationshipsButtons = new JPanel(new FlowLayout(FlowLayout.LEADING));
+        newRelationshipsButtons.add(newToOneButton);
+        newRelationshipsButtons.add(newToManyButton);
 
-        builder.addSeparator("Mapping to DbRelationships", cc.xywh(1, 9, 5, 1));
-        builder.add(new JScrollPane(pathTable), cc.xywh(1, 11, 3, 3));
-        builder.add(newToOneButton, cc.xywh(5, 11, 1, 1));
-        builder.add(newToManyButton, cc.xywh(5, 13, 1, 1));
+        builder.add(newRelationshipsButtons, cc.xywh(5, 15, 1, 3));
 
         add(builder.getPanel(), BorderLayout.CENTER);
-        add(
-            PanelFactory.createButtonPanel(new JButton[] { saveButton, cancelButton }),
-            BorderLayout.SOUTH);
+        add(PanelFactory.createButtonPanel(new JButton[] {
+                saveButton, cancelButton
+        }), BorderLayout.SOUTH);
     }
 
     /**
@@ -160,7 +194,43 @@
         }
     }
 
+    void initFromModel() {
+        // called too early in the cycle...
+        if (getController() == null || getController().getModel() == null) {
+            return;
+        }
+
+        ObjRelationshipInfoModel model = (ObjRelationshipInfoModel) getController()
+                .getModel();
+
+        boolean collectionTypeEnabled = model.isToMany();
+        collectionTypeCombo.setEnabled(collectionTypeEnabled);
+        collectionTypeLabel.setEnabled(collectionTypeEnabled);
+
+        boolean mapKeysEnabled = collectionTypeEnabled
+                && ObjRelationshipInfoModel.COLLECTION_TYPE_MAP
+                        .equals(collectionTypeCombo.getSelectedItem());
+        mapKeysCombo.setEnabled(mapKeysEnabled);
+        mapKeysLabel.setEnabled(mapKeysEnabled);
+    }
+
+    TableCellEditor createRelationshipEditor() {
+        JComboBox relationshipCombo = new JComboBox();
+        relationshipCombo.setEditable(false);
+
+        // enable disable collections when relationship semntics changes
+        relationshipCombo.addActionListener(new ActionListener() {
+
+            public void actionPerformed(ActionEvent event) {
+                initFromModel();
+            }
+        });
+
+        return new RelationshipPicker(this, relationshipCombo);
+    }
+
     class ObjRelationshipPathTable extends STable {
+
         final Dimension preferredSize = new Dimension(203, 100);
 
         ObjRelationshipPathTable() {
@@ -173,16 +243,11 @@
         }
     }
 
-    static final class RelationshipPicker extends DefaultCellEditor {
+    final class RelationshipPicker extends DefaultCellEditor {
+
         JComboBox comboBox;
         SwingView view;
 
-        static TableCellEditor createEditor(SwingView view) {
-            JComboBox relationshipCombo = new JComboBox();
-            relationshipCombo.setEditable(false);
-            return new RelationshipPicker(view, relationshipCombo);
-        }
-
         RelationshipPicker(SwingView view, JComboBox comboBox) {
             super(comboBox);
             this.comboBox = comboBox;
@@ -190,31 +255,32 @@
         }
 
         public Component getTableCellEditorComponent(
-            JTable table,
-            Object value,
-            boolean isSelected,
-            int row,
-            int column) {
+                JTable table,
+                Object value,
+                boolean isSelected,
+                int row,
+                int column) {
 
             // initialize combo box
-            ObjRelationshipInfoModel model =
-                (ObjRelationshipInfoModel) view.getBoundModel();
+            ObjRelationshipInfoModel model = (ObjRelationshipInfoModel) view
+                    .getBoundModel();
 
-            EntityRelationshipsModel relationshipWrapper =
-                (EntityRelationshipsModel) model.getDbRelationshipPath().get(row);
+            EntityRelationshipsModel relationshipWrapper = (EntityRelationshipsModel) model
+                    .getDbRelationshipPath()
+                    .get(row);
 
-            DefaultComboBoxModel comboModel =
-                new DefaultComboBoxModel(relationshipWrapper.getRelationshipNames());
+            DefaultComboBoxModel comboModel = new DefaultComboBoxModel(
+                    relationshipWrapper.getRelationshipNames());
             comboModel.setSelectedItem(value);
             comboBox.setModel(comboModel);
 
             // call super
             return super.getTableCellEditorComponent(
-                table,
-                value,
-                isSelected,
-                row,
-                column);
+                    table,
+                    value,
+                    isSelected,
+                    row,
+                    column);
         }
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java?rev=568119&r1=568118&r2=568119&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
Tue Aug 21 06:31:04 2007
@@ -29,6 +29,7 @@
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.Entity;
+import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.map.Relationship;
@@ -49,6 +50,8 @@
  */
 public class ObjRelationshipInfoModel extends BasicModel {
 
+    static final String COLLECTION_TYPE_MAP = "java.util.Map";
+
     public static final Selector DB_RELATIONSHIP_PATH_SELECTOR = Selector
             .fromString("dbRelationshipPath");
     public static final Selector SOURCE_ENTITY_NAME_SELECTOR = Selector
@@ -61,19 +64,34 @@
             .fromString("objectTargets");
     public static final Selector RELATIONSHIP_NAME_SELECTOR = Selector
             .fromString("relationshipName");
+    public static final Selector TARGET_COLLECTIONS_SELECTOR = Selector
+            .fromString("targetCollections");
+    public static final Selector TARGET_COLLECTION_SELECTOR = Selector
+            .fromString("targetCollection");
+    public static final Selector MAP_KEYS_SELECTOR = Selector.fromString("mapKeys");
+    public static final Selector MAP_KEY_SELECTOR = Selector.fromString("mapKey");
 
     protected ObjRelationship relationship;
     protected ListModel dbRelationshipPath;
     protected EntityRelationshipsModel selectedPathComponent;
     protected ObjEntity objectTarget;
     protected List objectTargets;
+    protected List targetCollections;
+    protected List mapKeys;
     protected String relationshipName;
+    protected String targetCollection;
+    protected String mapKey;
 
     public ObjRelationshipInfoModel(ObjRelationship relationship, Collection objEntities)
{
 
         this.relationship = relationship;
         this.relationshipName = relationship.getName();
         this.objectTarget = (ObjEntity) relationship.getTargetEntity();
+        this.mapKey = relationship.getMapKey();
+        this.targetCollection = relationship.getCollectionType();
+        if (targetCollection == null) {
+            targetCollection = ObjRelationship.DEFAULT_COLLECTION_TYPE;
+        }
 
         // prepare entities - copy those that have DbEntities mapped, and then sort
 
@@ -93,6 +111,15 @@
         // and target entities present, with DbEntities chosen.
         validateCanMap();
 
+        this.targetCollections = new ArrayList(4);
+        targetCollections.add("java.util.Collection");
+        targetCollections.add(ObjRelationship.DEFAULT_COLLECTION_TYPE);
+        targetCollections.add(COLLECTION_TYPE_MAP);
+        targetCollections.add("java.util.Set");
+
+        this.mapKeys = new ArrayList();
+        initMapKeys();
+
         // wrap path
         this.dbRelationshipPath = new ListModel();
         Iterator it = relationship.getDbRelationships().iterator();
@@ -101,6 +128,9 @@
             this.dbRelationshipPath.add(new EntityRelationshipsModel(dbRelationship));
         }
 
+        // this sets the right enabled state of collection type selectors
+        fireModelChange(ModelChangeTypes.VALUE_CHANGED, DB_RELATIONSHIP_PATH_SELECTOR);
+
         // add dummy last relationship if we are not connected
         connectEnds();
         this.dbRelationshipPath.addModelChangeListener(this);
@@ -150,6 +180,26 @@
             breakChain(-1);
             connectEnds();
             fireModelChange(ModelChangeTypes.VALUE_CHANGED, DB_RELATIONSHIP_PATH_SELECTOR);
+
+            // init available map keys
+            initMapKeys();
+        }
+    }
+
+    private void initMapKeys() {
+        this.mapKeys.clear();
+
+        Iterator attributes = this.objectTarget.getAttributes().iterator();
+        while (attributes.hasNext()) {
+            ObjAttribute attribute = (ObjAttribute) attributes.next();
+            mapKeys.add(attribute.getName());
+        }
+
+        fireModelChange(ModelChangeTypes.VALUE_CHANGED, MAP_KEYS_SELECTOR);
+
+        if (mapKey != null && !mapKeys.contains(mapKey)) {
+            mapKey = mapKeys.size() > 0 ? (String) mapKeys.get(0) : null;
+            fireModelChange(ModelChangeTypes.VALUE_CHANGED, MAP_KEY_SELECTOR);
         }
     }
 
@@ -199,40 +249,82 @@
         dbRelationshipPath.fireModelChange(VALUE_CHANGED, null);
     }
 
+    public boolean isToMany() {
+        // copied algorithm from ObjRelationship.calculateToMany(), only iterating through
+        // the unsaved dbrels selection.
+
+        Iterator dbRelIterator = dbRelationshipPath.iterator();
+        while (dbRelIterator.hasNext()) {
+            EntityRelationshipsModel next = (EntityRelationshipsModel) dbRelIterator
+                    .next();
+            Relationship relationship = next.getSelectedRelationship();
+            if (relationship != null && relationship.isToMany()) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Stores current state of the model in the internal ObjRelationship.
      */
     public synchronized boolean savePath() {
-        // check for modifications
-        if (relationship.getTargetEntity() == objectTarget) {
-            if (Util.nullSafeEquals(relationship.getName(), relationshipName)) {
-                List oldPath = relationship.getDbRelationships();
-                if (oldPath.size() == dbRelationshipPath.size()) {
-                    boolean hasChanges = false;
-                    for (int i = 0; i < oldPath.size(); i++) {
-                        EntityRelationshipsModel next = (EntityRelationshipsModel) dbRelationshipPath
-                                .get(i);
-                        if (oldPath.get(i) != next.getSelectedRelationship()) {
-                            hasChanges = true;
-                            break;
-                        }
-                    }
-
-                    if (!hasChanges) {
-                        return false;
-                    }
+        boolean hasChanges = false;
+
+        if (!Util.nullSafeEquals(relationship.getName(), relationshipName)) {
+            hasChanges = true;
+            relationship.setName(relationshipName);
+        }
+
+        if (!Util.nullSafeEquals(objectTarget.getName(), relationship
+                .getTargetEntityName())) {
+            hasChanges = true;
+
+            // note on events notification - this needs to be propagated
+            // via old modeler events, but we leave this to the controller
+            // since model knows nothing about Modeler mediator.
+            relationship.setTargetEntity(objectTarget);
+        }
+
+        // check for path modifications
+        List oldPath = relationship.getDbRelationships();
+        if (oldPath.size() != dbRelationshipPath.size()) {
+            hasChanges = true;
+            updatePath();
+        }
+        else {
+            for (int i = 0; i < oldPath.size(); i++) {
+                EntityRelationshipsModel next = (EntityRelationshipsModel) dbRelationshipPath
+                        .get(i);
+                if (oldPath.get(i) != next.getSelectedRelationship()) {
+                    hasChanges = true;
+                    updatePath();
+                    break;
                 }
             }
         }
 
-        // detected modifications, save...
-        relationship.clearDbRelationships();
+        String collectionType = ObjRelationship.DEFAULT_COLLECTION_TYPE
+                .equals(targetCollection)
+                || !relationship.isToMany() ? null : targetCollection;
+        if (!Util.nullSafeEquals(collectionType, relationship.getCollectionType())) {
+            hasChanges = true;
+            relationship.setCollectionType(collectionType);
+        }
+
+        // map key only makes sense for Map relationships
+        String mapKey = COLLECTION_TYPE_MAP.equals(collectionType) ? this.mapKey : null;
+        if (!Util.nullSafeEquals(mapKey, relationship.getMapKey())) {
+            hasChanges = true;
+            relationship.setMapKey(mapKey);
+        }
+
+        return hasChanges;
+    }
 
-        // note on events notification - this needs to be propagated
-        // via old modeler events, but we leave this to the controller
-        // since model knows nothing about Modeler mediator.
-        relationship.setTargetEntity(objectTarget);
-        relationship.setName(relationshipName);
+    private void updatePath() {
+        relationship.clearDbRelationships();
 
         Iterator it = dbRelationshipPath.iterator();
         while (it.hasNext()) {
@@ -244,8 +336,6 @@
 
             relationship.addDbRelationship((DbRelationship) nextPathComponent);
         }
-
-        return true;
     }
 
     private void breakChain(int index) {
@@ -327,5 +417,29 @@
 
     public DbEntity getEndEntity() {
         return objectTarget.getDbEntity();
+    }
+
+    public String getMapKey() {
+        return mapKey;
+    }
+
+    public void setMapKey(String mapKey) {
+        this.mapKey = mapKey;
+    }
+
+    public String getTargetCollection() {
+        return targetCollection;
+    }
+
+    public void setTargetCollection(String targetCollection) {
+        this.targetCollection = targetCollection;
+    }
+
+    public List getMapKeys() {
+        return mapKeys;
+    }
+
+    public List getTargetCollections() {
+        return targetCollections;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java?rev=568119&r1=568118&r2=568119&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
Tue Aug 21 06:31:04 2007
@@ -35,13 +35,14 @@
 import org.apache.cayenne.modeler.util.ProjectUtil;
 import org.apache.cayenne.util.Util;
 
-/** 
- * Table model to display ObjRelationships. 
+/**
+ * Table model to display ObjRelationships.
  * 
  * @author Misha Shengaout
  * @author Andrus Adamchik
  */
 public class ObjRelationshipTableModel extends CayenneTableModel {
+
     // Columns
     static final int REL_NAME = 0;
     static final int REL_TARGET = 1;
@@ -51,10 +52,8 @@
 
     protected ObjEntity entity;
 
-    public ObjRelationshipTableModel(
-        ObjEntity entity,
-        ProjectController mediator,
-        Object eventSource) {
+    public ObjRelationshipTableModel(ObjEntity entity, ProjectController mediator,
+            Object eventSource) {
         super(mediator, eventSource, new ArrayList(entity.getRelationships()));
         this.entity = entity;
 
@@ -83,37 +82,36 @@
 
     public String getColumnName(int column) {
         switch (column) {
-            case REL_NAME :
+            case REL_NAME:
                 return "Name";
-            case REL_TARGET :
+            case REL_TARGET:
                 return "Target";
-            case REL_LOCKING :
+            case REL_LOCKING:
                 return "Used for Locking";
-            case REL_SEMANTICS :
+            case REL_SEMANTICS:
                 return "Semantics";
-            case REL_DELETERULE :
+            case REL_DELETERULE:
                 return "Delete Rule";
 
-            default :
+            default:
                 return null;
         }
     }
 
     public Class getColumnClass(int col) {
         switch (col) {
-            case REL_TARGET :
+            case REL_TARGET:
                 return ObjEntity.class;
-            case REL_LOCKING :
+            case REL_LOCKING:
                 return Boolean.class;
-            default :
+            default:
                 return String.class;
         }
     }
 
     public ObjRelationship getRelationship(int row) {
-        return (row >= 0 && row < objectList.size())
-            ? (ObjRelationship) objectList.get(row)
-            : null;
+        return (row >= 0 && row < objectList.size()) ? (ObjRelationship) objectList
+                .get(row) : null;
     }
 
     public Object getValueAt(int row, int column) {
@@ -130,9 +128,21 @@
         }
         else if (column == REL_SEMANTICS) {
             String semantics = relationship.isToMany() ? "to many" : "to one";
-            if(relationship.isReadOnly()) {
+            if (relationship.isReadOnly()) {
                 semantics += ", read-only";
             }
+
+            String collection = "list";
+            if (relationship.getCollectionType() != null) {
+                int dot = relationship.getCollectionType().lastIndexOf('.');
+                collection = relationship
+                        .getCollectionType()
+                        .substring(dot + 1)
+                        .toLowerCase();
+            }
+
+            semantics += ", " + collection;
+
             return semantics;
         }
         else if (column == REL_DELETERULE) {
@@ -145,8 +155,7 @@
 
     public void setUpdatedValueAt(Object value, int row, int column) {
         ObjRelationship relationship = getRelationship(row);
-        RelationshipEvent event =
-            new RelationshipEvent(eventSource, relationship, entity);
+        RelationshipEvent event = new RelationshipEvent(eventSource, relationship, entity);
 
         if (column == REL_NAME) {
             String text = (String) value;
@@ -160,8 +169,8 @@
 
             // now try to connect DbEntities if we can do it in one step
             if (target != null) {
-                DbEntity srcDB =
-                    ((ObjEntity) relationship.getSourceEntity()).getDbEntity();
+                DbEntity srcDB = ((ObjEntity) relationship.getSourceEntity())
+                        .getDbEntity();
                 DbEntity targetDB = target.getDbEntity();
                 if (srcDB != null && targetDB != null) {
                     Relationship anyConnector = srcDB.getAnyRelationship(targetDB);
@@ -178,8 +187,8 @@
             fireTableCellUpdated(row, column);
         }
         else if (column == REL_LOCKING) {
-            relationship.setUsedForLocking(
-                (value instanceof Boolean) && ((Boolean) value).booleanValue());
+            relationship.setUsedForLocking((value instanceof Boolean)
+                    && ((Boolean) value).booleanValue());
             fireTableCellUpdated(row, column);
         }
 
@@ -208,15 +217,15 @@
     }
 
     final class RelationshipComparator implements Comparator {
+
         public int compare(Object o1, Object o2) {
             ObjRelationship r1 = (ObjRelationship) o1;
             ObjRelationship r2 = (ObjRelationship) o2;
 
             int delta = getWeight(r1) - getWeight(r2);
 
-            return (delta != 0)
-                ? delta
-                : Util.nullSafeCompare(true, r1.getName(), r2.getName());
+            return (delta != 0) ? delta : Util.nullSafeCompare(true, r1.getName(), r2
+                    .getName());
         }
 
         private int getWeight(ObjRelationship r) {



Mime
View raw message