X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2Fdnd%2FDropSuggestions.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2Fdnd%2FDropSuggestions.java;h=0ff4415415ad8c7e03dacafa4e38b26dc581001b;hb=252076ed80eef446cc62c5b193d34b7bea3cdc80;hp=0000000000000000000000000000000000000000;hpb=1f09c7df2e394be485a82de0acef444ae68a4c9b;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/dnd/DropSuggestions.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/dnd/DropSuggestions.java new file mode 100644 index 000000000..0ff441541 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/dnd/DropSuggestions.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2018 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.diagramEditor.dnd; + +import java.util.List; +import java.util.stream.Collectors; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Shell; +import org.simantics.NameLabelMode; +import org.simantics.NameLabelUtil; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ValidationException; +import org.simantics.db.layer0.request.IsLinkedTo; +import org.simantics.db.request.Write; +import org.simantics.layer0.Layer0; + +/** + * @author Tuukka Lehtonen + * @since 1.37.0 + */ +public class DropSuggestions { + + public static boolean askSuggestions(Shell parent, List suggestions) { + return Dialog.OK == MarkupDialog.open(parent, + "diagram.dropSuggestionDialog", //$NON-NLS-1$ + Messages.DropSuggestions_ApplySuggestions_DialogTitle, + Messages.DropSuggestions_ApplySuggestions_DialogMsg, + formatSuggestions(suggestions), + SWT.ICON_QUESTION, + SWT.NONE, + SWT.BORDER | SWT.WRAP | SWT.V_SCROLL); + } + + private static String formatSuggestions(List suggestions) { + return suggestions.stream() + .map(DropSuggestion::toString) + .collect(Collectors.joining("\n", "- ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + + public static Write performSuggestionsRequest(List suggestions) { + return (Write) graph -> { + for (DropSuggestion s : suggestions) { + s.fix(graph); + } + }; + } + + public static DropSuggestion linkToLibrary(ReadGraph graph, Resource linkSource, Resource linkTarget) throws DatabaseException { + return new LinkToLibrary( + NLS.bind(Messages.DropSuggestions_Description_LinkToLibrary, + NameLabelUtil.modalName(graph, linkSource, NameLabelMode.NAME_AND_LABEL), + NameLabelUtil.modalName(graph, linkTarget, NameLabelMode.NAME_AND_LABEL)) + , linkSource, linkTarget); + } + + public static class LinkToLibrary implements DropSuggestion { + + private final String desc; + private final Resource linkFrom; + private final Resource linkTo; + + public LinkToLibrary(String desc, Resource linkFrom, Resource linkTo) { + this.desc = desc; + this.linkFrom = linkFrom; + this.linkTo = linkTo; + } + + @Override + public void fix(WriteGraph graph) throws DatabaseException { + // Ensure that there is no reverse link between the namespaces + if (graph.syncRequest(new IsLinkedTo(linkFrom, linkTo))) { + throw new ValidationException( + NLS.bind(Messages.DropSuggestions_Problem_CyclicDependency + , NameUtils.getURIOrSafeNameInternal(graph, linkTo) + , NameUtils.getURIOrSafeNameInternal(graph, linkFrom))); + } + Layer0 L0 = Layer0.getInstance(graph); + graph.claim(linkFrom, L0.IsLinkedTo, L0.IsLinkedTo_Inverse, linkTo); + } + + @Override + public String toString() { + return desc; + } + + } + +}