Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeo Denault2014-02-14 16:12:30 +0000
committerLeo Denault2014-02-21 13:20:19 +0000
commit6c47e5bf7b9b81adb02fb00919b0bf72e49f5e92 (patch)
treec9d89e58b19c2406d90f39d858a1fc2e0386c93f
parent62fc23e7932a9265542ff16b67a4ce926d0c4752 (diff)
downloadorg.eclipse.e4.tools-6c47e5bf7b9b81adb02fb00919b0bf72e49f5e92.tar.gz
org.eclipse.e4.tools-6c47e5bf7b9b81adb02fb00919b0bf72e49f5e92.tar.xz
org.eclipse.e4.tools-6c47e5bf7b9b81adb02fb00919b0bf72e49f5e92.zip
Bug 426397 - Provide an eclipse editor based on OrionEditorControl
Added ability for OrionEditor to load text from a CSS file. Also added some test cases for isDirty(). Change-Id: I1f471821105b2c5510ead12bbb2b11034de5cc5a Signed-off-by: Leo Denault <ldena023@uottawa.ca>
-rw-r--r--bundles/org.eclipse.e4.tools.orion.text.editor/META-INF/MANIFEST.MF4
-rwxr-xr-xbundles/org.eclipse.e4.tools.orion.text.editor/src/org/eclipse/e4/tools/orion/text/editor/OrionEditor.java70
-rw-r--r--tests/org.eclipse.e4.tools.orion.text.editor.test/src/org/eclipse/e4/tools/orion/text/editor/test/OrionEditorTest.java65
3 files changed, 130 insertions, 9 deletions
diff --git a/bundles/org.eclipse.e4.tools.orion.text.editor/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.orion.text.editor/META-INF/MANIFEST.MF
index 487f225d..31dc62fc 100644
--- a/bundles/org.eclipse.e4.tools.orion.text.editor/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.tools.orion.text.editor/META-INF/MANIFEST.MF
@@ -7,7 +7,9 @@ Bundle-Version: 0.16.0.qualifier
Bundle-Activator: org.eclipse.e4.tools.orion.text.editor.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
- org.eclipse.e4.tools.orion.editor;bundle-version="0.15.0"
+ org.eclipse.e4.tools.orion.editor;bundle-version="0.15.0",
+ org.eclipse.core.resources;bundle-version="3.9.0",
+ org.eclipse.ui.ide;bundle-version="3.10.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.e4.tools.orion.text.editor
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 10d7451d..581becb8 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
@@ -11,7 +11,11 @@
package org.eclipse.e4.tools.orion.text.editor;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
@@ -23,10 +27,12 @@ import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
+import org.eclipse.ui.part.FileEditorInput;
public class OrionEditor extends EditorPart {
private OrionEditorControl control;
+ private IFile source;
@Override
public void doSave(IProgressMonitor monitor) {
@@ -42,13 +48,25 @@ public class OrionEditor extends EditorPart {
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
+ if (!(input instanceof FileEditorInput)) {
+ throw new PartInitException(new Status(IStatus.ERROR,
+ Activator.PLUGIN_ID,
+ "Expected editor input to be of type FileEditorInput"));
+ }
+
setSite(site);
setInput(input);
+
+ FileEditorInput fileInput = ((FileEditorInput) input);
+ if (fileInput != null) {
+ source = fileInput.getFile();
+ }
}
@Override
public boolean isDirty() {
- return control == null ? false : control.isDirty();
+ return (control == null || control.isDisposed()) ? false : control
+ .isDirty();
}
@Override
@@ -60,13 +78,18 @@ public class OrionEditor extends EditorPart {
@Override
public void createPartControl(Composite parent) {
try {
- control = new OrionEditorControl(parent, SWT.NONE, new CSSBuilder(""));
- } catch (IOException e) {
+ control = new OrionEditorControl(parent, SWT.NONE, new CSSBuilder(
+ ""));
+
+ if (source != null) {
+ control.setText(loadFile(source.getContents(), 1024));
+ }
+ } catch (Exception e) {
Activator
- .getDefault()
- .getLog()
- .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
- "Failed to load CSS", e));
+ .getDefault()
+ .getLog()
+ .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+ "Failed to load CSS", e));
}
}
@@ -76,4 +99,37 @@ public class OrionEditor extends EditorPart {
control.setFocus();
}
}
+
+ public String getContents() {
+ return control.getText();
+ }
+
+ /**
+ * Loads the content of the given InputStream into a String and returns it.
+ *
+ * @param in
+ * The InputStream of the file to read from.
+ * @param bufferSize
+ * The size of the buffer used to transfer the contents of the
+ * InputStream to the String.
+ * @return A String containing the contents of the file.
+ * @throws IOException
+ * If there was an error reading from the file.
+ */
+ public String loadFile(final InputStream in, final int bufferSize)
+ throws IOException {
+ final char[] buffer = new char[bufferSize];
+ final StringBuilder out = new StringBuilder();
+ final Reader reader = new InputStreamReader(in, "UTF-8");
+ try {
+ int size = reader.read(buffer, 0, buffer.length);
+ while (size > 0) {
+ out.append(buffer, 0, size);
+ size = reader.read(buffer, 0, buffer.length);
+ }
+ } finally {
+ reader.close();
+ }
+ return out.toString();
+ }
}
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 737e1595..42d00236 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
@@ -10,12 +10,19 @@
*******************************************************************************/
package org.eclipse.e4.tools.orion.text.editor.test;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
+import org.eclipse.e4.tools.orion.text.editor.OrionEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.internal.ErrorEditorPart;
+import org.eclipse.ui.internal.part.NullEditorInput;
import org.eclipse.ui.tests.harness.util.ArrayUtil;
import org.eclipse.ui.tests.harness.util.FileUtil;
import org.eclipse.ui.tests.harness.util.UITestCase;
@@ -51,7 +58,7 @@ public class OrionEditorTest extends UITestCase {
}
}
- public void testOpenEditorForCssFile() throws Throwable {
+ public void testOpenEditorForEmptyCssFile() throws Throwable {
proj = FileUtil.createProject("testOpenEditor");
IFile file = FileUtil.createFile("test.css", proj);
@@ -66,4 +73,60 @@ public class OrionEditorTest extends UITestCase {
assertEquals(editor.getSite().getId(), fWorkbench.getEditorRegistry()
.getDefaultEditor(file.getName()).getId());
}
+
+ public void testOpenEditorForNonEmptyCssFile() throws Throwable {
+ proj = FileUtil.createProject("testOpenEditor");
+ String fileContents = ".someClass { background: #000000; }";
+
+ // Insert text into the CSS file
+ IFile file = FileUtil.createFile("test.css", proj);
+ InputStream in = new ByteArrayInputStream(fileContents.getBytes("UTF-8"));
+ file.setContents(in, IFile.NONE, null);
+
+ // Make sure that the editor properly opens.
+ IEditorPart editor = IDE.openEditor(fActivePage, file, true);
+ assertTrue(ArrayUtil.contains(fActivePage.getEditors(), editor));
+ assertEquals(fActivePage.getActiveEditor(), editor);
+ assertEquals(editor.getSite().getId(), fWorkbench.getEditorRegistry()
+ .getDefaultEditor(file.getName()).getId());
+
+ // Check that the OrionEditorControl contains the text
+ // that was in the CSS file.
+ OrionEditor orionEditor = (OrionEditor)editor;
+ assertEquals(fileContents, orionEditor.getContents());
+
+ FileUtil.delete(file);
+ }
+
+ public void testIsDirtyReturnsFalseWhenOrionEditorControlIsNull() {
+ OrionEditor editor = new OrionEditor();
+ assertFalse(editor.isDirty());
+ }
+
+ public void testIsDirtyReturnsFalseWhenOrionEditorControlIsDisposed() throws Throwable {
+ proj = FileUtil.createProject("testOpenEditor");
+ IFile file = FileUtil.createFile("test.css", proj);
+
+ // Make sure that the editor properly opens.
+ IEditorPart editor = IDE.openEditor(fActivePage, file, true);
+ assertTrue(ArrayUtil.contains(fActivePage.getEditors(), editor));
+ assertEquals(fActivePage.getActiveEditor(), editor);
+ assertEquals(editor.getSite().getId(), fWorkbench.getEditorRegistry()
+ .getDefaultEditor(file.getName()).getId());
+
+ FileUtil.deleteProject(proj);
+ proj = null;
+
+ assertFalse(editor.isDirty());
+ }
+
+ @SuppressWarnings("restriction")
+ public void testInitThrowsExceptionWithNonFileEditorInput() {
+ try {
+ IEditorPart editor = IDE.openEditor(fActivePage, new NullEditorInput(), ORION_EDITOR_ID);
+ assertTrue(editor instanceof ErrorEditorPart);
+ } catch (PartInitException e) {
+ fail("The PartInitException should be caught internally.");
+ }
+ }
}

Back to the top