]> gerrit.simantics Code Review - simantics/platform.git/blob
a05d11241297456a56eeab4613174ac3eb3aa74e
[simantics/platform.git] /
1 package org.simantics.document.linking.actions;
2
3 import java.util.Collection;
4 import java.util.Collections;
5
6 import org.eclipse.jface.dialogs.MessageDialog;
7 import org.eclipse.swt.widgets.Display;
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.ActionFactory;
13 import org.simantics.db.request.Read;
14 import org.simantics.document.DocumentResource;
15 import org.simantics.document.linking.ge.FixAllReferencesAction;
16 import org.simantics.document.linking.utils.SourceLinkUtil;
17 import org.simantics.utils.datastructures.Pair;
18 import org.simantics.utils.ui.ExceptionUtils;
19
20
21 public class UpdateReferencesAction implements ActionFactory {
22         
23         
24         
25         @Override
26         public Runnable create(Object target) {
27                 if(!(target instanceof Resource))
28                         return null;
29                 final Resource document = (Resource)target;
30                 return new Runnable() {
31                         
32                         @Override
33                         public void run() {
34                                 try {
35                                         Collection<Resource> coll = Simantics.getSession().syncRequest(new Read<Collection<Resource>>() {
36                                                 @Override
37                                                 public Collection<Resource> perform(ReadGraph graph)
38                                                                 throws DatabaseException {
39                                                     return findDocumentReferences(graph, document);
40                                                 }
41                                         });
42                                         
43                                         if (coll == null)
44                                                 return;
45                                         
46                                         String dialogTitle = "Fix References";
47                                         String dialogMessage = "Fix " + coll.size() + " old references?";
48                                         String dialogButtonLabels[] = new String[]{"Ok","Cancel"};
49                                         int defaultIndex = 0;
50                                         if (coll.size() == 0) {
51                                                 dialogMessage = "Nothing to fix.";
52                                                 dialogButtonLabels = new String[]{"OK"};
53                                                 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
54                                                 dialog.open();
55                                                 return;
56                                         }
57                                         MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
58                                         if (dialog.open() != 0)
59                                                 return;
60                                         Simantics.getSession().markUndoPoint();
61                                         fixDocumentReferences(coll);
62                                 } catch (DatabaseException e) {
63                                         ExceptionUtils.logAndShowError("Cannot update references",e);
64                                 }
65                                 
66                                 
67                         }
68                 };
69         }
70         
71         public static Collection<Resource> findDocumentReferences(ReadGraph graph, Resource document) throws DatabaseException {
72         DocumentResource doc = DocumentResource.getInstance(graph);
73         if (!graph.hasStatement(document, doc.HasNewerVersion))
74             return null;
75         Resource model = SourceLinkUtil.getModel(graph, document);
76         if (model == null)
77             return null;
78         return SourceLinkUtil.findAllSources(graph, model, document);
79         }
80         
81         public static void fixDocumentReferences(Collection<Resource> resources) throws DatabaseException {
82         Simantics.getSession().syncRequest(new FixAllReferencesAction.FixAll(new Pair<Collection<Resource>, Collection<Resource>>(resources, Collections.<Resource> emptyList())));
83         }
84
85 }