KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11
12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.widgets.Event;
18 import org.eclipse.swt.widgets.Listener;
19
20 /**
21  * OwnerDrawLabelProvider is an abstract implementation of a label provider that
22  * handles custom draw.
23  *
24  * <p>
25  * <b>This class is intended to be subclassed by implementors.</b>
26  * </p>
27  *
28  * @since 3.3
29  *
30  */

31 public abstract class OwnerDrawLabelProvider extends CellLabelProvider {
32
33     /**
34      * Set up the owner draw callbacks for the viewer.
35      *
36      * @param viewer
37      * the viewer the owner draw is set up
38      */

39     public static void setUpOwnerDraw(final ColumnViewer viewer) {
40         viewer.getControl().addListener(SWT.MeasureItem, new Listener() {
41             /*
42              * (non-Javadoc)
43              *
44              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
45              */

46             public void handleEvent(Event event) {
47                 CellLabelProvider provider = viewer
48                         .getViewerColumn(event.index).getLabelProvider();
49                 Object JavaDoc element = event.item.getData();
50
51                 if (provider instanceof OwnerDrawLabelProvider)
52                     ((OwnerDrawLabelProvider) provider).measure(event, element);
53             }
54         });
55
56         viewer.getControl().addListener(SWT.PaintItem, new Listener() {
57             /*
58              * (non-Javadoc)
59              *
60              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
61              */

62             public void handleEvent(Event event) {
63                 CellLabelProvider provider = viewer
64                         .getViewerColumn(event.index).getLabelProvider();
65                 Object JavaDoc element = event.item.getData();
66
67                 if (provider instanceof OwnerDrawLabelProvider)
68                     ((OwnerDrawLabelProvider) provider).paint(event, element);
69             }
70         });
71
72         viewer.getControl().addListener(SWT.EraseItem, new Listener() {
73             /*
74              * (non-Javadoc)
75              *
76              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
77              */

78             public void handleEvent(Event event) {
79
80                 CellLabelProvider provider = getLabelProvider(viewer, event);
81                 Object JavaDoc element = getElement(event);
82
83                 if (provider instanceof OwnerDrawLabelProvider)
84                     ((OwnerDrawLabelProvider) provider).erase(event, element);
85
86             }
87
88             /**
89              * Return the item for the event
90              *
91              * @param event
92              * @return Object
93              */

94             private Object JavaDoc getElement(Event event) {
95                 return event.item.getData();
96             }
97
98             /**
99              * Return the label provider for the column.
100              *
101              * @param viewer
102              * @param event
103              * @return CellLabelProvider
104              */

105             private CellLabelProvider getLabelProvider(
106                     final ColumnViewer viewer, Event event) {
107                 return viewer.getViewerColumn(event.index).getLabelProvider();
108             }
109         });
110     }
111
112     /**
113      * Handle the erase event. The default implementation colors the background
114      * of selected areas with {@link SWT#COLOR_LIST_SELECTION} and foregrounds
115      * with {@link SWT#COLOR_LIST_SELECTION_TEXT}
116      *
117      * @param event
118      * the erase event
119      * @param element
120      * the model object
121      * @see SWT#EraseItem
122      * @see SWT#COLOR_LIST_SELECTION
123      * @see SWT#COLOR_LIST_SELECTION_TEXT
124      */

125     protected void erase(Event event, Object JavaDoc element) {
126
127         Rectangle bounds = event.getBounds();
128         if ((event.detail & SWT.SELECTED) != 0) {
129
130             Color oldForeground = event.gc.getForeground();
131             Color oldBackground = event.gc.getBackground();
132
133             event.gc.setBackground(event.item.getDisplay().getSystemColor(
134                     SWT.COLOR_LIST_SELECTION));
135             event.gc.setForeground(event.item.getDisplay().getSystemColor(
136                     SWT.COLOR_LIST_SELECTION_TEXT));
137             event.gc.fillRectangle(bounds);
138             /* restore the old GC colors */
139             event.gc.setForeground(oldForeground);
140             event.gc.setBackground(oldBackground);
141             /* ensure that default selection is not drawn */
142             event.detail &= ~SWT.SELECTED;
143
144         }
145
146     }
147
148     /**
149      * Handle the paint event.
150      *
151      * @param event
152      * the paint event
153      * @param element
154      * the model element
155      * @see SWT#PaintItem
156      */

157     protected abstract void paint(Event event, Object JavaDoc element);
158
159     /**
160      * Handle the measure event.
161      *
162      * @param event
163      * the measure event
164      * @param element
165      * the model element
166      * @see SWT#MeasureItem
167      */

168     protected abstract void measure(Event event, Object JavaDoc element);
169
170     /**
171      * Create a new instance of the receiver based on a column viewer.
172      *
173      */

174     public OwnerDrawLabelProvider() {
175
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.jface.viewers.ViewerLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
182      */

183     public void update(ViewerCell cell) {
184         // Force a redraw
185
Rectangle cellBounds = cell.getBounds();
186         cell.getControl().redraw(cellBounds.x, cellBounds.y, cellBounds.width,
187                 cellBounds.height, true);
188
189     }
190
191 }
192
Popular Tags