KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > model > LabelUpdate


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 package org.eclipse.debug.internal.ui.viewers.model;
12
13 import org.eclipse.debug.internal.core.commands.Request;
14 import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
15 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.TreePath;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.FontData;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.swt.widgets.TreeItem;
24
25 /**
26  * @since 3.3
27  */

28 class LabelUpdate extends Request implements ILabelUpdate {
29     
30     private TreePath fElementPath;
31     private String JavaDoc[] fColumnIds;
32     private RGB[] fBackgrounds;
33     private RGB[] fForegrounds;
34     private ImageDescriptor[] fImageDescriptors;
35     private String JavaDoc[] fLabels;
36     private FontData[] fFontDatas;
37     private TreeModelLabelProvider fProvider;
38     private TreeItem fItem;
39     private int fNumColumns;
40     private IPresentationContext fContext;
41     
42     /**
43      * Label data cache keys
44      * TODO: workaround for bug 159461
45      */

46     static String JavaDoc PREV_LABEL_KEY = "PREV_LABEL_KEY"; //$NON-NLS-1$
47
static String JavaDoc PREV_IMAGE_KEY = "PREV_IMAGE_KEY"; //$NON-NLS-1$
48
static String JavaDoc PREV_FONT_KEY = "PREV_FONT_KEY"; //$NON-NLS-1$
49
static String JavaDoc PREV_FOREGROUND_KEY = "PREV_FOREGROUND_KEY"; //$NON-NLS-1$
50
static String JavaDoc PREV_BACKGROUND_KEY = "PREV_BACKGROUND_KEY"; //$NON-NLS-1$
51

52     /**
53      * @param elementPath element the label is for
54      * @param item item the label is for
55      * @param provider label provider to callback to
56      * @param columnIds column identifiers or <code>null</code>
57      * @param context presentation context
58      */

59     public LabelUpdate(TreePath elementPath, TreeItem item, TreeModelLabelProvider provider, String JavaDoc[] columnIds, IPresentationContext context) {
60         fContext = context;
61         fElementPath = elementPath;
62         fProvider = provider;
63         fColumnIds = columnIds;
64         fItem = item;
65         fNumColumns = 1;
66         if (columnIds != null) {
67             fNumColumns = columnIds.length;
68         }
69         fLabels = new String JavaDoc[fNumColumns];
70         fProvider.updateStarted(this);
71     }
72
73     /* (non-Javadoc)
74      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#getColumnIds()
75      */

76     public String JavaDoc[] getColumnIds() {
77         return fColumnIds;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#getElement()
82      */

83     public TreePath getElementPath() {
84         return fElementPath;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#setBackground(org.eclipse.swt.graphics.RGB, int)
89      */

90     public void setBackground(RGB background, int columnIndex) {
91         if (background == null) {
92             return;
93         }
94         if (fBackgrounds == null) {
95             fBackgrounds = new RGB[fNumColumns];
96         }
97         fBackgrounds[columnIndex] = background;
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#setFontData(org.eclipse.swt.graphics.FontData, int)
102      */

103     public void setFontData(FontData fontData, int columnIndex) {
104         if (fontData == null) {
105             return;
106         }
107         if (fFontDatas == null) {
108             fFontDatas = new FontData[fNumColumns];
109         }
110         fFontDatas[columnIndex] = fontData;
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#setForeground(org.eclipse.swt.graphics.RGB, int)
115      */

116     public void setForeground(RGB foreground, int columnIndex) {
117         if (foreground == null) {
118             return;
119         }
120         if (fForegrounds == null) {
121             fForegrounds = new RGB[fNumColumns];
122         }
123         fForegrounds[columnIndex] = foreground;
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#setImageDescriptor(org.eclipse.jface.resource.ImageDescriptor, int)
128      */

129     public void setImageDescriptor(ImageDescriptor image, int columnIndex) {
130         if (image == null) {
131             return;
132         }
133         if (fImageDescriptors == null) {
134             fImageDescriptors = new ImageDescriptor[fNumColumns];
135         }
136         fImageDescriptors[columnIndex] = image;
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate#setLabel(java.lang.String, int)
141      */

142     public void setLabel(String JavaDoc text, int columnIndex) {
143         fLabels[columnIndex] = text;
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate#getPresentationContext()
148      */

149     public IPresentationContext getPresentationContext() {
150         return fContext;
151     }
152
153     /* (non-Javadoc)
154      * @see org.eclipse.core.runtime.IProgressMonitor#done()
155      */

156     public void done() {
157         fProvider.complete(this);
158     }
159
160     /**
161      * Applies settings to viewer cell
162      */

163     public void update() {
164         // label data is stored to prevent flickering of asynchronous view, see bug 159461
165
if (!fItem.isDisposed()) {
166             
167             for (int i=0; i<fNumColumns; i++){
168                 // text might be null if the launch has been terminated
169
fItem.setText(i,(fLabels[i] == null ? "" : fLabels[i])); //$NON-NLS-1$
170
}
171             fItem.setData(PREV_LABEL_KEY, fLabels);
172             
173             if (fImageDescriptors == null) {
174                 for (int i=0; i<fNumColumns; i++){
175                     fItem.setImage(i,null);
176                 }
177                 fItem.setData(PREV_IMAGE_KEY, null);
178             } else {
179                 Image[] images = new Image[fImageDescriptors.length];
180                 for (int i = 0; i < fImageDescriptors.length; i++) {
181                     images[i] = fProvider.getImage(fImageDescriptors[i]);
182                 }
183                 if (fColumnIds == null) {
184                     fItem.setImage(images[0]);
185                 } else {
186                     fItem.setImage(images);
187                 }
188                 fItem.setData(PREV_IMAGE_KEY, images);
189             }
190             
191             if (fForegrounds == null) {
192                 for (int i=0; i<fNumColumns; i++){
193                     fItem.setForeground(i,null);
194                 }
195                 fItem.setData(PREV_FOREGROUND_KEY, null);
196             } else {
197                 Color[] foregrounds = new Color[fForegrounds.length];
198                 for (int i = 0; i< foregrounds.length; i++) {
199                     foregrounds[i] = fProvider.getColor(fForegrounds[i]);
200                 }
201                 if (fColumnIds == null) {
202                     fItem.setForeground(0,foregrounds[0]);
203                 } else {
204                     for (int i = 0; i< foregrounds.length; i++) {
205                         fItem.setForeground(i, foregrounds[i]);
206                     }
207                 }
208                 fItem.setData(PREV_FOREGROUND_KEY, foregrounds);
209             }
210             
211             if (fBackgrounds == null) {
212                 for (int i=0; i<fNumColumns; i++){
213                     fItem.setBackground(i,null);
214                 }
215                 fItem.setData(PREV_BACKGROUND_KEY, null);
216             } else {
217                 Color[] backgrounds = new Color[fBackgrounds.length];
218                 for (int i = 0; i< backgrounds.length; i++) {
219                     backgrounds[i] = fProvider.getColor(fBackgrounds[i]);
220                 }
221                 if (fColumnIds == null) {
222                     fItem.setBackground(0,backgrounds[0]);
223                 } else {
224                     for (int i = 0; i< backgrounds.length; i++) {
225                         fItem.setBackground(i, backgrounds[i]);
226                     }
227                 }
228                 fItem.setData(PREV_BACKGROUND_KEY, backgrounds);
229             }
230             
231             if (fFontDatas == null) {
232                 for (int i=0; i<fNumColumns; i++){
233                     fItem.setFont(i,null);
234                 }
235                 fItem.setData(PREV_FONT_KEY, null);
236             } else {
237                 Font[] fonts = new Font[fFontDatas.length];
238                 for (int i = 0; i < fFontDatas.length; i++) {
239                     fonts[i] = fProvider.getFont(fFontDatas[i]);
240                 }
241                 if (fColumnIds == null) {
242                     fItem.setFont(0,fonts[0]);
243                 } else {
244                     for (int i = 0; i < fonts.length; i++) {
245                         fItem.setFont(i, fonts[i]);
246                     }
247                 }
248                 fItem.setData(PREV_FONT_KEY, fonts);
249             }
250         }
251         fProvider.updateComplete(this);
252     }
253
254     /* (non-Javadoc)
255      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate#getElement()
256      */

257     public Object JavaDoc getElement() {
258         return getElementPath().getLastSegment();
259     }
260
261     public String JavaDoc toString() {
262         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
263         buf.append("ILabelUpdate: "); //$NON-NLS-1$
264
buf.append(getElement());
265         return buf.toString();
266     }
267 }
268
Popular Tags