KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > properties > ComboBoxPropertyDescriptor


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.ui.views.properties;
12
13 import org.eclipse.jface.viewers.CellEditor;
14 import org.eclipse.jface.viewers.ComboBoxCellEditor;
15 import org.eclipse.jface.viewers.ILabelProvider;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Composite;
18
19 /**
20  * Descriptor for a property that has a value which should be edited
21  * with a combo box cell editor. This class provides a default
22  * <code>ILabelProvider</code> that will render the label of the given
23  * descriptor as the <code>String</code> found in the labels array at the
24  * currently selected index.
25  * <p>
26  * The value of the property is a 0-based <code>Integer</code> index into
27  * the labels array.
28  * </p>
29  * <p>
30  * This class may be instantiated; it is not intended to be subclassed.
31  * </p>
32  * <p>
33  * Example:
34  * <pre>
35  * String[] values = {"Top left", "Top right", "Bottom left", "Bottom right"};
36  * IPropertyDescriptor pd = new ComboBoxPropertyDescriptor("origin", "Origin", values);
37  * </pre>
38  * </p>
39  */

40 public class ComboBoxPropertyDescriptor extends PropertyDescriptor {
41
42     /**
43      * The labels to display in the combo box
44      */

45     private String JavaDoc[] labels;
46
47     /**
48      * Creates an property descriptor with the given id, display name, and list
49      * of value labels to display in the combo box cell editor.
50      *
51      * @param id the id of the property
52      * @param displayName the name to display for the property
53      * @param labelsArray the labels to display in the combo box
54      */

55     public ComboBoxPropertyDescriptor(Object JavaDoc id, String JavaDoc displayName,
56             String JavaDoc[] labelsArray) {
57         super(id, displayName);
58         labels = labelsArray;
59     }
60
61     /**
62      * The <code>ComboBoxPropertyDescriptor</code> implementation of this
63      * <code>IPropertyDescriptor</code> method creates and returns a new
64      * <code>ComboBoxCellEditor</code>.
65      * <p>
66      * The editor is configured with the current validator if there is one.
67      * </p>
68      */

69     public CellEditor createPropertyEditor(Composite parent) {
70         CellEditor editor = new ComboBoxCellEditor(parent, labels,
71                 SWT.READ_ONLY);
72         if (getValidator() != null) {
73             editor.setValidator(getValidator());
74         }
75         return editor;
76     }
77
78     /**
79      * The <code>ComboBoxPropertyDescriptor</code> implementation of this
80      * <code>IPropertyDescriptor</code> method returns the value set by
81      * the <code>setProvider</code> method or, if no value has been set
82      * it returns a <code>ComboBoxLabelProvider</code> created from the
83      * valuesArray of this <code>ComboBoxPropertyDescriptor</code>.
84      *
85      * @see #setLabelProvider
86      */

87     public ILabelProvider getLabelProvider() {
88         if (isLabelProviderSet()) {
89             return super.getLabelProvider();
90         } else {
91             return new ComboBoxLabelProvider(labels);
92         }
93     }
94 }
95
Popular Tags