Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuergen Haug2019-10-19 19:12:50 -0400
committerJuergen Haug2019-10-22 04:40:39 -0400
commitd97ffc5ca227d18b7ec08d7548362894233f296d (patch)
tree92754a6c5d2220b5473e95f24a73ebb6d9c803a5
parent0327d439a3964c16ef650d251c8599d2830ab452 (diff)
downloadorg.eclipse.etrice-d97ffc5ca227d18b7ec08d7548362894233f296d.tar.gz
org.eclipse.etrice-d97ffc5ca227d18b7ec08d7548362894233f296d.tar.xz
org.eclipse.etrice-d97ffc5ca227d18b7ec08d7548362894233f296d.zip
Bug 550882 - [diagram] diagram close with "don't save" does not work
* fixed cancel * refactoring Change-Id: I602a3d3fb30a58ee338e7bb59ac06943b06003a5
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/ISaveOnFocusLostEditor.java12
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveDialogEditor.java56
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostListener.java88
-rw-r--r--plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostXtextEditor.java51
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java72
-rw-r--r--plugins/org.eclipse.etrice.ui.common.base/plugin.xml3
-rw-r--r--plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java18
-rw-r--r--plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java28
-rw-r--r--plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/SaveOnFocusLostListener.java87
9 files changed, 176 insertions, 239 deletions
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/ISaveOnFocusLostEditor.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/ISaveOnFocusLostEditor.java
index 79ee499ff..e97370f1f 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/ISaveOnFocusLostEditor.java
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/ISaveOnFocusLostEditor.java
@@ -16,14 +16,12 @@ package org.eclipse.etrice.core.common.ui.editor;
import org.eclipse.ui.ISaveablePart2;
/**
- * @author Henrik Rentz-Reichert
- *
+ * Editor that can be saved automatically.
+ *
+ * @see {@link SaveOnFocusLostListener}
*/
public interface ISaveOnFocusLostEditor extends ISaveablePart2 {
- /**
- * if this editor is currently being closed (check in {@link #promptToSaveOnClose()}) this should return true
- * @return
- */
- boolean isClosing();
+ boolean shouldSaveOnFocusLost();
+
}
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveDialogEditor.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveDialogEditor.java
new file mode 100644
index 000000000..5ab5dcbbd
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveDialogEditor.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * CONTRIBUTORS:
+ * Juergen Haug (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.common.ui.editor;
+
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.ISaveablePart2;
+
+/**
+ * Replacement for save dialog of platform. Provides access to user choice.
+ */
+public class SaveDialogEditor {
+
+ private int lastResult = ISaveablePart2.CANCEL;
+
+ public int open(Shell shell, String name) {
+ // dialog mimic from SaveablesList in platform
+ String[] buttons = new String[] { "&Save", "Do&n't Save", "Cancel" };
+ String message = "Save changes in '"+ name + "'?";
+ MessageDialog dialog = new MessageDialog(shell, "Save Resource", null, message, MessageDialog.QUESTION, 0, buttons) {
+ @Override
+ protected int getShellStyle() {
+ return SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL | SWT.SHEET | getDefaultOrientation();
+ }
+ };
+
+ lastResult = dialog.open();
+ if(lastResult != ISaveablePart2.YES && lastResult != ISaveablePart2.NO) {
+ // treat all other options as cancel
+ lastResult = ISaveablePart2.CANCEL;
+ }
+
+ return lastResult;
+ }
+
+ public int getResult() {
+ return lastResult;
+ }
+
+ public void reset() {
+ lastResult = ISaveablePart2.CANCEL;
+ }
+}
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostListener.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostListener.java
deleted file mode 100644
index 26fba1d5d..000000000
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostListener.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License 2.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * CONTRIBUTORS:
- * Henrik Rentz-Reichert (initial contribution)
- *
- *******************************************************************************/
-
-package org.eclipse.etrice.core.common.ui.editor;
-
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.ui.IPartListener;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.xtext.ui.editor.XtextEditor;
-
-/**
- * @author Henrik Rentz-Reichert
- *
- */
-public class SaveOnFocusLostListener implements IPartListener {
-
- private XtextEditor editor;
- private String store;
- private String key;
-
- public SaveOnFocusLostListener(XtextEditor editor, String store, String key) {
- this.editor = editor;
- this.store = store;
- this.key = key;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
- */
- @Override
- public void partActivated(IWorkbenchPart part) {
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
- */
- @Override
- public void partBroughtToTop(IWorkbenchPart part) {
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
- */
- @Override
- public void partClosed(IWorkbenchPart part) {
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
- */
- @Override
- public void partDeactivated(IWorkbenchPart part) {
- if (!(editor instanceof IValidatingEditor))
- return;
-
- if (part instanceof ISaveOnFocusLostEditor) {
- if (((ISaveOnFocusLostEditor) part).isClosing()) {
- return;
- }
- }
-
- boolean save = Platform.getPreferencesService().getBoolean(store, key, true, null);
-
- if (save && editor.isDirty())
- if (((IValidatingEditor)editor).isValid())
- editor.doSave(new NullProgressMonitor());
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
- */
- @Override
- public void partOpened(IWorkbenchPart part) {
- }
-
-}
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostXtextEditor.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostXtextEditor.java
index cad121a18..bcfad177e 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostXtextEditor.java
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/editor/SaveOnFocusLostXtextEditor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2019 protos software gmbh (http://www.protos.de).
+ * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -11,26 +11,53 @@
* Henrik Rentz-Reichert (initial contribution)
*
*******************************************************************************/
+
package org.eclipse.etrice.core.common.ui.editor;
-import org.eclipse.xtext.ui.editor.XtextEditor;
+import static org.eclipse.etrice.core.common.validation.IssueUtils.isBlocking;
-/**
- * @author Henrik Rentz-Reichert
- *
- */
-public class SaveOnFocusLostXtextEditor extends XtextEditor implements ISaveOnFocusLostEditor {
+import java.util.List;
+
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.ui.editor.XtextEditor;
+import org.eclipse.xtext.util.CancelIndicator;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+import org.eclipse.xtext.validation.CheckMode;
+import org.eclipse.xtext.validation.IResourceValidator;
+import org.eclipse.xtext.validation.Issue;
- private boolean isClosing = false;
+import com.google.inject.Inject;
+public abstract class SaveOnFocusLostXtextEditor extends XtextEditor implements ISaveOnFocusLostEditor, IValidatingEditor {
+
+ @Inject
+ protected IResourceValidator resourceValidator;
+
+ protected SaveDialogEditor saveDialog = new SaveDialogEditor();
+
+ @Override
+ public boolean isValid() {
+ return getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {
+ @Override
+ public Boolean exec(XtextResource resource) throws Exception {
+ return isValid(resource);
+ }
+ });
+ }
+
+ protected boolean isValid(XtextResource resource) {
+ List<Issue> result = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, CancelIndicator.NullImpl);
+
+ return !result.stream().anyMatch((issue) -> issue.isSyntaxError() || isBlocking(issue));
+ }
+
@Override
public int promptToSaveOnClose() {
- isClosing = true;
- return DEFAULT;
+ return saveDialog.open(getShell(), getPartName());
}
@Override
- public boolean isClosing() {
- return isClosing;
+ public boolean shouldSaveOnFocusLost() {
+ return saveDialog.getResult() != NO && isDirty() && isValid();
}
}
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java
index 6f8156376..4fe0cff13 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java
@@ -12,83 +12,14 @@
package org.eclipse.etrice.core.ui.editor;
-import java.util.List;
-
-import org.eclipse.etrice.core.common.ui.editor.IValidatingEditor;
-import org.eclipse.etrice.core.common.ui.editor.SaveOnFocusLostListener;
import org.eclipse.etrice.core.common.ui.editor.SaveOnFocusLostXtextEditor;
-import org.eclipse.etrice.core.common.validation.IssueUtils;
-import org.eclipse.etrice.core.ui.preferences.RoomPreferenceConstants;
import org.eclipse.help.IContextProvider;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.util.CancelIndicator;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-import org.eclipse.xtext.validation.CheckMode;
-import org.eclipse.xtext.validation.IResourceValidator;
-import org.eclipse.xtext.validation.Issue;
-
-import com.google.inject.Inject;
/**
* @author Henrik Rentz-Reichert
*
*/
-public class RoomEditor extends SaveOnFocusLostXtextEditor implements IValidatingEditor {
-
- @Inject
- protected IResourceValidator resourceValidator;
-
- private SaveOnFocusLostListener partListener;
-
- /* (non-Javadoc)
- * @see org.eclipse.xtext.ui.editor.XtextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
- */
- @Override
- public void createPartControl(Composite parent) {
- super.createPartControl(parent);
-
- partListener = new SaveOnFocusLostListener(this, "org.eclipse.etrice.ui.common.base", RoomPreferenceConstants.SAVE_TEXT_ON_FOCUS_LOST);
- getSite().getPage().addPartListener(partListener);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.xtext.ui.editor.XtextEditor#dispose()
- */
- @Override
- public void dispose() {
- getSite().getPage().removePartListener(partListener);
-
- super.dispose();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.etrice.core.ui.editor.IValidatingEditor#isValid()
- */
- @Override
- public boolean isValid() {
- return getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {
- @Override
- public Boolean exec(XtextResource resource) throws Exception {
- List<Issue> result = resourceValidator.validate(resource, CheckMode.NORMAL_AND_FAST, new CancelIndicator() {
- public boolean isCanceled() {
- return false;
- }
- });
- if (!result.isEmpty()) {
- for (Issue issue : result) {
- if (issue.isSyntaxError()) {
- return false;
- }
- if (IssueUtils.isBlocking(issue)) {
- return false;
- }
- }
- }
- return true;
- }
- });
- }
+public class RoomEditor extends SaveOnFocusLostXtextEditor {
@Override
public <T> T getAdapter(Class<T> adapter) {
@@ -98,4 +29,5 @@ public class RoomEditor extends SaveOnFocusLostXtextEditor implements IValidatin
return super.getAdapter(adapter);
}
+
}
diff --git a/plugins/org.eclipse.etrice.ui.common.base/plugin.xml b/plugins/org.eclipse.etrice.ui.common.base/plugin.xml
index 2531c1c06..5b1071cd9 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/plugin.xml
+++ b/plugins/org.eclipse.etrice.ui.common.base/plugin.xml
@@ -7,6 +7,9 @@
<startup
class="org.eclipse.etrice.ui.common.base.concurrency.ConcurrentModificationObserver$Initializer">
</startup>
+ <startup
+ class="org.eclipse.etrice.ui.common.base.editor.SaveOnFocusLostListener">
+ </startup>
</extension>
<extension
point="org.eclipse.ltk.core.refactoring.renameParticipants">
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java
index f1d57252a..c9dae7be0 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/CustomPersistencyBehavior.java
@@ -24,18 +24,15 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.Diagnostic;
-import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.etrice.ui.common.base.UIBaseActivator;
-import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.ui.editor.DefaultPersistencyBehavior;
import org.eclipse.graphiti.ui.editor.DiagramBehavior;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IEditorPart;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.resource.SaveOptions;
import org.eclipse.xtext.resource.XtextResource;
@@ -49,20 +46,12 @@ import com.google.common.collect.Lists;
import com.google.inject.Inject;
public class CustomPersistencyBehavior extends DefaultPersistencyBehavior {
-
- private SaveOnFocusLostListener saveOnFocusListener;
public CustomPersistencyBehavior(DiagramBehavior diagramBehavior) {
super(diagramBehavior);
}
@Override
- public Diagram loadDiagram(URI uri) {
- saveOnFocusListener = new SaveOnFocusLostListener((IEditorPart)diagramBehavior.getDiagramContainer().getWorkbenchPart());
- return super.loadDiagram(uri);
- }
-
- @Override
protected Map<Resource, Map<?, ?>> createSaveOptions() {
// save options for xtext: activate formatting
SaveOptions xtextSaveOptions = SaveOptions.newBuilder().format().getOptions();
@@ -83,12 +72,9 @@ public class CustomPersistencyBehavior extends DefaultPersistencyBehavior {
public void saveDiagram(IProgressMonitor monitor) {
boolean valid = validateResourcesBeforeSave(monitor);
- if (valid)
+ if (valid) {
super.saveDiagram(monitor);
-
- // deactivate saveOnFocus for better usability
- // avoid retrigger loop from message box
- saveOnFocusListener.setActive(valid);
+ }
}
protected boolean validateResourcesBeforeSave(final IProgressMonitor monitor){
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java
index 9674a3860..9f009caf8 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/DiagramEditorBase.java
@@ -19,6 +19,7 @@ import static org.eclipse.ui.PlatformUI.getWorkbench;
import java.util.Collection;
import java.util.Map;
+import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
@@ -26,6 +27,7 @@ import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.common.ui.editor.ISaveOnFocusLostEditor;
+import org.eclipse.etrice.core.common.ui.editor.SaveDialogEditor;
import org.eclipse.etrice.core.common.ui.linking.GlobalNonPlatformURIEditorOpener;
import org.eclipse.etrice.core.fsm.fSM.ModelComponent;
import org.eclipse.etrice.core.fsm.ui.FSMUiModule;
@@ -64,7 +66,8 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU
private SuperClassListener superClassListener;
- private boolean isClosing = false;
+ private SaveDialogEditor saveDialog = new SaveDialogEditor();
+ private boolean lastSaveFailed = false;
public DiagramEditorBase(Object textEditorClass) {
this.textEditorClass = textEditorClass;
@@ -108,6 +111,7 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU
*/
@Override
public void setFocus() {
+ lastSaveFailed = false;
if (getGraphicalViewer() == null) {
return;
}
@@ -259,12 +263,24 @@ public abstract class DiagramEditorBase extends DiagramEditor implements IInputU
@Override
public int promptToSaveOnClose() {
- isClosing = true;
- return DEFAULT;
+ return saveDialog.open(getGraphicalControl().getShell(), getPartName());
}
-
- public boolean isClosing() {
- return isClosing;
+
+ @Override
+ public boolean shouldSaveOnFocusLost() {
+ // lastSaveFailed is heuristic to avoid unsuccessful re-save
+ // TODO implement IValidatingEditor
+ return saveDialog.getResult() != NO && !lastSaveFailed && isDirty();
+ }
+
+ @Override
+ public void doSave(IProgressMonitor monitor) {
+ super.doSave(monitor);
+
+ // avoid auto re-save
+ lastSaveFailed = isDirty();
+ // do not quit editor if save failed but was requested by user explicitly
+ monitor.setCanceled(lastSaveFailed && saveDialog.getResult() == YES);
}
}
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/SaveOnFocusLostListener.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/SaveOnFocusLostListener.java
index 8b6cf8913..a6ef08db3 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/SaveOnFocusLostListener.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/SaveOnFocusLostListener.java
@@ -14,75 +14,82 @@
package org.eclipse.etrice.ui.common.base.editor;
+import static org.eclipse.etrice.ui.common.base.UIBaseActivator.PLUGIN_ID;
+
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
import org.eclipse.etrice.core.common.ui.editor.ISaveOnFocusLostEditor;
-import org.eclipse.etrice.ui.common.base.UIBaseActivator;
import org.eclipse.etrice.ui.common.base.preferences.UIBasePreferenceConstants;
-import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IPartService;
+import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.statushandlers.StatusManager;
/**
- * @author Henrik Rentz-Reichert
- *
+ * Global listener in workbench for {@link ISaveOnFocusLostEditor }.
*/
-public class SaveOnFocusLostListener implements IPartListener {
-
- private final IEditorPart editor;
- private final IPreferenceStore store;
-
- private boolean isActive = true;
-
- @SuppressWarnings("unused")
- private boolean saveAfterCurrentCommand = false;
-
- public SaveOnFocusLostListener(IEditorPart editor) {
- this.editor = editor;
- this.store = UIBaseActivator.getDefault().getPreferenceStore();
- editor.getSite().getPage().addPartListener(this);
+public class SaveOnFocusLostListener implements IPartListener, IStartup {
+
+ @Override
+ public void earlyStartup() {
+ PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ IPartService partService = (window != null) ? window.getPartService() : null;
+ if(partService != null) {
+ partService.addPartListener(this);
+ } else {
+ Status status = new Status(Status.ERROR, PLUGIN_ID, "Failed to register " + getClass().getSimpleName());
+ StatusManager.getManager().handle(status);
+ }
+
+ });
}
-
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
+ */
@Override
public void partActivated(IWorkbenchPart part) {
}
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
+ */
@Override
public void partBroughtToTop(IWorkbenchPart part) {
}
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
+ */
@Override
public void partClosed(IWorkbenchPart part) {
- if(part != editor)
- return;
-
- setActive(false);
- part.getSite().getPage().removePartListener(this);
}
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
+ */
@Override
- public void partDeactivated(IWorkbenchPart part) {
- if (part != editor)
- return;
-
+ public void partDeactivated(IWorkbenchPart part) {
if (part instanceof ISaveOnFocusLostEditor) {
- if (((ISaveOnFocusLostEditor) part).isClosing()) {
- return;
+ ISaveOnFocusLostEditor editor = (ISaveOnFocusLostEditor) part;
+ boolean isPrefSave = Platform.getPreferencesService().getBoolean("org.eclipse.etrice.ui.common.base",
+ UIBasePreferenceConstants.SAVE_DIAG_ON_FOCUS_LOST, false, null);
+ if(isPrefSave && editor.shouldSaveOnFocusLost()) {
+ editor.doSave(new NullProgressMonitor());
}
}
-
- boolean isSaveOnFocus = store.getBoolean(UIBasePreferenceConstants.SAVE_DIAG_ON_FOCUS_LOST);
- if (isActive && isSaveOnFocus && editor.isDirty()) {
- editor.doSave(new NullProgressMonitor());
- }
}
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
+ */
@Override
public void partOpened(IWorkbenchPart part) {
}
-
- public void setActive(boolean isActive){
- this.isActive = isActive;
- }
}

Back to the top