diff options
5 files changed, 182 insertions, 5 deletions
diff --git a/org.eclipse.text.tests/META-INF/MANIFEST.MF b/org.eclipse.text.tests/META-INF/MANIFEST.MF index 6c2c821847f..2df2f20f11d 100644 --- a/org.eclipse.text.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.text.tests/META-INF/MANIFEST.MF @@ -16,3 +16,5 @@ Require-Bundle: org.junit;bundle-version="3.8.2" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Eclipse-BundleShape: dir +Import-Package: com.ibm.icu.text, + com.ibm.icu.util diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java new file mode 100644 index 00000000000..08222f20817 --- /dev/null +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/GlobalTemplateVariablesDateTest.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright (c) 2016 BSI Business Systems Integration AG. + * 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: + * BSI Business Systems Integration AG - initial API and implementation + *******************************************************************************/ +package org.eclipse.text.tests.templates; + +import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.SimpleDateFormat; +import com.ibm.icu.util.ULocale; + +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.templates.DocumentTemplateContext; +import org.eclipse.jface.text.templates.GlobalTemplateVariables; +import org.eclipse.jface.text.templates.TemplateBuffer; +import org.eclipse.jface.text.templates.TemplateContextType; +import org.eclipse.jface.text.templates.TemplateTranslator; +import org.eclipse.jface.text.templates.TemplateVariable; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class GlobalTemplateVariablesDateTest extends TestCase { + public static Test suite() { + return new TestSuite(GlobalTemplateVariablesDateTest.class); + } + + private TemplateTranslator fTranslator; + + private DocumentTemplateContext fContext; + + private TemplateContextType fType; + + @Override + protected void setUp() throws Exception { + super.setUp(); + fTranslator= new TemplateTranslator(); + + fType= new TemplateContextType(); + fType.addResolver(new GlobalTemplateVariables.Date()); + + fContext= new DocumentTemplateContext(fType, new Document(), 0, 0); + } + + public void testWithoutParameter() throws Exception { + TemplateBuffer buffer= fTranslator.translate("Today is ${date}!"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("Today is "); + expected.append(DateFormat.getDateInstance().format(new java.util.Date())); + expected.append("!"); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + public void testOneParameter() throws Exception { + TemplateBuffer buffer= fTranslator.translate("This format ${d:date('dd MMM YYYY')} and not ${p:date('YYYY-MM-dd')}"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("This format "); + expected.append(new SimpleDateFormat("dd MMM YYYY").format(new java.util.Date())); + expected.append(" and not "); + expected.append(new SimpleDateFormat("YYYY-MM-dd").format(new java.util.Date())); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + public void testSimpleLocale() throws Exception { + TemplateBuffer buffer= fTranslator.translate("From ${d:date('dd MMM YYYY', 'fr')} to ${d}"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("From "); + expected.append(new SimpleDateFormat("dd MMM YYYY", ULocale.FRENCH).format(new java.util.Date())); + expected.append(" to "); + expected.append(new SimpleDateFormat("dd MMM YYYY", ULocale.FRENCH).format(new java.util.Date())); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + public void testComplexLocale() throws Exception { + TemplateBuffer buffer= fTranslator.translate("France ${d:date('EEEE dd MMMM YYYY', 'fr_FR')} and Germany ${p:date('EEEE dd. MMMM YYYY', 'de_DE')}"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("France "); + expected.append(new SimpleDateFormat("EEEE dd MMMM YYYY", ULocale.FRANCE).format(new java.util.Date())); + expected.append(" and Germany "); + expected.append(new SimpleDateFormat("EEEE dd. MMMM YYYY", ULocale.GERMANY).format(new java.util.Date())); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + public void testInvalidDateFormat() throws Exception { + TemplateBuffer buffer= fTranslator.translate("Today is ${d:date('invalid')}!"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("Today is "); + expected.append(DateFormat.getDateInstance().format(new java.util.Date())); + expected.append("!"); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + public void testInvalidLocale() throws Exception { + TemplateBuffer buffer= fTranslator.translate("Today is ${d:date('YYYY-MM-dd', 'this_invalid_locale')}!"); + fType.resolve(buffer, fContext); + + StringBuffer expected= new StringBuffer(); + expected.append("Today is "); + expected.append(new SimpleDateFormat("YYYY-MM-dd", new ULocale("this_invalid_locale")).format(new java.util.Date())); + expected.append("!"); + assertBufferStringAndVariables(expected.toString(), buffer); + } + + /** + * Ensures that {@link TemplateBuffer#getString()} equals the expected String and that all + * {@link TemplateBuffer#getVariables()} are resolved and unambiguous. + * + * @param expected expected result + * @param buffer the template buffer + */ + private static void assertBufferStringAndVariables(String expected, TemplateBuffer buffer) { + assertEquals(expected, buffer.getString()); + for (TemplateVariable v : buffer.getVariables()) { + assertTrue(v.isResolved()); + assertTrue(v.isUnambiguous()); + } + } +} diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java index b053e7dfdab..df495d5a4a1 100755 --- a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java +++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2016 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Jeremie Bresson <jbr@bsiag.com> - Allow to specify format for date variable - https://bugs.eclipse.org/75981 *******************************************************************************/ package org.eclipse.text.tests.templates; @@ -26,6 +27,7 @@ public class TemplatesTestSuite { TestSuite suite = new TestSuite("Test Suite for org.eclipse.jface.text.templates"); //$NON-NLS-1$ //$JUnit-BEGIN$ suite.addTest(TemplateTranslatorTest.suite()); + suite.addTest(GlobalTemplateVariablesDateTest.suite()); //$JUnit-END$ return suite; diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java index 3a3b4d2d9a3..9eb4710464e 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2016 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 @@ -8,11 +8,16 @@ * Contributors: * IBM Corporation - initial API and implementation * Sebastian Davids: sdavids@gmx.de - see bug 25376 + * Jeremie Bresson <jbr@bsiag.com> - Allow to specify format for date variable - https://bugs.eclipse.org/75981 *******************************************************************************/ package org.eclipse.jface.text.templates; +import java.util.List; + import com.ibm.icu.text.DateFormat; +import com.ibm.icu.text.SimpleDateFormat; import com.ibm.icu.util.Calendar; +import com.ibm.icu.util.ULocale; /** * Global variables which are available in any context. @@ -106,7 +111,10 @@ public class GlobalTemplateVariables { } /** - * The date variable evaluates to the current date. + * The date variable evaluates to the current date. This supports a <code>pattern</code> and a + * <code>locale</code> as optional parameters. <code>pattern</code> is a pattern compatible with + * {@link SimpleDateFormat}. <code>locale</code> is a string representation of the locale + * compatible with the constructor parameter {@link ULocale#ULocale(String)}. */ public static class Date extends SimpleTemplateVariableResolver { /** @@ -115,6 +123,37 @@ public class GlobalTemplateVariables { public Date() { super("date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$ } + + @Override + public void resolve(TemplateVariable variable, TemplateContext context) { + List<String> params= variable.getVariableType().getParams(); + if (params.size() >= 1 && params.get(0) != null) { + resolveWithParams(variable, context, params); + } else { + // No parameter, use default format: + super.resolve(variable, context); + } + } + + private void resolveWithParams(TemplateVariable variable, TemplateContext context, List<String> params) { + try { + // There is a least one parameter (params.get(0) is not null), set the format depending on second parameter: + DateFormat format; + if (params.size() >= 2 && params.get(1) != null) { + format= new SimpleDateFormat(params.get(0), new ULocale(params.get(1))); + } else { + format= new SimpleDateFormat(params.get(0)); + } + + variable.setValue(format.format(new java.util.Date())); + variable.setUnambiguous(true); + variable.setResolved(true); + } catch (IllegalArgumentException e) { + // Date formating did not work, use default format instead: + super.resolve(variable, context); + } + } + @Override protected String resolve(TemplateContext context) { return DateFormat.getDateInstance().format(new java.util.Date()); diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties b/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties index a2549f0454e..dcba54e91df 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties +++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2008 IBM Corporation and others. +# Copyright (c) 2000, 2016 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 @@ -17,7 +17,7 @@ TemplateTranslator.error.incompatible.type=Template variable ''{0}'' has incompa # global variables GlobalVariables.variable.description.cursor=The cursor position after editing template variables GlobalVariables.variable.description.dollar=The dollar symbol -GlobalVariables.variable.description.date=Current date +GlobalVariables.variable.description.date=Current date. Use ${id\:date[(format[, locale])]} to format the date. GlobalVariables.variable.description.year=Current year GlobalVariables.variable.description.time=Current time GlobalVariables.variable.description.user=User name |