KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > template > contentassist > MultiVariable


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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.template.contentassist;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.text.templates.TemplateVariable;
20 import org.eclipse.jface.text.templates.TemplateVariableType;
21
22
23 /**
24  * {@link MultiVariable}s can store multiple sets of data; the currently active set is determined
25  * by the active <em>key</em>. The key may be set via {@link #setKey(Object)}. Data sets are
26  * opaque {@link Object} arrays that are converted to the {@link String} values expected by
27  * {@link TemplateVariable} using {@link Object#toString() toString}. The
28  * {@link #getCurrentChoice() choice} of a master variable is the {@link #setKey(Object) key} for
29  * the slave variable.
30  */

31 public class MultiVariable extends TemplateVariable {
32     private static final Object JavaDoc DEFAULT_KEY= new Object JavaDoc();
33     
34     private final Map JavaDoc fValueMap= new HashMap JavaDoc(); // <Object, Object[]>
35
/** The master key defining the active set. */
36     private Object JavaDoc fKey;
37     /** The currently active object. */
38     private Object JavaDoc fCurrentChoice;
39
40     public MultiVariable(TemplateVariableType type, String JavaDoc name, int[] offsets) {
41         super(type, name, name, offsets);
42         fKey= DEFAULT_KEY;
43         fValueMap.put(fKey, new String JavaDoc[] { name });
44         fCurrentChoice= getChoices()[0];
45     }
46
47     /**
48      * Sets the values of this variable under a specific key.
49      *
50      * @param key the key for which the values are valid
51      * @param values the possible values of this variable
52      */

53     public void setChoices(Object JavaDoc key, Object JavaDoc[] values) {
54         Assert.isNotNull(key);
55         Assert.isTrue(values.length > 0);
56         // no action when called from super ctor
57
if (fValueMap != null) {
58             fValueMap.put(key, values);
59             if (key.equals(fKey))
60                 fCurrentChoice= getChoices()[0];
61             setResolved(true);
62         }
63     }
64
65     public void setKey(Object JavaDoc defaultKey) {
66         Assert.isTrue(fValueMap.containsKey(defaultKey));
67         if (!fKey.equals(defaultKey)) {
68             fKey= defaultKey;
69             fCurrentChoice= getChoices()[0];
70         }
71     }
72     
73     public Object JavaDoc getCurrentChoice() {
74         return fCurrentChoice;
75     }
76     
77     public void setCurrentChoice(Object JavaDoc currentChoice) {
78         Assert.isTrue(Arrays.asList(getChoices()).contains(currentChoice));
79         fCurrentChoice= currentChoice;
80     }
81
82     /*
83      * @see org.eclipse.jface.text.templates.TemplateVariable#setValues(java.lang.String[])
84      */

85     public void setValues(String JavaDoc[] values) {
86         setChoices(values);
87     }
88     
89     public void setChoices(Object JavaDoc[] values) {
90         setChoices(DEFAULT_KEY, values);
91     }
92     
93     /*
94      * @see org.eclipse.jface.text.templates.TemplateVariable#getDefaultValue()
95      * @since 3.3
96      */

97     public String JavaDoc getDefaultValue() {
98         return toString(fCurrentChoice);
99     }
100
101     public String JavaDoc toString(Object JavaDoc object) {
102         return object.toString();
103     }
104
105     /*
106      * @see org.eclipse.jface.text.templates.TemplateVariable#getValues()
107      */

108     public String JavaDoc[] getValues() {
109         Object JavaDoc[] values= getChoices();
110         String JavaDoc[] result= new String JavaDoc[values.length];
111         for (int i= 0; i < result.length; i++)
112             result[i]= toString(values[i]);
113         return result;
114     }
115     
116     public Object JavaDoc[] getChoices() {
117         return getChoices(fKey);
118     }
119
120     /**
121      * Returns the choices for the set identified by <code>key</code>.
122      *
123      * @param key the key
124      * @return the choices for this variable and the given set, or
125      * <code>null</code> if the set is not defined.
126      */

127     public Object JavaDoc[] getChoices(Object JavaDoc key) {
128         return (Object JavaDoc[]) fValueMap.get(key);
129     }
130
131     public Object JavaDoc[][] getAllChoices() {
132         return (Object JavaDoc[][]) fValueMap.values().toArray(new Object JavaDoc[fValueMap.size()][]);
133     }
134 }
135
Popular Tags