001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.template.contentassist;
011:
012: import java.util.Arrays;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.Assert;
017:
018: import org.eclipse.jface.text.templates.TemplateVariable;
019: import org.eclipse.jface.text.templates.TemplateVariableType;
020:
021: /**
022: * {@link MultiVariable}s can store multiple sets of data; the currently active set is determined
023: * by the active <em>key</em>. The key may be set via {@link #setKey(Object)}. Data sets are
024: * opaque {@link Object} arrays that are converted to the {@link String} values expected by
025: * {@link TemplateVariable} using {@link Object#toString() toString}. The
026: * {@link #getCurrentChoice() choice} of a master variable is the {@link #setKey(Object) key} for
027: * the slave variable.
028: */
029: public class MultiVariable extends TemplateVariable {
030: private static final Object DEFAULT_KEY = new Object();
031:
032: private final Map fValueMap = new HashMap(); // <Object, Object[]>
033: /** The master key defining the active set. */
034: private Object fKey;
035: /** The currently active object. */
036: private Object fCurrentChoice;
037:
038: public MultiVariable(TemplateVariableType type, String name,
039: int[] offsets) {
040: super (type, name, name, offsets);
041: fKey = DEFAULT_KEY;
042: fValueMap.put(fKey, new String[] { name });
043: fCurrentChoice = getChoices()[0];
044: }
045:
046: /**
047: * Sets the values of this variable under a specific key.
048: *
049: * @param key the key for which the values are valid
050: * @param values the possible values of this variable
051: */
052: public void setChoices(Object key, Object[] values) {
053: Assert.isNotNull(key);
054: Assert.isTrue(values.length > 0);
055: // no action when called from super ctor
056: if (fValueMap != null) {
057: fValueMap.put(key, values);
058: if (key.equals(fKey))
059: fCurrentChoice = getChoices()[0];
060: setResolved(true);
061: }
062: }
063:
064: public void setKey(Object defaultKey) {
065: Assert.isTrue(fValueMap.containsKey(defaultKey));
066: if (!fKey.equals(defaultKey)) {
067: fKey = defaultKey;
068: fCurrentChoice = getChoices()[0];
069: }
070: }
071:
072: public Object getCurrentChoice() {
073: return fCurrentChoice;
074: }
075:
076: public void setCurrentChoice(Object currentChoice) {
077: Assert.isTrue(Arrays.asList(getChoices()).contains(
078: currentChoice));
079: fCurrentChoice = currentChoice;
080: }
081:
082: /*
083: * @see org.eclipse.jface.text.templates.TemplateVariable#setValues(java.lang.String[])
084: */
085: public void setValues(String[] values) {
086: setChoices(values);
087: }
088:
089: public void setChoices(Object[] values) {
090: setChoices(DEFAULT_KEY, values);
091: }
092:
093: /*
094: * @see org.eclipse.jface.text.templates.TemplateVariable#getDefaultValue()
095: * @since 3.3
096: */
097: public String getDefaultValue() {
098: return toString(fCurrentChoice);
099: }
100:
101: public String toString(Object object) {
102: return object.toString();
103: }
104:
105: /*
106: * @see org.eclipse.jface.text.templates.TemplateVariable#getValues()
107: */
108: public String[] getValues() {
109: Object[] values = getChoices();
110: String[] result = new String[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[] 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[] getChoices(Object key) {
128: return (Object[]) fValueMap.get(key);
129: }
130:
131: public Object[][] getAllChoices() {
132: return (Object[][]) fValueMap.values().toArray(
133: new Object[fValueMap.size()][]);
134: }
135: }
|