01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.templateeditor.template;
11:
12: import java.util.Arrays;
13: import java.util.Comparator;
14:
15: import org.eclipse.jface.text.templates.TemplateContext;
16: import org.eclipse.jface.text.templates.TemplateVariableResolver;
17:
18: /**
19: * Looks up existing ant variables and proposes them. The proposals are sorted by
20: * their prefix-likeness with the variable type.
21: */
22: public class AntVariableResolver extends TemplateVariableResolver {
23: /*
24: * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolveAll(org.eclipse.jface.text.templates.TemplateContext)
25: */
26: protected String[] resolveAll(TemplateContext context) {
27: String[] proposals = new String[] { "${srcDir}", "${dstDir}" }; //$NON-NLS-1$ //$NON-NLS-2$
28:
29: Arrays.sort(proposals, new Comparator() {
30:
31: public int compare(Object o1, Object o2) {
32: return getCommonPrefixLength(getType(), (String) o2)
33: - getCommonPrefixLength(getType(), (String) o1);
34: }
35:
36: private int getCommonPrefixLength(String type, String var) {
37: int i = 0;
38: CharSequence vSeq = var
39: .subSequence(2, var.length() - 1); // strip away ${}
40: while (i < type.length() && i < vSeq.length())
41: if (Character.toLowerCase(type.charAt(i)) == Character
42: .toLowerCase(vSeq.charAt(i)))
43: i++;
44: else
45: break;
46: return i;
47: }
48: });
49:
50: return proposals;
51: }
52: }
|