KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Tom Shindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * fix for bug 163317,200558
12  *******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import org.eclipse.jface.util.Policy;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.widgets.Widget;
20
21 /**
22  * Instances of this class represent a column of a {@link ColumnViewer}. Label
23  * providers and editing support can be configured for each column separately.
24  * Concrete subclasses of {@link ColumnViewer} should implement a matching
25  * concrete subclass of {@link ViewerColumn}.
26  *
27  * @since 3.3
28  *
29  */

30 public abstract class ViewerColumn {
31
32     private CellLabelProvider labelProvider;
33
34     static String JavaDoc COLUMN_VIEWER_KEY = Policy.JFACE + ".columnViewer";//$NON-NLS-1$
35

36     private EditingSupport editingSupport;
37
38     private ILabelProviderListener listener;
39
40     private boolean listenerRegistered = false;
41
42     /**
43      * Create a new instance of the receiver at columnIndex.
44      *
45      * @param viewer
46      * the viewer the column is part of
47      * @param columnOwner
48      * the widget owning the viewer in case the widget has no columns
49      * this could be the widget itself
50      */

51     protected ViewerColumn(final ColumnViewer viewer, Widget columnOwner) {
52         columnOwner.setData(ViewerColumn.COLUMN_VIEWER_KEY, this);
53         this.listener = new ILabelProviderListener() {
54
55             public void labelProviderChanged(LabelProviderChangedEvent event) {
56                 viewer.handleLabelProviderChanged(event);
57             }
58
59         };
60         columnOwner.addDisposeListener(new DisposeListener() {
61             public void widgetDisposed(DisposeEvent e) {
62                 handleDispose(viewer);
63             }
64         });
65     }
66
67     /**
68      * Return the label provider for the receiver.
69      *
70      * @return ViewerLabelProvider
71      */

72     /* package */CellLabelProvider getLabelProvider() {
73         return labelProvider;
74     }
75
76     /**
77      * Set the label provider for the column. Subclasses may extend but must
78      * call the super implementation.
79      *
80      * @param labelProvider
81      * the new {@link CellLabelProvider}
82      */

83     public void setLabelProvider(CellLabelProvider labelProvider) {
84         setLabelProvider(labelProvider, true);
85     }
86
87     /**
88      * @param labelProvider
89      * @param registerListener
90      */

91     /* package */void setLabelProvider(CellLabelProvider labelProvider,
92             boolean registerListener) {
93         if (listenerRegistered && this.labelProvider != null) {
94             this.labelProvider.removeListener(listener);
95             listenerRegistered = false;
96         }
97
98         this.labelProvider = labelProvider;
99
100         if (registerListener) {
101             this.labelProvider.addListener(listener);
102             listenerRegistered = true;
103         }
104     }
105
106     /**
107      * Return the editing support for the receiver.
108      *
109      * @return {@link EditingSupport}
110      */

111     /* package */EditingSupport getEditingSupport() {
112         return editingSupport;
113     }
114
115     /**
116      * Set the editing support. Subclasses may extend but must call the super
117      * implementation.
118      *
119      * @param editingSupport
120      * The {@link EditingSupport} to set.
121      */

122     public void setEditingSupport(EditingSupport editingSupport) {
123         this.editingSupport = editingSupport;
124     }
125
126     /**
127      * Refresh the cell for the given columnIndex. <strong>NOTE:</strong>the
128      * {@link ViewerCell} provided to this method is no longer valid after this
129      * method returns. Do not cache the cell for future use.
130      *
131      * @param cell
132      * {@link ViewerCell}
133      */

134     /* package */void refresh(ViewerCell cell) {
135         getLabelProvider().update(cell);
136     }
137
138     /**
139      * Disposes of the label provider (if set), unregisters the listener and
140      * nulls the references to the label provider and editing support. This
141      * method is called when the underlying widget is disposed. Subclasses may
142      * extend but must call the super implementation.
143      */

144     protected void handleDispose() {
145         boolean disposeLabelProvider = listenerRegistered;
146         CellLabelProvider cellLabelProvider = labelProvider;
147         setLabelProvider(null, false);
148         if (disposeLabelProvider) {
149             cellLabelProvider.dispose();
150         }
151         editingSupport = null;
152         listener = null;
153     }
154
155     private void handleDispose(ColumnViewer viewer) {
156         handleDispose();
157         viewer.clearLegacyEditingSetup();
158     }
159 }
160
Popular Tags