001: /*******************************************************************************
002: * Copyright (c) 2004-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: * Sebastian Davids - bug 69254
011: *******************************************************************************/package org.eclipse.jface.viewers;
012:
013: import org.eclipse.core.runtime.Assert;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.custom.CCombo;
016: import org.eclipse.swt.widgets.Combo;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019:
020: /**
021: * A concrete viewer based either on an SWT <code>Combo</code> control or <code>CCombo</code>
022: * control. This class is intended as an alternative to the JFace <code>ListViewer</code>, which displays
023: * its content in a combo box rather than a list. Wherever possible, this class attempts to behave
024: * like ListViewer. <p>
025: *
026: * This class is designed to be instantiated with a pre-existing SWT combo control
027: * and configured with a domain-specific content provider, label provider, element
028: * filter (optional), and element sorter (optional).
029: * </p>
030: *
031: * @see org.eclipse.jface.viewers.ListViewer
032: * @since 3.0
033: */
034: public final class ComboViewer extends AbstractListViewer {
035:
036: /**
037: * This viewer's list control if this viewer is instantiated with a combo control; otherwise
038: * <code>null</code>.
039: *
040: * @see #ComboViewer(Combo)
041: */
042: private Combo combo;
043:
044: /**
045: * This viewer's list control if this viewer is instantiated with a CCombo control; otherwise
046: * <code>null</code>.
047: *
048: * @see #ComboViewer(CCombo)
049: * @since 3.3
050: */
051: private CCombo ccombo;
052:
053: /**
054: * Creates a combo viewer on a newly-created combo control under the given parent.
055: * The viewer has no input, no content provider, a default label provider,
056: * no sorter, and no filters.
057: *
058: * @param parent the parent control
059: */
060: public ComboViewer(Composite parent) {
061: this (parent, SWT.READ_ONLY | SWT.BORDER);
062: }
063:
064: /**
065: * Creates a combo viewer on a newly-created combo control under the given parent.
066: * The combo control is created using the given SWT style bits.
067: * The viewer has no input, no content provider, a default label provider,
068: * no sorter, and no filters.
069: *
070: * @param parent the parent control
071: * @param style the SWT style bits
072: */
073: public ComboViewer(Composite parent, int style) {
074: this (new Combo(parent, style));
075: }
076:
077: /**
078: * Creates a combo viewer on the given combo control.
079: * The viewer has no input, no content provider, a default label provider,
080: * no sorter, and no filters.
081: *
082: * @param list the combo control
083: */
084: public ComboViewer(Combo list) {
085: this .combo = list;
086: hookControl(list);
087: }
088:
089: /**
090: * Creates a combo viewer on the given CCombo control.
091: * The viewer has no input, no content provider, a default label provider,
092: * no sorter, and no filters.
093: *
094: * @param list the CCombo control
095: * @since 3.3
096: */
097: public ComboViewer(CCombo list) {
098: this .ccombo = list;
099: hookControl(list);
100: }
101:
102: protected void listAdd(String string, int index) {
103: if (combo == null) {
104: ccombo.add(string, index);
105: } else {
106: combo.add(string, index);
107: }
108: }
109:
110: protected void listSetItem(int index, String string) {
111: if (combo == null) {
112: ccombo.setItem(index, string);
113: } else {
114: combo.setItem(index, string);
115: }
116: }
117:
118: protected int[] listGetSelectionIndices() {
119: if (combo == null) {
120: return new int[] { ccombo.getSelectionIndex() };
121: } else {
122: return new int[] { combo.getSelectionIndex() };
123: }
124: }
125:
126: protected int listGetItemCount() {
127: if (combo == null) {
128: return ccombo.getItemCount();
129: } else {
130: return combo.getItemCount();
131: }
132: }
133:
134: protected void listSetItems(String[] labels) {
135: if (combo == null) {
136: ccombo.setItems(labels);
137: } else {
138: combo.setItems(labels);
139: }
140: }
141:
142: protected void listRemoveAll() {
143: if (combo == null) {
144: ccombo.removeAll();
145: } else {
146: combo.removeAll();
147: }
148: }
149:
150: protected void listRemove(int index) {
151: if (combo == null) {
152: ccombo.remove(index);
153: } else {
154: combo.remove(index);
155: }
156: }
157:
158: /* (non-Javadoc)
159: * Method declared on Viewer.
160: */
161: public Control getControl() {
162: if (combo == null) {
163: return ccombo;
164: } else {
165: return combo;
166: }
167: }
168:
169: /**
170: * Returns this list viewer's list control. If the viewer was not created on
171: * a CCombo control, some kind of unchecked exception is thrown.
172: *
173: * @return the list control
174: * @since 3.3
175: */
176: public CCombo getCCombo() {
177: Assert.isNotNull(ccombo);
178: return ccombo;
179: }
180:
181: /**
182: * Returns this list viewer's list control. If the viewer was not created on
183: * a Combo control, some kind of unchecked exception is thrown.
184: *
185: * @return the list control
186: */
187: public Combo getCombo() {
188: Assert.isNotNull(combo);
189: return combo;
190: }
191:
192: /*
193: * Do nothing -- combos only display the selected element, so there is no way
194: * we can ensure that the given element is visible without changing the selection.
195: * Method defined on StructuredViewer.
196: */
197: public void reveal(Object element) {
198: }
199:
200: /* (non-Javadoc)
201: * @see org.eclipse.jface.viewers.AbstractListViewer#listSetSelection(int[])
202: */
203: protected void listSetSelection(int[] ixs) {
204: if (combo == null) {
205: for (int idx = 0; idx < ixs.length; idx++) {
206: ccombo.select(ixs[idx]);
207: }
208: } else {
209: for (int idx = 0; idx < ixs.length; idx++) {
210: combo.select(ixs[idx]);
211: }
212: }
213: }
214:
215: /* (non-Javadoc)
216: * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll()
217: */
218: protected void listDeselectAll() {
219: if (combo == null) {
220: ccombo.deselectAll();
221: ccombo.clearSelection();
222: } else {
223: combo.deselectAll();
224: combo.clearSelection();
225: }
226: }
227:
228: /* (non-Javadoc)
229: * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection()
230: */
231: protected void listShowSelection() {
232: }
233: }
|