Skip to content
Snippets Groups Projects
Commit 2aaa2d1f authored by Sebastian Bergemann's avatar Sebastian Bergemann
Browse files

Add sorting within the "new" context menu entries

Issue-Ref: 4323
Issue-Url: af3#4323



Signed-off-by: default avatarSebastian Bergemann <bergemann@fortiss.org>
parent cb27cd06
No related branches found
No related tags found
1 merge request!208Fix bad display of element choices in the in-editor context menu
AbstractNameEditingSupport.java c57336a0e0da18711a1610ca667dfea76728807f GREEN
ActionUtils.java 322f43d4f92f992daef8ac88eb0f9197c840c89b GREEN
ContextMenuUtils.java a55ceed42f2eb88ba263a6fbcb394ddb80b1eda0 GREEN
ContextMenuUtils.java 824b943c3f888d8aa1a427131f0bee3e9ad6e8c8 YELLOW
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,11 +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;
......@@ -64,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 =
......@@ -74,32 +75,49 @@ public final class ContextMenuUtils {
Menu newMenu = new Menu("New ...");
Menu reuseMenu = new Menu("Reuse ...");
List<Prototype> protos = pers.getComposablePrototypes(target.getClass());
for(Prototype p : protos) {
EObject prototypeCopy = p.getPrototypeCopy();
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 menuItem = new MenuItem(p.getName());
menuItem.setOnAction(evt -> {
css.runAsCommand(target, () -> {
ecs.compose(target, prototypeCopy, context);
});
});
if(isCorrectReuseElement(prototypeCopy)) {
MenuItem menuItem = createMenuItemForComposableElement(prototypeCopy, target,
context, prototype.getName());
String libraryName = getNameOfFirstRelatedLibrary(prototypeCopy);
menuItem.setText("[" + libraryName + "] " + p.getName());
menuItem.setText("[" + libraryName + "] " + prototype.getName());
reuseMenu.getItems().add(menuItem);
} else {
newMenu.getItems().add(menuItem);
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());
}
// TODO 4323: add priority system to prototypes (not only categories) and use it
// here to sort the newMenu list
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());
newMenu.getItems().add(menuItem);
}
if(!newMenu.getItems().isEmpty()) {
......@@ -112,6 +130,33 @@ public final class ContextMenuUtils {
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.
*
......
......@@ -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 e33c931185a01c6115f4cebd4a3cdc8552ea971e YELLOW
TransformationContextChainBase.java 1ef37880ab275778c563928e80ba378fec964cb6 GREEN
TransformationProviderBase.java 9e91100cc1f2c8fbd8d41af55aedfea34e02ff71 GREEN
......@@ -70,6 +70,14 @@ public abstract class PrototypeProviderBase implements IPrototypeProvider {
registerElementInCategory(prototypeObject, categoryName);
}
/** Registers the given {@link EObject} with the given name, category, and priority. */
protected final void registerPrototype(String name, EObject prototype, String categoryName,
int priority) {
Prototype prototypeObject = new Prototype(name, prototype, false, priority);
prototypes.add(prototypeObject);
registerElementInCategory(prototypeObject, categoryName);
}
/**
* Registers the given {@link EObject} with the given name as primary
* prototype with the given category.
......@@ -81,6 +89,17 @@ public abstract class PrototypeProviderBase implements IPrototypeProvider {
registerElementInCategory(prototypeObject, categoryName);
}
/**
* 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) {
Prototype prototypeObject = new Prototype(name, prototype, true, priority);
prototypes.add(prototypeObject);
registerElementInCategory(prototypeObject, categoryName);
}
/** {@inheritDoc} */
@Override
public final List<Prototype> getPrototypes() {
......
......@@ -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 dd4951fa6cbe1b05ea349e6de6c84eae875b3eb9 YELLOW
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,11 +35,23 @@ 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 int prototypePriority;
/** Constructor. */
public Prototype(String name, EObject prototype, boolean isPrimary) {
this.name = name;
this.prototype = prototype;
this.isPrimary = isPrimary;
prototypePriority = 0;
}
/** Constructor with priority. */
public Prototype(String name, EObject prototype, boolean isPrimary, int priority) {
this.name = name;
this.prototype = prototype;
this.isPrimary = isPrimary;
prototypePriority = priority;
}
/** Returns the {@link Prototype}'s name. */
......@@ -57,6 +69,16 @@ public class Prototype {
return copy(prototype);
}
/** Returns the priority of the prototype. */
public int getPriority() {
return prototypePriority;
}
/** Sets the priority of the category. */
public void setPriority(int priority) {
prototypePriority = priority;
}
/** {@inheritDoc} */
@Override
public String toString() {
......
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