Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.text')
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java43
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties4
2 files changed, 43 insertions, 4 deletions
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

Back to the top