Просмотр исходного кода

Check for read only file modification flag when changing the model via property page

Andreas Mülder 12 лет назад
Родитель
Сommit
1d767723eb

+ 2 - 2
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/propertysheets/AbstractEditorPropertySection.java

@@ -49,9 +49,9 @@ import com.google.inject.Injector;
 import de.itemis.xtext.utils.jface.fieldassist.CompletionProposalAdapter;
 import de.itemis.xtext.utils.jface.viewers.ContextElementAdapter;
 import de.itemis.xtext.utils.jface.viewers.ContextElementAdapter.IContextElementProvider;
-import de.itemis.xtext.utils.jface.viewers.util.ActiveEditorTracker;
 import de.itemis.xtext.utils.jface.viewers.FilteringMenuManager;
 import de.itemis.xtext.utils.jface.viewers.StyledTextXtextAdapter;
+import de.itemis.xtext.utils.jface.viewers.util.ActiveEditorTracker;
 
 /**
  * 
@@ -80,7 +80,7 @@ public abstract class AbstractEditorPropertySection extends AbstractModelerPrope
 		super.refresh();
 		if (bindingContext != null)
 			bindingContext.dispose();
-		bindingContext = new EMFDataBindingContext();
+		bindingContext = new ValidatingEMFDatabindingContext(this, form.getShell());
 		bindModel(bindingContext);
 	}
 

+ 61 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/propertysheets/ValidatingEMFDatabindingContext.java

@@ -0,0 +1,61 @@
+/**
+ * Copyright (c) 2014 committers of YAKINDU and others.
+ * 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.propertysheets;
+
+import org.eclipse.core.databinding.UpdateValueStrategy;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.emf.databinding.EMFDataBindingContext;
+import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gmf.runtime.common.ui.resources.FileModificationValidator;
+import org.eclipse.swt.widgets.Shell;
+
+import de.itemis.xtext.utils.jface.viewers.ContextElementAdapter.IContextElementProvider;
+
+/**
+ * Checks the file modification flag before updating the model.
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public class ValidatingEMFDatabindingContext extends EMFDataBindingContext {
+
+	private IContextElementProvider elementProvider;
+	private Shell shell;
+
+	public ValidatingEMFDatabindingContext(IContextElementProvider elementProvider, Shell shell) {
+		this.elementProvider = elementProvider;
+		this.shell = shell;
+	}
+
+	protected IStatus isWriteable() {
+		IFile file = WorkspaceSynchronizer.getFile(elementProvider.getContextObject().eResource());
+		return FileModificationValidator.getInstance().validateEdit(new IFile[] { file }, shell);
+
+	}
+
+	@Override
+	protected UpdateValueStrategy createTargetToModelUpdateValueStrategy(IObservableValue fromValue,
+			IObservableValue toValue) {
+		return new EMFUpdateValueStrategy() {
+			@Override
+			protected IStatus doSet(IObservableValue observableValue, Object value) {
+				IStatus writeableStatus = isWriteable();
+				if (!writeableStatus.isOK())
+					return writeableStatus;
+				return super.doSet(observableValue, value);
+			}
+		};
+	}
+
+}