This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch past-M2 in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit bb61527176a542b20079efe9ea59f8fe1c9eaef3 Author: Andrus Adamchik <[email protected]> AuthorDate: Sat Jun 13 09:27:43 2026 -0400 Modeler cleanup: fix undo Db/Obj handling --- .../modeler/ui/action/CreateAttributeAction.java | 7 +- .../cayenne/modeler/ui/action/RemoveAction.java | 17 +-- .../modeler/ui/action/RemoveAttributeAction.java | 10 +- .../ui/action/RemoveRelationshipAction.java | 7 +- .../autorelationship/InferRelationshipsDialog.java | 4 +- .../ui/dbrelationship/DbRelationshipDialog.java | 10 +- .../relinfo/ObjRelationshipInfoDialog.java | 10 +- .../modeler/undo/CreateAttributeUndoableEdit.java | 118 -------------------- .../undo/CreateDbAttributeUndoableEdit.java | 66 +++++++++++ ....java => CreateDbRelationshipUndoableEdit.java} | 59 +++------- .../undo/CreateObjAttributeUndoableEdit.java | 66 +++++++++++ ...java => CreateObjRelationshipUndoableEdit.java} | 54 +++------ .../modeler/undo/DbEntitySyncUndoableEdit.java | 6 +- .../modeler/undo/DbRelationshipUndoableEdit.java | 80 +++++++++++++ .../modeler/undo/ObjRelationshipUndoableEdit.java | 79 +++++++++++++ .../modeler/undo/RelationshipUndoableEdit.java | 124 --------------------- .../modeler/undo/RemoveAttributeUndoableEdit.java | 120 -------------------- .../undo/RemoveDbAttributeUndoableEdit.java | 60 ++++++++++ ....java => RemoveDbRelationshipUndoableEdit.java} | 47 ++------ .../RemoveEmbeddableAttributeUndoableEdit.java | 61 ++++++++++ .../undo/RemoveObjAttributeUndoableEdit.java | 60 ++++++++++ ...java => RemoveObjRelationshipUndoableEdit.java} | 47 ++------ 22 files changed, 563 insertions(+), 549 deletions(-) diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/CreateAttributeAction.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/CreateAttributeAction.java index 99ad1a671..7f0c42983 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/CreateAttributeAction.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/CreateAttributeAction.java @@ -41,7 +41,8 @@ import org.apache.cayenne.modeler.event.display.DbAttributeDisplayEvent; import org.apache.cayenne.modeler.event.display.EmbeddableAttributeDisplayEvent; import org.apache.cayenne.modeler.event.display.ObjAttributeDisplayEvent; import org.apache.cayenne.modeler.toolkit.AppAction; -import org.apache.cayenne.modeler.undo.CreateAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.CreateDbAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.CreateObjAttributeUndoableEdit; import org.apache.cayenne.modeler.undo.CreateEmbAttributeUndoableEdit; import java.awt.event.ActionEvent; @@ -128,7 +129,7 @@ public class CreateAttributeAction extends AppAction { createObjAttribute(session.getSelectedDataMap(), objEntity, attr); app.getUndoManager().addEdit( - new CreateAttributeUndoableEdit(session, (DataChannelDescriptor) session.project().getRootNode(), + new CreateObjAttributeUndoableEdit(session, (DataChannelDescriptor) session.project().getRootNode(), session.getSelectedDataMap(), objEntity, attr)); } else if (getProjectSession().getSelectedDbEntity() != null) { DbEntity dbEntity = getProjectSession().getSelectedDbEntity(); @@ -141,7 +142,7 @@ public class CreateAttributeAction extends AppAction { createDbAttribute(session.getSelectedDataMap(), dbEntity, attr); app.getUndoManager().addEdit( - new CreateAttributeUndoableEdit(session, (DataChannelDescriptor) session.project().getRootNode(), + new CreateDbAttributeUndoableEdit(session, (DataChannelDescriptor) session.project().getRootNode(), session.getSelectedDataMap(), dbEntity, attr)); } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAction.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAction.java index 00cecb83a..9a50e8a4a 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAction.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAction.java @@ -59,10 +59,13 @@ import org.apache.cayenne.modeler.ui.confirmremove.ConfirmRemoveDialog; import org.apache.cayenne.modeler.project.ProjectSession; import org.apache.cayenne.modeler.ui.project.editor.objentity.callbacks.CallbackType; import org.apache.cayenne.modeler.ui.project.editor.objentity.callbacks.ObjCallbackMethod; -import org.apache.cayenne.modeler.undo.RemoveAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveDbAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveDbRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveEmbeddableAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveObjAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveObjRelationshipUndoableEdit; import org.apache.cayenne.modeler.undo.RemoveCallbackMethodUndoableEdit; import org.apache.cayenne.modeler.undo.RemoveCompoundUndoableEdit; -import org.apache.cayenne.modeler.undo.RemoveRelationshipUndoableEdit; import org.apache.cayenne.modeler.undo.RemoveUndoableEdit; import javax.swing.*; @@ -230,7 +233,7 @@ public class RemoveAction extends AppAction { Embeddable embeddable = session.getSelectedEmbeddable(); app.getUndoManager() - .addEdit(new RemoveAttributeUndoableEdit(session,embeddable, embAttrs)); + .addEdit(new RemoveEmbeddableAttributeUndoableEdit(session, embeddable, embAttrs)); for (EmbeddableAttribute attrib : embAttrs) { embeddable.removeAttribute(attrib.getName()); @@ -251,7 +254,7 @@ public class RemoveAction extends AppAction { ObjEntity entity = session.getSelectedObjEntity(); - app.getUndoManager().addEdit(new RemoveAttributeUndoableEdit(session,entity, objAttrs)); + app.getUndoManager().addEdit(new RemoveObjAttributeUndoableEdit(session, entity, objAttrs)); for (ObjAttribute attrib : objAttrs) { entity.removeAttribute(attrib.getName()); @@ -272,7 +275,7 @@ public class RemoveAction extends AppAction { DbEntity entity = session.getSelectedDbEntity(); app.getUndoManager() - .addEdit(new RemoveAttributeUndoableEdit(session,entity, dbAttrs)); + .addEdit(new RemoveDbAttributeUndoableEdit(session, entity, dbAttrs)); for (DbAttribute attrib : dbAttrs) { entity.removeAttribute(attrib.getName()); @@ -298,7 +301,7 @@ public class RemoveAction extends AppAction { } DataMapOps.removeBrokenObjToDbMappings(session.getSelectedDataMap()); - app.getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(session,entity, dbRels)); + app.getUndoManager().addEdit(new RemoveDbRelationshipUndoableEdit(session, entity, dbRels)); } } } @@ -312,7 +315,7 @@ public class RemoveAction extends AppAction { ObjRelationshipEvent e = ObjRelationshipEvent.ofRemove(app.getFrame(), rel, entity); session.fireObjRelationshipEvent(e); } - app.getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(session,entity, rels)); + app.getUndoManager().addEdit(new RemoveObjRelationshipUndoableEdit(session, entity, rels)); } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAttributeAction.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAttributeAction.java index 62211c1e8..ef1e9cba2 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAttributeAction.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveAttributeAction.java @@ -36,7 +36,9 @@ import org.apache.cayenne.modeler.project.DataMapOps; import org.apache.cayenne.modeler.project.ObjEntityOps; import org.apache.cayenne.modeler.ui.confirmremove.ConfirmRemoveDialog; import org.apache.cayenne.modeler.project.ProjectSession; -import org.apache.cayenne.modeler.undo.RemoveAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveDbAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveEmbeddableAttributeUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveObjAttributeUndoableEdit; import java.awt.event.ActionEvent; import java.util.Collection; @@ -87,7 +89,7 @@ public class RemoveAttributeAction extends RemoveAction implements MultipleObjec Embeddable embeddable = session.getSelectedEmbeddable(); app.getUndoManager().addEdit( - new RemoveAttributeUndoableEdit(session,embeddable, embAttrs)); + new RemoveEmbeddableAttributeUndoableEdit(session, embeddable, embAttrs)); removeEmbeddableAttributes(embeddable, embAttrs); @@ -99,7 +101,7 @@ public class RemoveAttributeAction extends RemoveAction implements MultipleObjec ObjEntity entity = session.getSelectedObjEntity(); - app.getUndoManager().addEdit(new RemoveAttributeUndoableEdit(session,entity, objAttrs)); + app.getUndoManager().addEdit(new RemoveObjAttributeUndoableEdit(session, entity, objAttrs)); removeObjAttributes(entity, objAttrs); } @@ -110,7 +112,7 @@ public class RemoveAttributeAction extends RemoveAction implements MultipleObjec DbEntity entity = session.getSelectedDbEntity(); - app.getUndoManager().addEdit(new RemoveAttributeUndoableEdit(session,entity, dbAttrs)); + app.getUndoManager().addEdit(new RemoveDbAttributeUndoableEdit(session, entity, dbAttrs)); removeDbAttributes(session.getSelectedDataMap(), entity, dbAttrs); } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveRelationshipAction.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveRelationshipAction.java index 386916c56..347d5dc7c 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveRelationshipAction.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/RemoveRelationshipAction.java @@ -31,7 +31,8 @@ import org.apache.cayenne.modeler.event.model.ObjRelationshipEvent; import org.apache.cayenne.modeler.project.DataMapOps; import org.apache.cayenne.modeler.ui.confirmremove.ConfirmRemoveDialog; import org.apache.cayenne.modeler.project.ProjectSession; -import org.apache.cayenne.modeler.undo.RemoveRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveDbRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.RemoveObjRelationshipUndoableEdit; import java.awt.event.ActionEvent; @@ -78,7 +79,7 @@ public class RemoveRelationshipAction extends RemoveAction implements MultipleOb .shouldDelete("selected ObjRelationships"))) { ObjEntity entity = session.getSelectedObjEntity(); removeObjRelationships(entity, rels); - app.getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(session,entity, rels)); + app.getUndoManager().addEdit(new RemoveObjRelationshipUndoableEdit(session, entity, rels)); } } else { DbRelationship[] dbRels = getProjectSession() @@ -90,7 +91,7 @@ public class RemoveRelationshipAction extends RemoveAction implements MultipleOb .shouldDelete("selected DbRelationships"))) { DbEntity entity = session.getSelectedDbEntity(); removeDbRelationships(entity, dbRels); - app.getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(session,entity, dbRels)); + app.getUndoManager().addEdit(new RemoveDbRelationshipUndoableEdit(session, entity, dbRels)); } } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java index 9c441f862..852ca506e 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java @@ -28,7 +28,7 @@ import org.apache.cayenne.modeler.event.model.DbRelationshipEvent; import org.apache.cayenne.modeler.service.classloader.ModelerClassLoader; import org.apache.cayenne.modeler.toolkit.ProjectDialog; import org.apache.cayenne.modeler.project.ProjectSession; -import org.apache.cayenne.modeler.undo.CreateRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.CreateDbRelationshipUndoableEdit; import org.apache.cayenne.modeler.undo.InferRelationshipsUndoableEdit; import org.apache.cayenne.modeler.NameGeneratorPreferences; import org.slf4j.Logger; @@ -382,7 +382,7 @@ public class InferRelationshipsDialog extends ProjectDialog { rel.setToMany(temp.isToMany()); temp.getSource().addRelationship(rel); - undoableEdit.addEdit(new CreateRelationshipUndoableEdit( + undoableEdit.addEdit(new CreateDbRelationshipUndoableEdit( session, temp.getSource(), new DbRelationship[]{rel})); } JOptionPane.showMessageDialog(this, getSelectedEntitiesSize() + " relationships generated"); diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/dbrelationship/DbRelationshipDialog.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/dbrelationship/DbRelationshipDialog.java index 0d83b75b8..9e5b29f94 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/dbrelationship/DbRelationshipDialog.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/dbrelationship/DbRelationshipDialog.java @@ -41,8 +41,8 @@ import org.apache.cayenne.modeler.toolkit.combobox.AutoCompletion; import org.apache.cayenne.modeler.toolkit.combobox.CMAutoCompleteComboBoxCellEditor; import org.apache.cayenne.modeler.toolkit.combobox.CMComboBox; import org.apache.cayenne.modeler.toolkit.table.CMTable; -import org.apache.cayenne.modeler.undo.CreateRelationshipUndoableEdit; -import org.apache.cayenne.modeler.undo.RelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.CreateDbRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.DbRelationshipUndoableEdit; import org.apache.cayenne.project.extension.info.ObjectInfo; import javax.swing.*; @@ -85,7 +85,7 @@ public class DbRelationshipDialog extends ProjectDialog { private final DbRelationship reverseRelationship; private final boolean create; private boolean saved; - private final RelationshipUndoableEdit undo; + private final DbRelationshipUndoableEdit undo; // dialog-local editing state, applied to the relationship only on save private DbEntity currentTarget; @@ -117,7 +117,7 @@ public class DbRelationshipDialog extends ProjectDialog { this.relationship = relationship; this.create = create; - this.undo = new RelationshipUndoableEdit(session, relationship); + this.undo = new DbRelationshipUndoableEdit(session, relationship); this.reverseRelationship = relationship.getReverseRelationship(); this.currentTarget = relationship.getTargetEntity(); @@ -389,7 +389,7 @@ public class DbRelationshipDialog extends ProjectDialog { session.displayDbRelationship(rde); app.getUndoManager().addEdit( - new CreateRelationshipUndoableEdit(session, relationship.getSourceEntity(), + new CreateDbRelationshipUndoableEdit(session, relationship.getSourceEntity(), new DbRelationship[]{relationship})); } else { diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/relinfo/ObjRelationshipInfoDialog.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/relinfo/ObjRelationshipInfoDialog.java index d085d39e8..3b0d9f09f 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/relinfo/ObjRelationshipInfoDialog.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/project/editor/objentity/relinfo/ObjRelationshipInfoDialog.java @@ -41,8 +41,8 @@ import org.apache.cayenne.modeler.toolkit.combobox.CMComboBox; import org.apache.cayenne.modeler.toolkit.tree.EntityTreeModel; import org.apache.cayenne.modeler.toolkit.tree.EntityTreeRelationshipFilter; import org.apache.cayenne.modeler.ui.dbrelationship.DbRelationshipDialog; -import org.apache.cayenne.modeler.undo.CreateRelationshipUndoableEdit; -import org.apache.cayenne.modeler.undo.RelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.CreateObjRelationshipUndoableEdit; +import org.apache.cayenne.modeler.undo.ObjRelationshipUndoableEdit; import org.apache.cayenne.project.extension.info.ObjectInfo; import org.apache.cayenne.util.DeleteRuleUpdater; @@ -102,7 +102,7 @@ public class ObjRelationshipInfoDialog extends ProjectDialog implements TreeSele private String targetCollection; private String mapKey; - private RelationshipUndoableEdit undo; + private ObjRelationshipUndoableEdit undo; private boolean isCreate; public ObjRelationshipInfoDialog(ProjectSession session, Window owner) { @@ -149,7 +149,7 @@ public class ObjRelationshipInfoDialog extends ProjectDialog implements TreeSele public ObjRelationshipInfoDialog modifyRelationship(ObjRelationship rel) { this.relationship = rel; - this.undo = new RelationshipUndoableEdit(session, rel); + this.undo = new ObjRelationshipUndoableEdit(session, rel); // current limitation is that an ObjRelationship must have source // and target entities present, with DbEntities chosen. @@ -320,7 +320,7 @@ public class ObjRelationshipInfoDialog extends ProjectDialog implements TreeSele if (isCreate) { relationship.getSourceEntity().addRelationship(relationship); fireObjRelationshipEvent(this); - app.getUndoManager().addEdit(new CreateRelationshipUndoableEdit( + app.getUndoManager().addEdit(new CreateObjRelationshipUndoableEdit( session, relationship.getSourceEntity(), new ObjRelationship[]{relationship})); } else { session.fireObjRelationshipEvent(ObjRelationshipEvent.ofChange(this, relationship, diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateAttributeUndoableEdit.java deleted file mode 100644 index 87b545646..000000000 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateAttributeUndoableEdit.java +++ /dev/null @@ -1,118 +0,0 @@ -/***************************************************************** - * 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 - * - * https://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 javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; - -import org.apache.cayenne.configuration.DataChannelDescriptor; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; -import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; -import org.apache.cayenne.modeler.project.ProjectSession; -import org.apache.cayenne.modeler.event.display.DbEntityDisplayEvent; -import org.apache.cayenne.modeler.event.display.ObjEntityDisplayEvent; - -public class CreateAttributeUndoableEdit extends CayenneUndoableEdit { - - @Override - public boolean canRedo() { - return true; - } - - @Override - public String getPresentationName() { - return "Create Attribute"; - } - - private ObjEntity objEntity; - private ObjAttribute objAttr; - - private DataChannelDescriptor domain; - private DataMap dataMap; - - private DbEntity dbEntity; - private DbAttribute dbAttr; - - @Override - public void redo() throws CannotRedoException { - CreateAttributeAction action = globalActions - .getAction(CreateAttributeAction.class); - - if (objEntity != null) { - action.createObjAttribute(dataMap, objEntity, objAttr); - } - - if (dbEntity != null) { - action.createDbAttribute(dataMap, dbEntity, dbAttr); - } - } - - @Override - public void undo() throws CannotUndoException { - RemoveAttributeAction action = globalActions - .getAction(RemoveAttributeAction.class); - - if (objEntity != null) { - action.removeObjAttributes(objEntity, new ObjAttribute[] { - objAttr - }); - - session.displayObjEntity(new ObjEntityDisplayEvent( - this, - domain, - dataMap, - objEntity)); - } - - if (dbEntity != null) { - action.removeDbAttributes(dataMap, dbEntity, new DbAttribute[] { - dbAttr - }); - - session.displayDbEntity(new DbEntityDisplayEvent( - this, - domain, - dataMap, - dbEntity)); - } - } - - public CreateAttributeUndoableEdit(ProjectSession session, DataChannelDescriptor domain, DataMap map, - ObjEntity objEntity, ObjAttribute attr) { - super(session); - this.domain = domain; - this.dataMap = map; - this.objEntity = objEntity; - this.objAttr = attr; - } - - public CreateAttributeUndoableEdit(ProjectSession session, DataChannelDescriptor domain, DataMap map, - DbEntity dbEntity, DbAttribute attr) { - super(session); - this.domain = domain; - this.dataMap = map; - this.dbEntity = dbEntity; - this.dbAttr = attr; - } -} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbAttributeUndoableEdit.java new file mode 100644 index 000000000..0adca7cfa --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbAttributeUndoableEdit.java @@ -0,0 +1,66 @@ +/***************************************************************** + * 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 + * + * https://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 javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +import org.apache.cayenne.configuration.DataChannelDescriptor; +import org.apache.cayenne.map.DataMap; +import org.apache.cayenne.map.DbAttribute; +import org.apache.cayenne.map.DbEntity; +import org.apache.cayenne.modeler.event.display.DbEntityDisplayEvent; +import org.apache.cayenne.modeler.project.ProjectSession; +import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; +import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; + +public class CreateDbAttributeUndoableEdit extends CayenneUndoableEdit { + + private final DataChannelDescriptor domain; + private final DataMap dataMap; + private final DbEntity dbEntity; + private final DbAttribute attribute; + + public CreateDbAttributeUndoableEdit(ProjectSession session, DataChannelDescriptor domain, DataMap map, + DbEntity dbEntity, DbAttribute attribute) { + super(session); + this.domain = domain; + this.dataMap = map; + this.dbEntity = dbEntity; + this.attribute = attribute; + } + + @Override + public String getPresentationName() { + return "Create Attribute"; + } + + @Override + public void redo() throws CannotRedoException { + CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); + action.createDbAttribute(dataMap, dbEntity, attribute); + } + + @Override + public void undo() throws CannotUndoException { + RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); + action.removeDbAttributes(dataMap, dbEntity, new DbAttribute[] {attribute}); + session.displayDbEntity(new DbEntityDisplayEvent(this, domain, dataMap, dbEntity)); + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbRelationshipUndoableEdit.java similarity index 52% rename from modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateRelationshipUndoableEdit.java rename to modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbRelationshipUndoableEdit.java index 70661f3d5..db3d67e90 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateRelationshipUndoableEdit.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateDbRelationshipUndoableEdit.java @@ -24,32 +24,20 @@ import javax.swing.undo.CannotUndoException; import org.apache.cayenne.configuration.DataChannelDescriptor; import org.apache.cayenne.map.DbEntity; import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; +import org.apache.cayenne.modeler.event.display.DbEntityDisplayEvent; +import org.apache.cayenne.modeler.project.ProjectSession; import org.apache.cayenne.modeler.ui.action.CreateRelationshipAction; import org.apache.cayenne.modeler.ui.action.RemoveRelationshipAction; -import org.apache.cayenne.modeler.project.ProjectSession; -import org.apache.cayenne.modeler.event.display.DbEntityDisplayEvent; -import org.apache.cayenne.modeler.event.display.ObjEntityDisplayEvent; -public class CreateRelationshipUndoableEdit extends CayenneUndoableEdit { +public class CreateDbRelationshipUndoableEdit extends CayenneUndoableEdit { - private ObjEntity objEnt; - private ObjRelationship[] objectRel; - - private DbEntity dbEnt; - private DbRelationship[] dbRel; - - public CreateRelationshipUndoableEdit(ProjectSession session, ObjEntity objEnt, ObjRelationship[] objectRel) { - super(session); - this.objEnt = objEnt; - this.objectRel = objectRel; - } + private final DbEntity dbEntity; + private final DbRelationship[] relationships; - public CreateRelationshipUndoableEdit(ProjectSession session, DbEntity dbEnt, DbRelationship[] dbRel) { + public CreateDbRelationshipUndoableEdit(ProjectSession session, DbEntity dbEntity, DbRelationship[] relationships) { super(session); - this.dbEnt = dbEnt; - this.dbRel = dbRel; + this.dbEntity = dbEntity; + this.relationships = relationships; } @Override @@ -59,36 +47,17 @@ public class CreateRelationshipUndoableEdit extends CayenneUndoableEdit { @Override public void redo() throws CannotRedoException { - CreateRelationshipAction action = globalActions - .getAction(CreateRelationshipAction.class); - - if (objEnt != null) { - for (ObjRelationship rel : objectRel) { - action.createObjRelationship(objEnt, rel); - } - } - - if (dbEnt != null) { - for (DbRelationship rel : dbRel) { - action.createDbRelationship(dbEnt, rel); - } + CreateRelationshipAction action = globalActions.getAction(CreateRelationshipAction.class); + for (DbRelationship rel : relationships) { + action.createDbRelationship(dbEntity, rel); } } @Override public void undo() throws CannotUndoException { - RemoveRelationshipAction action = globalActions - .getAction(RemoveRelationshipAction.class); - + RemoveRelationshipAction action = globalActions.getAction(RemoveRelationshipAction.class); DataChannelDescriptor domain = (DataChannelDescriptor) session.project().getRootNode(); - if (objEnt != null) { - action.removeObjRelationships(objEnt, objectRel); - session.displayObjEntity(new ObjEntityDisplayEvent(this, domain, objEnt.getDataMap(), objEnt)); - } - - if (dbEnt != null) { - action.removeDbRelationships(dbEnt, dbRel); - session.displayDbEntity(new DbEntityDisplayEvent(this, domain, dbEnt.getDataMap(), dbEnt)); - } + action.removeDbRelationships(dbEntity, relationships); + session.displayDbEntity(new DbEntityDisplayEvent(this, domain, dbEntity.getDataMap(), dbEntity)); } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjAttributeUndoableEdit.java new file mode 100644 index 000000000..eeb161151 --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjAttributeUndoableEdit.java @@ -0,0 +1,66 @@ +/***************************************************************** + * 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 + * + * https://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 javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +import org.apache.cayenne.configuration.DataChannelDescriptor; +import org.apache.cayenne.map.DataMap; +import org.apache.cayenne.map.ObjAttribute; +import org.apache.cayenne.map.ObjEntity; +import org.apache.cayenne.modeler.event.display.ObjEntityDisplayEvent; +import org.apache.cayenne.modeler.project.ProjectSession; +import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; +import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; + +public class CreateObjAttributeUndoableEdit extends CayenneUndoableEdit { + + private final DataChannelDescriptor domain; + private final DataMap dataMap; + private final ObjEntity objEntity; + private final ObjAttribute attribute; + + public CreateObjAttributeUndoableEdit(ProjectSession session, DataChannelDescriptor domain, DataMap map, + ObjEntity objEntity, ObjAttribute attribute) { + super(session); + this.domain = domain; + this.dataMap = map; + this.objEntity = objEntity; + this.attribute = attribute; + } + + @Override + public String getPresentationName() { + return "Create Attribute"; + } + + @Override + public void redo() throws CannotRedoException { + CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); + action.createObjAttribute(dataMap, objEntity, attribute); + } + + @Override + public void undo() throws CannotUndoException { + RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); + action.removeObjAttributes(objEntity, new ObjAttribute[] {attribute}); + session.displayObjEntity(new ObjEntityDisplayEvent(this, domain, dataMap, objEntity)); + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjRelationshipUndoableEdit.java similarity index 58% copy from modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java copy to modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjRelationshipUndoableEdit.java index 544e5718e..0d7dac0bd 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/CreateObjRelationshipUndoableEdit.java @@ -21,65 +21,43 @@ package org.apache.cayenne.modeler.undo; import javax.swing.undo.CannotRedoException; import javax.swing.undo.CannotUndoException; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; +import org.apache.cayenne.configuration.DataChannelDescriptor; import org.apache.cayenne.map.ObjEntity; import org.apache.cayenne.map.ObjRelationship; +import org.apache.cayenne.modeler.event.display.ObjEntityDisplayEvent; +import org.apache.cayenne.modeler.project.ProjectSession; import org.apache.cayenne.modeler.ui.action.CreateRelationshipAction; import org.apache.cayenne.modeler.ui.action.RemoveRelationshipAction; -import org.apache.cayenne.modeler.project.ProjectSession; -public class RemoveRelationshipUndoableEdit extends BaseRemovePropertyUndoableEdit { +public class CreateObjRelationshipUndoableEdit extends CayenneUndoableEdit { - private ObjRelationship[] rels; - private DbRelationship[] dbRels; + private final ObjEntity objEntity; + private final ObjRelationship[] relationships; - public RemoveRelationshipUndoableEdit(ProjectSession session, ObjEntity objEntity, ObjRelationship[] rels) { + public CreateObjRelationshipUndoableEdit(ProjectSession session, ObjEntity objEntity, ObjRelationship[] relationships) { super(session); this.objEntity = objEntity; - this.rels = rels; - } - - public RemoveRelationshipUndoableEdit(ProjectSession session, DbEntity dbEntity, DbRelationship[] dbRels) { - super(session); - this.dbEntity = dbEntity; - this.dbRels = dbRels; + this.relationships = relationships; } @Override public String getPresentationName() { - if (objEntity != null) { - return "Remove Obj Relationship"; - } else { - return "Remove Db Relationship"; - } + return "Create Relationship"; } @Override public void redo() throws CannotRedoException { - RemoveRelationshipAction action = globalActions.getAction(RemoveRelationshipAction.class); - if (objEntity != null) { - action.removeObjRelationships(objEntity, rels); - focusObjEntity(); - } else { - action.removeDbRelationships(dbEntity, dbRels); - focusDBEntity(); + CreateRelationshipAction action = globalActions.getAction(CreateRelationshipAction.class); + for (ObjRelationship rel : relationships) { + action.createObjRelationship(objEntity, rel); } } @Override public void undo() throws CannotUndoException { - CreateRelationshipAction action = globalActions.getAction(CreateRelationshipAction.class); - if (objEntity != null) { - for (ObjRelationship r : rels) { - action.createObjRelationship(objEntity, r); - } - focusObjEntity(); - } else { - for (DbRelationship dr : dbRels) { - action.createDbRelationship(dbEntity, dr); - } - focusDBEntity(); - } + RemoveRelationshipAction action = globalActions.getAction(RemoveRelationshipAction.class); + DataChannelDescriptor domain = (DataChannelDescriptor) session.project().getRootNode(); + action.removeObjRelationships(objEntity, relationships); + session.displayObjEntity(new ObjEntityDisplayEvent(this, domain, objEntity.getDataMap(), objEntity)); } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbEntitySyncUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbEntitySyncUndoableEdit.java index d41077de3..2f08412a2 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbEntitySyncUndoableEdit.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbEntitySyncUndoableEdit.java @@ -62,11 +62,11 @@ public class DbEntitySyncUndoableEdit extends CompoundEdit { } public void objRelationshipAdded(ObjRelationship rel) { - addEdit(new CreateRelationshipUndoableEdit(session, entity, new ObjRelationship[] {rel})); + addEdit(new CreateObjRelationshipUndoableEdit(session, entity, new ObjRelationship[] {rel})); } public void objAttributeAdded(ObjAttribute attr) { - addEdit(new CreateAttributeUndoableEdit(session, domain, map, entity, attr)); + addEdit(new CreateObjAttributeUndoableEdit(session, domain, map, entity, attr)); } } @@ -86,7 +86,7 @@ public class DbEntitySyncUndoableEdit extends CompoundEdit { for (DbAttribute da : dbAttrs) { ObjAttribute oa = entity.getAttributeForDbAttribute(da); if (oa != null) { - addEdit(new RemoveAttributeUndoableEdit(session, entity, new ObjAttribute[] {oa})); + addEdit(new RemoveObjAttributeUndoableEdit(session, entity, new ObjAttribute[] {oa})); } } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbRelationshipUndoableEdit.java new file mode 100644 index 000000000..ea8439217 --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/DbRelationshipUndoableEdit.java @@ -0,0 +1,80 @@ +/***************************************************************** + * 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 + * + * https://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.DbEntity; +import org.apache.cayenne.map.DbJoin; +import org.apache.cayenne.map.DbRelationship; +import org.apache.cayenne.modeler.event.model.DbRelationshipEvent; +import org.apache.cayenne.modeler.project.ProjectSession; + +import javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +public class DbRelationshipUndoableEdit extends CayenneUndoableEdit { + + private final DbRelationship relationship; + private final DbRelationship prevRelationship; + + public DbRelationshipUndoableEdit(ProjectSession session, DbRelationship relationship) { + super(session); + this.relationship = relationship; + this.prevRelationship = copyRelationship(relationship); + } + + @Override + public void redo() throws CannotRedoException { + fireRelationshipEvent(relationship, prevRelationship); + } + + @Override + public void undo() throws CannotUndoException { + fireRelationshipEvent(prevRelationship, relationship); + } + + private void fireRelationshipEvent(DbRelationship relToFire, DbRelationship currRel) { + DbEntity dbEntity = currRel.getSourceEntity(); + dbEntity.removeRelationship(currRel.getName()); + dbEntity.addRelationship(relToFire); + session.fireDbRelationshipEvent(DbRelationshipEvent.ofAdd(this, relToFire, relToFire.getSourceEntity())); + } + + @Override + public String getRedoPresentationName() { + return "Redo Edit relationship"; + } + + @Override + public String getUndoPresentationName() { + return "Undo Edit relationship"; + } + + private DbRelationship copyRelationship(DbRelationship dbRelationship) { + DbRelationship rel = new DbRelationship(); + rel.setName(dbRelationship.getName()); + rel.setToDependentPK(dbRelationship.isToDependentPK()); + rel.setToMany(dbRelationship.isToMany()); + rel.setTargetEntityName(dbRelationship.getTargetEntityName()); + rel.setSourceEntity(dbRelationship.getSourceEntity()); + rel.setJoins(dbRelationship.getJoins().stream() + .map(j -> new DbJoin(rel, j.getSourceName(), j.getTargetName())) + .toList()); + return rel; + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ObjRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ObjRelationshipUndoableEdit.java new file mode 100644 index 000000000..547afbc1d --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/ObjRelationshipUndoableEdit.java @@ -0,0 +1,79 @@ +/***************************************************************** + * 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 + * + * https://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.ObjEntity; +import org.apache.cayenne.map.ObjRelationship; +import org.apache.cayenne.modeler.event.model.ObjRelationshipEvent; +import org.apache.cayenne.modeler.project.ProjectSession; + +import javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +public class ObjRelationshipUndoableEdit extends CayenneUndoableEdit { + + private final ObjRelationship relationship; + private final ObjRelationship prevRelationship; + + public ObjRelationshipUndoableEdit(ProjectSession session, ObjRelationship relationship) { + super(session); + this.relationship = relationship; + this.prevRelationship = copyRelationship(relationship); + } + + @Override + public void redo() throws CannotRedoException { + fireRelationshipEvent(relationship, prevRelationship); + } + + @Override + public void undo() throws CannotUndoException { + fireRelationshipEvent(prevRelationship, relationship); + } + + private void fireRelationshipEvent(ObjRelationship relToFire, ObjRelationship currRel) { + ObjEntity objEntity = currRel.getSourceEntity(); + objEntity.removeRelationship(currRel.getName()); + objEntity.addRelationship(relToFire); + session.fireObjRelationshipEvent(ObjRelationshipEvent.ofAdd(this, relToFire, relToFire.getSourceEntity())); + } + + @Override + public String getRedoPresentationName() { + return "Redo Edit relationship"; + } + + @Override + public String getUndoPresentationName() { + return "Undo Edit relationship"; + } + + private ObjRelationship copyRelationship(ObjRelationship objRelationship) { + ObjRelationship rel = new ObjRelationship(); + rel.setName(objRelationship.getName()); + rel.setTargetEntityName(objRelationship.getTargetEntityName()); + rel.setSourceEntity(objRelationship.getSourceEntity()); + rel.setDeleteRule(objRelationship.getDeleteRule()); + rel.setUsedForLocking(objRelationship.isUsedForLocking()); + rel.setDbRelationshipPath(objRelationship.getDbRelationshipPath()); + rel.setCollectionType(objRelationship.getCollectionType()); + rel.setMapKey(objRelationship.getMapKey()); + return rel; + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RelationshipUndoableEdit.java deleted file mode 100644 index b7c41913b..000000000 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RelationshipUndoableEdit.java +++ /dev/null @@ -1,124 +0,0 @@ -/***************************************************************** - * 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 - * - * https://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.DbEntity; -import org.apache.cayenne.map.DbJoin; -import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; -import org.apache.cayenne.map.Relationship; -import org.apache.cayenne.modeler.event.model.ObjRelationshipEvent; -import org.apache.cayenne.modeler.event.model.DbRelationshipEvent; -import org.apache.cayenne.modeler.project.ProjectSession; - -import javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; - -public class RelationshipUndoableEdit extends CayenneUndoableEdit { - - private final Relationship<?,?,?> relationship; - private final Relationship<?,?,?> prevRelationship; - private final boolean useDb; - - public RelationshipUndoableEdit(ProjectSession session, Relationship<?,?,?> relationship) { - super(session); - this.relationship = relationship; - this.useDb = relationship instanceof DbRelationship; - this.prevRelationship = copyRelationship(relationship); - } - - @Override - public void redo() throws CannotRedoException { - fireRelationshipEvent(relationship, prevRelationship); - } - - @Override - public void undo() throws CannotUndoException { - fireRelationshipEvent(prevRelationship, relationship); - } - - private void fireRelationshipEvent(Relationship<?,?,?> relToFire, Relationship<?,?,?> currRel) { - if(useDb) { - fireDbRelationshipEvent((DbRelationship) relToFire, (DbRelationship)currRel); - } else { - fireObjRelationshipEvent((ObjRelationship) relToFire, (ObjRelationship) currRel); - } - } - - private void fireDbRelationshipEvent(DbRelationship relToFire, DbRelationship currRel) { - DbEntity dbEntity = currRel.getSourceEntity(); - dbEntity.removeRelationship(currRel.getName()); - dbEntity.addRelationship(relToFire); - session - .fireDbRelationshipEvent( - DbRelationshipEvent.ofAdd(this, relToFire, relToFire.getSourceEntity())); - } - - private void fireObjRelationshipEvent(ObjRelationship relToFire, ObjRelationship currRel) { - ObjEntity objEntity = currRel.getSourceEntity(); - objEntity.removeRelationship(currRel.getName()); - objEntity.addRelationship(relToFire); - session - .fireObjRelationshipEvent( - ObjRelationshipEvent.ofAdd(this, relToFire, relToFire.getSourceEntity())); - } - - @Override - public String getRedoPresentationName() { - return "Redo Edit relationship"; - } - - @Override - public String getUndoPresentationName() { - return "Undo Edit relationship"; - } - - private Relationship<?,?,?> copyRelationship(Relationship<?,?,?> relationship) { - return useDb - ? getDbRelationship((DbRelationship) relationship) - : getObjRelationship((ObjRelationship) relationship); - } - - private DbRelationship getDbRelationship(DbRelationship dbRelationship) { - DbRelationship rel = new DbRelationship(); - rel.setName(dbRelationship.getName()); - rel.setToDependentPK(dbRelationship.isToDependentPK()); - rel.setToMany(dbRelationship.isToMany()); - rel.setTargetEntityName(dbRelationship.getTargetEntityName()); - rel.setSourceEntity(dbRelationship.getSourceEntity()); - rel.setJoins(dbRelationship.getJoins().stream() - .map(j -> new DbJoin(rel, j.getSourceName(), j.getTargetName())) - .toList()); - return rel; - } - - private ObjRelationship getObjRelationship(ObjRelationship objRelationship) { - ObjRelationship rel = new ObjRelationship(); - rel.setName(objRelationship.getName()); - rel.setTargetEntityName(objRelationship.getTargetEntityName()); - rel.setSourceEntity(objRelationship.getSourceEntity()); - rel.setDeleteRule(objRelationship.getDeleteRule()); - rel.setUsedForLocking(objRelationship.isUsedForLocking()); - rel.setDbRelationshipPath(objRelationship.getDbRelationshipPath()); - rel.setCollectionType(objRelationship.getCollectionType()); - rel.setMapKey(objRelationship.getMapKey()); - return rel; - } -} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveAttributeUndoableEdit.java deleted file mode 100644 index 2d95dde5c..000000000 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveAttributeUndoableEdit.java +++ /dev/null @@ -1,120 +0,0 @@ -/***************************************************************** - * 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 - * - * https://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 javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.Embeddable; -import org.apache.cayenne.map.EmbeddableAttribute; -import org.apache.cayenne.map.ObjAttribute; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; -import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; -import org.apache.cayenne.modeler.project.ProjectSession; - -public class RemoveAttributeUndoableEdit extends BaseRemovePropertyUndoableEdit { - - private DbAttribute[] dbAttributes; - private ObjAttribute[] objAttributes; - private EmbeddableAttribute[] embeddableAttrs; - - public RemoveAttributeUndoableEdit(ProjectSession session, Embeddable embeddable, EmbeddableAttribute[] embeddableAttributes) { - super(session); - this.embeddable = embeddable; - this.embeddableAttrs = embeddableAttributes; - } - - public RemoveAttributeUndoableEdit(ProjectSession session, ObjEntity entity, ObjAttribute[] objAttributes) { - super(session); - this.objEntity = entity; - this.objAttributes = objAttributes; - } - - public RemoveAttributeUndoableEdit(ProjectSession session, DbEntity entity, DbAttribute[] dbAttributes) { - super(session); - this.dbEntity = entity; - this.dbAttributes = dbAttributes; - } - - @Override - public void redo() throws CannotRedoException { - RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); - - if (objEntity != null) { - action.removeObjAttributes(objEntity, objAttributes); - focusObjEntity(); - } - - if (dbEntity != null) { - action.removeDbAttributes(dbEntity.getDataMap(), dbEntity, dbAttributes); - focusDBEntity(); - } - - if (embeddable != null) { - action.removeEmbeddableAttributes(embeddable, embeddableAttrs); - focusEmbeddable(); - } - } - - @Override - public void undo() throws CannotUndoException { - CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); - - if (objEntity != null) { - for (ObjAttribute attr : objAttributes) { - action.createObjAttribute(objEntity.getDataMap(), objEntity, attr); - } - focusObjEntity(); - } - - if (dbEntity != null) { - for (DbAttribute attr : dbAttributes) { - action.createDbAttribute(dbEntity.getDataMap(), dbEntity, attr); - } - focusDBEntity(); - } - - if (embeddable != null) { - for (EmbeddableAttribute attr : embeddableAttrs) { - action.createEmbAttribute(embeddable, attr); - } - focusEmbeddable(); - } - } - - @Override - public String getPresentationName() { - if (objEntity != null) { - return (objAttributes.length > 1) ? "Remove ObjAttributes" : "Remove ObjAttribute"; - } - - if (dbEntity != null) { - return (dbAttributes.length > 1) ? "Remove DbAttributes" : "Remove DbAttribute"; - } - - if (embeddableAttrs != null) { - return (embeddableAttrs.length > 1) ? "Remove Embeddable Attributes" : "Remove Embeddable Attribute"; - } - - return super.getPresentationName(); - } -} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbAttributeUndoableEdit.java new file mode 100644 index 000000000..d65774ae7 --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbAttributeUndoableEdit.java @@ -0,0 +1,60 @@ +/***************************************************************** + * 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 + * + * https://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 javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +import org.apache.cayenne.map.DbAttribute; +import org.apache.cayenne.map.DbEntity; +import org.apache.cayenne.modeler.project.ProjectSession; +import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; +import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; + +public class RemoveDbAttributeUndoableEdit extends BaseRemovePropertyUndoableEdit { + + private final DbAttribute[] attributes; + + public RemoveDbAttributeUndoableEdit(ProjectSession session, DbEntity entity, DbAttribute[] attributes) { + super(session); + this.dbEntity = entity; + this.attributes = attributes; + } + + @Override + public void redo() throws CannotRedoException { + RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); + action.removeDbAttributes(dbEntity.getDataMap(), dbEntity, attributes); + focusDBEntity(); + } + + @Override + public void undo() throws CannotUndoException { + CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); + for (DbAttribute attr : attributes) { + action.createDbAttribute(dbEntity.getDataMap(), dbEntity, attr); + } + focusDBEntity(); + } + + @Override + public String getPresentationName() { + return (attributes.length > 1) ? "Remove DbAttributes" : "Remove DbAttribute"; + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbRelationshipUndoableEdit.java similarity index 58% copy from modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java copy to modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbRelationshipUndoableEdit.java index 544e5718e..a4fb4712f 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveDbRelationshipUndoableEdit.java @@ -23,63 +23,38 @@ import javax.swing.undo.CannotUndoException; import org.apache.cayenne.map.DbEntity; import org.apache.cayenne.map.DbRelationship; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.ObjRelationship; +import org.apache.cayenne.modeler.project.ProjectSession; import org.apache.cayenne.modeler.ui.action.CreateRelationshipAction; import org.apache.cayenne.modeler.ui.action.RemoveRelationshipAction; -import org.apache.cayenne.modeler.project.ProjectSession; -public class RemoveRelationshipUndoableEdit extends BaseRemovePropertyUndoableEdit { +public class RemoveDbRelationshipUndoableEdit extends BaseRemovePropertyUndoableEdit { - private ObjRelationship[] rels; - private DbRelationship[] dbRels; + private final DbRelationship[] relationships; - public RemoveRelationshipUndoableEdit(ProjectSession session, ObjEntity objEntity, ObjRelationship[] rels) { - super(session); - this.objEntity = objEntity; - this.rels = rels; - } - - public RemoveRelationshipUndoableEdit(ProjectSession session, DbEntity dbEntity, DbRelationship[] dbRels) { + public RemoveDbRelationshipUndoableEdit(ProjectSession session, DbEntity dbEntity, DbRelationship[] relationships) { super(session); this.dbEntity = dbEntity; - this.dbRels = dbRels; + this.relationships = relationships; } @Override public String getPresentationName() { - if (objEntity != null) { - return "Remove Obj Relationship"; - } else { - return "Remove Db Relationship"; - } + return "Remove Db Relationship"; } @Override public void redo() throws CannotRedoException { RemoveRelationshipAction action = globalActions.getAction(RemoveRelationshipAction.class); - if (objEntity != null) { - action.removeObjRelationships(objEntity, rels); - focusObjEntity(); - } else { - action.removeDbRelationships(dbEntity, dbRels); - focusDBEntity(); - } + action.removeDbRelationships(dbEntity, relationships); + focusDBEntity(); } @Override public void undo() throws CannotUndoException { CreateRelationshipAction action = globalActions.getAction(CreateRelationshipAction.class); - if (objEntity != null) { - for (ObjRelationship r : rels) { - action.createObjRelationship(objEntity, r); - } - focusObjEntity(); - } else { - for (DbRelationship dr : dbRels) { - action.createDbRelationship(dbEntity, dr); - } - focusDBEntity(); + for (DbRelationship rel : relationships) { + action.createDbRelationship(dbEntity, rel); } + focusDBEntity(); } } diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveEmbeddableAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveEmbeddableAttributeUndoableEdit.java new file mode 100644 index 000000000..157e2e801 --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveEmbeddableAttributeUndoableEdit.java @@ -0,0 +1,61 @@ +/***************************************************************** + * 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 + * + * https://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 javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +import org.apache.cayenne.map.Embeddable; +import org.apache.cayenne.map.EmbeddableAttribute; +import org.apache.cayenne.modeler.project.ProjectSession; +import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; +import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; + +public class RemoveEmbeddableAttributeUndoableEdit extends BaseRemovePropertyUndoableEdit { + + private final EmbeddableAttribute[] attributes; + + public RemoveEmbeddableAttributeUndoableEdit(ProjectSession session, Embeddable embeddable, + EmbeddableAttribute[] attributes) { + super(session); + this.embeddable = embeddable; + this.attributes = attributes; + } + + @Override + public void redo() throws CannotRedoException { + RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); + action.removeEmbeddableAttributes(embeddable, attributes); + focusEmbeddable(); + } + + @Override + public void undo() throws CannotUndoException { + CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); + for (EmbeddableAttribute attr : attributes) { + action.createEmbAttribute(embeddable, attr); + } + focusEmbeddable(); + } + + @Override + public String getPresentationName() { + return (attributes.length > 1) ? "Remove Embeddable Attributes" : "Remove Embeddable Attribute"; + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjAttributeUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjAttributeUndoableEdit.java new file mode 100644 index 000000000..23ec37f79 --- /dev/null +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjAttributeUndoableEdit.java @@ -0,0 +1,60 @@ +/***************************************************************** + * 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 + * + * https://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 javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; + +import org.apache.cayenne.map.ObjAttribute; +import org.apache.cayenne.map.ObjEntity; +import org.apache.cayenne.modeler.project.ProjectSession; +import org.apache.cayenne.modeler.ui.action.CreateAttributeAction; +import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction; + +public class RemoveObjAttributeUndoableEdit extends BaseRemovePropertyUndoableEdit { + + private final ObjAttribute[] attributes; + + public RemoveObjAttributeUndoableEdit(ProjectSession session, ObjEntity entity, ObjAttribute[] attributes) { + super(session); + this.objEntity = entity; + this.attributes = attributes; + } + + @Override + public void redo() throws CannotRedoException { + RemoveAttributeAction action = globalActions.getAction(RemoveAttributeAction.class); + action.removeObjAttributes(objEntity, attributes); + focusObjEntity(); + } + + @Override + public void undo() throws CannotUndoException { + CreateAttributeAction action = globalActions.getAction(CreateAttributeAction.class); + for (ObjAttribute attr : attributes) { + action.createObjAttribute(objEntity.getDataMap(), objEntity, attr); + } + focusObjEntity(); + } + + @Override + public String getPresentationName() { + return (attributes.length > 1) ? "Remove ObjAttributes" : "Remove ObjAttribute"; + } +} diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjRelationshipUndoableEdit.java similarity index 58% rename from modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java rename to modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjRelationshipUndoableEdit.java index 544e5718e..a991aba53 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveRelationshipUndoableEdit.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/RemoveObjRelationshipUndoableEdit.java @@ -21,65 +21,40 @@ package org.apache.cayenne.modeler.undo; import javax.swing.undo.CannotRedoException; import javax.swing.undo.CannotUndoException; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.DbRelationship; import org.apache.cayenne.map.ObjEntity; import org.apache.cayenne.map.ObjRelationship; +import org.apache.cayenne.modeler.project.ProjectSession; import org.apache.cayenne.modeler.ui.action.CreateRelationshipAction; import org.apache.cayenne.modeler.ui.action.RemoveRelationshipAction; -import org.apache.cayenne.modeler.project.ProjectSession; -public class RemoveRelationshipUndoableEdit extends BaseRemovePropertyUndoableEdit { +public class RemoveObjRelationshipUndoableEdit extends BaseRemovePropertyUndoableEdit { - private ObjRelationship[] rels; - private DbRelationship[] dbRels; + private final ObjRelationship[] relationships; - public RemoveRelationshipUndoableEdit(ProjectSession session, ObjEntity objEntity, ObjRelationship[] rels) { + public RemoveObjRelationshipUndoableEdit(ProjectSession session, ObjEntity objEntity, ObjRelationship[] relationships) { super(session); this.objEntity = objEntity; - this.rels = rels; - } - - public RemoveRelationshipUndoableEdit(ProjectSession session, DbEntity dbEntity, DbRelationship[] dbRels) { - super(session); - this.dbEntity = dbEntity; - this.dbRels = dbRels; + this.relationships = relationships; } @Override public String getPresentationName() { - if (objEntity != null) { - return "Remove Obj Relationship"; - } else { - return "Remove Db Relationship"; - } + return "Remove Obj Relationship"; } @Override public void redo() throws CannotRedoException { RemoveRelationshipAction action = globalActions.getAction(RemoveRelationshipAction.class); - if (objEntity != null) { - action.removeObjRelationships(objEntity, rels); - focusObjEntity(); - } else { - action.removeDbRelationships(dbEntity, dbRels); - focusDBEntity(); - } + action.removeObjRelationships(objEntity, relationships); + focusObjEntity(); } @Override public void undo() throws CannotUndoException { CreateRelationshipAction action = globalActions.getAction(CreateRelationshipAction.class); - if (objEntity != null) { - for (ObjRelationship r : rels) { - action.createObjRelationship(objEntity, r); - } - focusObjEntity(); - } else { - for (DbRelationship dr : dbRels) { - action.createDbRelationship(dbEntity, dr); - } - focusDBEntity(); + for (ObjRelationship rel : relationships) { + action.createObjRelationship(objEntity, rel); } + focusObjEntity(); } }
