Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeo Denault2014-03-14 13:23:39 +0000
committerLeo Denault2014-04-04 16:01:41 +0000
commitf79e437c43614529a38436f9b139a0bcaa753300 (patch)
tree0208cee91ddd48c338ce356c294404ffa4cfa9ee
parentaf8a71a233a01cd2c2fff5bb0b5c4a54a0ef2ae7 (diff)
downloadorg.eclipse.e4.tools-f79e437c43614529a38436f9b139a0bcaa753300.tar.gz
org.eclipse.e4.tools-f79e437c43614529a38436f9b139a0bcaa753300.tar.xz
org.eclipse.e4.tools-f79e437c43614529a38436f9b139a0bcaa753300.zip
Bug 426397 - Provide an eclipse editor based on OrionEditorControl
Implemented Save As feature. Change-Id: I9723ac88b637e4dc99a5db23eb8a07f2bfc14e9d Signed-off-by: Leo Denault <ldena023@uottawa.ca>
-rwxr-xr-xbundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java100
-rw-r--r--tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java15
2 files changed, 85 insertions, 30 deletions
diff --git a/bundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java b/bundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java
index 8383cae3..fa88e770 100755
--- a/bundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java
+++ b/bundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java
@@ -17,6 +17,8 @@ import java.io.InputStreamReader;
import java.io.Reader;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@@ -26,12 +28,14 @@ import org.eclipse.e4.tools.orion.editor.builder.html.HTMLBuilder;
import org.eclipse.e4.tools.orion.editor.builder.js.JSBuilder;
import org.eclipse.e4.tools.orion.editor.swt.IDirtyListener;
import org.eclipse.e4.tools.orion.editor.swt.OrionEditorControl;
+import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISaveablePart;
import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.dialogs.SaveAsDialog;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;
@@ -47,25 +51,7 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
@Override
public void doSave(IProgressMonitor monitor) {
if (isDirty()) {
- boolean success = true;
- try {
- InputStream contentStream = new ByteArrayInputStream(control
- .getText().getBytes("UTF-8"));
- source.setContents(contentStream, IFile.KEEP_HISTORY, monitor);
- } catch (Exception e) {
- success = false;
- Activator
- .getDefault()
- .getLog()
- .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
- "Failed to save CSS", e));
- }
-
- if (success) {
- control.setDirty(false);
- } else {
- monitor.setCanceled(true);
- }
+ performSave(source, monitor);
} else {
monitor.done();
}
@@ -73,8 +59,65 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
@Override
public void doSaveAs() {
- // TODO Auto-generated method stub
+ if (isSaveAsAllowed()) {
+ SaveAsDialog dialog = new SaveAsDialog(getSite().getShell());
+
+ if (source != null) {
+ dialog.setOriginalFile(source);
+ }
+
+ IProgressMonitor monitor = getStatusLineManager()
+ .getProgressMonitor();
+ if (dialog.open() == SaveAsDialog.CANCEL) {
+ monitor.setCanceled(true);
+ } else {
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IFile file = workspace.getRoot().getFile(dialog.getResult());
+
+ if (performSave(file, monitor)) {
+ source = file;
+ setPartName(file.getName());
+ setInput(new FileEditorInput(file));
+ }
+ }
+ }
+ }
+
+ /**
+ * Attempts to save the contents of the editor to the specified
+ * {@link IFile} using the given {@link IProgressMonitor}.
+ *
+ * @param file
+ * The file to which the contents of the editor should be saved.
+ * @param monitor
+ * The progress monitor to use.
+ * @return True if the save operation succeeds, false otherwise.
+ */
+ private boolean performSave(IFile file, IProgressMonitor monitor) {
+ boolean success = true;
+ try {
+ InputStream contentStream = new ByteArrayInputStream(control
+ .getText().getBytes("UTF-8"));
+ if (file.exists()) {
+ file.setContents(contentStream, IFile.KEEP_HISTORY, monitor);
+ } else {
+ file.create(contentStream, IFile.KEEP_HISTORY, monitor);
+ }
+ } catch (Exception e) {
+ success = false;
+ Activator
+ .getDefault()
+ .getLog()
+ .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+ "Failed to save file", e));
+ }
+ if (success) {
+ control.setDirty(false);
+ } else {
+ monitor.setCanceled(true);
+ }
+ return success;
}
@Override
@@ -104,8 +147,7 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
@Override
public boolean isSaveAsAllowed() {
- // TODO Auto-generated method stub
- return false;
+ return !(control == null || control.isDisposed());
}
@Override
@@ -135,7 +177,7 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
.getDefault()
.getLog()
.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
- "Failed to load CSS", e));
+ "Failed to load file", e));
}
}
@@ -146,6 +188,14 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
}
}
+ @Override
+ public void dispose() {
+ if (control != null) {
+ control.dispose();
+ }
+ super.dispose();
+ }
+
public String getContents() {
return control != null ? control.getText() : "";
}
@@ -194,4 +244,8 @@ public class OrionEditor extends EditorPart implements IDirtyListener {
public void dirtyChanged(boolean dirty) {
firePropertyChange(ISaveablePart.PROP_DIRTY);
}
+
+ private IStatusLineManager getStatusLineManager() {
+ return getEditorSite().getActionBars().getStatusLineManager();
+ }
}
diff --git a/tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java b/tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java
index 5e60715b..d6b72466 100644
--- a/tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java
+++ b/tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java
@@ -225,16 +225,17 @@ public class OrionEditorTest extends UITestCase {
assertFalse(editor.isDirty());
}
- public void testIsDirtyReturnsFalseWhenOrionEditorControlIsDisposed()
+ public void testIsSaveAsAllowedReturnsFalseWhenOrionEditorControlIsNull() {
+ OrionEditor editor = new OrionEditor();
+ assertFalse(editor.isSaveAsAllowed());
+ }
+
+ public void testIsSaveAsAllowedReturnsTrueWhenOrionEditorControlExists()
throws Throwable {
proj = FileUtil.createProject("testOpenEditor");
- IFile file = FileUtil.createFile("test.css", proj);
+ IFile file = FileUtil.createFile("test.js", proj);
IEditorPart editor = openEditor(file);
-
- FileUtil.deleteProject(proj);
- proj = null;
-
- assertFalse(editor.isDirty());
+ assertTrue(editor.isSaveAsAllowed());
}
@SuppressWarnings("restriction")

Back to the top