]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/dnd/DropSuggestions.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / dnd / DropSuggestions.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.dnd;
13
14 import java.util.List;
15 import java.util.stream.Collectors;
16
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;
32
33 /**
34  * @author Tuukka Lehtonen
35  * @since 1.37.0
36  */
37 public class DropSuggestions {
38
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),
45                 SWT.ICON_QUESTION,
46                 SWT.NONE,
47                 SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
48     }
49
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$
54     }
55
56     public static Write performSuggestionsRequest(List<DropSuggestion> suggestions) {
57         return (Write) graph -> {
58             for (DropSuggestion s : suggestions) {
59                 s.fix(graph);
60             }
61         };
62     }
63
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);
70     }
71
72     public static class LinkToLibrary implements DropSuggestion {
73
74         private final String desc;
75         private final Resource linkFrom;
76         private final Resource linkTo;
77
78         public LinkToLibrary(String desc, Resource linkFrom, Resource linkTo) {
79             this.desc = desc;
80             this.linkFrom = linkFrom;
81             this.linkTo = linkTo;
82         }
83
84         @Override
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)));
92             }
93             Layer0 L0 = Layer0.getInstance(graph);
94             graph.claim(linkFrom, L0.IsLinkedTo, L0.IsLinkedTo_Inverse, linkTo);
95         }
96
97         @Override
98         public String toString() {
99             return desc;
100         }
101
102     }
103
104 }