001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.forms.widgets;
011:
012: import java.util.Hashtable;
013:
014: import org.eclipse.swt.graphics.*;
015:
016: /**
017: * @version 1.0
018: * @author
019: */
020: public class ImageSegment extends ObjectSegment {
021: public static final String SEL_IMAGE_PREFIX = "isel."; //$NON-NLS-1$
022:
023: public Image getImage(Hashtable objectTable) {
024: return getImage(getObjectId(), objectTable);
025: }
026:
027: private Image getImage(String key, Hashtable objectTable) {
028: if (key == null)
029: return null;
030: Object obj = objectTable.get(key);
031: if (obj == null)
032: return null;
033: if (obj instanceof Image)
034: return (Image) obj;
035: return null;
036: }
037:
038: private Image getSelectedImage(Hashtable objectTable,
039: SelectionData selData) {
040: String key = SEL_IMAGE_PREFIX + getObjectId();
041: Image image = getImage(key, objectTable);
042: if (image == null) {
043: image = FormUtil.createAlphaMashImage(selData.display,
044: getImage(objectTable));
045: if (image != null)
046: objectTable.put(key, image);
047: }
048: return image;
049: }
050:
051: /*
052: private String getSelectedImageId() {
053: if (getObjectId() == null)
054: return null;
055: return SEL_IMAGE_PREFIX + getObjectId();
056: }
057: */
058:
059: public void paint(GC gc, boolean hover, Hashtable resourceTable,
060: boolean selected, SelectionData selData,
061: Rectangle repaintRegion) {
062: Image image = getImage(resourceTable);
063: int iwidth = 0;
064: int iheight = 0;
065: if (image != null) {
066: Rectangle rect = image.getBounds();
067: iwidth = rect.width + (isSelectable() ? 2 : 0);
068: iheight = rect.height + (isSelectable() ? 2 : 0);
069: } else
070: return;
071: Rectangle bounds = getBounds();
072: int ix = bounds.x + (isSelectable() ? 1 : 0);
073: int iy = bounds.y + (isSelectable() ? 1 : 0);
074:
075: if (selData != null) {
076: int leftOffset = selData.getLeftOffset(bounds.height);
077: int rightOffset = selData.getRightOffset(bounds.height);
078: boolean firstRow = selData.isFirstSelectionRow(bounds.y,
079: bounds.height);
080: boolean lastRow = selData.isLastSelectionRow(bounds.y,
081: bounds.height);
082: boolean selectedRow = selData.isSelectedRow(bounds.y,
083: bounds.height);
084: if (selectedRow) {
085: if ((firstRow && leftOffset > ix)
086: || (lastRow && rightOffset < ix + iwidth / 2)) {
087: drawClipImage(gc, image, ix, iy, repaintRegion);
088: } else {
089: Color savedBg = gc.getBackground();
090: gc.setBackground(selData.bg);
091: int sx = ix;
092: int sy = iy;
093: if (repaintRegion != null) {
094: sx -= repaintRegion.x;
095: sy -= repaintRegion.y;
096: }
097: gc.fillRectangle(sx, sy, iwidth, iheight);
098: Image selImage = getSelectedImage(resourceTable,
099: selData);
100: gc.drawImage(selImage, sx, sy);
101: gc.setBackground(savedBg);
102: }
103: } else
104: drawClipImage(gc, image, ix, iy, repaintRegion);
105: } else
106: drawClipImage(gc, image, ix, iy, repaintRegion);
107: if (selected) {
108: int fx = bounds.x;
109: int fy = bounds.y;
110: if (repaintRegion != null) {
111: fx -= repaintRegion.x;
112: fy -= repaintRegion.y;
113: }
114: Color fg = gc.getForeground();
115: gc.setForeground(gc.getBackground());
116: // Clean up to avoid canceling out XOR if it is already
117: // selected.
118: gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1,
119: bounds.height - 1);
120: gc.setForeground(fg);
121: gc.drawFocus(fx, fy, bounds.width, bounds.height);
122: }
123: }
124:
125: private void drawClipImage(GC gc, Image image, int ix, int iy,
126: Rectangle repaintRegion) {
127: if (repaintRegion != null) {
128: ix -= repaintRegion.x;
129: iy -= repaintRegion.y;
130: }
131: gc.drawImage(image, ix, iy);
132: }
133:
134: protected Point getObjectSize(Hashtable resourceTable, int wHint) {
135: Image image = getImage(resourceTable);
136: if (image == null)
137: return new Point(0, 0);
138: Rectangle ibounds = image.getBounds();
139: return new Point(ibounds.width, ibounds.height);
140: }
141: }
|