KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > ImageSegment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.ui.internal.forms.widgets;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.swt.graphics.*;
16
17 /**
18  * @version 1.0
19  * @author
20  */

21 public class ImageSegment extends ObjectSegment {
22     public static final String JavaDoc SEL_IMAGE_PREFIX = "isel."; //$NON-NLS-1$
23

24     public Image getImage(Hashtable JavaDoc objectTable) {
25         return getImage(getObjectId(), objectTable);
26     }
27
28     private Image getImage(String JavaDoc key, Hashtable JavaDoc objectTable) {
29         if (key == null)
30             return null;
31         Object JavaDoc obj = objectTable.get(key);
32         if (obj == null)
33             return null;
34         if (obj instanceof Image)
35             return (Image) obj;
36         return null;
37     }
38
39     private Image getSelectedImage(Hashtable JavaDoc objectTable, SelectionData selData) {
40         String JavaDoc key = SEL_IMAGE_PREFIX + getObjectId();
41         Image image = getImage(key, objectTable);
42         if (image==null) {
43             image = FormUtil.createAlphaMashImage(selData.display, getImage(objectTable));
44             if (image!=null)
45                 objectTable.put(key, image);
46         }
47         return image;
48     }
49 /*
50     private String getSelectedImageId() {
51         if (getObjectId() == null)
52             return null;
53         return SEL_IMAGE_PREFIX + getObjectId();
54     }
55 */

56     
57     public void paint(GC gc, boolean hover, Hashtable JavaDoc resourceTable, boolean selected, SelectionData selData, Rectangle repaintRegion) {
58         Image image = getImage(resourceTable);
59         int iwidth = 0;
60         int iheight = 0;
61         if (image != null) {
62             Rectangle rect = image.getBounds();
63             iwidth = rect.width + (isSelectable()?2:0);
64             iheight = rect.height + (isSelectable()?2:0);
65         } else
66             return;
67         Rectangle bounds = getBounds();
68         int ix = bounds.x+(isSelectable()?1:0);
69         int iy = bounds.y+(isSelectable()?1:0);
70
71         if (selData != null) {
72             int leftOffset = selData.getLeftOffset(bounds.height);
73             int rightOffset = selData.getRightOffset(bounds.height);
74             boolean firstRow = selData.isFirstSelectionRow(bounds.y,
75                     bounds.height);
76             boolean lastRow = selData.isLastSelectionRow(bounds.y,
77                     bounds.height);
78             boolean selectedRow = selData
79                     .isSelectedRow(bounds.y, bounds.height);
80             if (selectedRow) {
81                 if ((firstRow && leftOffset > ix) ||
82                     (lastRow && rightOffset < ix + iwidth/2)) {
83                     drawClipImage(gc, image, ix, iy, repaintRegion);
84                 }
85                 else {
86                     Color savedBg = gc.getBackground();
87                     gc.setBackground(selData.bg);
88                     int sx = ix;
89                     int sy = iy;
90                     if (repaintRegion!=null) {
91                         sx -= repaintRegion.x;
92                         sy -= repaintRegion.y;
93                     }
94                     gc.fillRectangle(sx, sy, iwidth, iheight);
95                     Image selImage = getSelectedImage(resourceTable, selData);
96                     gc.drawImage(selImage, sx, sy);
97                     gc.setBackground(savedBg);
98                 }
99             }
100             else
101                 drawClipImage(gc, image, ix, iy, repaintRegion);
102         } else
103             drawClipImage(gc, image, ix, iy, repaintRegion);
104         if (selected) {
105             int fx = bounds.x;
106             int fy = bounds.y;
107             if (repaintRegion!=null) {
108                 fx -= repaintRegion.x;
109                 fy -= repaintRegion.y;
110             }
111             Color fg = gc.getForeground();
112             gc.setForeground(gc.getBackground());
113             // Clean up to avoid canceling out XOR if it is already
114
// selected.
115
gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1,
116                     bounds.height - 1);
117             gc.setForeground(fg);
118             gc.drawFocus(fx, fy, bounds.width, bounds.height);
119         }
120     }
121     private void drawClipImage(GC gc, Image image, int ix, int iy, Rectangle repaintRegion) {
122         if (repaintRegion!=null) {
123             ix -= repaintRegion.x;
124             iy -= repaintRegion.y;
125         }
126         gc.drawImage(image, ix, iy);
127     }
128
129     protected Point getObjectSize(Hashtable JavaDoc resourceTable, int wHint) {
130         Image image = getImage(resourceTable);
131         if (image==null)
132             return new Point(0, 0);
133         Rectangle ibounds = image.getBounds();
134         return new Point(ibounds.width, ibounds.height);
135     }
136 }
137
Popular Tags