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.ui.views.properties;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.viewers.CellEditor;
014: import org.eclipse.jface.viewers.ICellEditorValidator;
015: import org.eclipse.jface.viewers.ILabelProvider;
016: import org.eclipse.jface.viewers.LabelProvider;
017: import org.eclipse.swt.widgets.Composite;
018:
019: /**
020: * Standard implementation for property descriptors.
021: * <p>
022: * The required attributes of property descriptors (id and display name) are
023: * passed to the constructor; the optional attributes can be configured using
024: * the various set methods (all have reasonable default values):
025: * <ul>
026: * <li><code>setDescription</code></li>
027: * <li><code>setCategory</code></li>
028: * <li><code>setLabelProvider</code></li>
029: * <li><code>setHelpContexts</code></li>
030: * </ul>
031: * Subclasses should reimplement <code>getPropertyEditor</code> to provide a
032: * cell editor for changing the value; otherwise the property will be
033: * effectively read only.
034: * </p>
035: * <p>
036: * There are several concrete subclasses provided in this package that cover
037: * the most common cases:
038: * <ul>
039: * <li><code>TextPropertyDescriptor</code> - edits with a
040: * <code>TextCellEditor</code></li>
041: * <li><code>ComboBoxPropertyDescriptor - edits with a
042: * <code>ComboBoxCellEditor</code></code></li>
043: * <li><code>ColorPropertyDescriptor - edits with a
044: * <code>ColorCellEditor</code></code></li>
045: * </ul>
046: * </p>
047: */
048: public class PropertyDescriptor implements IPropertyDescriptor {
049:
050: /**
051: * The property id.
052: */
053: private Object id;
054:
055: /**
056: * The name to display for the property.
057: */
058: private String display;
059:
060: /**
061: * Category name, or <code>null</code> if none (the default).
062: */
063: private String category = null;
064:
065: /**
066: * Description of the property, or <code>null</code> if none (the default).
067: */
068: private String description = null;
069:
070: /**
071: * The help context ids, or <code>null</code> if none (the default).
072: */
073: private Object helpIds;
074:
075: /**
076: * The flags used to filter the property.
077: */
078: private String[] filterFlags;
079:
080: /**
081: * The object that provides the property value's text and image, or
082: * <code>null</code> if the default label provider is used (the default).
083: */
084: private ILabelProvider labelProvider = null;
085:
086: /**
087: * The object to validate the values in the cell editor, or <code>null</code>
088: * if none (the default).
089: */
090: private ICellEditorValidator validator;
091:
092: /**
093: * Indicates if the descriptor is compatible with other descriptors of this
094: * type. <code>false</code> by default.
095: */
096: private boolean incompatible = false;
097:
098: /**
099: * Creates a new property descriptor with the given id and display name
100: */
101: public PropertyDescriptor(Object id, String displayName) {
102: Assert.isNotNull(id);
103: Assert.isNotNull(displayName);
104: this .id = id;
105: this .display = displayName;
106: }
107:
108: /**
109: * The <code>PropertyDescriptor</code> implementation of this
110: * <code>IPropertyDescriptor</code> method returns <code>null</code>.
111: * <p>
112: * Since no cell editor is returned, the property is read only.
113: * </p>
114: */
115: public CellEditor createPropertyEditor(Composite parent) {
116: return null;
117: }
118:
119: /**
120: * Returns <code>true</code> if this property descriptor is to be always
121: * considered incompatible with any other property descriptor.
122: * This prevents a property from displaying during multiple
123: * selection.
124: *
125: * @return <code>true</code> to indicate always incompatible
126: */
127: protected boolean getAlwaysIncompatible() {
128: return incompatible;
129: }
130:
131: /**
132: * The <code>PropertyDescriptor</code> implementation of this
133: * <code>IPropertyDescriptor</code> method returns the value set by
134: * the <code>setCategory</code> method. If unset, this method returns
135: * <code>null</code> indicating the default category.
136: *
137: * @see #setCategory
138: */
139: public String getCategory() {
140: return category;
141: }
142:
143: /**
144: * The <code>PropertyDescriptor</code> implementation of this
145: * <code>IPropertyDescriptor</code> method returns the value set by
146: * the <code>setDescription</code> method. If unset, this method returns
147: * <code>null</code> indicating no description.
148: *
149: * @see #setDescription
150: */
151: public String getDescription() {
152: return description;
153: }
154:
155: /**
156: * The <code>SimplePropertyDescriptor</code> implementation of this
157: * <code>IPropertyDescriptor</code> method returns the value supplied
158: * on the constructor.
159: */
160: public String getDisplayName() {
161: return display;
162: }
163:
164: /**
165: * The <code>SimplePropertyDescriptor</code> implementation of this
166: * <code>IPropertyDescriptor</code> method returns the value set by
167: * the <code>setFilterFlags</code> method. If unset, this method returns
168: * <code>null</code>.
169: * <p>
170: * Valid values for these flags are declared as constants on
171: * <code>IPropertySheetEntry</code>
172: */
173: public String[] getFilterFlags() {
174: return filterFlags;
175: }
176:
177: /**
178: * The <code>SimplePropertyDescriptor</code> implementation of this
179: * <code>IPropertyDescriptor</code> method returns the value set by
180: * the <code>setHelpContextId</code> method. If unset, this method returns
181: * <code>null</code>.
182: *
183: * @see #setHelpContextIds
184: */
185: public Object getHelpContextIds() {
186: return helpIds;
187: }
188:
189: /**
190: * The <code>PropertyDescriptor</code> implementation of this
191: * <code>IPropertyDescriptor</code> method returns the value supplied
192: * on the constructor.
193: */
194: public Object getId() {
195: return id;
196: }
197:
198: /**
199: * The <code>PropertyDescriptor</code> implementation of this
200: * <code>IPropertyDescriptor</code> method returns the value set by
201: * the <code>setProvider</code> method or, if no value has been set
202: * it returns a <code>LabelProvider</code>
203: *
204: * @see #setLabelProvider
205: */
206: public ILabelProvider getLabelProvider() {
207: if (labelProvider != null) {
208: return labelProvider;
209: } else {
210: return new LabelProvider();
211: }
212: }
213:
214: /**
215: * Returns the input validator for editing the property.
216: *
217: * @return the validator used to verify correct values for this property,
218: * or <code>null</code>
219: */
220: protected ICellEditorValidator getValidator() {
221: return validator;
222: }
223:
224: /**
225: * Returns whether a label provider has been set on the receiver.
226: * @return whether a label provider has been set on the receiver.
227: * @see #setLabelProvider
228: * @since 3.0
229: */
230: public boolean isLabelProviderSet() {
231: return labelProvider != null;
232: }
233:
234: /**
235: * The <code>SimplePropertyDescriptor</code> implementation of this
236: * <code>IPropertyDescriptor</code> method returns true if the other
237: * property has the same id and category and <code>getAlwaysIncompatible()</code>
238: * returns false
239: */
240: public boolean isCompatibleWith(IPropertyDescriptor anotherProperty) {
241: if (getAlwaysIncompatible()) {
242: return false;
243: }
244:
245: // Compare id
246: Object id1 = getId();
247: Object id2 = anotherProperty.getId();
248: if (!id1.equals(id2)) {
249: return false;
250: }
251:
252: // Compare Category (may be null)
253: if (getCategory() == null) {
254: if (anotherProperty.getCategory() != null) {
255: return false;
256: }
257: } else {
258: if (!getCategory().equals(anotherProperty.getCategory())) {
259: return false;
260: }
261: }
262:
263: return true;
264: }
265:
266: /**
267: * Sets a flag indicating whether this property descriptor is to be always
268: * considered incompatible with any other property descriptor.
269: * Setting this flag prevents a property from displaying during multiple
270: * selection.
271: *
272: * @param flag <code>true</code> to indicate always incompatible
273: */
274: public void setAlwaysIncompatible(boolean flag) {
275: incompatible = flag;
276: }
277:
278: /**
279: * Sets the category for this property descriptor.
280: *
281: * @param category the category for the descriptor, or <code>null</code> if none
282: * @see #getCategory
283: */
284: public void setCategory(String category) {
285: this .category = category;
286: }
287:
288: /**
289: * Sets the description for this property descriptor.
290: * The description should be limited to a single line so that it can be
291: * displayed in the status line.
292: *
293: * @param description the description, or <code>null</code> if none
294: * @see #getDescription
295: */
296: public void setDescription(String description) {
297: this .description = description;
298: }
299:
300: /**
301: * Sets the the filter flags for this property descriptor.
302: * The description should be limited to a single line so that it can be
303: * displayed in the status line.
304: * <p>
305: * Valid values for these flags are declared as constants on
306: * <code>IPropertySheetEntry</code>
307: * </p>
308: *
309: * @param value the filter flags
310: * @see #getFilterFlags
311: */
312: public void setFilterFlags(String value[]) {
313: filterFlags = value;
314: }
315:
316: /**
317: * Sets the help context id for this property descriptor.
318: *
319: * NOTE: Help support system API's changed since 2.0 and arrays
320: * of contexts are no longer supported.
321: * </p>
322: * <p>
323: * Thus the only valid parameter type for this method
324: * is a <code>String</code> representing a context id.
325: * The previously valid parameter types are deprecated.
326: * The plural name for this method is unfortunate.
327: * </p>
328: *
329: * @param contextIds the help context ids, or <code>null</code> if none
330: * @see #getHelpContextIds
331: */
332: public void setHelpContextIds(Object contextIds) {
333: helpIds = contextIds;
334: }
335:
336: /**
337: * Sets the label provider for this property descriptor.
338: * <p>
339: * If no label provider is set an instance of <code>LabelProvider</code>
340: * will be created as the default when needed.
341: * </p>
342: *
343: * @param provider the label provider for the descriptor, or
344: * <code>null</code> if the default label provider should be used
345: * @see #getLabelProvider
346: */
347: public void setLabelProvider(ILabelProvider provider) {
348: labelProvider = provider;
349: }
350:
351: /**
352: * Sets the input validator for the cell editor for this property descriptor.
353: * <p>
354: * [Issue: This method should be unnecessary is the cell editor's own
355: * validator is used.
356: * ]
357: * </p>
358: *
359: * @param validator the cell input validator, or <code>null</code> if none
360: */
361: public void setValidator(ICellEditorValidator validator) {
362: this.validator = validator;
363: }
364: }
|