Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2012-05-11 14:58:38 +0000
committerHenrik Rentz-Reichert2012-05-11 14:58:38 +0000
commit07e6eb592a5627c828880d02ea351f5adc7cdfde (patch)
tree387c7888efff8f82330707e023b7d4cb905f7d7d /plugins/org.eclipse.etrice.core.room.ui
parent452f7629eb54a118807ca169f069a353c24395ca (diff)
downloadorg.eclipse.etrice-07e6eb592a5627c828880d02ea351f5adc7cdfde.tar.gz
org.eclipse.etrice-07e6eb592a5627c828880d02ea351f5adc7cdfde.tar.xz
org.eclipse.etrice-07e6eb592a5627c828880d02ea351f5adc7cdfde.zip
[core.room, ui.common] added preference for saving of textual editor on focus change
Diffstat (limited to 'plugins/org.eclipse.etrice.core.room.ui')
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/META-INF/MANIFEST.MF1
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/plugin.xml2
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/IValidatingEditor.java22
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/PreferenceConstants.java21
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java84
-rw-r--r--plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/SaveOnFocusLostListener.java78
6 files changed, 207 insertions, 1 deletions
diff --git a/plugins/org.eclipse.etrice.core.room.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.etrice.core.room.ui/META-INF/MANIFEST.MF
index 0b8c3a446..49e849cbc 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.etrice.core.room.ui/META-INF/MANIFEST.MF
@@ -22,6 +22,7 @@ Import-Package: org.apache.log4j
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: org.eclipse.etrice.core.ui,
org.eclipse.etrice.core.ui.contentassist.antlr,
+ org.eclipse.etrice.core.ui.editor,
org.eclipse.etrice.core.ui.linking,
org.eclipse.etrice.core.ui.newwizard
Bundle-Activator: org.eclipse.etrice.core.ui.internal.RoomActivator
diff --git a/plugins/org.eclipse.etrice.core.room.ui/plugin.xml b/plugins/org.eclipse.etrice.core.room.ui/plugin.xml
index 50710e54a..c9d1d69e8 100644
--- a/plugins/org.eclipse.etrice.core.room.ui/plugin.xml
+++ b/plugins/org.eclipse.etrice.core.room.ui/plugin.xml
@@ -6,7 +6,7 @@
<extension
point="org.eclipse.ui.editors">
<editor
- class="org.eclipse.etrice.core.ui.RoomExecutableExtensionFactory:org.eclipse.xtext.ui.editor.XtextEditor"
+ class="org.eclipse.etrice.core.ui.RoomExecutableExtensionFactory:org.eclipse.etrice.core.ui.editor.RoomEditor"
contributorClass="org.eclipse.ui.editors.text.TextEditorActionContributor"
default="true"
extensions="room"
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/IValidatingEditor.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/IValidatingEditor.java
new file mode 100644
index 000000000..4cc1ec1d4
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/IValidatingEditor.java
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * 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 v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.ui.editor;
+
+/**
+ * @author Henrik Rentz-Reichert
+ *
+ */
+public interface IValidatingEditor {
+
+ public boolean isValid();
+}
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/PreferenceConstants.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/PreferenceConstants.java
new file mode 100644
index 000000000..56bda751b
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/PreferenceConstants.java
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * 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 v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.ui.editor;
+
+/**
+ * Constant definitions for plug-in preferences
+ */
+public class PreferenceConstants {
+
+ public static final String SAVE_TEXT_ON_FOCUS_LOST = "SaveTextOnFocusLost";
+}
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
new file mode 100644
index 000000000..31b01cfcc
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/RoomEditor.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * 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 v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.ui.editor;
+
+import java.util.List;
+
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.xtext.diagnostics.Severity;
+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;
+
+import com.google.inject.Inject;
+
+/**
+ * @author Henrik Rentz-Reichert
+ *
+ */
+public class RoomEditor extends XtextEditor 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);
+ 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() || issue.getSeverity()==Severity.ERROR)
+ return false;
+ }
+ }
+ return true;
+ }
+ });
+ }
+}
diff --git a/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/SaveOnFocusLostListener.java b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/SaveOnFocusLostListener.java
new file mode 100644
index 000000000..77f5c8050
--- /dev/null
+++ b/plugins/org.eclipse.etrice.core.room.ui/src/org/eclipse/etrice/core/ui/editor/SaveOnFocusLostListener.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * 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 v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.core.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;
+
+ public SaveOnFocusLostListener(XtextEditor editor) {
+ this.editor = editor;
+ }
+
+ /* (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;
+
+ boolean save = Platform.getPreferencesService().getBoolean(
+ "org.eclipse.etrice.ui.common",
+ PreferenceConstants.SAVE_TEXT_ON_FOCUS_LOST, false, 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) {
+ }
+
+}

Back to the top