Skip to content

Commit

Permalink
Added Moving to MoveThroughHierarchy
Browse files Browse the repository at this point in the history
Added Moving to MoveThroughHierarchy by using
MoveElementsFromSubAppCommand
Also improved Dialog

Signed-off-by: Sebastian Hollersbacher <s.hollersbacher@outlook.at>
  • Loading branch information
sebHollersbacher authored and oberlehner committed May 7, 2024
1 parent 6bc4ece commit 0f51760
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
Expand Up @@ -116,6 +116,8 @@ public final class Messages extends NLS {

public static String MonitoringEditPart_Not_Available;

public static String MoveElementDialogTitle;

/** The New sub application action_ new subapplication text. */
public static String NewSubApplicationAction_NewSubapplicationText;

Expand Down
Expand Up @@ -74,10 +74,19 @@ public class MoveElementsFromSubAppCommand extends Command implements ScopedComm
private final Set<Connection> connsMovedToParent = new HashSet<>();

public MoveElementsFromSubAppCommand(final Collection<FBNetworkElement> elements, final Point destination) {
this(elements, destination, null);
}

public MoveElementsFromSubAppCommand(final Collection<FBNetworkElement> elements, final Point destination,
final FBNetwork destinationNetwork) {
this.elements = new ArrayList<>(elements);
setDestination(destination);
this.sourceSubApp = getSourceSubapp();
this.destinationNetwork = sourceSubApp != null ? sourceSubApp.getFbNetwork() : null;
if (destinationNetwork == null) {
this.destinationNetwork = sourceSubApp != null ? sourceSubApp.getFbNetwork() : null;
} else {
this.destinationNetwork = destinationNetwork;
}
}

private SubApp getSourceSubapp() {
Expand Down
Expand Up @@ -12,14 +12,22 @@
*******************************************************************************/
package org.eclipse.fordiac.ide.application.handlers;

import static org.eclipse.fordiac.ide.model.ui.editors.HandlerHelper.getCommandStack;
import static org.eclipse.fordiac.ide.model.ui.editors.HandlerHelper.getFBNetwork;
import static org.eclipse.fordiac.ide.model.ui.editors.HandlerHelper.openEditor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Status;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.application.Messages;
import org.eclipse.fordiac.ide.application.commands.MoveElementsFromSubAppCommand;
import org.eclipse.fordiac.ide.model.libraryElement.Application;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetwork;
import org.eclipse.fordiac.ide.model.libraryElement.FBNetworkElement;
Expand All @@ -29,13 +37,16 @@
import org.eclipse.fordiac.ide.model.libraryElement.UntypedSubApp;
import org.eclipse.fordiac.ide.model.ui.editors.HandlerHelper;
import org.eclipse.fordiac.ide.ui.imageprovider.FordiacImage;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.jface.viewers.TreeNodeContentProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISources;
Expand All @@ -54,26 +65,50 @@ public Object execute(final ExecutionEvent event) throws ExecutionException {

if (!fbelements.isEmpty()) {

final List<TreeNode> nodeList = new ArrayList<>();
EObject container = fbelements.get(0).eContainer();
while (container != null) {
if (container instanceof INamedElement) {
nodeList.add(new TreeNode(container));
final Object result = openDialog(fbelements.get(0));

FBNetwork destinationNetwork = null;
if (result instanceof final TreeNode node) {
if (node.getValue() instanceof final SubApp subapp) {
destinationNetwork = subapp.getSubAppNetwork();
} else if (node.getValue() instanceof final Application app) {
destinationNetwork = app.getFBNetwork();
}
container = container.eContainer();
}

final ElementTreeSelectionDialog dia = new ElementTreeSelectionDialog(
Display.getCurrent().getActiveShell(), new TreeNodeLabelProvider(),
new TreeNodeContentProvider());
dia.setInput(nodeList.toArray(new TreeNode[0]));
dia.open();
final Object res = ((TreeNode) dia.getResult()[0]).getValue();
if (destinationNetwork != null) {
final Point destination = getDestination(editor, fbelements, destinationNetwork);
final MoveElementsFromSubAppCommand cmd = new MoveElementsFromSubAppCommand(fbelements, destination,
destinationNetwork);
getCommandStack(editor).execute(cmd);

final GraphicalViewer viewer = HandlerHelper.openEditor(destinationNetwork.eContainer())
.getAdapter(GraphicalViewer.class);
HandlerHelper.selectElement(fbelements.get(0), viewer);
}
}
}
return Status.OK_STATUS;
}

private static Object openDialog(final FBNetworkElement fbElement) {
final List<TreeNode> nodeList = new ArrayList<>();
EObject container = fbElement.eContainer();
while (container != null) {
if (container instanceof Application || container instanceof SubApp) {
nodeList.add(new TreeNode(container));
}
container = container.eContainer();
}
Collections.reverse(nodeList);
final ElementTreeSelectionDialog dia = new ElementTreeSelectionDialog(Display.getCurrent().getActiveShell(),
new TreeNodeLabelProvider(), new TreeNodeContentProvider());
dia.setInput(nodeList.toArray(new TreeNode[0]));
dia.setTitle(Messages.MoveElementDialogTitle);
dia.open();
return dia.getResult() != null ? dia.getResult()[0] : null;
}

@Override
public void setEnabled(final Object evaluationContext) {
final ISelection selection = (ISelection) HandlerUtil.getVariable(evaluationContext,
Expand All @@ -91,6 +126,33 @@ public void setEnabled(final Object evaluationContext) {
setBaseEnabled(false);
}

private static GraphicalViewer getViewer(final FBNetwork subappNetwork, final IEditorPart parent) {
if (!getFBNetwork(parent).equals(subappNetwork)) {
// source subapp editor, subapp content is not open
return HandlerHelper.getViewer(openEditor(subappNetwork.eContainer()));
}
return HandlerHelper.getViewer(parent);
}

private static Point getDestination(final IEditorPart editor, final List<FBNetworkElement> fbelements,
final FBNetwork destinationNetwork) {
final GraphicalViewer viewer = getViewer(destinationNetwork, editor);
viewer.flush();

EObject obj = fbelements.get(0).getFbNetwork().eContainer();
while (obj.eContainer() != null && obj.eContainer() != destinationNetwork) {
obj = obj.eContainer();
}

final GraphicalEditPart ep = (GraphicalEditPart) viewer.getEditPartRegistry().get(obj);

final Rectangle bounds = ep.getFigure().getBounds().getCopy();
final int destX = bounds.x;
final int destY = bounds.y + bounds.height + 20;

return new Point(destX, destY);
}

public static class TreeNodeLabelProvider extends LabelProvider implements IStyledLabelProvider {

@Override
Expand Down
Expand Up @@ -57,6 +57,7 @@ MapToContributionItem_No_FB_Or_SubApp_Selected=[Empty Selection]
MapToDialog_Message=Select Resource
MapToDialog_Title=Map Elements to Resource
MonitoringEditPart_Not_Available=N/A
MoveElementDialogTitle=Select Target Network
NewSubApplicationAction_NewSubapplicationText=New Subapplication
OpenApplicationEditorAction_ERROR_OpenApplicationEditor=editor could not be created or initialized
OpenApplicationEditorAction_Name=Application Editor
Expand Down

0 comments on commit 0f51760

Please sign in to comment.