From 85d8d0161743ee374e713fe60d6996188815c0e1 Mon Sep 17 00:00:00 2001 From: Erle Czar Mantos Date: Thu, 28 Jul 2016 11:32:13 +0800 Subject: [PATCH] Fixes #339 - Adding Modeldependency empties model. Signed-off-by: Erle Czar Mantos --- .../perspective/dnd/ModelDropListener.java | 24 ++++++++++--------- .../dnd/ModelDropListenerFactory.java | 13 +++++----- .../perspective/view/DatatypeTreeViewer.java | 2 +- .../view/FunctionblockTreeViewer.java | 2 +- .../perspective/view/InfomodelTreeViewer.java | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListener.java b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListener.java index 1428eb2209..332d21d233 100644 --- a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListener.java +++ b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListener.java @@ -18,12 +18,13 @@ import java.util.Collection; import java.util.Objects; -import org.eclipse.jface.viewers.AbstractTreeViewer; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerDropAdapter; import org.eclipse.swt.dnd.TransferData; import org.eclipse.vorto.core.ui.model.IModelElement; +import org.eclipse.vorto.core.ui.model.IModelProject; +import org.eclipse.vorto.perspective.view.ILocalModelWorkspace; import org.eclipse.vorto.perspective.view.ModelProjectTreeViewer; /** @@ -36,13 +37,16 @@ // -erle- : This needs to be refactored. public class ModelDropListener extends ViewerDropAdapter { + + private ILocalModelWorkspace localModelBrowser; private Class allowedTarget; private Collection dropActors = new ArrayList(); - public ModelDropListener(Viewer viewer) { - super(viewer); + public ModelDropListener(Viewer viewer, ILocalModelWorkspace localModelBrowser) { + super(Objects.requireNonNull(viewer)); + this.localModelBrowser = Objects.requireNonNull(localModelBrowser); } public ModelDropListener setAllowedTarget(Class allowedTarget) { @@ -79,7 +83,6 @@ public boolean performDrop(Object data) { if (targetModelElement != null) { viewer.expandToLevel(targetModelElement, 1); } - return true; } @@ -99,15 +102,14 @@ private IModelElement findTarget(IModelElement target, Collection private Object getTarget() { Object target = this.getCurrentTarget(); - if (target == null) { - Viewer viewer = this.getViewer(); - if (viewer instanceof ModelProjectTreeViewer) { - target = ((ModelProjectTreeViewer) viewer).getLocalModelWorkspace().getProjectBrowser() - .getSelectedProject(); - } + + if (target instanceof IModelElement) { + // Get the latest version of this IModelElement + IModelProject project = localModelBrowser.getProjectBrowser().getSelectedProject(); + return project.getModelElementById(((IModelElement) target).getId()); } - return target; + throw new RuntimeException("Target is not an IModelElement"); } public boolean validateDrop(Object target, int operation, TransferData transferType) { diff --git a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListenerFactory.java b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListenerFactory.java index 4ac7e99c7b..f7db53f174 100644 --- a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListenerFactory.java +++ b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/dnd/ModelDropListenerFactory.java @@ -28,11 +28,12 @@ import org.eclipse.vorto.perspective.dnd.dropvalidator.DatatypeValidator; import org.eclipse.vorto.perspective.dnd.dropvalidator.TargetClassModelTypeValidator; import org.eclipse.vorto.perspective.dnd.dropvalidator.TargetClassSourceClassValidator; +import org.eclipse.vorto.perspective.view.ILocalModelWorkspace; public class ModelDropListenerFactory { - public static DropTargetListener datatypeViewPartDropListener(Viewer viewer) { - return new ModelDropListener(viewer) + public static DropTargetListener datatypeViewPartDropListener(Viewer viewer, ILocalModelWorkspace localModelBrowser) { + return new ModelDropListener(viewer, localModelBrowser) .addDropAction( new DatatypeValidator( DatatypeModelElement.class, @@ -49,8 +50,8 @@ public static DropTargetListener datatypeViewPartDropListener(Viewer viewer) { new AddSharedReferenceDropAction(ModelResource.class)); } - public static DropTargetListener functionblockViewPartDropListener(Viewer viewer) { - return new ModelDropListener(viewer) + public static DropTargetListener functionblockViewPartDropListener(Viewer viewer, ILocalModelWorkspace localModelBrowser) { + return new ModelDropListener(viewer, localModelBrowser) .addDropAction( new TargetClassSourceClassValidator( FunctionblockModelElement.class, @@ -72,8 +73,8 @@ public static DropTargetListener functionblockViewPartDropListener(Viewer viewer new AddSharedReferenceDropAction(ModelResource.class)); } - public static DropTargetListener infomodelViewPartDropListener(Viewer viewer) { - return new ModelDropListener(viewer) + public static DropTargetListener infomodelViewPartDropListener(Viewer viewer, ILocalModelWorkspace localModelBrowser) { + return new ModelDropListener(viewer, localModelBrowser) .addDropAction( new TargetClassSourceClassValidator( InformationModelElement.class, diff --git a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/DatatypeTreeViewer.java b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/DatatypeTreeViewer.java index 9ff10777ac..647f108a64 100644 --- a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/DatatypeTreeViewer.java +++ b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/DatatypeTreeViewer.java @@ -49,7 +49,7 @@ protected void init() { Transfer[] transferTypes = new Transfer[] { LocalSelectionTransfer.getTransfer() }; treeViewer.addDragSupport(operations, transferTypes, new ModelDragListener(treeViewer)); treeViewer.addDropSupport(operations, transferTypes, - ModelDropListenerFactory.datatypeViewPartDropListener(treeViewer)); + ModelDropListenerFactory.datatypeViewPartDropListener(treeViewer, localModelWorkspace)); } @Override diff --git a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/FunctionblockTreeViewer.java b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/FunctionblockTreeViewer.java index 131c5de4bc..86e4b3b28a 100644 --- a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/FunctionblockTreeViewer.java +++ b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/FunctionblockTreeViewer.java @@ -49,7 +49,7 @@ public void init() { Transfer[] transferTypes = new Transfer[] { LocalSelectionTransfer.getTransfer() }; treeViewer.addDragSupport(operations, transferTypes, new ModelDragListener(treeViewer)); treeViewer.addDropSupport(operations, transferTypes, - ModelDropListenerFactory.functionblockViewPartDropListener(treeViewer)); + ModelDropListenerFactory.functionblockViewPartDropListener(treeViewer, localModelWorkspace)); } protected void initContextMenu() { diff --git a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/InfomodelTreeViewer.java b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/InfomodelTreeViewer.java index bbbea17767..8d15ce6048 100644 --- a/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/InfomodelTreeViewer.java +++ b/bundles/org.eclipse.vorto.perspective/src/org/eclipse/vorto/perspective/view/InfomodelTreeViewer.java @@ -48,7 +48,7 @@ public void init() { Transfer[] transferTypes = new Transfer[] { LocalSelectionTransfer.getTransfer() }; treeViewer.addDropSupport(operations, transferTypes, - ModelDropListenerFactory.infomodelViewPartDropListener(treeViewer)); + ModelDropListenerFactory.infomodelViewPartDropListener(treeViewer, localModelWorkspace)); } protected void initContextMenu() {