1 package org.simantics.document.linking.actions;
3 import java.util.Collection;
4 import java.util.Collections;
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;
21 public class UpdateReferencesAction implements ActionFactory {
26 public Runnable create(Object target) {
27 if(!(target instanceof Resource))
29 final Resource document = (Resource)target;
30 return new Runnable() {
35 Collection<Resource> coll = Simantics.getSession().syncRequest(new Read<Collection<Resource>>() {
37 public Collection<Resource> perform(ReadGraph graph)
38 throws DatabaseException {
39 return findDocumentReferences(graph, document);
46 String dialogTitle = "Fix References";
47 String dialogMessage = "Fix " + coll.size() + " old references?";
48 String dialogButtonLabels[] = new String[]{"Ok","Cancel"};
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);
57 MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex);
58 if (dialog.open() != 0)
60 Simantics.getSession().markUndoPoint();
61 fixDocumentReferences(coll);
62 } catch (DatabaseException e) {
63 ExceptionUtils.logAndShowError("Cannot update references",e);
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))
75 Resource model = SourceLinkUtil.getModel(graph, document);
78 return SourceLinkUtil.findAllSources(graph, model, document);
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())));