X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Factions%2FUpdateReferencesAction.java;h=a3cb78734e704b016edcdd84aab5aaa9808f6081;hp=e59d6aa1a20b71c13f883e3e60fefa4abc1b067d;hb=8783f9ee2b67f83160d88f43a7aef02a6b25f955;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/actions/UpdateReferencesAction.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/actions/UpdateReferencesAction.java index e59d6aa1a..a3cb78734 100644 --- a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/actions/UpdateReferencesAction.java +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/actions/UpdateReferencesAction.java @@ -1,85 +1,89 @@ -package org.simantics.document.linking.actions; - -import java.util.Collection; -import java.util.Collections; - -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.swt.widgets.Display; -import org.simantics.Simantics; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.ActionFactory; -import org.simantics.db.request.Read; -import org.simantics.document.DocumentResource; -import org.simantics.document.linking.ge.FixAllReferencesAction; -import org.simantics.document.linking.utils.SourceLinkUtil; -import org.simantics.utils.datastructures.Pair; -import org.simantics.utils.ui.ExceptionUtils; - - -public class UpdateReferencesAction implements ActionFactory { - - - - @Override - public Runnable create(Object target) { - if(!(target instanceof Resource)) - return null; - final Resource document = (Resource)target; - return new Runnable() { - - @Override - public void run() { - try { - Collection coll = Simantics.getSession().syncRequest(new Read>() { - @Override - public Collection perform(ReadGraph graph) - throws DatabaseException { - return findDocumentReferences(graph, document); - } - }); - - if (coll == null) - return; - - String dialogTitle = "Fix References"; - String dialogMessage = "Fix " + coll.size() + " old references?"; - String dialogButtonLabels[] = new String[]{"Ok","Cancel"}; - int defaultIndex = 0; - if (coll.size() == 0) { - dialogMessage = "Nothing to fix."; - dialogButtonLabels = new String[]{"OK"}; - MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex); - dialog.open(); - return; - } - MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex); - if (dialog.open() != 0) - return; - Simantics.getSession().markUndoPoint(); - fixDocumentReferences(coll); - } catch (DatabaseException e) { - ExceptionUtils.logAndShowError("Cannot update references",e); - } - - - } - }; - } - - public static Collection findDocumentReferences(ReadGraph graph, Resource document) throws DatabaseException { - DocumentResource doc = DocumentResource.getInstance(graph); - if (!graph.hasStatement(document, doc.HasNewerVersion)) - return null; - Resource model = SourceLinkUtil.getModel(graph, document); - if (model == null) - return null; - return SourceLinkUtil.findAllSources(graph, model, document); - } - - public static void fixDocumentReferences(Collection resources) throws DatabaseException { - Simantics.getSession().syncRequest(new FixAllReferencesAction.FixAll(new Pair, Collection>(resources, Collections. emptyList()))); - } - -} +package org.simantics.document.linking.actions; + +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.widgets.Display; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.db.request.Read; +import org.simantics.document.DocumentResource; +import org.simantics.document.linking.ge.FixAllReferencesAction; +import org.simantics.document.linking.utils.SourceLinkUtil; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.ExceptionUtils; + + +public class UpdateReferencesAction implements ActionFactory { + + + + @Override + public Runnable create(Object target) { + if(!(target instanceof Resource)) + return null; + final Resource document = (Resource)target; + return new Runnable() { + + @Override + public void run() { + try { + Collection coll = Simantics.getSession().syncRequest(new Read>() { + @Override + public Collection perform(ReadGraph graph) + throws DatabaseException { + return findDocumentReferences(graph, document); + } + }); + + if (coll == null) + return; + + String dialogTitle = Messages.UpdateReferencesAction_FixReferences; + String dialogMessage = NLS.bind(Messages.UpdateReferencesAction_FixOldReferences, coll.size()); + String dialogButtonLabels[] = new String[] { IDialogConstants.OK_LABEL, + IDialogConstants.CANCEL_LABEL }; + int defaultIndex = 0; + if (coll.size() == 0) { + dialogMessage = Messages.UpdateReferencesAction_NothingToFix; + dialogButtonLabels = new String[] { IDialogConstants.OK_LABEL }; + MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, + null, dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex); + dialog.open(); + return; + } + MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), dialogTitle, null, + dialogMessage, MessageDialog.CONFIRM, dialogButtonLabels, defaultIndex); + if (dialog.open() != 0) + return; + Simantics.getSession().markUndoPoint(); + fixDocumentReferences(coll); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(Messages.UpdateReferencesAction_CannotUpdateReferences, e); + } + + } + }; + } + + public static Collection findDocumentReferences(ReadGraph graph, Resource document) throws DatabaseException { + DocumentResource doc = DocumentResource.getInstance(graph); + if (!graph.hasStatement(document, doc.HasNewerVersion)) + return null; + Resource model = SourceLinkUtil.getModel(graph, document); + if (model == null) + return null; + return SourceLinkUtil.findAllSources(graph, model, document); + } + + public static void fixDocumentReferences(Collection resources) throws DatabaseException { + Simantics.getSession().syncRequest(new FixAllReferencesAction.FixAll(new Pair, Collection>(resources, Collections. emptyList()))); + } + +}