KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > templates > GlobalTemplateVariables


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids: sdavids@gmx.de - see bug 25376
11  *******************************************************************************/

12 package org.eclipse.jface.text.templates;
13
14 import com.ibm.icu.text.DateFormat;
15 import com.ibm.icu.util.Calendar;
16
17 /**
18  * Global variables which are available in any context.
19  * <p>
20  * Clients may instantiate the classes contained within this class.
21  * </p>
22  *
23  * @since 3.0
24  */

25 public class GlobalTemplateVariables {
26
27     /** The type of the selection variables. */
28     public static final String JavaDoc SELECTION= "selection"; //$NON-NLS-1$
29

30     /**
31      * The cursor variable determines the cursor placement after template edition.
32      */

33     public static class Cursor extends SimpleTemplateVariableResolver {
34
35         /** Name of the cursor variable, value= {@value} */
36         public static final String JavaDoc NAME= "cursor"; //$NON-NLS-1$
37

38         /**
39          * Creates a new cursor variable
40          */

41         public Cursor() {
42             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.cursor")); //$NON-NLS-1$
43
setEvaluationString(""); //$NON-NLS-1$
44
}
45     }
46
47     /**
48      * The word selection variable determines templates that work on a full
49      * lines selection.
50      */

51     public static class WordSelection extends SimpleTemplateVariableResolver {
52
53         /** Name of the word selection variable, value= {@value} */
54         public static final String JavaDoc NAME= "word_selection"; //$NON-NLS-1$
55

56         /**
57          * Creates a new word selection variable
58          */

59         public WordSelection() {
60             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$
61
}
62         protected String JavaDoc resolve(TemplateContext context) {
63             String JavaDoc selection= context.getVariable(SELECTION);
64             if (selection == null)
65                 return ""; //$NON-NLS-1$
66
return selection;
67         }
68     }
69
70     /**
71      * The line selection variable determines templates that work on selected
72      * lines.
73      */

74     public static class LineSelection extends SimpleTemplateVariableResolver {
75
76         /** Name of the line selection variable, value= {@value} */
77         public static final String JavaDoc NAME= "line_selection"; //$NON-NLS-1$
78

79         /**
80          * Creates a new line selection variable
81          */

82         public LineSelection() {
83             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$
84
}
85         protected String JavaDoc resolve(TemplateContext context) {
86             String JavaDoc selection= context.getVariable(SELECTION);
87             if (selection == null)
88                 return ""; //$NON-NLS-1$
89
return selection;
90         }
91     }
92
93     /**
94      * The dollar variable inserts an escaped dollar symbol.
95      */

96     public static class Dollar extends SimpleTemplateVariableResolver {
97         /**
98          * Creates a new dollar variable
99          */

100         public Dollar() {
101             super("dollar", TextTemplateMessages.getString("GlobalVariables.variable.description.dollar")); //$NON-NLS-1$ //$NON-NLS-2$
102
setEvaluationString("$"); //$NON-NLS-1$
103
}
104     }
105
106     /**
107      * The date variable evaluates to the current date.
108      */

109     public static class Date extends SimpleTemplateVariableResolver {
110         /**
111          * Creates a new date variable
112          */

113         public Date() {
114             super("date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$
115
}
116         protected String JavaDoc resolve(TemplateContext context) {
117             return DateFormat.getDateInstance().format(new java.util.Date JavaDoc());
118         }
119     }
120
121     /**
122      * The year variable evaluates to the current year.
123      */

124     public static class Year extends SimpleTemplateVariableResolver {
125         /**
126          * Creates a new year variable
127          */

128         public Year() {
129             super("year", TextTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$
130
}
131         protected String JavaDoc resolve(TemplateContext context) {
132             return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));
133         }
134     }
135
136     /**
137      * The time variable evaluates to the current time.
138      */

139     public static class Time extends SimpleTemplateVariableResolver {
140         /**
141          * Creates a new time variable
142          */

143         public Time() {
144             super("time", TextTemplateMessages.getString("GlobalVariables.variable.description.time")); //$NON-NLS-1$ //$NON-NLS-2$
145
}
146
147         /**
148          * {@inheritDoc}
149          */

150         protected String JavaDoc resolve(TemplateContext context) {
151             return DateFormat.getTimeInstance().format(new java.util.Date JavaDoc());
152         }
153     }
154
155     /**
156      * The user variable evaluates to the current user.
157      */

158     public static class User extends SimpleTemplateVariableResolver {
159         /**
160          * Creates a new user name variable
161          */

162         public User() {
163             super("user", TextTemplateMessages.getString("GlobalVariables.variable.description.user")); //$NON-NLS-1$ //$NON-NLS-2$
164
}
165
166         /**
167          * {@inheritDoc}
168          */

169         protected String JavaDoc resolve(TemplateContext context) {
170             return System.getProperty("user.name"); //$NON-NLS-1$
171
}
172     }
173 }
174
Popular Tags