Merge "[553284] positioning of new subapp" into 1.11.x
diff --git a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editors/UIFBNetworkContextMenuProvider.java b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editors/UIFBNetworkContextMenuProvider.java
index ed62f93..ef64c22 100644
--- a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editors/UIFBNetworkContextMenuProvider.java
+++ b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editors/UIFBNetworkContextMenuProvider.java
@@ -20,6 +20,7 @@
 
 import java.util.List;
 
+import org.eclipse.draw2d.FigureCanvas;
 import org.eclipse.emf.common.util.EList;
 import org.eclipse.fordiac.ide.application.Messages;
 import org.eclipse.fordiac.ide.application.actions.FBInsertAction;
@@ -82,6 +83,7 @@
 	private Palette palette;
 	private DiagramEditorWithFlyoutPalette editor;
 	private Point pt;
+	private ZoomManager zoomManager;
 
 	/**
 	 * Instantiates a new uIFB network context menu provider.
@@ -95,16 +97,18 @@
 		super(editor.getViewer(), zoomManager, registry);
 		this.palette = palette;
 		this.editor = editor;
+		this.zoomManager = zoomManager;
 
 		getViewer().getControl().addMenuDetectListener(e -> {
 			pt = getViewer().getControl().toControl(e.x, e.y);
-			pt.x /= zoomManager.getZoom();
-			pt.y /= zoomManager.getZoom();
 		});
 	}
 
-	public Point getPoint() {
-		return pt;
+	public org.eclipse.draw2d.geometry.Point getPoint() {
+		FigureCanvas viewerControl = (FigureCanvas) editor.getViewer().getControl();
+		org.eclipse.draw2d.geometry.Point location = viewerControl.getViewport().getViewLocation();
+		return new org.eclipse.draw2d.geometry.Point(pt.x + location.x, pt.y + location.y)
+				.scale(1.0 / zoomManager.getZoom());
 	}
 
 	/*
@@ -115,15 +119,14 @@
 	 */
 	@Override
 	public void buildContextMenu(final IMenuManager menu) {
-
 		if (getViewer().getEditDomain().getActiveTool() instanceof AdvancedPanningSelectionTool) {
 			AdvancedPanningSelectionTool st = (AdvancedPanningSelectionTool) getViewer().getEditDomain()
 					.getActiveTool();
 			if (!st.isMoved()) { // pan executed
 				EditPart currentPart = getViewer().findObjectAt(st.getLocation());
 				EditPart selected = st.getTargetEditPart();
-				if (selected != null && selected.getSelected() == EditPart.SELECTED_NONE
-						&& selected instanceof AbstractViewEditPart) {
+				if ((selected != null) && (selected.getSelected() == EditPart.SELECTED_NONE)
+						&& (selected instanceof AbstractViewEditPart)) {
 					getViewer().select(currentPart);
 				}
 
@@ -153,7 +156,7 @@
 		menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);
 
 		action = getRegistry().getAction(GEFActionConstants.DIRECT_EDIT);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			menu.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
 		}
 
@@ -167,7 +170,7 @@
 		menu.appendToGroup(GEFActionConstants.GROUP_REST, new Separator());
 
 		action = getRegistry().getAction(UpdateFBTypeAction.ID);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			menu.appendToGroup(IWorkbenchActionConstants.GROUP_ADD, action);
 		}
 
@@ -183,7 +186,7 @@
 		String text = "Insert FB";
 		List eps = editor.getViewer().getSelectedEditParts();
 		for (Object ep : eps) {
-			if (ep instanceof FBEditPart || ep instanceof SubAppForFBNetworkEditPart) {
+			if ((ep instanceof FBEditPart) || (ep instanceof SubAppForFBNetworkEditPart)) {
 				text = "Change Type";
 				useChangeFBType = true;
 				break;
@@ -191,13 +194,13 @@
 		}
 		MenuManager submenu = new MenuManager(text);
 		menu.appendToGroup(IWorkbenchActionConstants.GROUP_ADD, submenu);
-		fillMenuForPalletteGroup(submenu, palette.getRootGroup().getSubGroups());
+		fillMenuForPaletteGroup(submenu, palette.getRootGroup().getSubGroups());
 		addFBMenuEntries(palette.getRootGroup(), submenu);
 	}
 
-	private boolean useChangeFBType = false;
+	private boolean useChangeFBType;
 
-	private void fillMenuForPalletteGroup(MenuManager insertTypeEntry, EList<PaletteGroup> subGroups) {
+	private void fillMenuForPaletteGroup(MenuManager insertTypeEntry, EList<PaletteGroup> subGroups) {
 		// TODO sort groups alphabetically
 
 		for (PaletteGroup group : subGroups) {
@@ -210,9 +213,9 @@
 	}
 
 	private void addFBMenuEntries(PaletteGroup group, MenuManager submenu) {
-		for (org.eclipse.fordiac.ide.model.Palette.PaletteEntry entry : group.getEntries()) {
+		for (PaletteEntry entry : group.getEntries()) {
 
-			if (entry instanceof FBTypePaletteEntry || entry instanceof SubApplicationTypePaletteEntry) {
+			if ((entry instanceof FBTypePaletteEntry) || (entry instanceof SubApplicationTypePaletteEntry)) {
 				Action action = getActionForPaletteEntry(entry);
 				if (null != action) {
 					setActionIcon(action, entry);
@@ -224,13 +227,13 @@
 
 	private MenuManager createSubMenu(PaletteGroup group) {
 		MenuManager submenu = new MenuManager(group.getLabel());
-		fillMenuForPalletteGroup(submenu, group.getSubGroups());
+		fillMenuForPaletteGroup(submenu, group.getSubGroups());
 		submenu.setImageDescriptor(
 				PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER));
 		return submenu;
 	}
 
-	private Action getActionForPaletteEntry(org.eclipse.fordiac.ide.model.Palette.PaletteEntry entry) {
+	private Action getActionForPaletteEntry(PaletteEntry entry) {
 		Action action;
 		if (useChangeFBType) {
 			action = (Action) getRegistry().getAction(entry.getFile().getFullPath().toString().concat("_") //$NON-NLS-1$
@@ -324,7 +327,7 @@
 				.getActiveEditor();
 		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
 
-		if (isFBorSubAppSelected(selection) && activeEditor instanceof FBNetworkEditor) {
+		if (isFBorSubAppSelected(selection) && (activeEditor instanceof FBNetworkEditor)) {
 			FBNetworkEditor fbEditor = (FBNetworkEditor) activeEditor;
 			List<Device> devices = fbEditor.getSystem().getSystemConfiguration().getDevices();
 
@@ -368,7 +371,7 @@
 	private static boolean isFBorSubAppSelected(ISelection selection) {
 		if (selection instanceof StructuredSelection) {
 			for (Object element : ((IStructuredSelection) selection).toArray()) {
-				if (element instanceof FBEditPart || element instanceof SubAppForFBNetworkEditPart) {
+				if ((element instanceof FBEditPart) || (element instanceof SubAppForFBNetworkEditPart)) {
 					return true;
 				}
 			}
diff --git a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editparts/ConnectionEditPart.java b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editparts/ConnectionEditPart.java
index 1acec41..6cc5086 100644
--- a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editparts/ConnectionEditPart.java
+++ b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/editparts/ConnectionEditPart.java
@@ -2,10 +2,10 @@
  * Copyright (c) 2008 - 2018 Profactor GmbH, TU Wien ACIN, fortiss GmbH, AIT,

  * 							 Johannes Kepler University

  * 

- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0.
- *
+ * This program and the accompanying materials are made available under the

+ * terms of the Eclipse Public License 2.0 which is available at

+ * http://www.eclipse.org/legal/epl-2.0.

+ *

  * SPDX-License-Identifier: EPL-2.0

  *

  * Contributors:

diff --git a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/handlers/NewSubApplication.java b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/handlers/NewSubApplication.java
index 88d19ef..9397565 100644
--- a/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/handlers/NewSubApplication.java
+++ b/plugins/org.eclipse.fordiac.ide.application/src/org/eclipse/fordiac/ide/application/handlers/NewSubApplication.java
@@ -17,11 +17,12 @@
 
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.draw2d.geometry.Point;
 import org.eclipse.fordiac.ide.application.commands.NewSubAppCommand;
 import org.eclipse.fordiac.ide.application.editors.FBNetworkEditor;
+import org.eclipse.fordiac.ide.application.editors.UIFBNetworkContextMenuProvider;
 import org.eclipse.fordiac.ide.model.helpers.FBNetworkHelper;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.swt.graphics.Point;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 public class NewSubApplication extends AbstractHandler {
@@ -30,11 +31,26 @@
 	public Object execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
 		FBNetworkEditor editor = (FBNetworkEditor) HandlerUtil.getActiveEditor(event);
 		StructuredSelection selection = (StructuredSelection) HandlerUtil.getCurrentSelection(event);
+		// ZoomManager zoomManager = (ZoomManager)
+		// editor.getViewer().getProperty(ZoomManager.class.toString());
+		if (selection.size() == 1) {
+			// new empty subapp at mouse cursor location
+			Point pos = getPositionInViewer(editor);
 
-		Point pos = FBNetworkHelper.getTopLeftCornerOfFBNetwork(selection.toList());
-		NewSubAppCommand cmd = new NewSubAppCommand(editor.getModel(), selection.toList(), pos.x, pos.y);
-		editor.getCommandStack().execute(cmd);
+			NewSubAppCommand cmd = new NewSubAppCommand(editor.getModel(), selection.toList(), pos.x, pos.y);
+			editor.getCommandStack().execute(cmd);
+		} else {
+			org.eclipse.swt.graphics.Point pos = FBNetworkHelper.getTopLeftCornerOfFBNetwork(selection.toList());
+			NewSubAppCommand cmd = new NewSubAppCommand(editor.getModel(), selection.toList(), pos.x, pos.y);
+			editor.getCommandStack().execute(cmd);
+		}
+
 		return null;
 	}
 
+	private static Point getPositionInViewer(FBNetworkEditor editor) {
+
+		return ((UIFBNetworkContextMenuProvider) editor.getViewer().getContextMenu()).getPoint();
+	}
+
 }
diff --git a/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/DiagramEditorWithFlyoutPalette.java b/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/DiagramEditorWithFlyoutPalette.java
index ed7e157..c708475 100644
--- a/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/DiagramEditorWithFlyoutPalette.java
+++ b/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/DiagramEditorWithFlyoutPalette.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * Copyright (c) 2008 - 2016 Profactor GbmH, TU Wien ACIN, fortiss GmbH
  * 				 2018 - 2019 Johannes Kepler University
- * 
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License 2.0 which is available at
  * http://www.eclipse.org/legal/epl-2.0.
@@ -12,7 +12,7 @@
  *   Gerhard Ebenhofer, Alois Zoitl, Monika Wenger
  *     - initial API and implementation and/or initial documentation
  *   Alois Zoitl - fixed copy/paste handling
- *   Alois Zoitl - added diagram font preference 
+ *   Alois Zoitl - added diagram font preference
  *******************************************************************************/
 package org.eclipse.fordiac.ide.gef;
 
@@ -80,7 +80,7 @@
 
 /**
  * A base editor for various graphical editors.
- * 
+ *
  * @author Gerhard Ebenhofer (gerhard.ebenhofer@profactor.at)
  */
 public abstract class DiagramEditorWithFlyoutPalette extends GraphicalEditorWithFlyoutPalette
@@ -103,7 +103,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#commandStackChanged(java.util
 	 * .EventObject)
 	 */
@@ -164,7 +164,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
 	 */
 	@Override
@@ -196,17 +196,17 @@
 
 	/**
 	 * Gets the edits the part factory.
-	 * 
+	 *
 	 * @return the edits the part factory
 	 */
 	protected abstract EditPartFactory getEditPartFactory();
 
 	/**
 	 * Gets the context menu provider.
-	 * 
+	 *
 	 * @param viewer the viewer
 	 * @param zoom   the zoom manager of the root edit part
-	 * 
+	 *
 	 * @return the context menu provider
 	 */
 	protected abstract ContextMenuProvider getContextMenuProvider(ScrollingGraphicalViewer viewer,
@@ -214,14 +214,14 @@
 
 	/**
 	 * Creates the transfer drop target listener.
-	 * 
+	 *
 	 * @return the transfer drop target listener
 	 */
 	protected abstract TransferDropTargetListener createTransferDropTargetListener();
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
 	 */
 	@Override
@@ -240,7 +240,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.ui.IEditorPart#init(org.eclipse.ui.IEditorSite,
 	 * org.eclipse.ui.IEditorInput)
 	 */
@@ -270,7 +270,7 @@
 
 	/**
 	 * Sets the model.
-	 * 
+	 *
 	 * @param input the new model
 	 */
 	protected void setModel(final IEditorInput input) {
@@ -285,14 +285,14 @@
 
 	/**
 	 * Gets the system.
-	 * 
+	 *
 	 * @return the system
 	 */
 	public abstract AutomationSystem getSystem();
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @seeorg.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
 	 * IProgressMonitor)
 	 */
@@ -301,7 +301,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#doSaveAs()
 	 */
 	@Override
@@ -309,7 +309,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#isSaveAsAllowed()
 	 */
 	@Override
@@ -319,7 +319,7 @@
 
 	/**
 	 * Gets the common key handler.
-	 * 
+	 *
 	 * @return the common key handler
 	 */
 	protected KeyHandler getCommonKeyHandler() {
@@ -339,7 +339,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#getAdapter(
 	 * java.lang.Class)
 	 */
@@ -362,7 +362,7 @@
 
 	/**
 	 * Gets the editor.
-	 * 
+	 *
 	 * @return the editor
 	 */
 	protected FigureCanvas getEditor() {
@@ -371,7 +371,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#createActions()
 	 */
 	@SuppressWarnings("unchecked")
@@ -419,7 +419,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ui.parts.GraphicalEditor#getSelectionActions()
 	 */
 	@SuppressWarnings("rawtypes")
@@ -430,7 +430,7 @@
 
 	/**
 	 * Gets the sel actions.
-	 * 
+	 *
 	 * @return the sel actions
 	 */
 	@SuppressWarnings("rawtypes")
@@ -440,7 +440,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.ui.IWorkbenchPart#dispose()
 	 */
 	@Override
@@ -451,7 +451,7 @@
 
 	/**
 	 * Returns the GraphicalViewer of this Editor.
-	 * 
+	 *
 	 * @return the GraphicalViewer
 	 */
 	public GraphicalViewer getViewer() {
@@ -460,7 +460,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @seeorg.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#
 	 * createPaletteViewerProvider()
 	 */
@@ -477,7 +477,7 @@
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see
 	 * org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor
 	 * #getContributorId()
diff --git a/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/ZoomUndoRedoContextMenuProvider.java b/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/ZoomUndoRedoContextMenuProvider.java
index e86fd44..f16ee1b 100644
--- a/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/ZoomUndoRedoContextMenuProvider.java
+++ b/plugins/org.eclipse.fordiac.ide.gef/src/org/eclipse/fordiac/ide/gef/ZoomUndoRedoContextMenuProvider.java
@@ -1,6 +1,6 @@
 /*******************************************************************************
  * Copyright (c) 2008, 2009, 2012 - 2015 Profactor GbmH, fortiss GmbH
- * 
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License 2.0 which is available at
  * http://www.eclipse.org/legal/epl-2.0.
@@ -28,7 +28,7 @@
 
 /**
  * The Class ZoomUndoRedoContextMenuProvider.
- * 
+ *
  * @author Gerhard Ebenhofer (gerhard.ebenhofer@profactor.at)
  */
 public class ZoomUndoRedoContextMenuProvider extends ContextMenuProvider {
@@ -38,83 +38,83 @@
 
 	/**
 	 * Instantiates a new zoom undo redo context menu provider.
-	 * 
-	 * @param viewer the viewer
+	 *
+	 * @param viewer      the viewer
 	 * @param zoomManager the zoom manager
-	 * @param registry the registry
+	 * @param registry    the registry
 	 */
-	public ZoomUndoRedoContextMenuProvider(final EditPartViewer viewer,
-			final ZoomManager zoomManager, final ActionRegistry registry) {
+	public ZoomUndoRedoContextMenuProvider(final EditPartViewer viewer, final ZoomManager zoomManager,
+			final ActionRegistry registry) {
 		super(viewer);
 		this.zoomManager = zoomManager;
 		this.registry = registry;
 	}
-	
+
 	public ActionRegistry getRegistry() {
 		return registry;
 	}
 
 	/*
 	 * (non-Javadoc)
-	 * 
+	 *
 	 * @see org.eclipse.gef.ContextMenuProvider#buildContextMenu(org.eclipse.jface
 	 * .action.IMenuManager)
 	 */
 	@Override
 	public void buildContextMenu(final IMenuManager menu) {
 		GEFActionConstants.addStandardActionGroups(menu);
-		
+
 		menu.appendToGroup(GEFActionConstants.GROUP_VIEW, new GraphZoomInAction(zoomManager));
 		menu.appendToGroup(GEFActionConstants.GROUP_VIEW, new GraphZoomOutAction(zoomManager));
-		
+
 		IAction action;
-		action = registry.getAction(ActionFactory.UNDO.getId());	
-		
+		action = registry.getAction(ActionFactory.UNDO.getId());
+
 		menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
 		action = registry.getAction(ActionFactory.REDO.getId());
 		menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
-		
+
 		// Alignment Actions
 		MenuManager submenu = new MenuManager("&Align");
 
 		action = registry.getAction(GEFActionConstants.ALIGN_LEFT);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
 		action = registry.getAction(GEFActionConstants.ALIGN_CENTER);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
 		action = registry.getAction(GEFActionConstants.ALIGN_RIGHT);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
 		submenu.add(new Separator());
 
 		action = registry.getAction(GEFActionConstants.ALIGN_TOP);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
 		action = registry.getAction(GEFActionConstants.ALIGN_MIDDLE);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
 		action = registry.getAction(GEFActionConstants.ALIGN_BOTTOM);
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			submenu.add(action);
 		}
 
-		if (!submenu.isEmpty()){
+		if (!submenu.isEmpty()) {
 			menu.appendToGroup(GEFActionConstants.GROUP_REST, submenu);
 		}
-		
+
 		action = registry.getAction(ActionFactory.PRINT.getId());
-		if (action != null && action.isEnabled()) {
+		if ((action != null) && action.isEnabled()) {
 			menu.appendToGroup(GEFActionConstants.GROUP_REST, action);
 		}
 	}