Skip to main content
summaryrefslogtreecommitdiffstats
blob: 571e6f9a07752f7dea95343996e392660de26b2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * 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
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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
 *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 486903
 *******************************************************************************/
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.
 * <p>
 * Clients may instantiate the classes contained within this class.
 * </p>
 *
 * @since 3.0
 */
public class GlobalTemplateVariables {

	/** The type of the selection variables. */
	public static final String SELECTION= "selection"; //$NON-NLS-1$

	/**
	 * The cursor variable determines the cursor placement after template edition.
	 */
	public static class Cursor extends SimpleTemplateVariableResolver {

		/** Name of the cursor variable, value= {@value} */
		public static final String NAME= "cursor"; //$NON-NLS-1$

		/**
		 * Creates a new cursor variable
		 */
		public Cursor() {
			super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.cursor")); //$NON-NLS-1$
			setEvaluationString(""); //$NON-NLS-1$
		}
	}

	/**
	 * The word selection variable determines templates that work on a full
	 * lines selection.
	 */
	public static class WordSelection extends SimpleTemplateVariableResolver {

		/** Name of the word selection variable, value= {@value} */
		public static final String NAME= "word_selection"; //$NON-NLS-1$

		/**
		 * Creates a new word selection variable
		 */
		public WordSelection() {
			super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$
		}
		@Override
		protected String resolve(TemplateContext context) {
			String selection= context.getVariable(SELECTION);
			if (selection == null)
				return ""; //$NON-NLS-1$
			return selection;
		}

		@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:
				super.resolve(variable, context);
			}
		}

		private void resolveWithParams(TemplateVariable variable, TemplateContext context, List<String> params) {
			String selection= context.getVariable(SELECTION);
			if (selection != null) {
				variable.setValue(selection);
			} else {
				String defaultValue= params.get(0);
				variable.setValue(defaultValue);
			}
			variable.setUnambiguous(true);
			variable.setResolved(true);
		}

	}

	/**
	 * The line selection variable determines templates that work on selected
	 * lines.
	 */
	public static class LineSelection extends SimpleTemplateVariableResolver {

		/** Name of the line selection variable, value= {@value} */
		public static final String NAME= "line_selection"; //$NON-NLS-1$

		/**
		 * Creates a new line selection variable
		 */
		public LineSelection() {
			super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$
		}
		@Override
		protected String resolve(TemplateContext context) {
			String selection= context.getVariable(SELECTION);
			if (selection == null)
				return ""; //$NON-NLS-1$
			return selection;
		}
	}

	/**
	 * The dollar variable inserts an escaped dollar symbol.
	 */
	public static class Dollar extends SimpleTemplateVariableResolver {
		/**
		 * Creates a new dollar variable
		 */
		public Dollar() {
			super("dollar", TextTemplateMessages.getString("GlobalVariables.variable.description.dollar")); //$NON-NLS-1$ //$NON-NLS-2$
			setEvaluationString("$"); //$NON-NLS-1$
		}
	}

	/**
	 * 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 {
		/**
		 * Creates a new date variable
		 */
		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());
		}
	}

	/**
	 * The year variable evaluates to the current year.
	 */
	public static class Year extends SimpleTemplateVariableResolver {
		/**
		 * Creates a new year variable
		 */
		public Year() {
			super("year", TextTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$
		}
		@Override
		protected String resolve(TemplateContext context) {
			return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));
		}
	}

	/**
	 * The time variable evaluates to the current time.
	 */
	public static class Time extends SimpleTemplateVariableResolver {
		/**
		 * Creates a new time variable
		 */
		public Time() {
			super("time", TextTemplateMessages.getString("GlobalVariables.variable.description.time")); //$NON-NLS-1$ //$NON-NLS-2$
		}

		@Override
		protected String resolve(TemplateContext context) {
			return DateFormat.getTimeInstance().format(new java.util.Date());
		}
	}

	/**
	 * The user variable evaluates to the current user.
	 */
	public static class User extends SimpleTemplateVariableResolver {
		/**
		 * Creates a new user name variable
		 */
		public User() {
			super("user", TextTemplateMessages.getString("GlobalVariables.variable.description.user")); //$NON-NLS-1$ //$NON-NLS-2$
		}

		@Override
		protected String resolve(TemplateContext context) {
			return System.getProperty("user.name"); //$NON-NLS-1$
		}
	}
}

Back to the top