diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/.ratings index 697cd6f282fa4836edb747c300ecf5eaeac31362..85880e1ae8cf462458bea54302c74c6e7bee8f24 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/.ratings +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/.ratings @@ -1,6 +1,6 @@ DiagramCoordinate.java 6b00aec99054d4cd19003a72bd4e5e774ac6a641 GREEN DiagramLayers.java aa1f95dbae290c8b00202abe4385b01b8f36e5ab GREEN -DiagramViewer.java 351c189b0811a472ef7728b227d4b2487f0db443 GREEN +DiagramViewer.java 537358db18da8b5ba2bf56082bfd87dec3ca88d8 YELLOW DiagramViewerDefaultTags.java 6230763252409c60009ab8887b4ef582cf883229 GREEN DiagramViewerFeatures.java 3dd78d9c117fc156924a151c6f8d770c53c103bc GREEN DiagramViewerSelection.java e833f592543bc97077907d980a39b123fc4044e6 GREEN diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/DiagramViewer.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/DiagramViewer.java index 351c189b0811a472ef7728b227d4b2487f0db443..537358db18da8b5ba2bf56082bfd87dec3ca88d8 100644 --- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/DiagramViewer.java +++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/lwfxef/DiagramViewer.java @@ -249,7 +249,7 @@ public class DiagramViewer { return viewerManager.diagramBundle; } - /** Shows the context menu when the mouse is pressed. */ + /** Hides the context menu if it is active. */ /* package */ void hideContextMenu() { if(contextMenu != null && contextMenu.isShowing()) { contextMenu.hide(); @@ -257,7 +257,7 @@ public class DiagramViewer { } } - /** Shows the context menu when the mouse is released. */ + /** Shows the context menu. */ public void showContextMenu(Node node, DiagramCoordinate diagramLocation, IMVCBundle mvcb) { List<MenuItem> items = mvcb.getController().contextMenuContributions(node, diagramLocation); if(items == null || items.isEmpty()) { diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/.ratings b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/.ratings index ac0692f78816a9d391d2f95d4479d24689437d37..8cad5c72f4ff79a0800198051548fc73c7fe06e7 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/.ratings +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/.ratings @@ -1,4 +1,4 @@ -AddElementContextMenu.java ab4536e73323802e46d50d0bb9f62d6becc9db39 GREEN -GetUpdatedOriginsContextMenu.java cfb16bb084be5a76a7ec70c2882242543535bc87 GREEN -UpdateFromLibContextMenu.java 58f56f4c2da66655a5c1bf93c370bd4f2dba5dd4 GREEN -UpdateInLibContextMenu.java cb5aeb92f8e511339bec675a3764284d977dd45d GREEN +AddElementContextMenu.java 592522843ed720c4e244793710accb0bd82bdd9d YELLOW +GetUpdatedOriginsContextMenu.java a356ea9869f8279f2700222b1fd8382a29b06399 YELLOW +UpdateFromLibContextMenu.java cbd01f8487f121872754ca7fdfe7f2817d805ab6 YELLOW +UpdateInLibContextMenu.java d2d1b21ec09dcd2e239974891884e8b77e94157b YELLOW diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/AddElementContextMenu.java b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/AddElementContextMenu.java index ab4536e73323802e46d50d0bb9f62d6becc9db39..592522843ed720c4e244793710accb0bd82bdd9d 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/AddElementContextMenu.java +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/AddElementContextMenu.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2021 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. | @@ -22,7 +22,9 @@ import static org.eclipse.jface.window.Window.OK; import static org.eclipse.ui.PlatformUI.getWorkbench; import static org.fortiss.tooling.ext.reuse.ui.prototypes.PrototypeProvider.DEFAULT_ROOT_REUSE_ELEMENT_NAME; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.manageExternalReferencesOfReuseElement; +import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfDeletedReuseElem; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryViewUtils.raiseAndUpdateReuseLibraryView; +import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.doesReuseElementExistWithContext; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getAllLocalReuseLibraries; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getReuseElementName; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.hasReusePossibility; @@ -59,12 +61,17 @@ import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider; public class AddElementContextMenu implements IContextMenuContributor { /** The title of the dialog shell(s). */ - private final String shellTitle = "Add Element to Reuse Library"; + private static final String SHELL_TITLE = "Add Element to Reuse Library"; + + /** The description of the processed action. */ + private static final String ACTION_DESCRIPTION = + "Adding a selected reuse element to a selected reuse library"; /** {@inheritDoc} */ @Override - public List<IContributionItem> getContributedItems(EObject selection, ContextMenuContextProvider contextProvider) { - if (selection != null && canBeAdded(selection)) { + public List<IContributionItem> getContributedItems(EObject selection, + ContextMenuContextProvider contextProvider) { + if(selection != null && canBeAdded(selection)) { AddToLibAction action = new AddToLibAction(selection); List<IContributionItem> contributionItems = new ArrayList<IContributionItem>(); contributionItems.add(new ActionContributionItem(action)); @@ -86,7 +93,7 @@ public class AddElementContextMenu implements IContextMenuContributor { /** Add one given element to specified {@link ReuseLibrary}. */ protected void addElement(ReuseLibrary library, EObject elementToAdd, String saveName) { - if (library == null || elementToAdd == null || saveName == null) { + if(library == null || elementToAdd == null || saveName == null) { return; } @@ -94,14 +101,15 @@ public class AddElementContextMenu implements IContextMenuContributor { try { addElementToLibrary(newElementForLibrary, library); - } catch (Exception e) { - openError(getActiveWorkbenchWindow().getShell(), "Error in '" + shellTitle + "'", e.getMessage()); + } catch(Exception e) { + openError(getActiveWorkbenchWindow().getShell(), "Error in '" + SHELL_TITLE + "'", + e.getMessage()); } PrototypeService.getInstance().updatePrototypes(); String currentActionType = "addToLibrary"; - manageExternalReferencesOfReuseElement(elementToAdd, currentActionType, shellTitle); + manageExternalReferencesOfReuseElement(elementToAdd, currentActionType, SHELL_TITLE); } /** The action "add element to {@link ReuseLibrary}". */ @@ -121,7 +129,8 @@ public class AddElementContextMenu implements IContextMenuContributor { /** Constructor. Takes the selected element as input. */ AddToLibAction(EObject element) { - super("Add to Reuse Library", ToolingReuseUIActivator.getImageDescriptor("icons/library_add.png")); + super("Add to Reuse Library", + ToolingReuseUIActivator.getImageDescriptor("icons/library_add.png")); selectedElement = element; } @@ -134,65 +143,80 @@ public class AddElementContextMenu implements IContextMenuContributor { Boolean createNewLibrary; String creationText; - if (libraryList != null && !libraryList.isEmpty()) { - // update existing library names - for (ReuseLibrary tmpLibrary : libraryList) { + if(libraryList != null && !libraryList.isEmpty()) { + // Update existing library names. + for(ReuseLibrary tmpLibrary : libraryList) { currentExistingLibraryNames.add(getReuseElementName(tmpLibrary)); } - // ask for library selection + // Ask for library selection. final Shell shell = getWorkbench().getActiveWorkbenchWindow().getShell(); - final AddElementToLibDialog librarySelectDialog = new AddElementToLibDialog(shell, shellTitle); + final AddElementToLibDialog librarySelectDialog = + new AddElementToLibDialog(shell, SHELL_TITLE); librarySelectDialog.setLibraryList(libraryList); librarySelectDialog.open(); - if (!librarySelectDialog.goAhead()) { + if(!librarySelectDialog.goAhead()) { return; } selectedLibrary = librarySelectDialog.getLibrary(); createNewLibrary = librarySelectDialog.createNew(); - creationText = "A new library will be created in which the element can be added.\nEnter a name for the library:\n "; + creationText = + "A new library will be created in which the element can be added.\nEnter a name for the library:\n "; } else { createNewLibrary = true; - creationText = "No library exists yet to add the element. A new one will be created.\nEnter a name for the library:\n "; + creationText = + "No library exists yet to add the element. A new one will be created.\nEnter a name for the library:\n "; } - if (createNewLibrary) { - final InputDialog creationDialog = new InputDialog(Display.getCurrent().getActiveShell(), shellTitle, - creationText, DEFAULT_ROOT_REUSE_ELEMENT_NAME, new NewLibNameValidator()); - if (creationDialog.open() != OK) { + if(createNewLibrary) { + final InputDialog creationDialog = new InputDialog( + Display.getCurrent().getActiveShell(), SHELL_TITLE, creationText, + DEFAULT_ROOT_REUSE_ELEMENT_NAME, new NewLibNameValidator()); + if(creationDialog.open() != OK) { + return; + } + if(doesReuseElementExistWithContext(selectedElement)) { + ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { + selectedLibrary = createAndAddReuseLibrary(creationDialog.getValue()); + }); + } else { + warnOfDeletedReuseElem(ACTION_DESCRIPTION, SHELL_TITLE); return; } - ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { - selectedLibrary = createAndAddReuseLibrary(creationDialog.getValue()); - }); } - if (selectedLibrary == null) { + if(selectedLibrary == null) { return; } - // update existing element names + // Update existing element names. currentExistingLibraryElementNames = new ArrayList<String>(); EList<EObject> elementList = selectedLibrary.getReuseElementList(); - for (EObject libraryElement : elementList) { + for(EObject libraryElement : elementList) { currentExistingLibraryElementNames.add(getReuseElementName(libraryElement)); } - // ask for new element name (when saving element to library) - final InputDialog addingDialog = new InputDialog(Display.getCurrent().getActiveShell(), shellTitle, - "Enter the name with which the element should be saved in the library '" - + getReuseElementName(selectedLibrary) + "':\n ", + // Ask for new element name (when saving element to library). + final InputDialog addingDialog = new InputDialog(Display.getCurrent().getActiveShell(), + SHELL_TITLE, + "Enter the name with which the element should be saved in the library '" + + getReuseElementName(selectedLibrary) + "':\n ", getReuseElementName(selectedElement), new AddedNameValidator()); - if (addingDialog.open() != OK) { + if(addingDialog.open() != OK) { return; } String newName = addingDialog.getValue(); - // execute "adding" action - ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { - addElement(selectedLibrary, selectedElement, newName); - }); + // Execute "adding" action (if the element still exists). + if(doesReuseElementExistWithContext(selectedElement)) { + ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { + addElement(selectedLibrary, selectedElement, newName); + }); + } else { + warnOfDeletedReuseElem(ACTION_DESCRIPTION, SHELL_TITLE); + return; + } raiseAndUpdateReuseLibraryView(); } @@ -203,11 +227,11 @@ public class AddElementContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public String isValid(String newText) { - if (newText.equals("")) { + if(newText.equals("")) { return "A name needs to be specified."; } - for (String name : currentExistingLibraryNames) { - if (newText.equals(name)) { + for(String name : currentExistingLibraryNames) { + if(newText.equals(name)) { return "A library with this name already exists."; } } @@ -221,11 +245,11 @@ public class AddElementContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public String isValid(String newText) { - if (newText.equals("")) { + if(newText.equals("")) { return "A name needs to be specified."; } - for (String name : currentExistingLibraryElementNames) { - if (newText.equals(name)) { + for(String name : currentExistingLibraryElementNames) { + if(newText.equals(name)) { return "This name already exists. Please use another."; } } diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/GetUpdatedOriginsContextMenu.java b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/GetUpdatedOriginsContextMenu.java index cfb16bb084be5a76a7ec70c2882242543535bc87..a356ea9869f8279f2700222b1fd8382a29b06399 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/GetUpdatedOriginsContextMenu.java +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/GetUpdatedOriginsContextMenu.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2022 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. | @@ -18,6 +18,8 @@ package org.fortiss.tooling.ext.reuse.ui.commands; import static java.util.Collections.emptyList; import static org.conqat.ide.commons.ui.dialog.MessageUtils.showInfo; import static org.fortiss.tooling.ext.reuse.ui.commands.UpdateFromLibContextMenu.UPDATE_FROM_CONTEXT_MENU_STRING; +import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfDeletedReuseElem; +import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.doesReuseElementExistWithContext; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getAllUpdatedOrigins; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getFirstSourceLibraryOfElement; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getLibraryIDName; @@ -48,15 +50,20 @@ import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider; public class GetUpdatedOriginsContextMenu implements IContextMenuContributor { /** The title of the dialog shell(s). */ - private final String shellTitle = "Information about updated reuse origins"; + private static final String SHELL_TITLE = "Information about updated reuse origins"; + + /** The description of the processed action. */ + private static final String ACTION_DESCRIPTION = + "Informing the user about updated reuse origins"; /** The string of the context menu entry. */ public static final String updatedOrigincontextMenuString = "Get reuse update information"; /** {@inheritDoc} */ @Override - public List<IContributionItem> getContributedItems(EObject selection, ContextMenuContextProvider contextProvider) { - if (selection != null && canBeAdded(selection)) { + public List<IContributionItem> getContributedItems(EObject selection, + ContextMenuContextProvider contextProvider) { + if(selection != null && canBeAdded(selection)) { GetUpdateInfoAction action = new GetUpdateInfoAction(selection); List<IContributionItem> contributionItems = new ArrayList<IContributionItem>(); contributionItems.add(new ActionContributionItem(action)); @@ -84,7 +91,7 @@ public class GetUpdatedOriginsContextMenu implements IContextMenuContributor { * element. */ protected void displayOriginUpdateInfo(EObject reuseElement) { - if (reuseElement == null) { + if(reuseElement == null) { return; } @@ -94,26 +101,29 @@ public class GetUpdatedOriginsContextMenu implements IContextMenuContributor { // It could also be possible to provide directly update options within this // info/dialog box. String infoMessage; - if (updatedReuseOrigins.isEmpty()) { - infoMessage = "None of the found reuse origins was updated since the selected reuse element was reused or updated (based of these origins)." - + "\n\nTherefore, it is not needed to get updates from any of them except you want to get the original version (e.g., if you have manipulated the selected reuse element in the meantime and wants to reset it)." - + "\n\nKeep in mind: Updated reuse origins cannot be found if your local reuse libraries are not up to date as well!"; + if(updatedReuseOrigins.isEmpty()) { + infoMessage = + "None of the found reuse origins was updated since the selected reuse element was reused or updated (based of these origins)." + + "\n\nTherefore, it is not needed to get updates from any of them except you want to get the original version (e.g., if you have manipulated the selected reuse element in the meantime and wants to reset it)." + + "\n\nKeep in mind: Updated reuse origins cannot be found if your local reuse libraries are not up to date as well!"; } else { - infoMessage = "The following reuse origins of the selected reuse element were updated since the selected reuse element was reused or updated (based of these origins):\n"; - for (EObject updatedReuseOrigin : updatedReuseOrigins) { + infoMessage = + "The following reuse origins of the selected reuse element were updated since the selected reuse element was reused or updated (based of these origins):\n"; + for(EObject updatedReuseOrigin : updatedReuseOrigins) { String reuseElementName = getReuseElementName(updatedReuseOrigin); ReuseLibrary library = getFirstSourceLibraryOfElement(updatedReuseOrigin); String libraryName = getLibraryIDName(library); boolean stringIsForDisplay = true; - String lastUpdate = getReuseElementLastUpdateString(updatedReuseOrigin, stringIsForDisplay); + String lastUpdate = + getReuseElementLastUpdateString(updatedReuseOrigin, stringIsForDisplay); infoMessage += "\nName: '" + reuseElementName + "'\n"; infoMessage += "Within library: '" + libraryName + "'\n"; infoMessage += "Last updated: " + lastUpdate + "\n"; } - infoMessage += "\nIf you want to get one of these updates, please use the '" - + UPDATE_FROM_CONTEXT_MENU_STRING + "' context menu entry."; + infoMessage += "\nIf you want to get one of these updates, please use the '" + + UPDATE_FROM_CONTEXT_MENU_STRING + "' context menu entry."; } - showInfo(shellTitle, infoMessage); + showInfo(SHELL_TITLE, infoMessage); } /** The action "get info about reuse origin updates". */ @@ -132,9 +142,14 @@ public class GetUpdatedOriginsContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public void run() { - ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { - displayOriginUpdateInfo(selectedElement); - }); + if(doesReuseElementExistWithContext(selectedElement)) { + ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { + displayOriginUpdateInfo(selectedElement); + }); + } else { + warnOfDeletedReuseElem(ACTION_DESCRIPTION, SHELL_TITLE); + return; + } } } } diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateFromLibContextMenu.java b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateFromLibContextMenu.java index 58f56f4c2da66655a5c1bf93c370bd4f2dba5dd4..cbd01f8487f121872754ca7fdfe7f2817d805ab6 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateFromLibContextMenu.java +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateFromLibContextMenu.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2021 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. | @@ -24,7 +24,9 @@ import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.checkFo import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.isKeepingAdditionalReferencesRequested; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.manageExternalReferencesOfReuseElement; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.selectCorrectReuseSpec; +import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfDeletedReuseElem; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfMissingLib; +import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.doesReuseElementExistWithContext; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getLibraryIDName; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getLocalReuseLibraryByID; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getNumberOfContainedReuseSpecs; @@ -65,13 +67,18 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { /** The title of the dialog shell(s). */ private static final String SHELL_TITLE = "Update Element From Reuse Library"; + /** The description of the processed action. */ + private static final String ACTION_DESCRIPTION = + "Informing the user about updated reuse origins"; + /** The string of the context menu entry. */ public static final String UPDATE_FROM_CONTEXT_MENU_STRING = "Update from Reuse Library"; /** {@inheritDoc} */ @Override - public List<IContributionItem> getContributedItems(EObject selection, ContextMenuContextProvider contextProvider) { - if (selection != null && canBeUpdated(selection)) { + public List<IContributionItem> getContributedItems(EObject selection, + ContextMenuContextProvider contextProvider) { + if(selection != null && canBeUpdated(selection)) { UpdateFromLibAction action = new UpdateFromLibAction(selection); List<IContributionItem> contributionItems = new ArrayList<IContributionItem>(); contributionItems.add(new ActionContributionItem(action)); @@ -101,22 +108,24 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { */ protected void updateElementFromLibrary(EObject elementToBeUpdated, ReuseLibrary library, ReuseElementSpec referencingSpec, String newName, boolean keepAdditionalReferences) { - if (library == null || elementToBeUpdated == null || referencingSpec == null || newName == null) { + if(library == null || elementToBeUpdated == null || referencingSpec == null || + newName == null) { return; } - // find original element inside the library - EObject originalLibraryElement = checkForElementInLibrary(library, referencingSpec, SHELL_TITLE); - if (originalLibraryElement == null) { + // Find original element inside the library. + EObject originalLibraryElement = + checkForElementInLibrary(library, referencingSpec, SHELL_TITLE); + if(originalLibraryElement == null) { return; } - EObject updatedElement = replaceElementInProject(elementToBeUpdated, originalLibraryElement, newName, - keepAdditionalReferences); - if (updatedElement == null) { + EObject updatedElement = replaceElementInProject(elementToBeUpdated, originalLibraryElement, + newName, keepAdditionalReferences); + if(updatedElement == null) { String infoTitle = SHELL_TITLE + " - Replacement Failed"; - String infoMessage = "The replacement of '" + getReuseElementName(elementToBeUpdated) + "' with '" - + getReuseElementName(originalLibraryElement) + "' has failed."; + String infoMessage = "The replacement of '" + getReuseElementName(elementToBeUpdated) + + "' with '" + getReuseElementName(originalLibraryElement) + "' has failed."; showInfo(infoTitle, infoMessage); return; } @@ -124,7 +133,8 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { fixIDs(updatedElement); String currentActionType = "updateFromLibrary"; - manageExternalReferencesOfReuseElement(originalLibraryElement, currentActionType, SHELL_TITLE); + manageExternalReferencesOfReuseElement(originalLibraryElement, currentActionType, + SHELL_TITLE); } /** The action "update element from {@link ReuseLibrary}". */ @@ -143,40 +153,43 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public void run() { - ReuseElementSpec correctLibrarySpec = selectCorrectReuseSpec(selectedElement, SHELL_TITLE); - if (correctLibrarySpec == null) { + ReuseElementSpec correctLibrarySpec = + selectCorrectReuseSpec(selectedElement, SHELL_TITLE); + if(correctLibrarySpec == null) { return; } - ReuseLibrary correctLibrary = getLocalReuseLibraryByID(correctLibrarySpec.getSourceLibUUID()); + ReuseLibrary correctLibrary = + getLocalReuseLibraryByID(correctLibrarySpec.getSourceLibUUID()); - if (correctLibrary == null) { + if(correctLibrary == null) { warnOfMissingLib(correctLibrarySpec, SHELL_TITLE); return; } String currentNameInProject = getReuseElementName(selectedElement); String currentProjectName = "not found"; - EList<INamedCommentedElement> parentList = getParentsWithType(selectedElement, - INamedCommentedElement.class); - if (parentList != null && !parentList.isEmpty()) { - // safe due to check above and last element is the highest parent aka root + EList<INamedCommentedElement> parentList = + getParentsWithType(selectedElement, INamedCommentedElement.class); + if(parentList != null && !parentList.isEmpty()) { + // Safe due to check above and last element is the highest parent aka root. INamedCommentedElement root = parentList.get(parentList.size() - 1); currentProjectName = root.getName(); } - // find original element inside the library - EObject originalLibraryElement = checkForElementInLibrary(correctLibrary, correctLibrarySpec, SHELL_TITLE); - if (originalLibraryElement == null) { + // Find original element inside the library. + EObject originalLibraryElement = + checkForElementInLibrary(correctLibrary, correctLibrarySpec, SHELL_TITLE); + if(originalLibraryElement == null) { return; } - // in case of multiple library references within the selected element, ask if + // In case of multiple library references within the selected element, ask if // they should be kept or if all should be overridden/replaced by the library - // reference of the current update - boolean keepAdditionalReferences = isKeepingAdditionalReferencesRequested(selectedElement, - correctLibrarySpec, SHELL_TITLE); + // reference of the current update. + boolean keepAdditionalReferences = isKeepingAdditionalReferencesRequested( + selectedElement, correctLibrarySpec, SHELL_TITLE); - // confirm the update + // Confirm the update. String currentNameInLibrary = getReuseElementName(originalLibraryElement); List<String> textLines = new ArrayList<String>(); textLines.add("Confirm the following update (from source to target):"); @@ -186,8 +199,8 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { textLines.add("\n- Update target: element in current project"); textLines.add(" > element name: " + currentNameInProject); textLines.add(" > in project: " + currentProjectName); - if (getNumberOfContainedReuseSpecs(selectedElement) > 1) { - if (keepAdditionalReferences) { + if(getNumberOfContainedReuseSpecs(selectedElement) > 1) { + if(keepAdditionalReferences) { textLines.add( "\nBesides the current update reference, all (old) additional library references will be kept within the element."); } else { @@ -197,25 +210,31 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { } boolean confirmed = confirm(SHELL_TITLE, join("\n", textLines)); - if (!confirmed) { + if(!confirmed) { return; } - // ask for element name (when updating element in project) - final InputDialog updateDialog = new InputDialog(Display.getCurrent().getActiveShell(), SHELL_TITLE, - "Enter the name with which the element should be updated in the project:\n(Current name in project: " - + currentNameInProject + ")\n ", + // Ask for element name (when updating element in project). + final InputDialog updateDialog = new InputDialog(Display.getCurrent().getActiveShell(), + SHELL_TITLE, + "Enter the name with which the element should be updated in the project:\n(Current name in project: " + + currentNameInProject + ")\n ", currentNameInProject, new UpdatedNameValidator()); - if (updateDialog.open() != OK) { + if(updateDialog.open() != OK) { return; } String updateName = updateDialog.getValue(); - // execute "updating" action - ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { - updateElementFromLibrary(selectedElement, correctLibrary, correctLibrarySpec, updateName, - keepAdditionalReferences); - }); + // Execute "updating" action (if the selected element still exists). + if(doesReuseElementExistWithContext(selectedElement)) { + ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { + updateElementFromLibrary(selectedElement, correctLibrary, correctLibrarySpec, + updateName, keepAdditionalReferences); + }); + } else { + warnOfDeletedReuseElem(ACTION_DESCRIPTION, SHELL_TITLE); + return; + } } /** Validator for the name of the updated element. */ @@ -224,7 +243,7 @@ public class UpdateFromLibContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public String isValid(String newText) { - if (newText.equals("")) { + if(newText.equals("")) { return "A name needs to be specified."; } return null; diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateInLibContextMenu.java b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateInLibContextMenu.java index cb5aeb92f8e511339bec675a3764284d977dd45d..d2d1b21ec09dcd2e239974891884e8b77e94157b 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateInLibContextMenu.java +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/commands/UpdateInLibContextMenu.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2021 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. | @@ -25,8 +25,10 @@ import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.s import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.checkForElementInLibrary; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.manageExternalReferencesOfReuseElement; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.selectCorrectReuseSpec; +import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfDeletedReuseElem; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryUIUtils.warnOfMissingLib; import static org.fortiss.tooling.ext.reuse.ui.utils.ReuseLibraryViewUtils.raiseAndUpdateReuseLibraryView; +import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.doesReuseElementExistWithContext; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getLibraryIDName; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getLocalReuseLibraryByID; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryUtilsBasics.getReuseElementName; @@ -66,13 +68,18 @@ public class UpdateInLibContextMenu implements IContextMenuContributor { /** The title of the dialog shell(s). */ private static final String SHELL_TITLE = "Update Element In Reuse Library"; + /** The description of the processed action. */ + private static final String ACTION_DESCRIPTION = + "Updating the selected reuse element inside the selected reuse library"; + /** The string of the context menu entry. */ public static final String UPDATE_IN_CONTEXT_MENU_STRING = "Update in Reuse Library"; /** {@inheritDoc} */ @Override - public List<IContributionItem> getContributedItems(EObject selection, ContextMenuContextProvider contextProvider) { - if (selection != null && canBeUpdated(selection)) { + public List<IContributionItem> getContributedItems(EObject selection, + ContextMenuContextProvider contextProvider) { + if(selection != null && canBeUpdated(selection)) { UpdateInLibAction action = new UpdateInLibAction(selection); List<IContributionItem> contributionItems = new ArrayList<IContributionItem>(); contributionItems.add(new ActionContributionItem(action)); @@ -101,22 +108,24 @@ public class UpdateInLibContextMenu implements IContextMenuContributor { * {@link ReuseElementSpec} is the one that connects reuse element and * {@link ReuseLibrary}. */ - protected void updateElemInLib(EObject newElement, ReuseLibrary library, ReuseElementSpec referencingSpec, - String newName) { - if (library == null || newElement == null || referencingSpec == null || newName == null) { + protected void updateElemInLib(EObject newElement, ReuseLibrary library, + ReuseElementSpec referencingSpec, String newName) { + if(library == null || newElement == null || referencingSpec == null || newName == null) { return; } - EObject originalLibraryElement = checkForElementInLibrary(library, referencingSpec, SHELL_TITLE); - if (originalLibraryElement == null) { + EObject originalLibraryElement = + checkForElementInLibrary(library, referencingSpec, SHELL_TITLE); + if(originalLibraryElement == null) { return; } replaceElementInLibrary(originalLibraryElement, newElement, referencingSpec, newName); try { saveReuseLibrary(library); - } catch (Exception e) { - openError(getActiveWorkbenchWindow().getShell(), "Error in '" + SHELL_TITLE + "'", e.getMessage()); + } catch(Exception e) { + openError(getActiveWorkbenchWindow().getShell(), "Error in '" + SHELL_TITLE + "'", + e.getMessage()); } PrototypeService.getInstance().updatePrototypes(); @@ -144,33 +153,36 @@ public class UpdateInLibContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public void run() { - ReuseElementSpec correctLibrarySpec = selectCorrectReuseSpec(selectedElement, SHELL_TITLE); - if (correctLibrarySpec == null) { + ReuseElementSpec correctLibrarySpec = + selectCorrectReuseSpec(selectedElement, SHELL_TITLE); + if(correctLibrarySpec == null) { return; } - ReuseLibrary correctLibrary = getLocalReuseLibraryByID(correctLibrarySpec.getSourceLibUUID()); + ReuseLibrary correctLibrary = + getLocalReuseLibraryByID(correctLibrarySpec.getSourceLibUUID()); - if (correctLibrary == null) { + if(correctLibrary == null) { warnOfMissingLib(correctLibrarySpec, SHELL_TITLE); return; } - // find original element inside the library - EObject originalLibraryElement = checkForElementInLibrary(correctLibrary, correctLibrarySpec, SHELL_TITLE); - if (originalLibraryElement == null) { + // Find original element inside the library. + EObject originalLibraryElement = + checkForElementInLibrary(correctLibrary, correctLibrarySpec, SHELL_TITLE); + if(originalLibraryElement == null) { return; } String currentNameInLibrary = getReuseElementName(originalLibraryElement); String currentProjectName = "not found"; - EList<INamedCommentedElement> parentList = getParentsWithType(selectedElement, - INamedCommentedElement.class); - if (parentList != null && !parentList.isEmpty()) { - // safe due to check above and last element is the highest parent aka root + EList<INamedCommentedElement> parentList = + getParentsWithType(selectedElement, INamedCommentedElement.class); + if(parentList != null && !parentList.isEmpty()) { + // Safe due to check above and last element is the highest parent aka root. INamedCommentedElement root = parentList.get(parentList.size() - 1); currentProjectName = root.getName(); } - // confirm the update + // Confirm the update. List<String> textLines = new ArrayList<String>(); textLines.add("Confirm the following update (from source to target):"); textLines.add("\n- Update source: element in current project"); @@ -181,35 +193,42 @@ public class UpdateInLibContextMenu implements IContextMenuContributor { textLines.add(" > in library: " + getLibraryIDName(correctLibrary)); boolean confirmed = confirm(SHELL_TITLE, join("\n", textLines)); - if (!confirmed) { + if(!confirmed) { return; } - // update existing element names + // Update existing element names. currentExistingLibraryElementNames = new ArrayList<String>(); EList<EObject> elementList = correctLibrary.getReuseElementList(); - for (EObject libraryElement : elementList) { + for(EObject libraryElement : elementList) { String name = getReuseElementName(libraryElement); - if (!name.equals(currentNameInLibrary)) { + if(!name.equals(currentNameInLibrary)) { currentExistingLibraryElementNames.add(name); } } - // ask for element name (when updating element in library) - final InputDialog updateDialog = new InputDialog(Display.getCurrent().getActiveShell(), SHELL_TITLE, - "Enter the name with which the element should be updated in the library '" - + getReuseElementName(correctLibrary) + "':\n(Current name in library: " - + currentNameInLibrary + ")\n ", + // Ask for element name (when updating element in library). + final InputDialog updateDialog = new InputDialog(Display.getCurrent().getActiveShell(), + SHELL_TITLE, + "Enter the name with which the element should be updated in the library '" + + getReuseElementName(correctLibrary) + "':\n(Current name in library: " + + currentNameInLibrary + ")\n ", currentNameInLibrary, new UpdatedNameValidator()); - if (updateDialog.open() != OK) { + if(updateDialog.open() != OK) { return; } String updateName = updateDialog.getValue(); - // execute "updating" action - ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { - updateElemInLib(selectedElement, correctLibrary, correctLibrarySpec, updateName); - }); + // Execute "updating" action (if the selected element still exists). + if(doesReuseElementExistWithContext(selectedElement)) { + ICommandStackService.getInstance().runAsCommand(selectedElement, () -> { + updateElemInLib(selectedElement, correctLibrary, correctLibrarySpec, + updateName); + }); + } else { + warnOfDeletedReuseElem(ACTION_DESCRIPTION, SHELL_TITLE); + return; + } raiseAndUpdateReuseLibraryView(); } @@ -220,11 +239,11 @@ public class UpdateInLibContextMenu implements IContextMenuContributor { /** {@inheritDoc} */ @Override public String isValid(String newText) { - if (newText.equals("")) { + if(newText.equals("")) { return "A name needs to be specified."; } - for (String name : currentExistingLibraryElementNames) { - if (newText.equals(name)) { + for(String name : currentExistingLibraryElementNames) { + if(newText.equals(name)) { return "This name already exists. Please use another."; } } diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/.ratings b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/.ratings index e057b2fb5a0d78886b8f9ca83ae197146e330326..5df61ff44ab96bf49b2d46d33fc74743cc86bb57 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/.ratings +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/.ratings @@ -1,2 +1,2 @@ -ReuseLibraryUIUtils.java fed62784975bf8e6fc1625505636e247dfb6c86e GREEN +ReuseLibraryUIUtils.java 1e800053a4f14dde685224b5da9ee2e55f8b2d21 YELLOW ReuseLibraryViewUtils.java 34a852dc692ec56cb3e9fd8dcea99d64f31503b3 GREEN diff --git a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/ReuseLibraryUIUtils.java b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/ReuseLibraryUIUtils.java index fed62784975bf8e6fc1625505636e247dfb6c86e..1e800053a4f14dde685224b5da9ee2e55f8b2d21 100644 --- a/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/ReuseLibraryUIUtils.java +++ b/org.fortiss.tooling.ext.reuse.ui/src/org/fortiss/tooling/ext/reuse/ui/utils/ReuseLibraryUIUtils.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2021 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. | @@ -65,31 +65,33 @@ public class ReuseLibraryUIUtils { * {@link ReuseElementSpec}s of the given element. Returns null if no * {@link ReuseElementSpec} could be selected. * - * @param element The target reuse element (source of the - * {@link ReuseElementSpec}) - * @param dialogTitle The title for the dialog with the user + * @param element + * The target reuse element (source of the + * {@link ReuseElementSpec}) + * @param dialogTitle + * The title for the dialog with the user * @return The {@link ReuseElementSpec} selected by the user */ public static ReuseElementSpec selectCorrectReuseSpec(EObject element, String dialogTitle) { - if (element instanceof IModelElement) { - List<ReuseElementSpec> specs = getAllReuseSpecs((IModelElement) element); - if (specs.size() == 1) { - // get(0) is safe due to size check + if(element instanceof IModelElement) { + List<ReuseElementSpec> specs = getAllReuseSpecs(element); + if(specs.size() == 1) { + // get(0) is safe due to size check. return specs.get(0); - } else if (specs.size() > 1) { - // ask for selection + } else if(specs.size() > 1) { + // Ask for selection. List<String> referencedLibrarySelection = new ArrayList<String>(); - for (ReuseElementSpec spec : specs) { - referencedLibrarySelection - .add(getLibraryIDNameFormat(spec.getSourceLibUUID(), spec.getSourceLibName()) - + " with reference to element '" + spec.getElementName() + "'"); + for(ReuseElementSpec spec : specs) { + referencedLibrarySelection.add(getLibraryIDNameFormat(spec.getSourceLibUUID(), + spec.getSourceLibName()) + " with reference to element '" + + spec.getElementName() + "'"); } final Shell shell = getActiveWorkbenchWindow().getShell(); final SelectLibDialog selectLibraryDialog = new SelectLibDialog(shell, dialogTitle); selectLibraryDialog.setLibraryNameList(referencedLibrarySelection); selectLibraryDialog.open(); - if (selectLibraryDialog.goAhead()) { + if(selectLibraryDialog.goAhead()) { return specs.get(selectLibraryDialog.getSelectionIndex()); } } @@ -103,38 +105,45 @@ public class ReuseLibraryUIUtils { * given main {@link ReuseElementSpec} or wants to override/delete all of them. * If no additional {@link ReuseElementSpec} was found, it will return true. * - * @param element The target reuse element (containing the library - * references) - * @param mainReference The main library reference to which the others are - * additional - * @param dialogTitle The title for the dialog with the user + * @param element + * The target reuse element (containing the library + * references) + * @param mainReference + * The main library reference to which the others are + * additional + * @param dialogTitle + * The title for the dialog with the user * @return True if (possible) additional library references should be kept or * false if they should be overridden */ - public static boolean isKeepingAdditionalReferencesRequested(EObject element, ReuseElementSpec mainReference, - String dialogTitle) { - if (element instanceof IModelElement) { - List<ReuseElementSpec> specs = getAllReuseSpecs((IModelElement) element); - // only if we have any additional references/reuse specifications, we need to + public static boolean isKeepingAdditionalReferencesRequested(EObject element, + ReuseElementSpec mainReference, String dialogTitle) { + if(element instanceof IModelElement) { + List<ReuseElementSpec> specs = getAllReuseSpecs(element); + // Only if we have any additional references/reuse specifications, we need to // ask (otherwise keeping is false, because "not existing refs/specs" do not be - // need to kept) - if (specs.size() > 1) { + // need to kept). + if(specs.size() > 1) { List<String> additionalLibraryReferences = new ArrayList<String>(); - for (ReuseElementSpec spec : specs) { - if (spec != mainReference) { - additionalLibraryReferences.add( - "- Library '" + getLibraryIDNameFormat(spec.getSourceLibUUID(), spec.getSourceLibName()) - + "' with '" + spec.getElementName() + "' as element name in the library"); + for(ReuseElementSpec spec : specs) { + if(spec != mainReference) { + additionalLibraryReferences.add("- Library '" + + getLibraryIDNameFormat(spec.getSourceLibUUID(), + spec.getSourceLibName()) + + "' with '" + spec.getElementName() + + "' as element name in the library"); } } List<String> textLines = new ArrayList<String>(); - textLines.add("The following additional library references were found in the selected element:"); + textLines.add( + "The following additional library references were found in the selected element:"); textLines.addAll(additionalLibraryReferences); textLines.add("\nDo you want to keep them after the update?"); textLines.add( "- Yes: All additional (old) references will be kept together with the one of the current update."); - textLines.add("- No: Then, only the library reference of the current update will further exist."); + textLines.add( + "- No: Then, only the library reference of the current update will further exist."); return askQuestion(dialogTitle, join("\n", textLines)); } } @@ -146,17 +155,20 @@ public class ReuseLibraryUIUtils { * by the given {@link ReuseElementSpec}. Also, displays warning (with * dialogTitle as header) if it could not be found, and then returns null. * - * @param library The target/source reuse library - * @param referencingSpec The reuse specification that reference the requested - * reuse element - * @param dialogTitle The title for the dialog + * @param library + * The target/source reuse library + * @param referencingSpec + * The reuse specification that reference the requested + * reuse element + * @param dialogTitle + * The title for the dialog * @return The requested reuse element if it could be found, otherwise null */ - public static EObject checkForElementInLibrary(ReuseLibrary library, ReuseElementSpec referencingSpec, - String dialogTitle) { + public static EObject checkForElementInLibrary(ReuseLibrary library, + ReuseElementSpec referencingSpec, String dialogTitle) { String targetUuid = referencingSpec.getElementUUID(); EObject originalElementInLibrary = getElementInsideLibrary(library, targetUuid); - if (originalElementInLibrary == null) { + if(originalElementInLibrary == null) { warnOfMissingReuseElem(referencingSpec, dialogTitle); } return originalElementInLibrary; @@ -166,16 +178,19 @@ public class ReuseLibraryUIUtils { * Displays a warning message to the user about a missing reuse element (with * extracted properties from the given {@link ReuseElementSpec} and title). * - * @param referencingSpec The reuse specification that has the information about - * the missing reuse element - * @param dialogTitle The title for the dialog + * @param referencingSpec + * The reuse specification that has the information about + * the missing reuse element + * @param dialogTitle + * The title for the dialog */ - public static void warnOfMissingReuseElem(ReuseElementSpec referencingSpec, String dialogTitle) { + public static void warnOfMissingReuseElem(ReuseElementSpec referencingSpec, + String dialogTitle) { List<String> textLines = new ArrayList<String>(); textLines.add("The referenced element does not exist in the referenced library."); textLines.add("\nSearched for element UUID: " + referencingSpec.getElementUUID()); - textLines.add("in library: " - + getLibraryIDNameFormat(referencingSpec.getSourceLibUUID(), referencingSpec.getSourceLibName())); + textLines.add("in library: " + getLibraryIDNameFormat(referencingSpec.getSourceLibUUID(), + referencingSpec.getSourceLibName())); showWarning(dialogTitle + " - Missing Element", join("\n", textLines)); } @@ -184,78 +199,102 @@ public class ReuseLibraryUIUtils { * (with extracted properties from the given {@link ReuseElementSpec} and * title). * - * @param referencingSpec The reuse specification that has the information about - * the missing reuse library - * @param dialogTitle The title for the dialog + * @param referencingSpec + * The reuse specification that has the information about + * the missing reuse library + * @param dialogTitle + * The title for the dialog */ public static void warnOfMissingLib(ReuseElementSpec referencingSpec, String dialogTitle) { List<String> textLines = new ArrayList<String>(); textLines.add("The selected library does not exist in the library directory."); textLines.add("\nPlease add it there and repeat."); - textLines.add("\nMissing library (UUID and name): " - + getLibraryIDNameFormat(referencingSpec.getSourceLibUUID(), referencingSpec.getSourceLibName())); + textLines.add("\nMissing library (UUID and name): " + getLibraryIDNameFormat( + referencingSpec.getSourceLibUUID(), referencingSpec.getSourceLibName())); textLines.add("\nLibrary directory location: " + REUSE_PROJECT_DIR.getAbsolutePath()); showWarning(dialogTitle + " - Missing Library", join("\n", textLines)); } + /** + * Displays a warning message to the user about a failed action due to a missing reuse element + * for it. + * + * @param currentAction + * The action description that has failed + * @param dialogTitle + * The title for the dialog + */ + public static void warnOfDeletedReuseElem(String currentAction, String dialogTitle) { + List<String> textLines = new ArrayList<String>(); + textLines.add( + "The following action could not successfully be performed since the initially selected reuse element for this action does not exist anymore."); + textLines.add("\nFailed action: '" + currentAction + "'."); + textLines.add( + "\nPlease, do not delete a reuse element as long as your chosen action for it has not fully terminated."); + showWarning(dialogTitle + " - Missing Element", join("\n", textLines)); + } + /** * Manages the external references (like user-defined functions specified * externally in a data dictionary) of the given reuse element. Currently, this * means to inform the user about existing ones. * - * @param reuseElement The reuse element in which external references should be - * managed/searched - * @param actionType The identifier of the current action. Currently - * available: "addToLibrary", "updateInLibrary", - * "updateFromLibrary" - * @param shellTitle The title for the current shell that will be used as - * dialog title + * @param reuseElement + * The reuse element in which external references should be + * managed/searched + * @param actionType + * The identifier of the current action. Currently + * available: "addToLibrary", "updateInLibrary", + * "updateFromLibrary" + * @param shellTitle + * The title for the current shell that will be used as + * dialog title */ - public static void manageExternalReferencesOfReuseElement(EObject reuseElement, String actionType, - String shellTitle) { - // currently, it is just checking for external references and informing the user + public static void manageExternalReferencesOfReuseElement(EObject reuseElement, + String actionType, String shellTitle) { + // Currently, it is just checking for external references and informing the user. // TODO #4150 : store them automatically together with the reuse element - // inside the library and use them also when the reuse element is used again + // inside the library and use them also when the reuse element is used again. - // some text depends on the action during which the external references are + // Some text depends on the action during which the external references are // searched: String infoTitle = shellTitle + " - External References"; String actionText = ""; String currentReuseTest = ""; String sourceText = ""; - switch (actionType) { - case "addToLibrary": - actionText = "added to the library"; - sourceText = "the added element"; - break; - case "updateInLibrary": - actionText = "updated in the library"; - sourceText = "the source element from the project"; - break; - case "updateFromLibrary": - actionText = "updated from the library"; - currentReuseTest = " (like now)"; - sourceText = "the source element from the library"; - break; - default: - showInfo(infoTitle, - "Cannot manage external references of the reuse element, " - + "because the given action type is not known: '" + actionType - + "'\n\nPlease inform a developer to correct this in the source code."); - return; + switch(actionType) { + case "addToLibrary": + actionText = "added to the library"; + sourceText = "the added element"; + break; + case "updateInLibrary": + actionText = "updated in the library"; + sourceText = "the source element from the project"; + break; + case "updateFromLibrary": + actionText = "updated from the library"; + currentReuseTest = " (like now)"; + sourceText = "the source element from the library"; + break; + default: + showInfo(infoTitle, "Cannot manage external references of the reuse element, " + + "because the given action type is not known: '" + actionType + + "'\n\nPlease inform a developer to correct this in the source code."); + return; } List<EObject> externalRefs = getExternalReferencesOfElement(reuseElement); - if (!externalRefs.isEmpty()) { - String headerText = "The following elements were found as references inside the reuse element you " - + actionText + ".\n\nIn case of reusing this element" + currentReuseTest - + ", these referenced elements will be needed in the new environment, too. " - + "Therefore, you might want to add/update these reference elements as well (if not already done).\n"; + if(!externalRefs.isEmpty()) { + String headerText = + "The following elements were found as references inside the reuse element you " + + actionText + ".\n\nIn case of reusing this element" + currentReuseTest + + ", these referenced elements will be needed in the new environment, too. " + + "Therefore, you might want to add/update these reference elements as well (if not already done).\n"; String bodyText = ""; - for (EObject externalRef : externalRefs) { - bodyText += "\n- " + getReuseElementName(externalRef) + " (" + externalRef.getClass().getSimpleName() - + ")"; - if (getReuseElementName(externalRef).equals(getReuseElementName(reuseElement))) { + for(EObject externalRef : externalRefs) { + bodyText += "\n- " + getReuseElementName(externalRef) + " (" + + externalRef.getClass().getSimpleName() + ")"; + if(getReuseElementName(externalRef).equals(getReuseElementName(reuseElement))) { bodyText += " [ignore this if it is actually " + sourceText + "]"; } } @@ -268,14 +307,18 @@ public class ReuseLibraryUIUtils { * Creates and displays a information box with only an OK button. Everything is * already set except the text for the title, the header and the body. * - * @param windowTitle The title of the dialog window - * @param headerText The text of the header between title and body - * @param bodyText The actual text body + * @param windowTitle + * The title of the dialog window + * @param headerText + * The text of the header between title and body + * @param bodyText + * The actual text body */ - public static void showBasicScrollableInformation(String windowTitle, String headerText, String bodyText) { + public static void showBasicScrollableInformation(String windowTitle, String headerText, + String bodyText) { boolean showSymbol = true; - showCustomScrollableAlert(INFORMATION, windowTitle, headerText, bodyText, showSymbol, DIALOG_WINDOW_WIDTH_PREF, - DIALOG_WINDOW_HEIGHT_PREF); + showCustomScrollableAlert(INFORMATION, windowTitle, headerText, bodyText, showSymbol, + DIALOG_WINDOW_WIDTH_PREF, DIALOG_WINDOW_HEIGHT_PREF); } /** @@ -284,21 +327,29 @@ public class ReuseLibraryUIUtils { * the symbol besides the header should be displayed, the actual text body and * the preferred size of the window (null if nothing is preferred). * - * @param alertType The alert type like Information, Error, etc. - * @param windowTitle The title of the dialog window - * @param headerText The text of the header between title and body - * @param bodyText The actual text body - * @param showSymbol Whether the symbol besides the header text should be - * displayed - * @param windowPrefWidth Preferred width of the window - * @param windowPrefHeight Preferred height of the window + * @param alertType + * The alert type like Information, Error, etc. + * @param windowTitle + * The title of the dialog window + * @param headerText + * The text of the header between title and body + * @param bodyText + * The actual text body + * @param showSymbol + * Whether the symbol besides the header text should be + * displayed + * @param windowPrefWidth + * Preferred width of the window + * @param windowPrefHeight + * Preferred height of the window */ - public static void showCustomScrollableAlert(AlertType alertType, String windowTitle, String headerText, - String bodyText, boolean showSymbol, Double windowPrefWidth, Double windowPrefHeight) { + public static void showCustomScrollableAlert(AlertType alertType, String windowTitle, + String headerText, String bodyText, boolean showSymbol, Double windowPrefWidth, + Double windowPrefHeight) { Alert alert = new Alert(alertType); - if (!showSymbol) { + if(!showSymbol) { alert.setGraphic(null); } alert.setTitle(windowTitle); @@ -311,17 +362,17 @@ public class ReuseLibraryUIUtils { DialogPane dialogPane = alert.getDialogPane(); Image taskbarIcon = getFXImage(ToolingReuseUIActivator.PLUGIN_ID, "icons/af3_icon64.png"); - Stage stage = (Stage) dialogPane.getScene().getWindow(); + Stage stage = (Stage)dialogPane.getScene().getWindow(); stage.getIcons().add(taskbarIcon); - // use setExpandableContent(area) if the scrollable text should be first hidden + // Use setExpandableContent(area) if the scrollable text should be first hidden // under "Show details". Then, alert.setContentText() can be used to show a // short message before dialogPane.setContent(area); - if (windowPrefWidth != null && windowPrefWidth > 0.0) { + if(windowPrefWidth != null && windowPrefWidth > 0.0) { dialogPane.setPrefWidth(windowPrefWidth); } - if (windowPrefHeight != null && windowPrefHeight > 0.0) { + if(windowPrefHeight != null && windowPrefHeight > 0.0) { dialogPane.setPrefHeight(windowPrefHeight); } diff --git a/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/.ratings b/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/.ratings index 710fd89bbcd11381e0e04f630ac34c5a65e4ac41..b6e17035cff501aa256f38a03b8c5ec9ba7f98f2 100644 --- a/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/.ratings +++ b/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/.ratings @@ -1,3 +1,3 @@ ReuseLibraryModelElementFactory.java 4ee3eb7449e212643992a3dec6cfb8f4278efb70 GREEN -ReuseLibraryUtilsBasics.java 56ab0cab9189efd7f2408150b471f3c6cc99f30f GREEN +ReuseLibraryUtilsBasics.java 1d60936f98bd09e1ba1715192f5263f89b4942fe YELLOW ReuseLibraryUtilsManipulation.java 77a646db5a63ba7c61664dbcaf34a9036003fde5 GREEN diff --git a/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/ReuseLibraryUtilsBasics.java b/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/ReuseLibraryUtilsBasics.java index 56ab0cab9189efd7f2408150b471f3c6cc99f30f..1d60936f98bd09e1ba1715192f5263f89b4942fe 100644 --- a/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/ReuseLibraryUtilsBasics.java +++ b/org.fortiss.tooling.ext.reuse/src/org/fortiss/tooling/ext/reuse/utils/ReuseLibraryUtilsBasics.java @@ -1,5 +1,5 @@ /*-------------------------------------------------------------------------+ -| Copyright 2022 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. | @@ -33,8 +33,10 @@ import org.fortiss.tooling.base.model.element.IModelElementSpecification; import org.fortiss.tooling.ext.reuse.model.ReuseElementSpec; import org.fortiss.tooling.ext.reuse.model.ReuseLibrary; import org.fortiss.tooling.ext.reuse.service.IReuseProviderService; +import org.fortiss.tooling.kernel.extension.data.ITopLevelElement; import org.fortiss.tooling.kernel.model.INamedCommentedElement; import org.fortiss.tooling.kernel.model.INamedElement; +import org.fortiss.tooling.kernel.service.IPersistencyService; /** * Utility methods for {@link ReuseLibrary}s and related reuse elements and @@ -76,10 +78,10 @@ public class ReuseLibraryUtilsBasics { * @return True if the given element is a reuse element, otherwise false */ public static boolean isCorrectReuseElement(EObject element) { - // first, check if it is a (possible) reuse element in general + // First, check if it is a (possible) reuse element in general. if(hasReusePossibility(element)) { - // second, check if it has a library reference - // (only then it is an actual (active) reuse element) + // Second, check if it has a library reference (only then it is an actual (active) reuse + // element). if(element instanceof IModelElement) { for(IModelElementSpecification spec : ((IModelElement)element) .getSpecifications()) { @@ -251,7 +253,7 @@ public class ReuseLibraryUtilsBasics { public static String getFirstReuseElementUUID(EObject element) { List<String> allUUIDs = getAllReuseElementUUID(element); if(allUUIDs != null && !allUUIDs.isEmpty()) { - // get(0) is safe due to check above + // get(0) is safe due to check above. return allUUIDs.get(0); } return ""; @@ -341,7 +343,7 @@ public class ReuseLibraryUtilsBasics { public static ZonedDateTime getReuseElementLastUpdate(EObject element) { List<ReuseElementSpec> specs = getAllReuseSpecs(element); if(specs != null && !specs.isEmpty()) { - // get(0) is safe due to check above + // get(0) is safe due to check above. return specs.get(0).getLastUpdate(); } return null; @@ -424,7 +426,7 @@ public class ReuseLibraryUtilsBasics { if(element instanceof IModelElement) { for(IModelElementSpecification spec : ((IModelElement)element).getSpecifications()) { if(spec instanceof ReuseElementSpec) { - // (cast is safe due to instance check) + // Cast is safe due to instance check. specs.add((ReuseElementSpec)spec); } } @@ -443,7 +445,7 @@ public class ReuseLibraryUtilsBasics { public static ReuseElementSpec getFirstReuseSpec(EObject element) { List<ReuseElementSpec> specs = getAllReuseSpecs(element); if(!specs.isEmpty()) { - // get(0) safe due to empty check above + // get(0) safe due to empty check above. return specs.get(0); } return null; @@ -476,13 +478,13 @@ public class ReuseLibraryUtilsBasics { IModelElement projectElement, IModelElement libraryElement) { List<ReuseElementSpec> projectSpecs = getAllReuseSpecs(projectElement); List<ReuseElementSpec> librarySpecs = getAllReuseSpecs(libraryElement); - // elements in library must have only a single specification + // Elements in library must have only a single specification. if(librarySpecs.size() == 1) { - // get(0) is safe due to size check above + // get(0) is safe due to size check above. ReuseElementSpec librarySpec = librarySpecs.get(0); for(ReuseElementSpec specToBeChecked : projectSpecs) { - // both UUIDs (of element and library) must be identical + // Both UUIDs (of element and library) must be identical. if(specToBeChecked.getSourceLibUUID().equals(librarySpec.getSourceLibUUID()) && specToBeChecked.getElementUUID().equals(librarySpec.getElementUUID())) { return specToBeChecked; @@ -499,8 +501,8 @@ public class ReuseLibraryUtilsBasics { * @return A list of all local reuse libraries (or empty list) */ public static List<ReuseLibrary> getAllLocalReuseLibraries() { - // currently, this is just all existing local libraries which are stored - // separately in the library directory + // Currently, this is just all existing local libraries which are stored + // separately in the library directory. return getAllSeparatelyStoredReuseLibraries(); } @@ -579,7 +581,7 @@ public class ReuseLibraryUtilsBasics { * @return The requested reuse element (or null) */ public static EObject getElementInsideLibrary(ReuseLibrary library, String elementUUID) { - // find original element inside the library by identical UUID + // Find original element inside the library by identical UUID. for(EObject libraryElement : library.getReuseElementList()) { for(IModelElementSpecification spec : ((IModelElement)libraryElement) .getSpecifications()) { @@ -638,7 +640,7 @@ public class ReuseLibraryUtilsBasics { String storedHashForThisLibrary = getReuseElementHashForLibrary(element, sourceLibrary); String currentHashForThisLibrary = getReuseElementHashForLibrary(originElementInLib, sourceLibrary); - // if hashes are valid but different, the origin was updated + // If hashes are valid but different, the origin was updated. if(!storedHashForThisLibrary.isEmpty() && !currentHashForThisLibrary.isEmpty()) { if(!storedHashForThisLibrary.equals(currentHashForThisLibrary)) { updatedOrigins.add(originElementInLib); @@ -647,4 +649,20 @@ public class ReuseLibraryUtilsBasics { } return updatedOrigins; } + + /** + * Returns whether the given {@link EObject} exists and has also a valid context (top level + * element). + * + * @param element + * The targeted element. + * @return True if the element exists within a context, otherwise False. + */ + public static boolean doesReuseElementExistWithContext(EObject element) { + IPersistencyService ps = IPersistencyService.getInstance(); + ITopLevelElement context = ps.getTopLevelElementFor(element); + // Context is either null because element is null or because the context is actually + // missing. Therefore, checking for element == null is already included in context == null. + return context != null; + } }