KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ComboViewer


1 /*******************************************************************************
2  * Copyright (c) 2004-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  * Sebastian Davids - bug 69254
11  *******************************************************************************/

12
13 package org.eclipse.jface.viewers;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.CCombo;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
22 /**
23  * A concrete viewer based either on an SWT <code>Combo</code> control or <code>CCombo</code>
24  * control. This class is intended as an alternative to the JFace <code>ListViewer</code>, which displays
25  * its content in a combo box rather than a list. Wherever possible, this class attempts to behave
26  * like ListViewer. <p>
27  *
28  * This class is designed to be instantiated with a pre-existing SWT combo control
29  * and configured with a domain-specific content provider, label provider, element
30  * filter (optional), and element sorter (optional).
31  * </p>
32  *
33  * @see org.eclipse.jface.viewers.ListViewer
34  * @since 3.0
35  */

36 public final class ComboViewer extends AbstractListViewer {
37
38     /**
39      * This viewer's list control if this viewer is instantiated with a combo control; otherwise
40      * <code>null</code>.
41      *
42      * @see #ComboViewer(Combo)
43      */

44     private Combo combo;
45     
46     /**
47      * This viewer's list control if this viewer is instantiated with a CCombo control; otherwise
48      * <code>null</code>.
49      *
50      * @see #ComboViewer(CCombo)
51      * @since 3.3
52      */

53     private CCombo ccombo;
54
55     /**
56      * Creates a combo viewer on a newly-created combo control under the given parent.
57      * The viewer has no input, no content provider, a default label provider,
58      * no sorter, and no filters.
59      *
60      * @param parent the parent control
61      */

62     public ComboViewer(Composite parent) {
63         this(parent, SWT.READ_ONLY | SWT.BORDER);
64     }
65
66     /**
67      * Creates a combo viewer on a newly-created combo control under the given parent.
68      * The combo control is created using the given SWT style bits.
69      * The viewer has no input, no content provider, a default label provider,
70      * no sorter, and no filters.
71      *
72      * @param parent the parent control
73      * @param style the SWT style bits
74      */

75     public ComboViewer(Composite parent, int style) {
76         this(new Combo(parent, style));
77     }
78
79     /**
80      * Creates a combo viewer on the given combo control.
81      * The viewer has no input, no content provider, a default label provider,
82      * no sorter, and no filters.
83      *
84      * @param list the combo control
85      */

86     public ComboViewer(Combo list) {
87         this.combo = list;
88         hookControl(list);
89     }
90     
91     /**
92      * Creates a combo viewer on the given CCombo control.
93      * The viewer has no input, no content provider, a default label provider,
94      * no sorter, and no filters.
95      *
96      * @param list the CCombo control
97      * @since 3.3
98      */

99     public ComboViewer(CCombo list) {
100         this.ccombo = list;
101         hookControl(list);
102     }
103
104     protected void listAdd(String JavaDoc string, int index) {
105         if (combo == null) {
106             ccombo.add(string, index);
107         } else {
108             combo.add(string, index);
109         }
110     }
111
112     protected void listSetItem(int index, String JavaDoc string) {
113         if (combo == null) {
114             ccombo.setItem(index, string);
115         } else {
116             combo.setItem(index, string);
117         }
118     }
119
120     protected int[] listGetSelectionIndices() {
121         if (combo == null) {
122             return new int[] { ccombo.getSelectionIndex() };
123         } else {
124             return new int[] { combo.getSelectionIndex() };
125         }
126     }
127
128     protected int listGetItemCount() {
129         if (combo == null) {
130             return ccombo.getItemCount();
131         } else {
132             return combo.getItemCount();
133         }
134     }
135
136     protected void listSetItems(String JavaDoc[] labels) {
137         if (combo == null) {
138             ccombo.setItems(labels);
139         } else {
140             combo.setItems(labels);
141         }
142     }
143
144     protected void listRemoveAll() {
145         if (combo == null) {
146             ccombo.removeAll();
147         } else {
148             combo.removeAll();
149         }
150     }
151
152     protected void listRemove(int index) {
153         if (combo == null) {
154             ccombo.remove(index);
155         } else {
156             combo.remove(index);
157         }
158     }
159
160     /* (non-Javadoc)
161      * Method declared on Viewer.
162      */

163     public Control getControl() {
164         if (combo == null) {
165             return ccombo;
166         } else {
167             return combo;
168         }
169     }
170
171     /**
172      * Returns this list viewer's list control. If the viewer was not created on
173      * a CCombo control, some kind of unchecked exception is thrown.
174      *
175      * @return the list control
176      * @since 3.3
177      */

178     public CCombo getCCombo() {
179         Assert.isNotNull(ccombo);
180         return ccombo;
181     }
182
183     /**
184      * Returns this list viewer's list control. If the viewer was not created on
185      * a Combo control, some kind of unchecked exception is thrown.
186      *
187      * @return the list control
188      */

189     public Combo getCombo() {
190         Assert.isNotNull(combo);
191         return combo;
192     }
193     
194     /*
195      * Do nothing -- combos only display the selected element, so there is no way
196      * we can ensure that the given element is visible without changing the selection.
197      * Method defined on StructuredViewer.
198      */

199     public void reveal(Object JavaDoc element) {
200     }
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.jface.viewers.AbstractListViewer#listSetSelection(int[])
204      */

205     protected void listSetSelection(int[] ixs) {
206         if (combo == null) {
207             for (int idx = 0; idx < ixs.length; idx++) {
208                 ccombo.select(ixs[idx]);
209             }
210         } else {
211             for (int idx = 0; idx < ixs.length; idx++) {
212                 combo.select(ixs[idx]);
213             }
214         }
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.jface.viewers.AbstractListViewer#listDeselectAll()
219      */

220     protected void listDeselectAll() {
221         if (combo == null) {
222             ccombo.deselectAll();
223             ccombo.clearSelection();
224         } else {
225             combo.deselectAll();
226             combo.clearSelection();
227         }
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.jface.viewers.AbstractListViewer#listShowSelection()
232      */

233     protected void listShowSelection() {
234     }
235 }
Popular Tags