Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1bbaa02c62f75060da062ca9dee3954261d0554f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.properties.celleditors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wst.sse.ui.internal.Logger;

/**
 * A simple ComboBoxCellEditor, which allow value and display string to be
 * different.
 * 
 * @author mengbo
 */
public class LabeledComboBoxCellEditor extends ComboBoxCellEditor {
	private boolean _fSettingValue = false;

	private Object[] _values;

	public static LabeledComboBoxCellEditor newInstance(Composite parent,
			Map valueLabelMap, int style) {
		// we'll sort according to label. since that is what being show to user.
		List list = new ArrayList();
		for (Iterator iter = valueLabelMap.keySet().iterator(); iter.hasNext();) {
			Object key = iter.next();
			String label = (String) valueLabelMap.get(key);
			list.add(new Object[] { key, label });
		}
		// sort by label
		Collections.sort(list, new Comparator() {
			public int compare(Object o1, Object o2) {
				String label1 = (String) ((Object[]) o1)[1];
				String label2 = (String) ((Object[]) o2)[1];
				return label1.compareTo(label2);
			}
		});
		Object[] values = new Object[list.size()];
		String[] labels = new String[list.size()];
		for (int i = 0, n = list.size(); i < n; i++) {
			values[i] = ((Object[]) list.get(i))[0];
			labels[i] = (String) ((Object[]) list.get(i))[1];
		}
		return new LabeledComboBoxCellEditor(parent, values, labels, style);
	}

	/**
	 * @param parent
	 * @param items
	 */
	public LabeledComboBoxCellEditor(Composite parent, Object[] values,
			String[] labels) {
		this(parent, values, labels, SWT.NONE);
	}

	/**
	 * @param parent
	 * @param items
	 * @param style
	 */
	public LabeledComboBoxCellEditor(Composite parent, Object[] values,
			String[] labels, int style) {
		super(parent, labels, style);
		_values = values;
	}

	protected Object doGetValue() {
		// otherwise limits to set of valid values
		Object index = super.doGetValue();
		int selection = -1;
		if (index instanceof Integer) {
			selection = ((Integer) index).intValue();
		}
		if (selection >= 0) {
			return _values[selection];
		} else if (getControl() instanceof CCombo) {
			// retrieve the actual text as the list of valid items doesn't
			// contain the value
			return ((CCombo) getControl()).getText();
		}
		return null;
	}

	protected void doSetValue(Object value) {
		if (_fSettingValue) {
			return;
		}
		_fSettingValue = true;
		if (value instanceof Integer) {
			super.doSetValue(value);
		} else {
			String stringValue = value.toString();
			int selection = -1;
			for (int i = 0; i < _values.length; i++) {
				if (_values[i].equals(stringValue)) {
					selection = i;
				}
			}
			if (selection >= 0) {
				super.doSetValue(new Integer(selection));
			} else {
				super.doSetValue(new Integer(-1));
				if (getControl() instanceof CCombo
						&& !stringValue.equals(((CCombo) getControl())
								.getText())) {
					// update the Text widget
					((CCombo) getControl()).setText(stringValue);
				}
			}
		}
		_fSettingValue = false;
	}

	public void setItems(String[] newItems) {
		if (getControl() == null || getControl().isDisposed()) {
			Logger.log(Logger.ERROR,
					"Attempted to update item list for disposed cell editor"); //$NON-NLS-1$
			return;
		}

		// keep selection if possible
		Object previousSelectedValue = getValue();
		super.setItems(newItems);
		if (previousSelectedValue != null && getControl() instanceof CCombo) {
			for (int i = 0; i < newItems.length; i++) {
				if (newItems[i].equals(previousSelectedValue)) {
					setValue(previousSelectedValue);
				}
			}
		}
	}
}

Back to the top