blob: 3938a7efb1b4a62f381ed06953ef0cb6a349e036 [file] [log] [blame]
jeffliuec1c4782006-05-24 14:16:24 +00001/*******************************************************************************
amywuecebb042007-04-10 20:07:35 +00002 * Copyright (c) 2002, 2006 IBM Corporation and others.
jeffliuec1c4782006-05-24 14:16:24 +00003 * 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
amywuecebb042007-04-10 20:07:35 +00007 *
jeffliuec1c4782006-05-24 14:16:24 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
csaltera3a2daa2005-06-15 06:53:51 +000013package org.eclipse.wst.xml.ui.internal.catalog;
14
15import java.util.Hashtable;
16
17import org.eclipse.jface.resource.CompositeImageDescriptor;
18import org.eclipse.jface.resource.ImageDescriptor;
19import org.eclipse.jface.resource.ImageRegistry;
20import org.eclipse.swt.graphics.Image;
21import org.eclipse.swt.graphics.ImageData;
22import org.eclipse.swt.graphics.Point;
23import org.eclipse.wst.xml.ui.internal.XMLUIPlugin;
24
david_williamsef7420b2006-11-23 04:30:09 +000025public class ImageFactory {
26 public static final int TOP_LEFT = 1;
27 public static final int TOP_RIGHT = 2;
28 public static final int BOTTOM_LEFT = 3;
29 public static final int BOTTOM_RIGHT = 4;
csaltera3a2daa2005-06-15 06:53:51 +000030
david_williamsef7420b2006-11-23 04:30:09 +000031 protected static final int[][] OVERLAY_COORDINATE = {{0, 0}, {0, 2}, {2, 0}, {2, 2}};
csaltera3a2daa2005-06-15 06:53:51 +000032
david_williamsef7420b2006-11-23 04:30:09 +000033 protected static ImageFactory INSTANCE = new ImageFactory();
csaltera3a2daa2005-06-15 06:53:51 +000034
david_williamsef7420b2006-11-23 04:30:09 +000035 protected Hashtable compositeImageTable = new Hashtable();
36
37 public ImageFactory() {
38 super();
csaltera3a2daa2005-06-15 06:53:51 +000039 }
40
david_williamsef7420b2006-11-23 04:30:09 +000041 public Image getImage(String iconName) {
42 ImageRegistry imageRegistry = XMLUIPlugin.getDefault().getImageRegistry();
43
44 if (imageRegistry.get(iconName) != null) {
45 return imageRegistry.get(iconName);
46 }
47 else {
48 imageRegistry.put(iconName, ImageDescriptor.createFromFile(getClass(), iconName));
49 return imageRegistry.get(iconName);
50 }
51 }
52
53 public Image createCompositeImage(Image base, Image overlay, int overlayPosition) {
54 String key = base + "*" + overlay + "*" + overlayPosition; //$NON-NLS-1$ //$NON-NLS-2$
55 Image result = (Image) compositeImageTable.get(key);
56 if (result == null) {
57 ImageDescriptor overlays[][] = new ImageDescriptor[3][3];
58 int[] coord = OVERLAY_COORDINATE[overlayPosition];
59 overlays[coord[1]][coord[0]] = new ImageBasedImageDescriptor(overlay);
60 OverlayIcon icon = new OverlayIcon(new ImageBasedImageDescriptor(base), overlays, new Point(16, 16));
61 result = icon.createImage();
62 compositeImageTable.put(key, result);
63 }
64 return result;
65 }
csaltera3a2daa2005-06-15 06:53:51 +000066
67
david_williamsef7420b2006-11-23 04:30:09 +000068 public static ImageDescriptor createImageDescriptorWrapper(Image image) {
69 return new ImageBasedImageDescriptor(image);
70 }
csaltera3a2daa2005-06-15 06:53:51 +000071
david_williamsef7420b2006-11-23 04:30:09 +000072 /**
73 * An OverlayIcon consists of a main icon and several adornments.
74 */
75 class OverlayIcon extends CompositeImageDescriptor {
csaltera3a2daa2005-06-15 06:53:51 +000076
david_williamsef7420b2006-11-23 04:30:09 +000077 static final int DEFAULT_WIDTH = 22;
csaltera3a2daa2005-06-15 06:53:51 +000078
david_williamsef7420b2006-11-23 04:30:09 +000079 static final int DEFAULT_HEIGHT = 16;
csaltera3a2daa2005-06-15 06:53:51 +000080
david_williamsef7420b2006-11-23 04:30:09 +000081 private Point fSize = null;
csaltera3a2daa2005-06-15 06:53:51 +000082
david_williamsef7420b2006-11-23 04:30:09 +000083 private ImageDescriptor fBase;
csaltera3a2daa2005-06-15 06:53:51 +000084
david_williamsef7420b2006-11-23 04:30:09 +000085 private ImageDescriptor fOverlays[][];
csaltera3a2daa2005-06-15 06:53:51 +000086
david_williamsef7420b2006-11-23 04:30:09 +000087 public OverlayIcon(ImageDescriptor base, ImageDescriptor[][] overlays, Point size) {
88 fBase = base;
89 fOverlays = overlays;
90 fSize = size;
91 }
csaltera3a2daa2005-06-15 06:53:51 +000092
david_williamsef7420b2006-11-23 04:30:09 +000093 protected void drawBottomLeft(ImageDescriptor[] overlays) {
94 if (overlays == null) {
95 return;
96 }
97 int length = overlays.length;
98 int x = 0;
99 for (int i = 0; i < 3; i++) {
100 if ((i < length) && (overlays[i] != null)) {
101 ImageData id = overlays[i].getImageData();
102 drawImage(id, x, getSize().y - id.height);
103 x += id.width;
104 }
105 }
106 }
csaltera3a2daa2005-06-15 06:53:51 +0000107
david_williamsef7420b2006-11-23 04:30:09 +0000108 protected void drawBottomRight(ImageDescriptor[] overlays) {
109 if (overlays == null) {
110 return;
111 }
112 int length = overlays.length;
113 int x = getSize().x;
114 for (int i = 2; i >= 0; i--) {
115 if ((i < length) && (overlays[i] != null)) {
116 ImageData id = overlays[i].getImageData();
117 x -= id.width;
118 drawImage(id, x, getSize().y - id.height);
119 }
120 }
121 }
csaltera3a2daa2005-06-15 06:53:51 +0000122
david_williamsef7420b2006-11-23 04:30:09 +0000123 /**
124 * @see CompositeImageDescriptor#drawCompositeImage(int, int)
125 */
126 protected void drawCompositeImage(int width, int height) {
127 ImageData bg;
128 if ((fBase == null) || ((bg = fBase.getImageData()) == null)) {
129 bg = DEFAULT_IMAGE_DATA;
130 }
131 drawImage(bg, 0, 0);
csaltera3a2daa2005-06-15 06:53:51 +0000132
david_williamsef7420b2006-11-23 04:30:09 +0000133 if (fOverlays != null) {
134 if (fOverlays.length > 0) {
135 drawTopRight(fOverlays[0]);
136 }
csaltera3a2daa2005-06-15 06:53:51 +0000137
david_williamsef7420b2006-11-23 04:30:09 +0000138 if (fOverlays.length > 1) {
139 drawBottomRight(fOverlays[1]);
140 }
csaltera3a2daa2005-06-15 06:53:51 +0000141
david_williamsef7420b2006-11-23 04:30:09 +0000142 if (fOverlays.length > 2) {
143 drawBottomLeft(fOverlays[2]);
144 }
csaltera3a2daa2005-06-15 06:53:51 +0000145
david_williamsef7420b2006-11-23 04:30:09 +0000146 if (fOverlays.length > 3) {
147 drawTopLeft(fOverlays[3]);
148 }
149 }
150 }
csaltera3a2daa2005-06-15 06:53:51 +0000151
david_williamsef7420b2006-11-23 04:30:09 +0000152 protected void drawTopLeft(ImageDescriptor[] overlays) {
153 if (overlays == null) {
154 return;
155 }
156 int length = overlays.length;
157 int x = 0;
158 for (int i = 0; i < 3; i++) {
159 if ((i < length) && (overlays[i] != null)) {
160 ImageData id = overlays[i].getImageData();
161 drawImage(id, x, 0);
162 x += id.width;
163 }
164 }
165 }
csaltera3a2daa2005-06-15 06:53:51 +0000166
david_williamsef7420b2006-11-23 04:30:09 +0000167 protected void drawTopRight(ImageDescriptor[] overlays) {
168 if (overlays == null) {
169 return;
170 }
171 int length = overlays.length;
172 int x = getSize().x;
173 for (int i = 2; i >= 0; i--) {
174 if ((i < length) && (overlays[i] != null)) {
175 ImageData id = overlays[i].getImageData();
176 x -= id.width;
177 drawImage(id, x, 0);
178 }
179 }
180 }
csaltera3a2daa2005-06-15 06:53:51 +0000181
david_williamsef7420b2006-11-23 04:30:09 +0000182 /**
183 * @see CompositeImageDescriptor#getSize()
184 */
185 protected Point getSize() {
186 return fSize;
187 }
188 }
csaltera3a2daa2005-06-15 06:53:51 +0000189
david_williamsef7420b2006-11-23 04:30:09 +0000190 static class ImageBasedImageDescriptor extends ImageDescriptor {
191 protected Image image;
csaltera3a2daa2005-06-15 06:53:51 +0000192
david_williamsef7420b2006-11-23 04:30:09 +0000193 public ImageBasedImageDescriptor(Image image) {
194 this.image = image;
195 }
csaltera3a2daa2005-06-15 06:53:51 +0000196
david_williamsef7420b2006-11-23 04:30:09 +0000197 public ImageData getImageData() {
198 return image.getImageData();
199 }
200 }
csaltera3a2daa2005-06-15 06:53:51 +0000201}