cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject [1/3] cayenne git commit: CAY-2150 UI bug: PK generation custom sequence is getting reset Fix: UI is updated correctly + added undo support for generator type change
Date Thu, 24 Nov 2016 14:32:17 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 2bf86e9fa -> ac1c8c5d7


CAY-2150
UI bug: PK generation custom sequence is getting reset
Fix: UI is updated correctly + added undo support for generator type change


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

Branch: refs/heads/master
Commit: 17da69bc3e59d72b14ae379222e4cb6735e4c0ea
Parents: f527275
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Wed Nov 23 12:20:56 2016 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Wed Nov 23 12:20:56 2016 +0300

----------------------------------------------------------------------
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |   1 +
 .../modeler/editor/dbentity/DbEntityTab.java    |   8 +-
 .../PKCustomSequenceGeneratorPanel.java         |  13 +-
 .../editor/dbentity/PKDBGeneratorPanel.java     |   3 +-
 .../dbentity/PKDefaultGeneratorPanel.java       |   2 +-
 .../editor/dbentity/PKGeneratorPanel.java       |  17 ++-
 .../undo/ChangePKGeneratorUndoableEdit.java     | 132 +++++++++++++++++++
 7 files changed, 159 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index b6e2f8b..3eddba9 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -69,6 +69,7 @@ CAY-2131 Modeler NullPointerException in reverse engineering when importing
diff
 CAY-2138 NVARCHAR, LONGNVARCHAR and NCLOB types are missing from Firebird types.xml
 CAY-2143 NPE in BaseSchemaUpdateStrategy
 CAY-2144 cdbimport always fails for databases which don't support catalogs
+CAY-2150 UI bug: PK generation custom sequence is getting reset
 
 ----------------------------------
 Release: 4.0.M3

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/DbEntityTab.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/DbEntityTab.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/DbEntityTab.java
index 097ec8b..1d7be0d 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/DbEntityTab.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/DbEntityTab.java
@@ -213,10 +213,10 @@ public class DbEntityTab extends JPanel implements ExistingSelectionProcessor,
D
             ((PKGeneratorPanel) pkGeneratorDetail.getComponent(i)).setDbEntity(entity);
         }
 
-        if (!e.isEntityChanged()) {
-            // name.getComponent().requestFocusInWindow();
-            return;
-        }
+//        if (!e.isEntityChanged()) {
+//            // name.getComponent().requestFocusInWindow();
+//            return;
+//        }
 
         name.setText(entity.getName());
         catalog.setText(entity.getCatalog());

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKCustomSequenceGeneratorPanel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKCustomSequenceGeneratorPanel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKCustomSequenceGeneratorPanel.java
index 269f567..f603ffd 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKCustomSequenceGeneratorPanel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKCustomSequenceGeneratorPanel.java
@@ -82,14 +82,15 @@ public class PKCustomSequenceGeneratorPanel extends PKGeneratorPanel {
         add(builder.getPanel(), BorderLayout.CENTER);
     }
 
-    public void onInit(DbEntity entity) {
-
+    protected void onInitInternal(DbEntity entity) {
         resetStrategy(entity, false, true);
 
         if (entity.getPrimaryKeyGenerator() == null) {
             DbKeyGenerator generator = new DbKeyGenerator();
             generator.setGeneratorType(DbKeyGenerator.ORACLE_TYPE);
             entity.setPrimaryKeyGenerator(generator);
+        } else {
+            setDbEntity(entity);
         }
     }
 
@@ -98,11 +99,9 @@ public class PKCustomSequenceGeneratorPanel extends PKGeneratorPanel {
 
         if (generator != null) {
             customPKName.setText(generator.getGeneratorName());
-            customPKSize.setText(generator.getKeyCacheSize() != null ? generator
-                    .getKeyCacheSize()
-                    .toString() : "0");
-        }
-        else {
+            customPKSize.setText(generator.getKeyCacheSize() != null ?
+                    generator.getKeyCacheSize().toString() : "0");
+        } else {
             customPKName.setText(null);
             customPKSize.setText(null);
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDBGeneratorPanel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDBGeneratorPanel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDBGeneratorPanel.java
index 845c623..a523a73 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDBGeneratorPanel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDBGeneratorPanel.java
@@ -24,7 +24,6 @@ import java.awt.Component;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.util.Collection;
-import java.util.Iterator;
 
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.JComboBox;
@@ -73,7 +72,7 @@ public class PKDBGeneratorPanel extends PKGeneratorPanel {
         }
     }
 
-    public void onInit(DbEntity entity) {
+    protected void onInitInternal(DbEntity entity) {
         resetStrategy(entity, true, false);
 
         Collection pkAttributes = entity.getPrimaryKeys();

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDefaultGeneratorPanel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDefaultGeneratorPanel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDefaultGeneratorPanel.java
index d28e8b8..76a65ae 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDefaultGeneratorPanel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKDefaultGeneratorPanel.java
@@ -32,7 +32,7 @@ public class PKDefaultGeneratorPanel extends PKGeneratorPanel {
         // noop
     }
 
-    public void onInit(DbEntity entity) {
+    protected void onInitInternal(DbEntity entity) {
         resetStrategy(entity, true, true);
     }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKGeneratorPanel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKGeneratorPanel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKGeneratorPanel.java
index 5253df8..bb6d20e 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKGeneratorPanel.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbentity/PKGeneratorPanel.java
@@ -19,14 +19,13 @@
 
 package org.apache.cayenne.modeler.editor.dbentity;
 
-import java.util.Iterator;
-
 import javax.swing.JPanel;
 
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.event.EntityEvent;
 import org.apache.cayenne.modeler.ProjectController;
+import org.apache.cayenne.modeler.undo.ChangePKGeneratorUndoableEdit;
 
 public abstract class PKGeneratorPanel extends JPanel {
 
@@ -46,7 +45,19 @@ public abstract class PKGeneratorPanel extends JPanel {
     /**
      * Called by parent when the panel becomes visible.
      */
-    public abstract void onInit(DbEntity entity);
+    public void onInit(DbEntity entity) {
+        ChangePKGeneratorUndoableEdit edit = new ChangePKGeneratorUndoableEdit(entity);
+        edit.captureOldState();
+
+        onInitInternal(entity);
+
+        edit.captureNewState();
+        if(edit.hasRealChange()) {
+            mediator.getApplication().getUndoManager().addEdit(edit);
+        }
+    }
+
+    protected abstract void onInitInternal(DbEntity entity);
 
     protected void resetStrategy(
             DbEntity entity,

http://git-wip-us.apache.org/repos/asf/cayenne/blob/17da69bc/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ChangePKGeneratorUndoableEdit.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ChangePKGeneratorUndoableEdit.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ChangePKGeneratorUndoableEdit.java
new file mode 100644
index 0000000..9b41d7c
--- /dev/null
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ChangePKGeneratorUndoableEdit.java
@@ -0,0 +1,132 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.modeler.undo;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbKeyGenerator;
+import org.apache.cayenne.map.event.EntityEvent;
+import org.apache.cayenne.modeler.event.EntityDisplayEvent;
+
+import javax.swing.undo.CannotRedoException;
+import javax.swing.undo.CannotUndoException;
+
+public class ChangePKGeneratorUndoableEdit extends CayenneUndoableEdit {
+
+    private DbEntity dbEntity;
+
+    private PkGeneratorState oldState;
+
+    private PkGeneratorState newState;
+
+    public ChangePKGeneratorUndoableEdit(DbEntity dbEntity) {
+        this.dbEntity = dbEntity;
+    }
+
+    public void captureOldState() {
+        oldState = captureState();
+    }
+
+    public void captureNewState() {
+        newState = captureState();
+    }
+
+    private PkGeneratorState captureState() {
+        return new PkGeneratorState(dbEntity.getPrimaryKeyGenerator(), findGeneratedAttribute());
+    }
+
+    private DbAttribute findGeneratedAttribute() {
+        for (DbAttribute attribute : dbEntity.getPrimaryKeys()) {
+            if(attribute.isGenerated()) {
+                return attribute;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public void redo() throws CannotRedoException {
+        newState.apply();
+        fireEvents();
+    }
+
+    @Override
+    public void undo() throws CannotUndoException {
+        oldState.apply();
+        fireEvents();
+    }
+
+    private void fireEvents() {
+        controller.fireDbEntityEvent(new EntityEvent(this, dbEntity));
+        controller.fireDbEntityDisplayEvent(new EntityDisplayEvent(this, dbEntity));
+    }
+
+    public boolean hasRealChange() {
+        return !oldState.equals(newState);
+    }
+
+    private class PkGeneratorState {
+        private DbKeyGenerator generator;
+        private DbAttribute generatedAttribute;
+        private PkGeneratorState(DbKeyGenerator generator, DbAttribute generatedAttribute)
{
+            this.generator = generator;
+            this.generatedAttribute = generatedAttribute;
+        }
+
+        private void resetState() {
+            DbAttribute oldAttribute = findGeneratedAttribute();
+            if(oldAttribute != null) {
+                oldAttribute.setGenerated(false);
+            }
+            dbEntity.setPrimaryKeyGenerator(null);
+        }
+
+        private void apply() {
+            resetState();
+
+            if(generator != null) {
+                dbEntity.setPrimaryKeyGenerator(generator);
+            }
+            if(generatedAttribute != null) {
+                generatedAttribute.setGenerated(true);
+            }
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+
+            PkGeneratorState that = (PkGeneratorState) o;
+
+            if (generator != null ? !generator.equals(that.generator) : that.generator !=
null) return false;
+            return generatedAttribute != null ? generatedAttribute.equals(that.generatedAttribute)
: that.generatedAttribute == null;
+
+        }
+
+        @Override
+        public int hashCode() {
+            int result = generator != null ? generator.hashCode() : 0;
+            result = 31 * result + (generatedAttribute != null ? generatedAttribute.hashCode()
: 0);
+            return result;
+        }
+    }
+
+}


Mime
View raw message