This commit was manufactured by cvs2svn to create branch 'R3_0_maintenance'.
diff --git a/tests/org.eclipse.wst.jsdt.web.core.tests/src/org/eclipse/wst/jsdt/web/core/tests/AllWebCoreTests.java b/tests/org.eclipse.wst.jsdt.web.core.tests/src/org/eclipse/wst/jsdt/web/core/tests/AllWebCoreTests.java
new file mode 100644
index 0000000..bd584ac
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.core.tests/src/org/eclipse/wst/jsdt/web/core/tests/AllWebCoreTests.java
@@ -0,0 +1,19 @@
+package org.eclipse.wst.jsdt.web.core.tests;
+
+import org.eclipse.wst.jsdt.web.core.tests.translation.TestHtmlTranslation;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class AllWebCoreTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite("JSDT Web Core Tests");
+ //$JUnit-BEGIN$
+
+ //$JUnit-END$
+ suite.addTestSuite(TestHtmlTranslation.class);
+ return suite;
+ }
+
+}
diff --git a/tests/org.eclipse.wst.jsdt.web.core.tests/test.xml b/tests/org.eclipse.wst.jsdt.web.core.tests/test.xml
new file mode 100644
index 0000000..6d34689
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.core.tests/test.xml
@@ -0,0 +1,114 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project
+ name="testsuite"
+ default="run"
+ basedir=".">
+
+ <!-- The following properties should be passed into this script -->
+ <!-- ${eclipse-home} -->
+ <!-- ${buildDirectory} -->
+ <!-- ${buildLabel} -->
+
+ <!-- should be little need to change what's above -->
+ <property
+ name="plugin-name"
+ value="org.eclipse.wst.jsdt.web.core.tests" />
+ <property
+ name="classname"
+ value="org.eclipse.wst.jsdt.web.core.tests.AllWebCoreTests" />
+ <property
+ name="testType"
+ value="core-test" />
+
+ <!-- should be little need to change what's below -->
+
+ <echo message="basedir: ${basedir}" />
+ <echo message="eclipse-home: ${eclipse-home}" />
+ <echo message="buildDirectory: ${buildDirectory}" />
+ <echo message="plugin-name: ${plugin-name}" />
+ <echo message="classname: ${classname}" />
+ <echo message="testType ${testType}" />
+
+
+ <property
+ name="library-file"
+ value="${eclipse-home}/plugins/org.eclipse.test_3.1.0/library.xml" />
+ <property
+ name="workspace"
+ value="${eclipse-home}/junitworkspaces/${plugin-name}" />
+
+ <!-- This target holds all initialization code that needs to be done for -->
+ <!-- all tests that are to be run. Initialization for individual tests -->
+ <!-- should be done within the body of the suite target. -->
+ <target name="init">
+ <tstamp />
+ <delete>
+ <fileset
+ dir="${eclipse-home}"
+ includes="${plugin-name}.*xml" />
+ </delete>
+ <!-- make directory, in case path doesn't exist yet -->
+ <mkdir dir="${workspace}" />
+ <!-- but delete to make sure fresh contents-->
+ <delete
+ dir="${workspace}"
+ quiet="true" />
+
+ </target>
+
+ <!-- This target defines the tests that need to be run. -->
+ <target name="suite">
+
+ <ant
+ target="${testType}"
+ antfile="${library-file}"
+ dir="${eclipse-home}">
+ <property
+ name="data-dir"
+ value="${workspace}" />
+ <property
+ name="plugin-name"
+ value="${plugin-name}" />
+ <property
+ name="classname"
+ value="${classname}" />
+ <property
+ name="plugin-path"
+ value="${eclipse-home}/plugins/${plugin-name}" />
+ </ant>
+
+ <copy
+ failonerror="false"
+ file="${workspace}/.metadata/.log"
+ tofile="${buildDirectory}/${buildLabel}/testResults/consolelogs/${plugin-name}.consolelog.txt" />
+
+ </target>
+
+ <!-- This target holds code to cleanup the testing environment after -->
+ <!-- after all of the tests have been run. You can use this target to -->
+ <!-- delete temporary files that have been created. -->
+ <target name="cleanup">
+ <!-- usually no need to delete workspace until next run, and leaving it allows inspection -->
+ <!-- <delete dir="${workspace}" quiet="true" /> -->
+ </target>
+
+ <!-- This target runs the test suite. Any actions that need to happen -->
+ <!-- after all the tests have been run should go here. -->
+ <target
+ name="run"
+ depends="init,suite,cleanup">
+ <ant
+ target="collect"
+ antfile="${library-file}"
+ dir="${eclipse-home}">
+ <property
+ name="includes"
+ value="${plugin-name}.*xml" />
+ <property
+ name="output-file"
+ value="${plugin-name}.xml" />
+ </ant>
+ </target>
+
+</project>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.wst.jsdt.web.ui.tests/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..92ac2b4
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/META-INF/MANIFEST.MF
@@ -0,0 +1,24 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %Bundle-Name
+Bundle-SymbolicName: org.eclipse.wst.jsdt.web.ui.tests;singleton:=true
+Bundle-Version: 1.0.0
+Bundle-Activator: org.eclipse.wst.jsdt.web.ui.tests.Activator
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.core.commands,
+ org.junit,
+ org.eclipse.wst.sse.core,
+ org.eclipse.wst.sse.ui,
+ org.eclipse.wst.jsdt.core,
+ org.eclipse.wst.jsdt.ui,
+ org.eclipse.wst.jsdt.web.core,
+ org.eclipse.core.resources,
+ org.eclipse.ui.ide,
+ org.eclipse.jface,
+ org.eclipse.ui.workbench,
+ org.eclipse.wst.xml.core,
+ org.eclipse.wst.xml.ui
+Bundle-RequiredExecutionEnvironment: J2SE-1.4
+Bundle-ActivationPolicy: lazy
+Export-Package: org.eclipse.wst.jsdt.web.ui.tests,
+ org.eclipse.wst.jsdt.web.ui.tests.translation
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/OSGI-INF/l10n/bundle.properties b/tests/org.eclipse.wst.jsdt.web.ui.tests/OSGI-INF/l10n/bundle.properties
new file mode 100644
index 0000000..f9d8c9b
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/OSGI-INF/l10n/bundle.properties
@@ -0,0 +1,2 @@
+#Properties file for org.eclipse.wst.jsdt.web.ui.tests
+Bundle-Name = Tests Plug-in
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/build.properties b/tests/org.eclipse.wst.jsdt.web.ui.tests/build.properties
new file mode 100644
index 0000000..948f0bd
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/build.properties
@@ -0,0 +1,6 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .,\
+ OSGI-INF/,\
+ plugin.xml
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.properties b/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.properties
new file mode 100644
index 0000000..73f8bb2
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.properties
@@ -0,0 +1 @@
+_UI_SHOW_TRANSLATION=Show Translation
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.xml b/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.xml
new file mode 100644
index 0000000..6e359b5
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/plugin.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.3"?>
+<plugin>
+ <extension
+ point="org.eclipse.ui.commands">
+ <command
+ id="org.eclipse.wst.jsdt.web.showTranslation"
+ name="Show JavaScript Translation">
+ </command>
+ <command
+ id="org.eclipse.wst.jsdt.web.runCode"
+ name="Run Example Handler">
+ </command>
+ </extension>
+ <extension point="org.eclipse.ui.bindings">
+ <!-- win32: M1=CTRL, M2=SHIFT, M3=ALT, M4=-
+ carbon: M1=COMMAND, M2=SHIFT, M3=ALT, M4=CTRL
+ gtk: ?
+ -->
+ <key
+ sequence="M1+M2+8"
+ contextId="org.eclipse.wst.sse.ui.structuredTextEditorScope"
+ commandId="org.eclipse.wst.jsdt.web.showTranslation"
+ schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
+ <key
+ sequence="M1+M2+7"
+ contextId="org.eclipse.wst.jsdt.ui.javaEditorScope"
+ commandId="org.eclipse.wst.jsdt.web.runCode"
+ schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
+ </extension>
+ <extension point="org.eclipse.ui.handlers">
+ <handler
+ class="org.eclipse.wst.jsdt.web.ui.tests.translation.ShowTranslationHandler"
+ commandId="org.eclipse.wst.jsdt.web.showTranslation"/>
+ <handler
+ class="org.eclipse.wst.jsdt.web.ui.tests.translation.RunCodeHandler"
+ commandId="org.eclipse.wst.jsdt.web.runCode"/>
+ </extension>
+ <extension point="org.eclipse.ui.bindings">
+ <!-- win32: M1=CTRL, M2=SHIFT, M3=ALT, M4=-
+ carbon: M1=COMMAND, M2=SHIFT, M3=ALT, M4=CTRL
+ gtk: ?
+ -->
+ <key
+ sequence="M1+M2+8"
+ contextId="org.eclipse.wst.jsdt.ui.javascriptViewScope"
+ commandId="org.eclipse.wst.jsdt.web.executeArbitrarySnippet"
+ schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
+ </extension>
+ <extension
+ point="org.eclipse.ui.commands">
+ <command
+ id="org.eclipse.wst.jsdt.web.executeArbitrarySnippet"
+ name="%_UI_RUN"/>
+ </extension>
+</plugin>
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/AllWebUITests.java b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/AllWebUITests.java
new file mode 100644
index 0000000..4365435
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/AllWebUITests.java
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.wst.jsdt.web.ui.tests;
+
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.eclipse.wst.jsdt.web.ui.tests.style.StyleTests;
+
+public class AllWebUITests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite("JSDT Web UI Tests");
+ // $JUnit-BEGIN$
+ suite.addTestSuite(StyleTests.class);
+ // $JUnit-END$
+ return suite;
+ }
+
+}
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/style/StyleTests.java b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/style/StyleTests.java
new file mode 100644
index 0000000..d88e182
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/style/StyleTests.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2009 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.wst.jsdt.web.ui.tests.style;
+
+import java.io.ByteArrayInputStream;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.wst.jsdt.core.IJavaScriptProject;
+import org.eclipse.wst.jsdt.core.JavaScriptCore;
+import org.eclipse.wst.jsdt.core.LibrarySuperType;
+import org.eclipse.wst.jsdt.internal.core.JavaProject;
+import org.eclipse.wst.jsdt.launching.JavaRuntime;
+import org.eclipse.wst.jsdt.web.core.internal.project.JsWebNature;
+
+/**
+ * @author nitin
+ *
+ */
+public class StyleTests extends TestCase {
+
+ /**
+ *
+ */
+ public StyleTests() {
+ super();
+ }
+
+ /**
+ * @param name
+ */
+ public StyleTests(String name) {
+ super(name);
+ }
+
+ public void testKeywordStyle() throws CoreException {
+ String projectName = getName();
+ String fileName = "snippet.html";
+ String contents = "<html><head><script> var params = \"someBadString\".substring(1,2,3,4);\nparahnas.shift();</script></head><body> </body></html>";
+
+ // Create the JavaScript project
+ IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
+ description.setNatureIds(new String[]{JavaScriptCore.NATURE_ID});
+ IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ iProject.create(description, new NullProgressMonitor());
+ iProject.open(null);
+
+ // Setup the JavaScript project with Web support
+ IJavaScriptProject project = JavaScriptCore.create(iProject);
+ project.setRawIncludepath(null, new NullProgressMonitor());
+ LibrarySuperType superType = new LibrarySuperType(new Path(JavaRuntime.DEFAULT_SUPER_TYPE_LIBRARY), project, JavaRuntime.DEFAULT_SUPER_TYPE);
+ ((JavaProject) project).setCommonSuperType(superType);
+ new JsWebNature(iProject, null).configure();
+
+ // create test file
+ IFile file = iProject.getFile(fileName);
+ file.create(new ByteArrayInputStream(contents.getBytes()), true, null);
+ IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
+ assertNotNull("editor failed to open", editor);
+ Control control = (Control) editor.getAdapter(Control.class);
+ assertNotNull("editor did not return a Control adapter", control);
+ assertTrue("editor Control adapter is not a StyledText widget", control instanceof StyledText);
+ int varIndex = contents.indexOf("var");
+ StyleRange[] styleRanges = ((StyledText) control).getStyleRanges(varIndex, 3);
+ assertTrue("no style range for 'var' keyword", styleRanges.length > 0);
+ assertNotNull("no foreground color for 'var' keyword", styleRanges[0].foreground);
+ assertNotSame("foreground color has same RGB as black", PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_BLACK).getRGB(), styleRanges[0].foreground.getRGB());
+ }
+}
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/RunCodeHandler.java b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/RunCodeHandler.java
new file mode 100644
index 0000000..bbb8177
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/RunCodeHandler.java
@@ -0,0 +1,418 @@
+/*******************************************************************************
+ * Copyright (c) 2008 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.wst.jsdt.web.ui.tests.translation;
+
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.wst.jsdt.core.IBuffer;
+import org.eclipse.wst.jsdt.core.IFunction;
+import org.eclipse.wst.jsdt.core.IJavaScriptElement;
+import org.eclipse.wst.jsdt.core.IJavaScriptUnit;
+import org.eclipse.wst.jsdt.core.IType;
+import org.eclipse.wst.jsdt.core.JavaScriptCore;
+import org.eclipse.wst.jsdt.core.JavaScriptModelException;
+import org.eclipse.wst.jsdt.core.dom.AST;
+import org.eclipse.wst.jsdt.core.dom.ASTNode;
+import org.eclipse.wst.jsdt.core.dom.ASTParser;
+import org.eclipse.wst.jsdt.core.dom.ASTVisitor;
+import org.eclipse.wst.jsdt.core.dom.Assignment;
+import org.eclipse.wst.jsdt.core.dom.Block;
+import org.eclipse.wst.jsdt.core.dom.ExpressionStatement;
+import org.eclipse.wst.jsdt.core.dom.FunctionInvocation;
+import org.eclipse.wst.jsdt.core.dom.JavaScriptUnit;
+import org.eclipse.wst.jsdt.core.dom.ObjectLiteral;
+import org.eclipse.wst.jsdt.core.dom.ObjectLiteralField;
+import org.eclipse.wst.jsdt.core.dom.QualifiedName;
+import org.eclipse.wst.jsdt.core.dom.StringLiteral;
+import org.eclipse.wst.jsdt.internal.core.DocumentAdapter;
+import org.eclipse.wst.jsdt.internal.corext.ValidateEditException;
+import org.eclipse.wst.jsdt.internal.corext.util.JavaModelUtil;
+import org.eclipse.wst.jsdt.internal.ui.javaeditor.JavaEditor;
+
+
+/**
+ * Connected handler for whatever we need it to be
+ *
+ * Invoke with M1+M2+7
+ *
+ * @author nitin
+ */
+public class RunCodeHandler extends AbstractHandler {
+
+ /**
+ *
+ */
+ public RunCodeHandler() {
+ }
+
+
+ static final class MyNodeVisitor extends ASTVisitor{
+ IJavaScriptUnit jsUnit;
+ String className;
+ private JavaScriptUnit cu;
+
+ public MyNodeVisitor(IJavaScriptUnit jsUnit, String className, ASTNode ast) {
+ super();
+ this.jsUnit = jsUnit;
+ this.className = className;
+ this.cu = (JavaScriptUnit) ast;
+ }
+
+ public boolean visit(org.eclipse.wst.jsdt.core.dom.FunctionDeclaration node) {
+// this.jsUnit = (IJavaScriptUnit) node.getRoot();
+ Block body = node.getBody();
+ try {
+ IJavaScriptElement element = jsUnit.getElementAt(body.getStartPosition());
+ if (element instanceof IFunction) {
+ IFunction function = (IFunction) element;
+ if (!function.isConstructor() && function.getDisplayName().equals("sayHello")) {
+ IType type = function.getDeclaringType();
+ if (type != null /*&& type.getDisplayName().equals(className)*/) {
+ cu.recordModifications();
+ AST ast = node.getAST();
+
+// String newField = "dojo.declare(\"temp\", null,{handler : function(){ }});";
+ String functionLiteral = "handler : function(args){alert(\"Hello\");}";
+ ObjectLiteralField field = getObjectLiteralField(functionLiteral);
+
+ //creating the function
+// AST parentAST = node.getParent().getAST();
+// FunctionDeclaration decl = parentAST.newFunctionDeclaration();
+// decl.setName(parentAST.newSimpleName("handler"));
+// Block block = parentAST.newBlock();
+//// block.statements().add(parentAST.newExpressionStatement(parentAST.newBlockComment()));
+// decl.setBody(block);
+ List fields = ((ObjectLiteral)(node.getParent().getParent().getParent())).fields();
+
+ fields.add(field);
+// node.getParent().getBody().statements().add(decl);
+
+// SourceType sty ;
+// sty.createMethod(contents, sibling, force, monitor)
+// ExpressionStatement st = parentAST.newExpressionStatement(decl);
+
+
+
+ FunctionInvocation methodInvocation = ast.newFunctionInvocation();
+ QualifiedName name = ast.newQualifiedName(ast.newSimpleName("dojo"),ast.newSimpleName("connect"));//$NON-NLS-1$
+ methodInvocation.setExpression(name);
+ methodInvocation.arguments().add(ast.newSimpleName("a"));
+ StringLiteral lit = ast.newStringLiteral();
+ lit.setLiteralValue("a");
+ methodInvocation.arguments().add(lit);
+ ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
+ body.statements().add(expressionStatement);
+
+ TextEdit edits= cu.rewrite(getDocument(jsUnit), JavaScriptCore.getDefaultOptions());
+ try {
+ JavaModelUtil.applyEdit(jsUnit, edits, true, new NullProgressMonitor());
+ } catch (ValidateEditException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (CoreException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+// jsUnit.becomeWorkingCopy(null);
+
+ // get source and make changes to the body directly in the buffer
+// String source = jsUnit.getBuffer().getText(body.getStartPosition(), body.getLength());
+// String toInsert = "\tdojo.connect(a ,b , c, d);\n";
+// int offset = body.getStartPosition()+ body.getLength()-1;
+// IBuffer buffer = jsUnit.getBuffer();
+// buffer.replace(offset, 0, toInsert);
+
+ // OR build the syntax up using AST manipulation
+
+//
+
+// ImportDeclaration id = nodeAst.newImportDeclaration();
+// id.setName(nodeAst.newName(new String[] {"java", "util", "Set"}));
+//
+// FunctionInvocation replacement= nodeAst.newFunctionInvocation();
+//
+//
+//
+// ASTRewrite rewriter = ASTRewrite.create(nodeAst);
+//
+// replacement.setName((SimpleName) rewriter.createCopyTarget(function.getName()));
+// replacement.arguments().add(nodeAst.newThisExpression());
+// replacement.setExpression((Expression) rewriter.createCopyTarget(right));
+// rewriter.replace(method, replacement, null);
+//
+//
+// TypeDeclaration td = (TypeDeclaration) cu.types().get(0);
+// ITrackedNodePosition tdLocation = rewriter.track(td);
+// ListRewrite lrw = rewriter.getListRewrite(cu, JavaScriptUnit.TYPES_PROPERTY);
+// lrw.insertLast(id, null);
+// IDocument doc = getDocument(jsUnit);
+// TextEdit edits = rewriter.rewriteAST(doc, null);
+// try {
+// UndoEdit undo = edits.apply(doc);
+// } catch (MalformedTreeException e1) {
+// // TODO Auto-generated catch block
+// e1.printStackTrace();
+// } catch (BadLocationException e1) {
+// // TODO Auto-generated catch block
+// e1.printStackTrace();
+// }
+//
+
+// jsUnit.commitWorkingCopy(true, new NullProgressMonitor());
+// jsUnit.discardWorkingCopy();
+ }
+ }
+ }
+ }
+ catch (JavaScriptModelException e) {
+ e.printStackTrace();
+ }
+ return false;
+ };
+ }
+
+ protected void doExecute(IFile jsFile) {
+ IProject project = jsFile.getProject();
+// String namespace = CodeGenUtil.getNameSpace(project);
+// final String className = IPCCommandUtil.getPortletHelperClassName(jsFile, namespace);
+ final IJavaScriptUnit jsUnit = (IJavaScriptUnit) JavaScriptCore.create(jsFile);
+
+ if (jsUnit == null)
+ return;
+ IProgressMonitor monitor = null;
+ if (monitor == null) {
+ monitor = new NullProgressMonitor();
+ }
+
+
+ ////////////////////////////////////////////////////////////////////////////////
+
+ // this part may not be of interest for what you're trying to do
+// IType t = null;
+// try{
+// for (org.eclipse.wst.jsdt.core.IFunction function : jsUnit.getFunctions()) {
+// // functions declared in the file outside of any types
+// org.eclipse.wst.jsdt.core.ISourceRange sourceRange = function.getSourceRange();
+// String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
+// }
+//
+// // start with the types in the file
+// IType[] types = jsUnit.getTypes();
+//
+// for (IType type: types) {
+//// if (type.getDisplayName().equals(className)) {
+// t = type;
+// for (IFunction function : type.getFunctions()) {
+// // functions declared in a type
+// if(function.getDisplayName().equals("sayHello") && !function.isConstructor()) {
+// ISourceRange sourceRange = function.getSourceRange();
+// String source = jsUnit.getBuffer().getText(sourceRange.getOffset(), sourceRange.getLength());
+// }
+// }
+//// }
+// }
+// }catch (JavaScriptModelException e) {
+// // TODO: handle exception
+// //;
+// }
+
+ //It would be more accurate to walk the AST of the file instead of searching just the top-level children:
+// SourceType st = null;
+// if(t instanceof SourceType)
+// st = (SourceType) t;
+// try {
+// if(st!=null)
+// st.createMethod("function hello(args){}", null, false, null);
+//
+// } catch (JavaScriptModelException e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+ ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser.newParser(AST.JLS3);
+ parser.setSource(jsUnit);
+ ASTNode ast = parser.createAST(null);
+ ASTVisitor visitor = new MyNodeVisitor(jsUnit,/*className*/null , ast);
+ ast.accept(visitor);
+
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+ ////////////////////////////////////////////////////////////////////////////////
+
+
+
+
+
+
+
+
+
+
+
+
+
+//
+// try {
+// // TODO: check for dirty state first, and save all the opened documents
+// //then do the operation
+//
+// jsUnit.becomeWorkingCopy(monitor);
+// IJavaScriptElement[] children = jsUnit.getChildren();
+// for (IJavaScriptElement javaScriptElement : children) {
+// if(javaScriptElement instanceof SourceType){
+// SourceType st = (SourceType) javaScriptElement;
+// if(st.isClass()){
+// if(st.getDisplayName().equals(className)){
+// SourceTypeElementInfo info = (SourceTypeElementInfo) st.getElementInfo();
+// IJavaScriptElement[] infoChildren =info.getChildren();
+// boolean found = false;
+// SourceMethod method = null;
+// for (IJavaScriptElement javaScriptElement2 : infoChildren) {
+// if(javaScriptElement2 instanceof SourceMethod){
+// method = (SourceMethod) javaScriptElement2;
+// if(method.getDisplayName().equals("sayHello")){
+// found = true;
+// break;
+// }
+// }
+// }
+// if(found){
+//
+// IPath filePath = jsFile.getFullPath();
+// String toInsert = "dojo.connect(a ,b , c, d)";
+// int offset = method.getSourceRange().getOffset();
+// IBuffer source = jsUnit.getBuffer();
+//// source.replace(offset+1, 0, toInsert);
+//// st.createMethod("hello: function(){ var x=5 }", null, true, monitor);
+// //method.
+// IDocument document = getDocument(jsUnit);
+// CompilationUnitChange result = new CompilationUnitChange("Insert dojo connect", jsUnit);
+// AST cu;
+// if(jsUnit instanceof JavaScriptUnit){
+// cu =((JavaScriptUnit)jsUnit).getAST();
+// ASTRewrite rewriter = ASTRewrite.create(cu);
+//
+// }else {
+// ASTParser parser = ASTParser.newParser(AST.JLS3);
+// parser.setSource(document.get().toCharArray());
+// JavaScriptUnit cu = (JavaScriptUnit) parser.createAST(null);
+// cu = cu.getAST();
+// cu.getBodyChild();
+// List<Object> stmts = cu.statements();
+// ExpressionStatement stmt = (ExpressionStatement) stmts.get(2);
+//// stmt.get
+// for (Object object : stmts) {
+// object = object;
+// ((ExpressionStatement)object).getAST();
+// //ASTNodes.getBodyDeclarations((ExpressionStatement)object);
+// FunctionInvocation fi = (FunctionInvocation) (ASTNodes.getChildren((ExpressionStatement)object)).get(0);
+// fi.getBodyChild();
+//// fi.
+// }
+//
+// FunctionInvocation invocation = cu.newFunctionInvocation();
+// }
+//
+//
+//
+// //testing only...
+//// IJavaScriptElement[] elements = method.getChildren();
+//// String source = method.getSource();
+//// Object info1 = method.getElementInfo();
+//// IJavaScriptModel model = method.getJavaScriptModel();
+////// model = model;
+//// st.createMethod("myfun : function(){}", method, true, monitor);
+// }
+//// info = info;
+// }
+//
+// }
+// }
+// }
+//// String toInsert = "dojo.connect(a ,b , c, d)";
+//// int offset = 50;
+//// IBuffer source = jsUnit.getBuffer();
+//// source.replace(offset+1, 0, toInsert);
+//// IBuffer source = jsUnit.getBuffer();
+//// jsUnit.getBuffer().append("\n" + "var x;");
+// jsUnit.commitWorkingCopy(true, monitor);
+// jsUnit.discardWorkingCopy();
+// } catch (JavaScriptModelException e) {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// }
+
+
+
+ protected static IDocument getDocument(IJavaScriptUnit cu) throws JavaScriptModelException {
+ IBuffer buffer = cu.getBuffer();
+ if (buffer instanceof IDocument)
+ return (IDocument) buffer;
+ return new DocumentAdapter(buffer);
+ }
+
+ static ObjectLiteralField getObjectLiteralField(String source){
+ String js = "x.prototype = {" + source + "}";
+ JavaScriptUnit jsu = getAST(js);
+ ExpressionStatement stmt = (ExpressionStatement)jsu.statements().get(0);
+ ObjectLiteral lit = (ObjectLiteral)((Assignment) stmt.getExpression()).getRightHandSide();
+ ObjectLiteralField field = (ObjectLiteralField) lit.fields().get(0);
+ ASTNode node = ASTNode.copySubtree(jsu.getAST(), field);
+ return (ObjectLiteralField) node;
+ }
+
+ static JavaScriptUnit getAST(String source){
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(source.toCharArray());
+ ASTNode ast = parser.createAST(null);
+ return (JavaScriptUnit) ast;
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
+ * .ExecutionEvent)
+ */
+ public Object execute(final ExecutionEvent event) throws ExecutionException {
+ IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
+ if (!(activeEditor instanceof JavaEditor)) {
+ return null;
+ }
+ doExecute((IFile) HandlerUtil.getActiveEditor(event).getEditorInput().getAdapter(IFile.class));
+ return null;
+ }
+}
diff --git a/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/ShowTranslationHandler.java b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/ShowTranslationHandler.java
new file mode 100644
index 0000000..d2d9d43
--- /dev/null
+++ b/tests/org.eclipse.wst.jsdt.web.ui.tests/src/org/eclipse/wst/jsdt/web/ui/tests/translation/ShowTranslationHandler.java
@@ -0,0 +1,132 @@
+/*******************************************************************************
+ * Copyright (c) 2008 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.wst.jsdt.web.ui.tests.translation;
+
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IStorageEditorInput;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.progress.UIJob;
+import org.eclipse.ui.texteditor.AnnotationTypeLookup;
+import org.eclipse.ui.texteditor.ITextEditor;
+import org.eclipse.wst.jsdt.core.compiler.IProblem;
+import org.eclipse.wst.jsdt.ui.JavaScriptUI;
+import org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation;
+import org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter;
+import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
+
+
+/**
+ * Opens the current markup editor's current translated source in the JavaScript editor
+ *
+ * Invoke with M1+M2+8
+ *
+ * @author nitin
+ */
+public class ShowTranslationHandler extends AbstractHandler {
+
+ /**
+ *
+ */
+ public ShowTranslationHandler() {
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
+ * .ExecutionEvent)
+ */
+ public Object execute(final ExecutionEvent event) throws ExecutionException {
+ // IDE.openEditor(event.getApplicationContext(), createEditorInput(),
+ // JavaUI.ID_CU_EDITOR, true);
+ ISelection selection = HandlerUtil.getCurrentSelection(event);
+ if (selection instanceof IStructuredSelection) {
+ List list = ((IStructuredSelection) selection).toList();
+ if (!list.isEmpty()) {
+ if (list.get(0) instanceof IDOMNode) {
+ final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
+ INodeAdapter adapter = model.getDocument().getAdapterFor(IJsTranslation.class);
+ if (adapter != null) {
+ Job opener = new UIJob("Opening JavaScript Translation") {
+ public IStatus runInUIThread(IProgressMonitor monitor) {
+ JsTranslationAdapter translationAdapter = (JsTranslationAdapter) model.getDocument().getAdapterFor(IJsTranslation.class);
+ final IJsTranslation translation = translationAdapter.getJsTranslation(false);
+
+ // create an IEditorInput for the Java editor
+ final IStorageEditorInput input = new JSTranslationEditorInput(model);
+ try {
+ IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaScriptUI.ID_CU_EDITOR, true);
+ // Now add the problems we found
+ if (editor instanceof ITextEditor) {
+ IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
+ translation.reconcileCompilationUnit();
+ List problemsList = translation.getProblems();
+ IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
+ AnnotationTypeLookup lookup = new AnnotationTypeLookup();
+ for (int i = 0; i < problems.length; i++) {
+ int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
+ Position position = new Position(problems[i].getSourceStart(), length);
+ Annotation annotation = null;
+ String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
+ if (problems[i].isError()) {
+ type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
+ }
+ else if (problems[i].isWarning()) {
+ type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
+ }
+ annotation = new Annotation(type, false, problems[i].getMessage());
+ if (annotation != null) {
+ annotationModel.addAnnotation(annotation, position);
+ }
+ }
+ }
+ }
+ catch (PartInitException e) {
+ e.printStackTrace();
+ Display.getCurrent().beep();
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ opener.setSystem(false);
+ opener.setUser(true);
+ opener.schedule();
+ }
+ }
+ }
+ }
+ return null;
+ }
+}