KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > OwnerDrawSupport


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.jdt.internal.ui.viewsupport;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.graphics.TextLayout;
22 import org.eclipse.swt.graphics.TextStyle;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Item;
27 import org.eclipse.swt.widgets.Listener;
28 import org.eclipse.swt.widgets.TableItem;
29 import org.eclipse.swt.widgets.TreeItem;
30
31 /**
32  * Adding owner draw support to a control
33  */

34 public abstract class OwnerDrawSupport implements Listener {
35     
36     private TextLayout fTextLayout;
37     private final Control fControl;
38     
39     public OwnerDrawSupport(Control control) {
40         fControl= control;
41         fTextLayout= new TextLayout(control.getDisplay());
42         
43         control.addListener(SWT.PaintItem, this);
44         control.addListener(SWT.EraseItem, this);
45         control.addListener(SWT.Dispose, this);
46     }
47     
48     /**
49      * Return the colored label for the given item.
50      * @param item the item to return the colored label for
51      * @return the colored string
52      */

53     public abstract ColoredString getColoredLabel(Item item);
54     
55     /**
56      * Return the color for the given style
57      * @param foregroundColorName the name of the color
58      * @param display the current display
59      * @return the color
60      */

61     public abstract Color getColor(String JavaDoc foregroundColorName, Display display);
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
65      */

66     public void handleEvent(Event event) {
67         if (event.type == SWT.PaintItem) {
68             performPaint(event);
69         } else if (event.type == SWT.EraseItem) {
70             performErase(event);
71         } else if (event.type == SWT.Dispose) {
72             dispose();
73         }
74     }
75     
76     private void performErase(Event event) {
77         event.detail &= ~SWT.FOREGROUND;
78     }
79             
80     private void performPaint(Event event) {
81         Item item= (Item) event.item;
82         GC gc= event.gc;
83
84         ColoredString coloredLabel= getColoredLabel(item);
85         boolean isSelected= (event.detail & SWT.SELECTED) != 0 && fControl.isFocusControl();
86         if (item instanceof TreeItem) {
87             TreeItem treeItem= (TreeItem) item;
88             Image image = treeItem.getImage(event.index);
89             if (image != null) {
90                 processImage(image, gc, treeItem.getImageBounds(event.index));
91             }
92             Rectangle textBounds= treeItem.getTextBounds(event.index);
93             Font font= treeItem.getFont(event.index);
94             processColoredLabel(coloredLabel, gc, textBounds, isSelected, font);
95             
96             Rectangle bounds= treeItem.getBounds();
97             if ((event.detail & SWT.FOCUSED) != 0) {
98                 gc.drawFocus(bounds.x, bounds.y, bounds.width, bounds.height);
99             }
100         } else if (item instanceof TableItem) {
101             TableItem tableItem= (TableItem) item;
102             Image image = tableItem.getImage(event.index);
103             if (image != null) {
104                 processImage(image, gc, tableItem.getImageBounds(event.index));
105             }
106             Rectangle textBounds= tableItem.getTextBounds(event.index);
107             Font font= tableItem.getFont(event.index);
108             processColoredLabel(coloredLabel, gc, textBounds, isSelected, font);
109             
110             Rectangle bounds= tableItem.getBounds();
111             if ((event.detail & SWT.FOCUSED) != 0) {
112                 gc.drawFocus(bounds.x, bounds.y, bounds.width, bounds.height);
113             }
114         }
115     }
116     
117     private void processImage(Image image, GC gc, Rectangle imageBounds) {
118         Rectangle bounds= image.getBounds();
119         int x= imageBounds.x + Math.max(0, (imageBounds.width - bounds.width) / 2);
120         int y= imageBounds.y + Math.max(0, (imageBounds.height - bounds.height) / 2);
121         gc.drawImage(image, x, y);
122     }
123     
124     private void processColoredLabel(ColoredString richLabel, GC gc, Rectangle textBounds, boolean isSelected, Font font) {
125         String JavaDoc text= richLabel.getString();
126         fTextLayout.setText(text);
127         fTextLayout.setFont(font);
128         
129         if (!isSelected) {
130             // apply the styled ranges only when element is not selected
131
Display display= (Display) gc.getDevice();
132             Iterator JavaDoc ranges= richLabel.getRanges();
133             while (ranges.hasNext()) {
134                 ColoredString.StyleRange curr= (ColoredString.StyleRange) ranges.next();
135                 ColoredString.Style style= curr.style;
136                 if (style != null) {
137                     Color foreground= getColor(style.getForegroundColorName(), display);
138                     TextStyle textStyle= new TextStyle(null, foreground, null);
139                     fTextLayout.setStyle(textStyle, curr.offset, curr.offset + curr.length - 1);
140                 }
141             }
142         }
143         
144         Rectangle bounds= fTextLayout.getBounds();
145         int x= textBounds.x;
146         int y = textBounds.y + Math.max(0, (textBounds.height - bounds.height) / 2);
147         
148         fTextLayout.draw(gc, x, y);
149         fTextLayout.setText(""); // clear all ranges //$NON-NLS-1$
150
}
151     
152     public void dispose() {
153         if (fTextLayout != null) {
154             fTextLayout.dispose();
155             fTextLayout= null;
156         }
157         if (!fControl.isDisposed()) {
158             fControl.removeListener(SWT.PaintItem, this);
159             fControl.removeListener(SWT.EraseItem, this);
160             fControl.removeListener(SWT.Dispose, this);
161         }
162     }
163 }
164
Popular Tags