cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject [2/4] cayenne git commit: fix autocompletion problems
Date Wed, 16 Dec 2015 12:57:05 GMT
fix autocompletion problems


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/c52fac63
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/c52fac63
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/c52fac63

Branch: refs/heads/master
Commit: c52fac637206fd053c1710bf777ae88d6f3eb9c5
Parents: 7e8e07c
Author: AlexandrShestak <shestakalexandr@mail.ru>
Authored: Mon Dec 14 16:15:13 2015 +0300
Committer: AlexandrShestak <shestakalexandr@mail.ru>
Committed: Mon Dec 14 16:15:13 2015 +0300

----------------------------------------------------------------------
 .../modeler/editor/ObjAttributeTableModel.java  |  2 +-
 .../editor/ObjRelationshipTableModel.java       |  4 +-
 .../util/DbAttributePathComboBoxEditor.java     | 32 +---------
 .../util/DbRelationshipPathComboBoxEditor.java  | 64 ++++++++++++++------
 .../util/PathChooserComboBoxCellEditor.java     | 60 ++++++++++++++----
 5 files changed, 101 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/c52fac63/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
index 16db808..8881829 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjAttributeTableModel.java
@@ -158,7 +158,7 @@ public class ObjAttributeTableModel extends CayenneTableModel<ObjAttributeWrappe
             case OBJ_ATTRIBUTE_TYPE:
                 return "Java Type";
             case DB_ATTRIBUTE:
-                return "DbAttributePath";
+                return "DbAttribute Path";
             case DB_ATTRIBUTE_TYPE:
                 return "DB Type";
             case LOCKING:

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c52fac63/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
index eab255b..cfc7596 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/ObjRelationshipTableModel.java
@@ -95,9 +95,9 @@ public class ObjRelationshipTableModel extends CayenneTableModel {
             case REL_COLLECTION_TYPE:
                 return "Collection Type";
             case REL_MAP_KEY:
-                return "Map key";
+                return "Map Key";
             case REL_TARGET_PATH:
-                return "DbRelationshipPath";
+                return "DbRelationship Path";
             default:
                 return null;
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c52fac63/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbAttributePathComboBoxEditor.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbAttributePathComboBoxEditor.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbAttributePathComboBoxEditor.java
index 14cb155..5eda3d0 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbAttributePathComboBoxEditor.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbAttributePathComboBoxEditor.java
@@ -30,14 +30,10 @@ import org.apache.cayenne.util.CayenneMapEntry;
 import org.apache.commons.lang.StringUtils;
 
 import javax.swing.DefaultComboBoxModel;
-import javax.swing.DefaultListCellRenderer;
 import javax.swing.JLabel;
-import javax.swing.JList;
 import javax.swing.JTable;
 import javax.swing.text.JTextComponent;
-import java.awt.Color;
 import java.awt.Component;
-import java.awt.Font;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
@@ -73,29 +69,7 @@ public class DbAttributePathComboBoxEditor extends PathChooserComboBoxCellEditor
     @Override
     protected void initializeCombo(CayenneTableModel model, int row, final JTable table)
{
         super.initializeCombo(model, row, table);
-        ((JTextComponent) (comboBoxPathChooser).
-                getEditor().getEditorComponent()).
-                setText(((ObjAttributeTableModel) model).getAttribute(row).getValue().getDbAttributePath());
-        comboBoxPathChooser.setRenderer(new DefaultListCellRenderer() {
-            @Override
-            public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
-                Object currentNode = getCurrentNode((String) value);
-                JLabel jLabel = new JLabel();
-                jLabel.setFont(new Font("Verdana", Font.PLAIN, 13));
-                if (isSelected) {
-                    jLabel.setOpaque(true);
-                    jLabel.setBackground(new Color(0xB4B4B4));
-                }
-                if (currentNode instanceof DbRelationship) {
-                    if (((String) value).charAt(((String) value).length() - 1) != '.') {
-                        jLabel.setText(ModelerUtil.getObjectName(value) + " ->");
-                    }
-                    return jLabel;
-                }
-                jLabel.setText(ModelerUtil.getObjectName(value));
-                return jLabel;
-            }
-        });
+        comboBoxPathChooser.setSelectedItem(((ObjAttributeTableModel) model).getAttribute(row).getValue().getDbAttributePath());
     }
 
 
@@ -129,8 +103,8 @@ public class DbAttributePathComboBoxEditor extends PathChooserComboBoxCellEditor
 
             if (table.getCellEditor() != null) {
                 table.getCellEditor().stopCellEditing();
-                model.getAttribute(row).setDbAttributePath(dbAttributePath);
                 model.setUpdatedValueAt(dbAttributePath, row, DB_ATTRIBUTE_PATH_COLUMN);
+                model.getAttribute(row).getValue().setDbAttributePath(dbAttributePath);
             }
         }else if (ModelerUtil.getObjectName(currentNode).equals(lastStringInPath) &&
                 currentNode instanceof DbRelationship) {
@@ -143,9 +117,9 @@ public class DbAttributePathComboBoxEditor extends PathChooserComboBoxCellEditor
                         getEditor().getEditorComponent()).setText(dbAttributePath);
             }
             List<String> currentNodeChildren = new ArrayList<>();
-            currentNodeChildren.add(dbAttributePath);
             currentNodeChildren.addAll(getChildren(getCurrentNode(dbAttributePath), dbAttributePath));
             comboBoxPathChooser.setModel(new DefaultComboBoxModel(currentNodeChildren.toArray()));
+            comboBoxPathChooser.setSelectedItem(dbAttributePath);
             comboBoxPathChooser.showPopup();
             comboBoxPathChooser.setPopupVisible(true);
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c52fac63/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbRelationshipPathComboBoxEditor.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbRelationshipPathComboBoxEditor.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbRelationshipPathComboBoxEditor.java
index 829f874..8a5ca0a 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbRelationshipPathComboBoxEditor.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DbRelationshipPathComboBoxEditor.java
@@ -28,22 +28,24 @@ import javax.swing.JLabel;
 import javax.swing.JTable;
 import javax.swing.text.JTextComponent;
 import java.awt.Component;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
 import java.util.Collection;
 import java.util.regex.Pattern;
 
-public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEditor implements
ActionListener {
+public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEditor implements
 FocusListener {
 
     private static final int REL_TARGET_PATH_COLUMN = 2;
     private static int enterPressedCount = 0;
-
+    private JTable table;
+    private String savePath;
     private ObjRelationshipTableModel model;
 
     @Override
     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
         this.model = (ObjRelationshipTableModel) table.getModel();
         this.row = row;
+        this.table = table;
         treeModel = createTreeModelForComboBox(row);
         if (treeModel == null) {
             return new JLabel("You should select table for this ObjectEntity");
@@ -64,19 +66,21 @@ public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEdi
     @Override
     protected void initializeCombo(CayenneTableModel model, int row, final JTable table)
{
         super.initializeCombo(model, row, table);
-        ((JTextComponent) (comboBoxPathChooser).
-                getEditor().getEditorComponent()).
-                setText(((ObjRelationshipTableModel) model).getRelationship(row).getDbRelationshipPath());
+        comboBoxPathChooser.setSelectedItem(((ObjRelationshipTableModel) model).getRelationship(row).getDbRelationshipPath());
 
         enterPressedCount = 0;
-        comboBoxPathChooser.addActionListener(this);
         comboBoxPathChooser.setToolTipText("To choose relationship press enter two times.
\n To choose next relationship press dot.");
+        JTextComponent textEditor = (JTextComponent) (comboBoxPathChooser).
+                getEditor().getEditorComponent();
+        textEditor.addFocusListener(this);
+        savePath = this.model.getRelationship(row).getDbRelationshipPath();
     }
 
     @Override
     protected void enterPressed(JTable table) {
         String dbRelationshipPath = ((JTextComponent) (comboBoxPathChooser).
                 getEditor().getEditorComponent()).getText();
+        changeObjEntity(dbRelationshipPath);
         Object currentNode = getCurrentNode(dbRelationshipPath);
         String[] pathStrings = dbRelationshipPath.split(Pattern.quote("."));
         String lastStringInPath = pathStrings[pathStrings.length - 1];
@@ -88,7 +92,9 @@ public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEdi
                 if (table.getCellEditor() != null) {
 
                     table.getCellEditor().stopCellEditing();
-                    model.getRelationship(row).setDbRelationshipPath(dbRelationshipPath);
+                    if (dbRelationshipPath.equals(savePath)) {
+                        return;
+                    }
 
                     //we need object target to save it in model
                     DbEntity lastEntity = ((DbRelationship) currentNode).getTargetEntity();
@@ -96,17 +102,27 @@ public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEdi
                             getDataMap().getMappedEntities(lastEntity);
                     ObjEntity objectTarget = objEntities.isEmpty() ? null : objEntities.iterator().next();
                     model.getRelationship(row).setTargetEntityName(objectTarget);
+                    model.setUpdatedValueAt(dbRelationshipPath, row, REL_TARGET_PATH_COLUMN);
+                    model.getRelationship(row).setDbRelationshipPath(dbRelationshipPath);
+                    model.getRelationship(row).setMapKey(null);
                 }
                 table.repaint();
-            } else {
-                enterPressedCount = 1;
             }
+            enterPressedCount = 1;
         }
     }
 
     @Override
+    protected void processDotEntered() {
+        super.processDotEntered();
+    }
+
+    @Override
     protected void parsePathString(char lastEnteredCharacter) {
         super.parsePathString(lastEnteredCharacter);
+        String dbRelationshipPath = ((JTextComponent) (comboBoxPathChooser).
+                getEditor().getEditorComponent()).getText();
+        changeObjEntity(dbRelationshipPath);
         enterPressedCount = 0;
     }
 
@@ -138,14 +154,26 @@ public class DbRelationshipPathComboBoxEditor extends PathChooserComboBoxCellEdi
         return pathString.replaceAll(lastStringInPath + '$', "");
     }
 
+    private void changeObjEntity(String path){
+        Object currentNode = getCurrentNode(path);
+        if (currentNode instanceof DbEntity){
+            return;
+        }
+        DbEntity lastEntity = ((DbRelationship) currentNode).getTargetEntity();
+        Collection<ObjEntity> objEntities = ((DbRelationship) currentNode).getTargetEntity().
+                getDataMap().getMappedEntities(lastEntity);
+        ObjEntity objectTarget = objEntities.isEmpty() ? null : objEntities.iterator().next();
+        model.getRelationship(row).setTargetEntityName(objectTarget);
+        table.repaint();
+    }
+
     @Override
-    public void actionPerformed(ActionEvent e) {
-        model.getRelationship(row).setMapKey(null);
+    public void focusGained(FocusEvent focusEvent) {
+    }
 
-        //for some reason dbRelationshipPathCombo don't load selected item text, so we made
it by hand
-        if (comboBoxPathChooser.getSelectedIndex() != (-1)) {
-            ((JTextComponent) (comboBoxPathChooser).
-                    getEditor().getEditorComponent()).setText(comboBoxPathChooser.getSelectedItem().toString());
-        }
+    @Override
+    public void focusLost(FocusEvent focusEvent) {
+        String path = model.getRelationship(row).getDbRelationshipPath();
+        changeObjEntity(path);
     }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c52fac63/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/PathChooserComboBoxCellEditor.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/PathChooserComboBoxCellEditor.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/PathChooserComboBoxCellEditor.java
index 051d8a4..07633da 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/PathChooserComboBoxCellEditor.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/PathChooserComboBoxCellEditor.java
@@ -19,6 +19,7 @@
 
 package org.apache.cayenne.modeler.util;
 
+import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.util.combo.AutoCompletion;
 import org.apache.commons.lang.StringUtils;
@@ -26,11 +27,20 @@ import org.apache.commons.lang.StringUtils;
 import javax.swing.AbstractCellEditor;
 import javax.swing.BorderFactory;
 import javax.swing.DefaultComboBoxModel;
+import javax.swing.DefaultListCellRenderer;
+import javax.swing.ImageIcon;
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JPanel;
 import javax.swing.JTable;
+import javax.swing.ListCellRenderer;
 import javax.swing.table.TableCellEditor;
 import javax.swing.text.JTextComponent;
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.Font;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.util.ArrayList;
@@ -76,24 +86,24 @@ public abstract class PathChooserComboBoxCellEditor extends AbstractCellEditor
i
         AutoCompletion.enable(comboBoxPathChooser, true, true);
         ((JComponent) comboBoxPathChooser.getEditor().getEditorComponent()).setBorder(null);
         comboBoxPathChooser.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
+        comboBoxPathChooser.setRenderer(new PathChooserComboBoxCellRenderer());
     }
 
     private void setComboModelAccordingToPath(String pathString) {
         List<String> currentNodeChildren = new ArrayList<>();
-        currentNodeChildren.add(pathString);
         currentNodeChildren.addAll(getChildren(getCurrentNode(pathString), pathString));
         comboBoxPathChooser.setModel(new DefaultComboBoxModel(currentNodeChildren.toArray()));
+        comboBoxPathChooser.setSelectedItem(pathString);
         comboBoxPathChooser.showPopup();
         comboBoxPathChooser.setPopupVisible(true);
     }
 
     protected void parsePathString(char lastEnteredCharacter) {
         JTextComponent editorComponent = (JTextComponent) (comboBoxPathChooser).getEditor().getEditorComponent();
-
         String pathString = editorComponent.getText();
         if (pathString != null && pathString.isEmpty()) {
             setComboModelAccordingToPath("");
-            previousEmbeddedLevel = StringUtils.countMatches(pathString, ".");
+            previousEmbeddedLevel = 0;
             return;
         }
 
@@ -106,19 +116,19 @@ public abstract class PathChooserComboBoxCellEditor extends AbstractCellEditor
i
         int currentEmbeddedLevel = StringUtils.countMatches(pathString, ".");
         if (previousEmbeddedLevel != currentEmbeddedLevel) {
             previousEmbeddedLevel = currentEmbeddedLevel;
-            List<String> currentNodeChildren = new ArrayList<>();
             String[] pathStrings = pathString.split(Pattern.quote("."));
             String lastStringInPath = pathStrings[pathStrings.length - 1];
             String saveDbAttributePath = pathString;
             pathString = pathString.replaceAll(lastStringInPath + "$", "");
+            List<String> currentNodeChildren = new ArrayList<>();
             currentNodeChildren.addAll(getChildren(getCurrentNode(pathString), pathString));
             comboBoxPathChooser.setModel(new DefaultComboBoxModel(currentNodeChildren.toArray()));
-            editorComponent.setText(saveDbAttributePath);
+            comboBoxPathChooser.setSelectedItem(saveDbAttributePath);
             return;
         }
     }
 
-    private void processDotEntered() {
+    protected void processDotEntered() {
         JTextComponent editorComponent = (JTextComponent) (comboBoxPathChooser).getEditor().getEditorComponent();
 
         String dbAttributePath = editorComponent.getText();
@@ -142,22 +152,22 @@ public abstract class PathChooserComboBoxCellEditor extends AbstractCellEditor
i
             //previous root is treeModel.getRoot()
             dbAttributePathForPreviousNode = "";
         } else {
-            dbAttributePathForPreviousNode = dbAttributePath.replace('.' + lastStringInPath,
"");
+            dbAttributePathForPreviousNode = dbAttributePath.replaceAll('.' + lastStringInPath
+ ".$", "");
         }
         List<String> potentialVariantsToChoose = getChildren(getCurrentNode(dbAttributePathForPreviousNode),
"");
-        if (potentialVariantsToChoose.contains(lastStringInPath)) {
+        if (potentialVariantsToChoose.contains(lastStringInPath) &&
+                !(getCurrentNode(dbAttributePath) instanceof DbAttribute)) {
             setComboModelAccordingToPath(dbAttributePath);
         } else {
             editorComponent.setText(dbAttributePath.substring(0, dbAttributePath.length()
- 1));
         }
-        previousEmbeddedLevel = StringUtils.countMatches(dbAttributePath, ".");
     }
 
     /**
-     * find current node by dbAttributePath
+     * find current node by path
      *
      * @param pathString
-     * @return last node in dbAttributePath which matches DbRelationship or DbAttribute
+     * @return last node in path which matches DbRelationship or DbAttribute
      */
     protected Object getCurrentNode(String pathString) {
         //case for new attribute
@@ -193,4 +203,32 @@ public abstract class PathChooserComboBoxCellEditor extends AbstractCellEditor
i
         }
         return currentNodeChildren;
     }
+
+    private final class PathChooserComboBoxCellRenderer extends DefaultListCellRenderer {
+
+        private  final ImageIcon rightArrow = ModelerUtil.buildIcon("scroll_right.gif");
+
+        @Override
+        public Component getListCellRendererComponent(JList<?> list, Object value,
int index,
+                                                      boolean isSelected, boolean cellHasFocus)
{
+
+            JPanel panel = new JPanel(new BorderLayout());
+            JLabel label = new JLabel((String) value);
+            label.setFont(new Font("Verdana", Font.PLAIN , 12));
+            panel.add(label);
+
+            Object currentNode = getCurrentNode((String) value);
+            if (treeModel.isLeaf(currentNode)) {
+                ListCellRenderer leafRenderer = CellRenderers.listRenderer();
+                return leafRenderer.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
+            } else {
+                DefaultListCellRenderer nonLeafTextRenderer = new DefaultListCellRenderer();
+                Component text = nonLeafTextRenderer.getListCellRendererComponent(list, value,
index, isSelected,
+                        cellHasFocus);
+                panel.setBackground(text.getBackground());
+                panel.add(new JLabel(rightArrow), BorderLayout.EAST);
+                return panel;
+            }
+        }
+    }
 }


Mime
View raw message