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.pde.internal.ui.util;
011:
012: import org.eclipse.jface.resource.CompositeImageDescriptor;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.swt.graphics.ImageData;
015: import org.eclipse.swt.graphics.Point;
016:
017: /**
018: * An OverlayIcon consists of a main icon and several adornments.
019: */
020: public abstract class AbstractOverlayIcon extends
021: CompositeImageDescriptor {
022:
023: static final int DEFAULT_WIDTH = 16;
024: static final int DEFAULT_HEIGHT = 16;
025:
026: private Point fSize = null;
027:
028: private ImageDescriptor fOverlays[][];
029:
030: public AbstractOverlayIcon(ImageDescriptor[][] overlays) {
031: this (overlays, null);
032: }
033:
034: public AbstractOverlayIcon(ImageDescriptor[][] overlays, Point size) {
035: fOverlays = overlays;
036: if (size != null)
037: fSize = size;
038: else
039: fSize = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
040: }
041:
042: protected void drawBottomLeft(ImageDescriptor[] overlays) {
043: if (overlays == null)
044: return;
045: int length = overlays.length;
046: int x = 0;
047: for (int i = 0; i < 3; i++) {
048: if (i < length && overlays[i] != null) {
049: ImageData id = overlays[i].getImageData();
050: drawImage(id, x, getSize().y - id.height);
051: x += id.width;
052: }
053: }
054: }
055:
056: protected void drawBottomRight(ImageDescriptor[] overlays) {
057: if (overlays == null)
058: return;
059: int length = overlays.length;
060: int x = getSize().x;
061: for (int i = 2; i >= 0; i--) {
062: if (i < length && overlays[i] != null) {
063: ImageData id = overlays[i].getImageData();
064: x -= id.width;
065: drawImage(id, x, getSize().y - id.height);
066: }
067: }
068: }
069:
070: protected abstract ImageData getBaseImageData();
071:
072: protected void drawCompositeImage(int width, int height) {
073: ImageData base = getBaseImageData();
074: drawImage(base, 0, 0);
075: if (fOverlays != null) {
076: if (fOverlays.length > 0)
077: drawTopRight(fOverlays[0]);
078:
079: if (fOverlays.length > 1)
080: drawBottomRight(fOverlays[1]);
081:
082: if (fOverlays.length > 2)
083: drawBottomLeft(fOverlays[2]);
084:
085: if (fOverlays.length > 3)
086: drawTopLeft(fOverlays[3]);
087: }
088: }
089:
090: protected void drawTopLeft(ImageDescriptor[] overlays) {
091: if (overlays == null)
092: return;
093: int length = overlays.length;
094: int x = 0;
095: for (int i = 0; i < 3; i++) {
096: if (i < length && overlays[i] != null) {
097: ImageData id = overlays[i].getImageData();
098: drawImage(id, x, 0);
099: x += id.width;
100: }
101: }
102: }
103:
104: protected void drawTopRight(ImageDescriptor[] overlays) {
105: if (overlays == null)
106: return;
107: int length = overlays.length;
108: int x = getSize().x;
109: for (int i = 2; i >= 0; i--) {
110: if (i < length && overlays[i] != null) {
111: ImageData id = overlays[i].getImageData();
112: x -= id.width;
113: drawImage(id, x, 0);
114: }
115: }
116: }
117:
118: protected Point getSize() {
119: return fSize;
120: }
121: }
|