KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > decorators > DecorationBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Sascha Zelzer <zelzer@mathi.uni-heidelberg.de> -
11  * Fix for Bug 152927 [Decorators] ArrayOutOfBoundsException in DecorationBuilder.java
12  *******************************************************************************/

13 package org.eclipse.ui.internal.decorators;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.viewers.DecorationContext;
20 import org.eclipse.jface.viewers.IDecoration;
21 import org.eclipse.jface.viewers.IDecorationContext;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.ui.internal.WorkbenchPlugin;
25
26 /**
27  * The Decoration builder is the object that builds a decoration.
28  */

29 public class DecorationBuilder implements IDecoration {
30
31     private static int DECORATOR_ARRAY_SIZE = 5;
32
33     private List JavaDoc prefixes = new ArrayList JavaDoc();
34
35     private List JavaDoc suffixes = new ArrayList JavaDoc();
36
37     private ImageDescriptor[] descriptors = new ImageDescriptor[DECORATOR_ARRAY_SIZE];
38
39     private Color foregroundColor;
40
41     private Color backgroundColor;
42
43     private Font font;
44
45     LightweightDecoratorDefinition currentDefinition;
46
47     //A flag set if a value has been added
48
private boolean valueSet = false;
49
50     private final IDecorationContext context;
51
52     /**
53      * Default constructor.
54      */

55     DecorationBuilder() {
56         this(DecorationContext.DEFAULT_CONTEXT);
57     }
58
59     /**
60      * Create a decoration builder for the given context
61      * @param context a decoration context
62      */

63     public DecorationBuilder(IDecorationContext context) {
64         this.context = context;
65     }
66
67     /**
68      * Set the value of the definition we are currently working on.
69      *
70      * @param definition
71      */

72     void setCurrentDefinition(LightweightDecoratorDefinition definition) {
73         this.currentDefinition = definition;
74     }
75
76     /**
77      * @see org.eclipse.jface.viewers.IDecoration#addOverlay(org.eclipse.jface.resource.ImageDescriptor)
78      */

79     public void addOverlay(ImageDescriptor overlay) {
80         int quadrant = currentDefinition.getQuadrant();
81         if (descriptors[quadrant] == null) {
82             descriptors[quadrant] = overlay;
83         }
84         valueSet = true;
85     }
86
87     /**
88      * @see org.eclipse.jface.viewers.IDecoration#addOverlay(org.eclipse.jface.resource.ImageDescriptor)
89      */

90     public void addOverlay(ImageDescriptor overlay, int quadrant) {
91         if (quadrant >= 0 && quadrant < DECORATOR_ARRAY_SIZE) {
92             if (descriptors[quadrant] == null) {
93                 descriptors[quadrant] = overlay;
94             }
95             valueSet = true;
96         } else {
97             WorkbenchPlugin
98                     .log("Unable to apply decoration for " + currentDefinition.getId() + " invalid quadrant: " + quadrant); //$NON-NLS-1$ //$NON-NLS-2$
99
}
100     }
101
102     /**
103      * @see org.eclipse.jface.viewers.IDecoration#addPrefix(java.lang.String)
104      */

105     public void addPrefix(String JavaDoc prefixString) {
106         prefixes.add(prefixString);
107         valueSet = true;
108     }
109
110     /**
111      * @see org.eclipse.jface.viewers.IDecoration#addSuffix(java.lang.String)
112      */

113     public void addSuffix(String JavaDoc suffixString) {
114         suffixes.add(suffixString);
115         valueSet = true;
116     }
117
118     /**
119      * Clear the current values and return a DecorationResult.
120      * @return DecorationResult
121      */

122     DecorationResult createResult() {
123         DecorationResult newResult = new DecorationResult(new ArrayList JavaDoc(
124                 prefixes), new ArrayList JavaDoc(suffixes), descriptors,
125                 foregroundColor, backgroundColor, font);
126
127         return newResult;
128     }
129
130     /**
131      * Clear the contents of the result so it can be reused.
132      */

133     void clearContents() {
134         this.prefixes.clear();
135         this.suffixes.clear();
136         this.descriptors = new ImageDescriptor[DECORATOR_ARRAY_SIZE];
137         valueSet = false;
138     }
139
140     /**
141      * Return whether or not a value has been set.
142      *
143      * @return boolean
144      */

145     boolean hasValue() {
146         return valueSet;
147     }
148
149     /**
150      * Apply the previously calculates result to the receiver.
151      *
152      * @param result
153      */

154     void applyResult(DecorationResult result) {
155         prefixes.addAll(result.getPrefixes());
156         suffixes.addAll(result.getSuffixes());
157         ImageDescriptor[] resultDescriptors = result.getDescriptors();
158         if (resultDescriptors != null) {
159             for (int i = 0; i < descriptors.length; i++) {
160                 if (resultDescriptors[i] != null) {
161                     descriptors[i] = resultDescriptors[i];
162                 }
163             }
164         }
165         
166         setForegroundColor(result.getForegroundColor());
167         setBackgroundColor(result.getBackgroundColor());
168         setFont(result.getFont());
169         valueSet = true;
170     }
171
172     /*
173      * (non-Javadoc)
174      * @see org.eclipse.jface.viewers.IDecoration#setBackgroundColor(org.eclipse.swt.graphics.Color)
175      */

176     
177     public void setBackgroundColor(Color bgColor) {
178         this.backgroundColor = bgColor;
179         valueSet = true;
180     }
181
182     /*
183      * (non-Javadoc)
184      * @see org.eclipse.jface.viewers.IDecoration#setFont(org.eclipse.swt.graphics.Font)
185      */

186     public void setFont(Font newFont) {
187         this.font = newFont;
188         valueSet = true;
189     }
190
191     /*
192      * (non-Javadoc)
193      * @see org.eclipse.jface.viewers.IDecoration#setForegroundColor(org.eclipse.swt.graphics.Color)
194      */

195     public void setForegroundColor(Color fgColor) {
196         this.foregroundColor = fgColor;
197         valueSet = true;
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.jface.viewers.IDecoration#getDecorationContext()
202      */

203     public IDecorationContext getDecorationContext() {
204         return context;
205     }
206 }
207
Popular Tags