diff options
author | Jeremie Bresson | 2016-01-23 07:27:38 +0000 |
---|---|---|
committer | Jeremie Bresson | 2016-01-23 07:27:38 +0000 |
commit | b15d692552891c96627cde1a8652ae9216b57e99 (patch) | |
tree | 5306a1042482b19b0aee3105dc14b01ad190fd53 /org.eclipse.text/src/org | |
parent | c87d70c7a9444b9248ad407b0753bc62a0695518 (diff) | |
download | eclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.tar.gz eclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.tar.xz eclipse.platform.text-b15d692552891c96627cde1a8652ae9216b57e99.zip |
Bug 75981: [templates][preferences] Allow to specify format of date variable in templates
https://bugs.eclipse.org/bugs/show_bug.cgi?id=75981
Change-Id: I8d1e2661d8698e47ea92963ba6caab0edaf8ab31
Signed-off-by: Dani Megert <Daniel_Megert@ch.ibm.com>
Signed-off-by: Jeremie Bresson <jbr@bsiag.com>
Signed-off-by: Pramod Goyal <pramod.goyal@gmail.com>
Signed-off-by: Thomas Reinhardt <thomas@reinhardt.com>
Diffstat (limited to 'org.eclipse.text/src/org')
-rw-r--r-- | org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java | 43 | ||||
-rw-r--r-- | org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties | 4 |
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 |