Skip to content
Snippets Groups Projects
Commit 1826f614 authored by Simon Barner's avatar Simon Barner
Browse files

Merge branch '4323' into 'master'

Fix bad display of element choices in the in-editor context menu

Closes af3#4323

See merge request !208
parents 6d252361 66a30e47
No related branches found
No related tags found
1 merge request!208Fix bad display of element choices in the in-editor context menu
Showing
with 337 additions and 170 deletions
......@@ -7,7 +7,8 @@ Bundle-Activator: org.fortiss.tooling.base.ui.ToolingBaseUIActivator
Require-Bundle: org.fortiss.tooling.base;bundle-version="2.23.0";visibility:=reexport,
org.fortiss.tooling.kernel.ui;bundle-version="2.23.0";visibility:=reexport,
org.eclipse.swt,
org.fortiss.tooling.common.ui
org.fortiss.tooling.common.ui,
org.fortiss.tooling.ext.reuse
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-Vendor: fortiss GmbH
......
AbstractNameEditingSupport.java c57336a0e0da18711a1610ca667dfea76728807f GREEN
ActionUtils.java 322f43d4f92f992daef8ac88eb0f9197c840c89b GREEN
ContextMenuUtils.java a55ceed42f2eb88ba263a6fbcb394ddb80b1eda0 GREEN
ContextMenuUtils.java 75af4d1e995f4baba451e608d1a4716f13388dbd GREEN
EllipseLayoutUIUtils.java 0af2cfc038661828b1bb8c51c0a3816d453e8313 GREEN
FXDNDUtils.java 6ce94e239e68f9e2b3cc0524b072606f4a120076 GREEN
FontUtils.java a167a05bdaa8da9853705cc5134f30f6d81bc9f2 GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2019 fortiss GmbH |
| Copyright 2023 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
......@@ -15,9 +15,13 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.utils;
import static java.util.Collections.reverse;
import static org.fortiss.tooling.base.utils.LayoutModelElementFactory.createPoint;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getNameOfFirstRelatedLibrary;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.isCorrectReuseElement;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
......@@ -62,7 +66,6 @@ public final class ContextMenuUtils {
IElementCompositionContext context, boolean onlyForEditedObject) {
IPrototypeService pers = IPrototypeService.getInstance();
IElementCompositorService ecs = IElementCompositorService.getInstance();
ICommandStackService css = ICommandStackService.getInstance();
IModelEditor<EObject> activeEditor =
IModelEditorBindingService.getInstance().getActiveEditor();
boolean isMenuForEditedObject =
......@@ -70,31 +73,94 @@ public final class ContextMenuUtils {
List<MenuItem> result = new ArrayList<>();
Menu newMenu = new Menu("New ...");
List<Prototype> protos = pers.getComposablePrototypes(target.getClass());
for(Prototype p : protos) {
EObject prototypeCopy = p.getPrototypeCopy();
Menu reuseMenu = new Menu("Reuse ...");
List<Prototype> prototypes = pers.getComposablePrototypes(target.getClass());
List<Prototype> prototypesForNewMenu = new ArrayList<>();
for(Prototype prototype : prototypes) {
EObject prototypeCopy = prototype.getPrototypeCopy();
// If the context menu is created for the currently edited object, offer all composable
// elements. Otherwise (i.e., if context menu is created for a structural element within
// the currently edited object), offer only (composable) connectors.
if((!onlyForEditedObject || isMenuForEditedObject ||
prototypeCopy instanceof IConnector) &&
ecs.canCompose(target, prototypeCopy, context)) {
MenuItem mi = new MenuItem(p.getName());
mi.setOnAction(evt -> {
css.runAsCommand(target, () -> {
ecs.compose(target, prototypeCopy, context);
});
});
newMenu.getItems().add(mi);
if(isCorrectReuseElement(prototypeCopy)) {
MenuItem menuItem = createMenuItemForComposableElement(prototypeCopy, target,
context, prototype.getName());
String libraryName = getNameOfFirstRelatedLibrary(prototypeCopy);
menuItem.setText("[" + libraryName + "] " + prototype.getName());
// To prevent that underscores are swallowed up:
menuItem.setMnemonicParsing(false);
reuseMenu.getItems().add(menuItem);
} else {
prototypesForNewMenu.add(prototype);
}
}
}
// Sort first by priority and then by name (lexicographical).
prototypesForNewMenu.sort(new Comparator<Prototype>() {
@Override
public int compare(Prototype prototype1, Prototype prototype2) {
int result = prototype1.getPriority() - prototype2.getPriority();
if(result == 0) {
// Negate integer to have the case that the order <A, B> corresponds to "A is
// greater than B" (= positive)
result = -prototype1.getName().compareTo(prototype2.getName());
}
return result;
}
});
// Reverse to get a descending order (highest prio at first / on top).
reverse(prototypesForNewMenu);
for(Prototype prototype : prototypesForNewMenu) {
EObject prototypeCopy = prototype.getPrototypeCopy();
MenuItem menuItem = createMenuItemForComposableElement(prototypeCopy, target, context,
prototype.getName());
// To prevent that underscores are swallowed up:
menuItem.setMnemonicParsing(false);
newMenu.getItems().add(menuItem);
}
if(!newMenu.getItems().isEmpty()) {
result.add(newMenu);
}
if(!reuseMenu.getItems().isEmpty()) {
result.add(reuseMenu);
}
return result;
}
/**
* Returns a menu item for the given element with which the element can be composed with the
* given target (in the given context).
*
* @param composableElement
* The element that should be composed.
* @param target
* The target with which the element should be composed.
* @param context
* The context of the composition.
* @param itemName
* The name of the new menu item.
* @return The newly created menu item.
*/
private static MenuItem createMenuItemForComposableElement(EObject composableElement,
EObject target, IElementCompositionContext context, String itemName) {
ICommandStackService css = ICommandStackService.getInstance();
IElementCompositorService ecs = IElementCompositorService.getInstance();
MenuItem menuItem = new MenuItem(itemName);
menuItem.setOnAction(evt -> {
css.runAsCommand(target, () -> {
ecs.compose(target, composableElement, context);
});
});
return menuItem;
}
/**
* Creates the menu populated with composable prototypes.
*
......
ReuseLibraryModelElementFactory.java 4ee3eb7449e212643992a3dec6cfb8f4278efb70 GREEN
ReuseLibraryUtilsBasics.java b651b31f6d94ff98a8f965440d662bfc4655c31b GREEN
ReuseLibraryUtilsBasics.java 56ab0cab9189efd7f2408150b471f3c6cc99f30f GREEN
ReuseLibraryUtilsManipulation.java 77a646db5a63ba7c61664dbcaf34a9036003fde5 GREEN
......@@ -3,6 +3,6 @@ ConstraintViolationBase.java ec66973ab2183623f0cd4a85c59c886dddad6cf6 GREEN
DialogMessage.java 8420640e999e4fb15fa644333e5d71e1d16c2559 GREEN
ElementCompositorBase.java 7a445e5adde11878fe0515baca8b915287149b28 GREEN
MultiViolationConstraintCheckerBase.java 30886a94c99cf8948f64401b1db821abe06e1e6c GREEN
PrototypeProviderBase.java 4625d5ed40d3bcadfc6dda3ed4cc1f3873a23307 GREEN
PrototypeProviderBase.java 7418c494275bf75318504de65e82035535b9e9d8 GREEN
TransformationContextChainBase.java 1ef37880ab275778c563928e80ba378fec964cb6 GREEN
TransformationProviderBase.java 9e91100cc1f2c8fbd8d41af55aedfea34e02ff71 GREEN
......@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.kernel.extension.IPrototypeProvider;
......@@ -65,9 +66,13 @@ public abstract class PrototypeProviderBase implements IPrototypeProvider {
/** Registers the given {@link EObject} with the given name and category. */
protected final void registerPrototype(String name, EObject prototype, String categoryName) {
Prototype prototypeObject = new Prototype(name, prototype, false);
prototypes.add(prototypeObject);
registerElementInCategory(prototypeObject, categoryName);
registerPrototype(name, prototype, categoryName, false, OptionalInt.empty());
}
/** Registers the given {@link EObject} with the given name, category, and priority. */
protected final void registerPrototype(String name, EObject prototype, String categoryName,
int priority) {
registerPrototype(name, prototype, categoryName, false, OptionalInt.of(priority));
}
/**
......@@ -76,7 +81,27 @@ public abstract class PrototypeProviderBase implements IPrototypeProvider {
*/
protected final void registerPrimaryPrototype(String name, EObject prototype,
String categoryName) {
Prototype prototypeObject = new Prototype(name, prototype, true);
registerPrototype(name, prototype, categoryName, true, OptionalInt.empty());
}
/**
* Registers the given {@link EObject} with the given name and priority as primary prototype
* with the given category.
*/
protected final void registerPrimaryPrototype(String name, EObject prototype,
String categoryName, int priority) {
registerPrototype(name, prototype, categoryName, true, OptionalInt.of(priority));
}
/** Base method that actually performs the registration of prototypes. */
private final void registerPrototype(String name, EObject prototype, String categoryName,
boolean primary, OptionalInt priority) {
Prototype prototypeObject;
if(priority.isPresent()) {
prototypeObject = new Prototype(name, prototype, primary, priority.getAsInt());
} else {
prototypeObject = new Prototype(name, prototype, primary);
}
prototypes.add(prototypeObject);
registerElementInCategory(prototypeObject, categoryName);
}
......
......@@ -7,7 +7,7 @@ ITransformationContext.java f00a0ab19a410c3ae2fc6256483aeb4207a86395 GREEN
LogMessage.java 14204ed9d51b356f50be52362247cfbbe0cbd5c7 GREEN
ModelElementTransformationContext.java 5a41bd3a75ce434c3174d50d2fdfab28b66f09f2 GREEN
ModelStorageError.java 2aef480044047e960e64811111a7f27310011cc2 GREEN
Prototype.java 5b91ecc45950569a19371470d0e3ae44cca86cf3 GREEN
Prototype.java f4b13f86b7511edacc138053ffb80cecbac70868 GREEN
PrototypeCategory.java ca500b4816ed42b9536488669aeab89561d2f08c GREEN
TransformationProviderChain.java 67ec6d0b4c23d295323572649606d79f3b897437 GREEN
TutorialAtomicStep.java 09c0d6597d542b431b5bbdc790ee9e533d9f77fb GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2011 fortiss GmbH |
| Copyright 2023 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
......@@ -35,36 +35,53 @@ public class Prototype {
/** Flag for primary class prototypes. */
private final boolean isPrimary;
/** Stores the priority of the category. Is important for sorting: lower value = lower prio. */
private final int prototypePriority;
/** Constructor. */
public Prototype(String name, EObject prototype, boolean isPrimary) {
this.name = name;
this.prototype = prototype;
this.isPrimary = isPrimary;
this.prototypePriority = 0;
}
/** Constructor with priority. */
public Prototype(String name, EObject prototype, boolean isPrimary, int priority) {
this.name = name;
this.prototype = prototype;
this.isPrimary = isPrimary;
this.prototypePriority = priority;
}
/** Returns the {@link Prototype}'s name. */
public String getName() {
return name;
return this.name;
}
/** Returns the {@link Prototype} instance (NOT a copy!). */
public EObject getPrototype() {
return prototype;
return this.prototype;
}
/** Returns a copy of the {@link Prototype}. This method is potentially expensive. */
public EObject getPrototypeCopy() {
return copy(prototype);
return copy(this.prototype);
}
/** Returns the priority of the prototype. */
public int getPriority() {
return this.prototypePriority;
}
/** {@inheritDoc} */
@Override
public String toString() {
return "Prototype for: " + name;
return "Prototype for: " + this.name;
}
/** Returns whether this {@link Prototype} is a primary one or not. */
public boolean isPrimary() {
return isPrimary;
return this.isPrimary;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment