KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > templates > AntVariableResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.templates;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.jface.text.templates.TemplateContext;
17 import org.eclipse.jface.text.templates.TemplateVariableResolver;
18
19 /**
20  * Looks up existing ant variables and proposes them. The proposals are sorted by
21  * their prefix-likeness with the variable type.
22  */

23 public class AntVariableResolver extends TemplateVariableResolver {
24     /*
25      * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolveAll(org.eclipse.jface.text.templates.TemplateContext)
26      */

27     protected String JavaDoc[] resolveAll(TemplateContext context) {
28         String JavaDoc[] proposals= new String JavaDoc[] { "${srcDir}", "${dstDir}" }; //$NON-NLS-1$ //$NON-NLS-2$
29

30         Arrays.sort(proposals, new Comparator JavaDoc() {
31
32             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
33                 return getCommonPrefixLength(getType(), (String JavaDoc) o2) - getCommonPrefixLength(getType(), (String JavaDoc) o1);
34             }
35
36             private int getCommonPrefixLength(String JavaDoc type, String JavaDoc var) {
37                 int i= 0;
38                 CharSequence JavaDoc vSeq= var.subSequence(2, var.length() - 1); // strip away ${}
39
while (i < type.length() && i < vSeq.length())
40                     if (Character.toLowerCase(type.charAt(i)) == Character.toLowerCase(vSeq.charAt(i)))
41                         i++;
42                     else
43                         break;
44                 return i;
45             }
46         });
47         
48         return proposals;
49     }
50 }
51
Popular Tags