001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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: * Tom Shindl <tom.schindl@bestsolution.at> - initial API and implementation
011: * fix for bug 163317, 201905
012: *******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import org.eclipse.jface.util.Policy;
015: import org.eclipse.swt.events.DisposeEvent;
016: import org.eclipse.swt.events.DisposeListener;
017: import org.eclipse.swt.widgets.Widget;
018:
019: /**
020: * Instances of this class represent a column of a {@link ColumnViewer}. Label
021: * providers and editing support can be configured for each column separately.
022: * Concrete subclasses of {@link ColumnViewer} should implement a matching
023: * concrete subclass of {@link ViewerColumn}.
024: *
025: * @since 3.3
026: *
027: */
028: public abstract class ViewerColumn {
029:
030: private CellLabelProvider labelProvider;
031:
032: static String COLUMN_VIEWER_KEY = Policy.JFACE + ".columnViewer";//$NON-NLS-1$
033:
034: private EditingSupport editingSupport;
035:
036: private ILabelProviderListener listener;
037:
038: private boolean listenerRegistered = false;
039:
040: /**
041: * Create a new instance of the receiver at columnIndex.
042: *
043: * @param viewer
044: * the viewer the column is part of
045: * @param columnOwner
046: * the widget owning the viewer in case the widget has no columns
047: * this could be the widget itself
048: */
049: protected ViewerColumn(final ColumnViewer viewer, Widget columnOwner) {
050: columnOwner.setData(ViewerColumn.COLUMN_VIEWER_KEY, this );
051: this .listener = new ILabelProviderListener() {
052:
053: public void labelProviderChanged(
054: LabelProviderChangedEvent event) {
055: viewer.handleLabelProviderChanged(event);
056: }
057:
058: };
059: columnOwner.addDisposeListener(new DisposeListener() {
060: public void widgetDisposed(DisposeEvent e) {
061: handleDispose(viewer);
062: }
063: });
064: }
065:
066: /**
067: * Return the label provider for the receiver.
068: *
069: * @return ViewerLabelProvider
070: */
071: /* package */CellLabelProvider getLabelProvider() {
072: return labelProvider;
073: }
074:
075: /**
076: * Set the label provider for the column. Subclasses may extend but must
077: * call the super implementation.
078: *
079: * @param labelProvider
080: * the new {@link CellLabelProvider}
081: */
082: public void setLabelProvider(CellLabelProvider labelProvider) {
083: setLabelProvider(labelProvider, true);
084: }
085:
086: /**
087: * @param labelProvider
088: * @param registerListener
089: */
090: /* package */void setLabelProvider(
091: CellLabelProvider labelProvider, boolean registerListener) {
092: if (listenerRegistered && this .labelProvider != null) {
093: this .labelProvider.removeListener(listener);
094: listenerRegistered = false;
095: }
096:
097: this .labelProvider = labelProvider;
098:
099: if (registerListener) {
100: this .labelProvider.addListener(listener);
101: listenerRegistered = true;
102: }
103: }
104:
105: /**
106: * Return the editing support for the receiver.
107: *
108: * @return {@link EditingSupport}
109: */
110: /* package */EditingSupport getEditingSupport() {
111: return editingSupport;
112: }
113:
114: /**
115: * Set the editing support. Subclasses may extend but must call the super
116: * implementation.
117: *
118: * @param editingSupport
119: * The {@link EditingSupport} to set.
120: */
121: public void setEditingSupport(EditingSupport editingSupport) {
122: this .editingSupport = editingSupport;
123: }
124:
125: /**
126: * Refresh the cell for the given columnIndex. <strong>NOTE:</strong>the
127: * {@link ViewerCell} provided to this method is no longer valid after this
128: * method returns. Do not cache the cell for future use.
129: *
130: * @param cell
131: * {@link ViewerCell}
132: */
133: /* package */void refresh(ViewerCell cell) {
134: getLabelProvider().update(cell);
135: }
136:
137: /**
138: * Disposes of the label provider (if set), unregisters the listener and
139: * nulls the references to the label provider and editing support. This
140: * method is called when the underlying widget is disposed. Subclasses may
141: * extend but must call the super implementation.
142: */
143: protected void handleDispose() {
144: boolean disposeLabelProvider = listenerRegistered;
145: CellLabelProvider cellLabelProvider = labelProvider;
146: setLabelProvider(null, false);
147: if (disposeLabelProvider) {
148: cellLabelProvider.dispose();
149: }
150: editingSupport = null;
151: listener = null;
152: }
153:
154: private void handleDispose(ColumnViewer viewer) {
155: handleDispose();
156: //viewer.clearLegacyEditingSetup();
157: }
158: }
|