001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: *******************************************************************************/package org.eclipse.ui.internal.decorators;
011:
012: import java.util.List;
013: import java.util.ListIterator;
014:
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.swt.graphics.Color;
017: import org.eclipse.swt.graphics.Font;
018: import org.eclipse.swt.graphics.Image;
019:
020: /**
021: * The Decoration Result is the result of a decoration.
022: */
023: public class DecorationResult {
024:
025: private List prefixes;
026:
027: private List suffixes;
028:
029: private ImageDescriptor[] descriptors;
030:
031: private Color foregroundColor;
032:
033: private Color backgroundColor;
034:
035: private Font font;
036:
037: DecorationResult(List prefixList, List suffixList,
038: ImageDescriptor[] imageDescriptors,
039: Color resultForegroundColor, Color resultBackgroundColor,
040: Font resultFont) {
041: prefixes = prefixList;
042: suffixes = suffixList;
043:
044: //Don't set the field if there are no entries
045: if (hasOverlays(imageDescriptors)) {
046: descriptors = imageDescriptors;
047: }
048: foregroundColor = resultForegroundColor;
049: backgroundColor = resultBackgroundColor;
050: font = resultFont;
051: }
052:
053: /**
054: * Return whether or not any of the imageDescriptors
055: * are non-null.
056: * @param imageDescriptors
057: * @return <code>true</code> if there are some non-null
058: * overlays
059: */
060: private boolean hasOverlays(ImageDescriptor[] imageDescriptors) {
061: for (int i = 0; i < imageDescriptors.length; i++) {
062: if (imageDescriptors[i] != null) {
063: return true;
064: }
065: }
066: return false;
067: }
068:
069: /**
070: * Decorate the Image supplied with the overlays.
071: * @param image
072: * @param overlayCache
073: * @return Image
074: */
075: Image decorateWithOverlays(Image image, OverlayCache overlayCache) {
076:
077: //Do not try to do anything if there is no source or overlays
078: if (image == null || descriptors == null) {
079: return image;
080: }
081:
082: return overlayCache.applyDescriptors(image, descriptors);
083: }
084:
085: /**
086: * Decorate the String supplied with the prefixes and suffixes.
087: * This method is public for use by the test suites and is not intended
088: * to be referenced by other workbench internals.
089: * @param text
090: * @return String
091: */
092: public String decorateWithText(String text) {
093:
094: if (prefixes.isEmpty() && suffixes.isEmpty()) {
095: return text;
096: }
097:
098: StringBuffer result = new StringBuffer();
099:
100: ListIterator prefixIterator = prefixes.listIterator();
101:
102: while (prefixIterator.hasNext()) {
103: result.append(prefixIterator.next());
104: }
105:
106: result.append(text);
107:
108: ListIterator suffixIterator = suffixes.listIterator();
109:
110: while (suffixIterator.hasNext()) {
111: result.append(suffixIterator.next());
112: }
113:
114: return result.toString();
115: }
116:
117: /**
118: * Get the descriptor array for the receiver.
119: * @return ImageDescriptor[] or <code>null</code>
120: */
121: ImageDescriptor[] getDescriptors() {
122: return descriptors;
123: }
124:
125: /**
126: * Get the prefixes for the receiver.
127: * @return List
128: */
129: List getPrefixes() {
130: return prefixes;
131: }
132:
133: /**
134: * Get the suffixes for the receiver.
135: * @return List
136: */
137: List getSuffixes() {
138: return suffixes;
139: }
140:
141: /**
142: * Return the background Color for the result.
143: * @return Color
144: */
145: Color getBackgroundColor() {
146: return backgroundColor;
147: }
148:
149: /**
150: * Return the font for the result.
151: * @return Font
152: */
153: Font getFont() {
154: return font;
155: }
156:
157: /**
158: * Return the foreground color for the result.
159: * @return Color
160: */
161: Color getForegroundColor() {
162: return foregroundColor;
163: }
164: }
|