1 package org.simantics.modeling.ui.handlers.e4;
\r
3 import javax.inject.Named;
\r
5 import org.eclipse.e4.core.di.annotations.CanExecute;
\r
6 import org.eclipse.e4.core.di.annotations.Execute;
\r
7 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
\r
8 import org.eclipse.e4.ui.services.IServiceConstants;
\r
9 import org.eclipse.jface.dialogs.MessageDialog;
\r
10 import org.eclipse.swt.widgets.Shell;
\r
11 import org.eclipse.ui.IEditorPart;
\r
12 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
\r
13 import org.simantics.Simantics;
\r
14 import org.simantics.db.Resource;
\r
15 import org.simantics.db.Session;
\r
16 import org.simantics.db.exception.DatabaseException;
\r
17 import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;
\r
18 import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;
\r
19 import org.simantics.modeling.ui.property.TypicalPropertyTester;
\r
20 import org.simantics.modeling.ui.typicals.RuleChooserDialog;
\r
21 import org.simantics.ui.workbench.IResourceEditorInput;
\r
22 import org.simantics.utils.ui.ErrorLogger;
\r
23 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
26 * @author Tuukka Lehtonen
\r
29 public class SyncCurrentTypicalInstanceWithTemplate {
\r
32 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) throws DatabaseException, InterruptedException {
\r
33 // TODO: Fix this when we get rid of CompatibilityEditors
\r
34 IEditorPart activeEditor = null;
\r
35 if (part != null && part.getObject() instanceof CompatibilityEditor) {
\r
36 CompatibilityEditor compEditor = (CompatibilityEditor) part.getObject();
\r
37 activeEditor = compEditor.getEditor();
\r
39 // TODO: This is not good practice with E4 but an OK fallback for now
\r
40 activeEditor = WorkbenchUtils.getActiveEditor();
\r
42 if (activeEditor == null)
\r
44 IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
\r
45 return TypicalPropertyTester.isTypicalInstanceEditor(Simantics.getSession(), input.getResource());
\r
49 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {
\r
51 // TODO: Fix this when we get rid of CompatibilityEditors
\r
52 IEditorPart activeEditor = null;
\r
53 Object editor = mActiveEditor.getObject();
\r
54 if (editor instanceof CompatibilityEditor) {
\r
55 CompatibilityEditor compEditor = (CompatibilityEditor) editor;
\r
56 activeEditor = compEditor.getEditor();
\r
58 // TODO: This is not good practice with E4 but an OK fallback for now
\r
59 activeEditor = WorkbenchUtils.getActiveEditor();
\r
61 if (activeEditor == null)
\r
64 IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
\r
65 Session session = Simantics.getSession();
\r
67 // if (!MessageDialog
\r
68 // .openConfirm(shell,
\r
69 // "Synchronize Typical Instance With Template",
\r
70 // "Are you sure you want to synchronize this typical instance with its template?"))
\r
74 if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) {
\r
75 MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor.");
\r
79 RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() });
\r
80 if(result == null) return;
\r
82 SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging);
\r
83 session.syncRequest(req);
\r
84 if (result.logging) {
\r
85 for(Resource log : req.logs)
\r
86 new OpenPlainTextDocumentAdapter().openEditor(log);
\r
88 } catch (Exception e) {
\r
89 ErrorLogger.defaultLogError(e);
\r