summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-04-03 15:09:36 (EDT)
committer pelder2006-04-03 15:09:36 (EDT)
commitde01baad84509b7ff10b69e7d1cbeb3741f75524 (patch)
treef3becd4b2477ea05315303e3ce0404294816c367
parent5b6cc83f3c74503e884d171d23f3d6b3adc89fed (diff)
downloadorg.eclipse.jet-de01baad84509b7ff10b69e7d1cbeb3741f75524.zip
org.eclipse.jet-de01baad84509b7ff10b69e7d1cbeb3741f75524.tar.gz
org.eclipse.jet-de01baad84509b7ff10b69e7d1cbeb3741f75524.tar.bz2
[134583] Use resources API instead of FileBuffers API to commit contents.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/compiler/JET2Compiler.java42
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java116
2 files changed, 104 insertions, 54 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/compiler/JET2Compiler.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/compiler/JET2Compiler.java
index ddec8af..98429be 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/compiler/JET2Compiler.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/compiler/JET2Compiler.java
@@ -19,12 +19,10 @@ package org.eclipse.jet.compiler;
import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Properties;
-import org.eclipse.core.filebuffers.FileBuffers;
-import org.eclipse.core.filebuffers.ITextFileBuffer;
-import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
@@ -166,6 +164,10 @@ public class JET2Compiler
{
InternalJET2Platform.logError(JET2Messages.JET2Compiler_ErrorWritingJava, e);
}
+ catch (UnsupportedEncodingException e)
+ {
+ InternalJET2Platform.logError(JET2Messages.JET2Compiler_ErrorWritingJava, e);
+ }
}
/**
@@ -318,12 +320,6 @@ public class JET2Compiler
IFile targetFile = file.getProject().getFile(javaClassProjectPath(qualifiedClassName));
try
{
- // XXX: Temporarily restore .jet files that were set to derived by an earlier version of this compiler.
- // XXX: Remove before shipping.
- if (file.isDerived())
- {
- file.setDerived(false);
- }
writeFile(javaSrc, targetFile, file.getCharset());
targetFile.setDerived(true);
compilerState.mapTemplate(templatePath, qualifiedClassName);
@@ -332,6 +328,10 @@ public class JET2Compiler
{
InternalJET2Platform.logError(JET2Messages.JET2Compiler_ErrorWritingJava, e);
}
+ catch (UnsupportedEncodingException e)
+ {
+ InternalJET2Platform.logError(JET2Messages.JET2Compiler_ErrorWritingJava, e);
+ }
}
/**
@@ -411,8 +411,9 @@ public class JET2Compiler
* @param targetFile
* @param charset the character encoding of the .jet source file
* @throws CoreException
+ * @throws UnsupportedEncodingException
*/
- private void writeFile(String javaSrc, IFile targetFile, String charset) throws CoreException
+ private void writeFile(String javaSrc, IFile targetFile, String charset) throws CoreException, UnsupportedEncodingException
{
ensureDirsExist(targetFile.getParent());
@@ -421,24 +422,13 @@ public class JET2Compiler
targetFile.create(new ByteArrayInputStream(new byte[]{}), true, new SubProgressMonitor(monitor, 1));
}
- final ITextFileBufferManager mgr = FileBuffers.getTextFileBufferManager();
-
- mgr.connect(targetFile.getLocation(), new SubProgressMonitor(monitor, 1));
- try
+ if(!targetFile.getCharset().equals(charset))
{
- final ITextFileBuffer textFileBuffer = mgr.getTextFileBuffer(targetFile.getLocation());
- if(!targetFile.getCharset().equalsIgnoreCase(charset))
- {
- textFileBuffer.setEncoding(charset);
- }
- textFileBuffer.getDocument().set(javaSrc);
-
- textFileBuffer.commit(new SubProgressMonitor(monitor, 1), true);
- }
- finally
- {
- mgr.disconnect(targetFile.getLocation(), new SubProgressMonitor(monitor, 1));
+ targetFile.setCharset(charset, new SubProgressMonitor(monitor, 1));
}
+
+ targetFile.setContents(new ByteArrayInputStream(javaSrc.getBytes(charset)),
+ true, false, new SubProgressMonitor(monitor, 1));
}
private void ensureDirsExist(IContainer container) throws CoreException
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
index 94d8ab0..11a4d8f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
@@ -28,9 +28,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.MessageFormat;
-import org.eclipse.core.filebuffers.FileBuffers;
-import org.eclipse.core.filebuffers.ITextFileBuffer;
-import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
@@ -39,7 +36,11 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.jobs.IJobManager;
+import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.taglib.JET2TagException;
@@ -115,15 +116,38 @@ public final class ActionsUtil
JET2Messages.WsFileFromWriterAction_WritingFile,
new Object []{ file.getFullPath().toString() });
- monitor.beginTask(fileMessage, 4);
+ monitor.beginTask(fileMessage, 5);
-
if (file.exists() && !replace)
{
return false;
}
- // Step 1: ensure containing folder exists...
+ // Step 1: set the encoding...
+ if (encoding != null)
+ {
+ if (!file.exists())
+ {
+ // can't set encoding until the file exists, create the file with null-contents
+ // now, and set it's contents later.
+ file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+ file.setCharset(encoding.length() > 0 ? encoding : null, new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(2);
+ }
+
+ // Step 2: convert the contents to a byte stream of the proper type
+ ByteArrayInputStream source = new ByteArrayInputStream(contents.getBytes(file.getCharset()));
+ monitor.worked(1);
+
+ // Step 3: ensure containing folder exists...
if (file.getParent() instanceof IFolder)
{
ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
@@ -132,38 +156,26 @@ public final class ActionsUtil
{
monitor.worked(1);
}
- if(!file.exists())
- {
- file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
- }
- final ITextFileBufferManager mgr = FileBuffers.getTextFileBufferManager();
-
- // Step 2: connect to the file manager, set the contents and the encoding...
- mgr.connect(file.getLocation(), new SubProgressMonitor(monitor, 1));
- try
+ // Step 4: write the contents
+ if (file.exists())
{
- final ITextFileBuffer textFileBuffer = mgr.getTextFileBuffer(file.getLocation());
-
- textFileBuffer.getDocument().set(contents);
-
- if (encoding != null)
- {
- textFileBuffer.setEncoding(encoding);
- }
-
- textFileBuffer.commit(new SubProgressMonitor(monitor, 1), false);
+ file.setContents(source, false, true, new SubProgressMonitor(monitor, 2));
}
- finally
+ else
{
- mgr.disconnect(file.getLocation(), new SubProgressMonitor(monitor, 1));
+ file.create(source, false, new SubProgressMonitor(monitor, 1));
}
-
+ // RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
// Step 5: Apply generated file properties...
file.setDerived(derived);
return true;
}
+ catch (UnsupportedEncodingException e)
+ {
+ throw new JET2TagException(e);
+ }
catch (CoreException e)
{
throw new JET2TagException(e);
@@ -413,9 +425,57 @@ public final class ActionsUtil
* </p>
* @param build <code>true</code> if a workspace build should be performed after the event broadcast.
* @see IWorkspace#checkpoint(boolean)
+ * @deprecated Use {@link #checkpointWorkspace(boolean,IProgressMonitor)} instead
*/
public static void checkpointWorkspace(boolean build)
{
+ checkpointWorkspace(build, new NullProgressMonitor());
+ }
+
+ /**
+ * Force the workspace to broadcast accumulated workspace change events, and optionally do a build.
+ * This method is useful if a pending workspace action depends on prior actions being processed
+ * by other actors. For example, if a Project and corresponding .project file are created that describe
+ * the project as having the Java nature, then JDT must be informed of these changes before JDT APIs will
+ * see the new project properly.
+ * <p>
+ * Generally, workspace actions authors will not need this tag.
+ * </p>
+ * @param build <code>true</code> if a workspace build should be performed after the event broadcast.
+ * @param monitor TODO
+ * @see IWorkspace#checkpoint(boolean)
+ */
+ public static void checkpointWorkspace(boolean build, IProgressMonitor monitor)
+ {
ResourcesPlugin.getWorkspace().checkpoint(build);
+ if(build)
+ {
+ waitForBuildToComplete(monitor);
+ }
+
}
+
+ private static void waitForBuildToComplete(IProgressMonitor monitor)
+ {
+ // wait for in-progress auto-build to complete
+ IJobManager jobManager = Platform.getJobManager();
+ Job[] build = jobManager.find(ResourcesPlugin.FAMILY_AUTO_BUILD);
+ if (build.length == 1)
+ {
+ monitor.beginTask(JET2Messages.ProjectTemplateBundleDescriptor_WaitingForBuild, 1);
+ try
+ {
+ build[0].join();
+ }
+ catch (InterruptedException e)
+ {
+ // nothing to do, continue...
+ }
+ finally
+ {
+ monitor.done();
+ }
+ }
+ }
+
}