001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sascha Zelzer <zelzer@mathi.uni-heidelberg.de> -
011: * Fix for Bug 152927 [Decorators] ArrayOutOfBoundsException in DecorationBuilder.java
012: *******************************************************************************/package org.eclipse.ui.internal.decorators;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.jface.viewers.DecorationContext;
019: import org.eclipse.jface.viewers.IDecoration;
020: import org.eclipse.jface.viewers.IDecorationContext;
021: import org.eclipse.swt.graphics.Color;
022: import org.eclipse.swt.graphics.Font;
023: import org.eclipse.ui.internal.WorkbenchPlugin;
024:
025: /**
026: * The Decoration builder is the object that builds a decoration.
027: */
028: public class DecorationBuilder implements IDecoration {
029:
030: private static int DECORATOR_ARRAY_SIZE = 5;
031:
032: private List prefixes = new ArrayList();
033:
034: private List suffixes = new ArrayList();
035:
036: private ImageDescriptor[] descriptors = new ImageDescriptor[DECORATOR_ARRAY_SIZE];
037:
038: private Color foregroundColor;
039:
040: private Color backgroundColor;
041:
042: private Font font;
043:
044: LightweightDecoratorDefinition currentDefinition;
045:
046: //A flag set if a value has been added
047: private boolean valueSet = false;
048:
049: private final IDecorationContext context;
050:
051: /**
052: * Default constructor.
053: */
054: DecorationBuilder() {
055: this (DecorationContext.DEFAULT_CONTEXT);
056: }
057:
058: /**
059: * Create a decoration builder for the given context
060: * @param context a decoration context
061: */
062: public DecorationBuilder(IDecorationContext context) {
063: this .context = context;
064: }
065:
066: /**
067: * Set the value of the definition we are currently working on.
068: *
069: * @param definition
070: */
071: void setCurrentDefinition(LightweightDecoratorDefinition definition) {
072: this .currentDefinition = definition;
073: }
074:
075: /**
076: * @see org.eclipse.jface.viewers.IDecoration#addOverlay(org.eclipse.jface.resource.ImageDescriptor)
077: */
078: public void addOverlay(ImageDescriptor overlay) {
079: int quadrant = currentDefinition.getQuadrant();
080: if (descriptors[quadrant] == null) {
081: descriptors[quadrant] = overlay;
082: }
083: valueSet = true;
084: }
085:
086: /**
087: * @see org.eclipse.jface.viewers.IDecoration#addOverlay(org.eclipse.jface.resource.ImageDescriptor)
088: */
089: public void addOverlay(ImageDescriptor overlay, int quadrant) {
090: if (quadrant >= 0 && quadrant < DECORATOR_ARRAY_SIZE) {
091: if (descriptors[quadrant] == null) {
092: descriptors[quadrant] = overlay;
093: }
094: valueSet = true;
095: } else {
096: WorkbenchPlugin
097: .log("Unable to apply decoration for " + currentDefinition.getId() + " invalid quadrant: " + quadrant); //$NON-NLS-1$ //$NON-NLS-2$
098: }
099: }
100:
101: /**
102: * @see org.eclipse.jface.viewers.IDecoration#addPrefix(java.lang.String)
103: */
104: public void addPrefix(String prefixString) {
105: prefixes.add(prefixString);
106: valueSet = true;
107: }
108:
109: /**
110: * @see org.eclipse.jface.viewers.IDecoration#addSuffix(java.lang.String)
111: */
112: public void addSuffix(String suffixString) {
113: suffixes.add(suffixString);
114: valueSet = true;
115: }
116:
117: /**
118: * Clear the current values and return a DecorationResult.
119: * @return DecorationResult
120: */
121: DecorationResult createResult() {
122: DecorationResult newResult = new DecorationResult(
123: new ArrayList(prefixes), new ArrayList(suffixes),
124: descriptors, foregroundColor, backgroundColor, font);
125:
126: return newResult;
127: }
128:
129: /**
130: * Clear the contents of the result so it can be reused.
131: */
132: void clearContents() {
133: this .prefixes.clear();
134: this .suffixes.clear();
135: this .descriptors = new ImageDescriptor[DECORATOR_ARRAY_SIZE];
136: valueSet = false;
137: }
138:
139: /**
140: * Return whether or not a value has been set.
141: *
142: * @return boolean
143: */
144: boolean hasValue() {
145: return valueSet;
146: }
147:
148: /**
149: * Apply the previously calculates result to the receiver.
150: *
151: * @param result
152: */
153: void applyResult(DecorationResult result) {
154: prefixes.addAll(result.getPrefixes());
155: suffixes.addAll(result.getSuffixes());
156: ImageDescriptor[] resultDescriptors = result.getDescriptors();
157: if (resultDescriptors != null) {
158: for (int i = 0; i < descriptors.length; i++) {
159: if (resultDescriptors[i] != null) {
160: descriptors[i] = resultDescriptors[i];
161: }
162: }
163: }
164:
165: setForegroundColor(result.getForegroundColor());
166: setBackgroundColor(result.getBackgroundColor());
167: setFont(result.getFont());
168: valueSet = true;
169: }
170:
171: /*
172: * (non-Javadoc)
173: * @see org.eclipse.jface.viewers.IDecoration#setBackgroundColor(org.eclipse.swt.graphics.Color)
174: */
175:
176: public void setBackgroundColor(Color bgColor) {
177: this .backgroundColor = bgColor;
178: valueSet = true;
179: }
180:
181: /*
182: * (non-Javadoc)
183: * @see org.eclipse.jface.viewers.IDecoration#setFont(org.eclipse.swt.graphics.Font)
184: */
185: public void setFont(Font newFont) {
186: this .font = newFont;
187: valueSet = true;
188: }
189:
190: /*
191: * (non-Javadoc)
192: * @see org.eclipse.jface.viewers.IDecoration#setForegroundColor(org.eclipse.swt.graphics.Color)
193: */
194: public void setForegroundColor(Color fgColor) {
195: this .foregroundColor = fgColor;
196: valueSet = true;
197: }
198:
199: /* (non-Javadoc)
200: * @see org.eclipse.jface.viewers.IDecoration#getDecorationContext()
201: */
202: public IDecorationContext getDecorationContext() {
203: return context;
204: }
205: }
|