1 package org.simantics.document.linking.ge;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import java.util.Stack;
8 import org.eclipse.jface.dialogs.IDialogConstants;
9 import org.eclipse.jface.dialogs.MessageDialog;
10 import org.eclipse.osgi.util.NLS;
11 import org.eclipse.swt.widgets.Display;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.WriteRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.ActionFactory;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.request.Read;
21 import org.simantics.document.linking.ontology.DocumentLink;
22 import org.simantics.document.linking.utils.SourceLinkUtil;
23 import org.simantics.utils.datastructures.Pair;
24 import org.simantics.utils.ui.ExceptionUtils;
26 public class FixAllReferencesAction implements ActionFactory {
29 public Runnable create(final Object target) {
30 return new Runnable() {
34 System.out.println(target);
36 Pair<Collection<Resource>, Collection<Resource>> refs = Simantics.getSession()
37 .syncRequest(new FindFixable(target));
38 String dialogTitle = Messages.FixAllReferencesAction_FixReferences;
39 String dialogMessage = NLS.bind(Messages.FixAllReferencesAction_FixOldReferences,
40 new Object[] { refs.first.size(), refs.second.size() });
41 String dialogButtonLabels[] = new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL };
43 if (refs.first.size() == 0 && refs.second.size() == 0) {
44 dialogMessage = Messages.FixAllReferencesAction_NothingToFix;
45 dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL };
46 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle,
47 null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
51 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null,
52 dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
53 if (dialog.open() != 0)
55 Simantics.getSession().markUndoPoint();
56 Simantics.getSession().syncRequest(new FixAll(refs));
57 } catch (DatabaseException e) {
58 ExceptionUtils.logAndShowError(Messages.FixAllReferencesAction_CannotFixReferences, e);
64 public static class FindFixable implements Read<Pair<Collection<Resource>, Collection<Resource>>> {
67 List<Resource> removed;
70 public FindFixable(Object target) {
75 public Pair<Collection<Resource>, Collection<Resource>> perform(ReadGraph graph) throws DatabaseException {
76 old = new ArrayList<Resource>();
77 removed = new ArrayList<Resource>();
79 DocumentLink sl = DocumentLink.getInstance(graph);
80 ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
81 Stack<Object> stack = new Stack<Object>();
83 while (!stack.isEmpty()) {
84 Object o = stack.pop();
85 @SuppressWarnings("unchecked")
86 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
88 if (children.size() == 0) {
89 if (o instanceof Variable) {
90 Variable v = (Variable)o;
91 Resource r = v.getPossibleRepresents(graph);
93 } else if (o instanceof Resource) {
94 Resource r = (Resource)o;
98 stack.addAll(children);
103 Pair<Collection<Resource>, Collection<Resource>> result = new Pair<Collection<Resource>, Collection<Resource>>(old, removed);
109 void process(ReadGraph graph, Resource r) throws DatabaseException{
110 if (SourceLinkUtil.isValidSource(graph, r)) {
111 if (!SourceLinkUtil.isUpToDateSource(graph, r)) {
112 if (!old.contains(r))
115 } else if (SourceLinkUtil.isSource(graph, r)){
116 if (!removed.contains(r))
122 public static class FixAll extends WriteRequest {
123 Pair<Collection<Resource>, Collection<Resource>> refs;
125 public FixAll(Pair<Collection<Resource>, Collection<Resource>> refs) {
131 public void perform(WriteGraph graph) throws DatabaseException {
132 Collection<Resource> old = refs.first;;
133 Collection<Resource> removed = refs.second;
135 for (Resource r : old)
136 SourceLinkUtil.updateToLatest(graph, r);
137 for (Resource r : removed)
143 public static class FixAllSilent extends WriteRequest {
146 public FixAllSilent(Resource target) {
147 this.target = target;
151 public void perform(WriteGraph graph) throws DatabaseException {
152 // TODO: using ModelChildRule makes the data processing very slow.
153 DocumentLink sl = DocumentLink.getInstance(graph);
154 ModelChildRule modelChildRule = new ModelChildRule(graph, sl.ModelViewpointBrowseContext2_ChildRule);
155 Stack<Object> stack = new Stack<Object>();
157 while (!stack.isEmpty()) {
158 Object o = stack.pop();
159 @SuppressWarnings("unchecked")
160 Collection<Object> children = (Collection<Object>) modelChildRule.getChildren(graph, o);
162 if (children.size() == 0) {
163 if (o instanceof Variable) {
164 Variable v = (Variable)o;
165 Resource r = v.getPossibleRepresents(graph);
167 } else if (o instanceof Resource) {
168 Resource r = (Resource)o;
172 stack.addAll(children);
179 void process(WriteGraph graph, Resource r) throws DatabaseException{
180 if (SourceLinkUtil.isValidSource(graph, r))
181 SourceLinkUtil.updateToLatest(graph, r);