Skip to content

Commit

Permalink
Merge pull request #204 from bsinno/feature/expand_model_after_drop
Browse files Browse the repository at this point in the history
Fix for #183 and #184
  • Loading branch information
aedelmann committed May 31, 2016
2 parents e06f559 + cb3c9c4 commit 1a0f4c6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -26,22 +27,22 @@
import org.eclipse.vorto.perspective.view.ModelProjectTreeViewer;

/**
* A drop listener that can be configured with :
* - An allowed target : the target class allowed for this drop listener
* - Drop actions that consist of a validator and the actual action to be performed if
* the validator allows the drop.
* A drop listener that can be configured with : - An allowed target : the
* target class allowed for this drop listener - Drop actions that consist of a
* validator and the actual action to be performed if the validator allows the
* drop.
*
*/
public class ModelDropListener extends ViewerDropAdapter {

private Class<?> allowedTarget;

private Collection<DropSourceValidatorAndAction> dropActors = new ArrayList<DropSourceValidatorAndAction>();

public ModelDropListener(Viewer viewer) {
super(viewer);
}

public ModelDropListener setAllowedTarget(Class<?> allowedTarget) {
this.allowedTarget = allowedTarget;
return this;
Expand All @@ -54,55 +55,68 @@ public ModelDropListener addDropAction(IDropValidator validator, IDropAction act
return this;
}

@SuppressWarnings("unchecked")
public boolean performDrop(Object data) {
Object target = getTarget();

if (data instanceof IStructuredSelection) {
Object droppedResource = ((IStructuredSelection) data)
.getFirstElement();
Object droppedResource = ((IStructuredSelection) data).getFirstElement();
IModelElement result = null;
for(DropSourceValidatorAndAction dropActor : dropActors) {
for (DropSourceValidatorAndAction dropActor : dropActors) {
if (dropActor.validator.allow(target, droppedResource)) {
result = dropActor.action.performDrop(target, droppedResource);
break;
}
}

if (result != null) {
ModelProjectTreeViewer viewer = (ModelProjectTreeViewer)this.getViewer();
viewer.getLocalModelWorkspace().refresh();
viewer.expandToLevel(result, 2);

return true;
ModelProjectTreeViewer viewer = (ModelProjectTreeViewer) this.getViewer();
viewer.getLocalModelWorkspace().refreshCurrent();
IModelElement targetModelElement = findTarget((IModelElement) target,
(Collection<IModelElement>) viewer.getInput());
if (targetModelElement != null) {
viewer.expandToLevel(targetModelElement, 1);
}

return true;
}

}

return false;
}

private IModelElement findTarget(IModelElement target, Collection<IModelElement> inputModelElements) {
for(IModelElement e : inputModelElements) {
if (e.getId().equals(target.getId())) {
return e;
}
}
return null;
}

private Object getTarget() {
Object target = this.getCurrentTarget();
if (target == null) {
Viewer viewer = this.getViewer();
if (viewer instanceof ModelProjectTreeViewer) {
target = ((ModelProjectTreeViewer)viewer).getLocalModelWorkspace().getProjectBrowser().getSelectedProject();
target = ((ModelProjectTreeViewer) viewer).getLocalModelWorkspace().getProjectBrowser()
.getSelectedProject();
}
}

return target;
}

public boolean validateDrop(Object target, int operation,
TransferData transferType) {
public boolean validateDrop(Object target, int operation, TransferData transferType) {
return allowedTarget == null || allowedTarget.isInstance(target);
}

private class DropSourceValidatorAndAction {
IDropValidator validator;
IDropAction action;
public DropSourceValidatorAndAction(IDropValidator validator,
IDropAction action) {

public DropSourceValidatorAndAction(IDropValidator validator, IDropAction action) {
this.validator = validator;
this.action = action;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.vorto.perspective.dnd.dropaction.AddSharedReferenceDropAction;
import org.eclipse.vorto.perspective.dnd.dropaction.RepositoryResourceDropAction;
import org.eclipse.vorto.perspective.dnd.dropvalidator.DatatypeValidator;
import org.eclipse.vorto.perspective.dnd.dropvalidator.ModelTypeValidator;
import org.eclipse.vorto.perspective.dnd.dropvalidator.TargetClassModelTypeValidator;
import org.eclipse.vorto.perspective.dnd.dropvalidator.TargetClassSourceClassValidator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface ILocalModelWorkspace {
* refreshes the model browser by reloading all models for the selected project
*/
void refresh();

/**
* Only refreshes the current selected project
*/
void refreshCurrent();

public interface IModelProjectBrowser {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public String getText(Object element) {
});

projectSelectionViewer.setContentProvider(ArrayContentProvider.getInstance());
projectSelectionViewer.setInput(getModelProjects());
Collection<IModelProject> modelProjects = getModelProjects();
projectSelectionViewer.setInput(modelProjects);

projectSelectionViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {

if (event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection iSelection = (IStructuredSelection) event.getSelection();
IModelProject project = (IModelProject) iSelection.getFirstElement();
Expand All @@ -136,9 +136,9 @@ public void selectionChanged(SelectionChangedEvent event) {
infoModelTreeViewer = new InfomodelTreeViewer(modelPanel, this);

getSite().setSelectionProvider(infoModelTreeViewer.treeViewer);

if (!getModelProjects().isEmpty()) {
setSelectedProject(getModelProjects().iterator().next());
if (!modelProjects.isEmpty()) {
setSelectedProject(modelProjects.iterator().next());
}

}
Expand Down Expand Up @@ -168,6 +168,12 @@ public void refresh() {
populate(project);
}
}

public void refreshCurrent() {
// setting the selected project will call populate on that project which in turn
// will refresh it
setSelectedProject(selectedProject);
}

private ComboViewer createProjectSelectionViewer(final Composite container, String labelStr) {
Button newProjectButton = new Button(container, SWT.NONE);
Expand Down

0 comments on commit 1a0f4c6

Please sign in to comment.