1 /*******************************************************************************
2 * Copyright (c) 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.dnd;
14 import java.util.List;
15 import java.util.stream.Collectors;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Shell;
21 import org.simantics.NameLabelMode;
22 import org.simantics.NameLabelUtil;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.utils.NameUtils;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.exception.ValidationException;
29 import org.simantics.db.layer0.request.IsLinkedTo;
30 import org.simantics.db.request.Write;
31 import org.simantics.layer0.Layer0;
34 * @author Tuukka Lehtonen
37 public class DropSuggestions {
39 public static boolean askSuggestions(Shell parent, List<DropSuggestion> suggestions) {
40 return Dialog.OK == MarkupDialog.open(parent,
41 "diagram.dropSuggestionDialog", //$NON-NLS-1$
42 Messages.DropSuggestions_ApplySuggestions_DialogTitle,
43 Messages.DropSuggestions_ApplySuggestions_DialogMsg,
44 formatSuggestions(suggestions),
47 SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
50 private static String formatSuggestions(List<DropSuggestion> suggestions) {
51 return suggestions.stream()
52 .map(DropSuggestion::toString)
53 .collect(Collectors.joining("\n", "- ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
56 public static Write performSuggestionsRequest(List<DropSuggestion> suggestions) {
57 return (Write) graph -> {
58 for (DropSuggestion s : suggestions) {
64 public static DropSuggestion linkToLibrary(ReadGraph graph, Resource linkSource, Resource linkTarget) throws DatabaseException {
65 return new LinkToLibrary(
66 NLS.bind(Messages.DropSuggestions_Description_LinkToLibrary,
67 NameLabelUtil.modalName(graph, linkSource, NameLabelMode.NAME_AND_LABEL),
68 NameLabelUtil.modalName(graph, linkTarget, NameLabelMode.NAME_AND_LABEL))
69 , linkSource, linkTarget);
72 public static class LinkToLibrary implements DropSuggestion {
74 private final String desc;
75 private final Resource linkFrom;
76 private final Resource linkTo;
78 public LinkToLibrary(String desc, Resource linkFrom, Resource linkTo) {
80 this.linkFrom = linkFrom;
85 public void fix(WriteGraph graph) throws DatabaseException {
86 // Ensure that there is no reverse link between the namespaces
87 if (graph.syncRequest(new IsLinkedTo(linkFrom, linkTo))) {
88 throw new ValidationException(
89 NLS.bind(Messages.DropSuggestions_Problem_CyclicDependency
90 , NameUtils.getURIOrSafeNameInternal(graph, linkTo)
91 , NameUtils.getURIOrSafeNameInternal(graph, linkFrom)));
93 Layer0 L0 = Layer0.getInstance(graph);
94 graph.claim(linkFrom, L0.IsLinkedTo, L0.IsLinkedTo_Inverse, linkTo);
98 public String toString() {