KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > DebugViewDecoratingLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.views;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.jface.viewers.DecoratingLabelProvider;
16 import org.eclipse.jface.viewers.IDelayedLabelDecorator;
17 import org.eclipse.jface.viewers.ILabelDecorator;
18 import org.eclipse.jface.viewers.ILabelProvider;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.jface.viewers.ViewerLabel;
21 import org.eclipse.swt.graphics.Image;
22
23 /**
24  * A label provider which receives notification of labels computed in
25  * the background by a LaunchViewLabelDecorator.
26  */

27 public class DebugViewDecoratingLabelProvider extends DecoratingLabelProvider {
28     
29     /**
30      * A map of text computed for elements. Items are added to this map
31      * when notification is received that text has been computed for an element
32      * and they are removed the first time the text is returned for an
33      * element.
34      * key: Object the element
35      * value: String the label text
36      */

37     private Map JavaDoc computedText= new HashMap JavaDoc();
38     private StructuredViewer viewer= null;
39     private boolean disposed= false;
40     
41     /**
42      * @see DecoratingLabelProvider#DecoratingLabelProvider(org.eclipse.jface.viewers.ILabelProvider, org.eclipse.jface.viewers.ILabelDecorator)
43      */

44     public DebugViewDecoratingLabelProvider(StructuredViewer viewer, ILabelProvider provider, DebugViewLabelDecorator decorator) {
45         super(provider, decorator);
46         decorator.setLabelProvider(this);
47         this.viewer= viewer;
48     }
49     
50     /**
51      * Notifies this label provider that the given text was computed
52      * for the given element. The given text will be returned the next
53      * time its text is requested.
54      *
55      * @param element the element whose label was computed
56      * @param text the label
57      */

58     public void textComputed(Object JavaDoc element, String JavaDoc text) {
59         computedText.put(element, text);
60     }
61     
62     /**
63      * Labels have been computed for the given block of elements.
64      * This method tells the label provider to update the
65      * given elements in the view.
66      *
67      * @param elements the elements which have had their text computed
68      */

69     public void labelsComputed(Object JavaDoc[] elements) {
70         if (!disposed) {
71             viewer.update(elements, null);
72         }
73         for (int i = 0; i < elements.length; i++) {
74             computedText.remove(elements[i]);
75         }
76     }
77     
78     /**
79      * Returns the stored text computed by the background decorator
80      * or delegates to the decorating label provider to compute text.
81      * The stored value is not cleared - the value is cleared when
82      * #lablesComputed(...) has completed the update of its elements.
83      *
84      * @see DecoratingLabelProvider#getText(java.lang.Object)
85      */

86     public String JavaDoc getText(Object JavaDoc element) {
87         String JavaDoc text= (String JavaDoc) computedText.get(element);
88         if (text != null) {
89             return text;
90         }
91         return super.getText(element);
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
96      */

97     public void dispose() {
98         disposed= true;
99         super.dispose();
100     }
101     
102     /*
103      * (non-Javadoc)
104      * @see org.eclipse.jface.viewers.IViewerLabelProvider#updateLabel(org.eclipse.jface.viewers.ViewerLabel, java.lang.Object)
105      */

106     public void updateLabel(ViewerLabel settings, Object JavaDoc element) {
107
108         ILabelDecorator currentDecorator = getLabelDecorator();
109         String JavaDoc oldText = settings.getText();
110         String JavaDoc text= getText(element);
111         boolean decorationReady = !DebugViewInterimLabelProvider.PENDING_LABEL.equals(text);
112         if (currentDecorator instanceof IDelayedLabelDecorator) {
113             IDelayedLabelDecorator delayedDecorator = (IDelayedLabelDecorator) currentDecorator;
114             if (!delayedDecorator.prepareDecoration(element, oldText)) {
115                 // The decoration is not ready but has been queued for processing
116
decorationReady = false;
117             }
118         }
119         // update icon and label
120

121         if (decorationReady || oldText == null
122                 || settings.getText().length() == 0)
123             settings.setText(getText(element));
124
125         Image oldImage = settings.getImage();
126         if (decorationReady || oldImage == null) {
127             settings.setImage(getImage(element));
128         }
129  
130         if(decorationReady)
131             updateForDecorationReady(settings,element);
132
133     }
134 }
135
Popular Tags