001: package net.refractions.udig.style.sld;
002:
003: import org.eclipse.jface.resource.ImageDescriptor;
004: import org.eclipse.swt.SWT;
005: import org.eclipse.swt.graphics.Color;
006: import org.eclipse.swt.graphics.GC;
007: import org.eclipse.swt.graphics.Image;
008: import org.eclipse.swt.graphics.ImageData;
009: import org.eclipse.swt.graphics.PaletteData;
010: import org.eclipse.swt.graphics.RGB;
011: import org.eclipse.swt.widgets.Display;
012: import org.eclipse.ui.PlatformUI;
013:
014: public class StyleGlyph {
015:
016: static final int DEFAULT_WIDTH = 16;
017: static final int DEFAULT_HEIGHT = 16;
018: static final int DEFAULT_DEPTH = 24;
019:
020: private static Image image(Display display, RGB[] rgb) {
021: PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
022: ImageData imageData = new ImageData(DEFAULT_WIDTH,
023: DEFAULT_HEIGHT, DEFAULT_DEPTH, palette);
024: imageData.transparentPixel = palette.getPixel(display
025: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB());
026:
027: return new Image(display, imageData);
028: }
029:
030: private static ImageDescriptor descriptor(ImageData imageData,
031: Image image, GC gc) {
032: final ImageData finalImageData = (ImageData) image
033: .getImageData().clone();
034:
035: image.dispose();
036: gc.dispose();
037:
038: return new ImageDescriptor() {
039: public ImageData getImageData() {
040: return finalImageData;
041: }
042: };
043: }
044:
045: public static ImageDescriptor point(int color, int width) {
046: Display display = PlatformUI.getWorkbench().getDisplay();
047: Color fill = display.getSystemColor(color);
048: Color back = display
049: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
050:
051: Image image = image(display, new RGB[] { fill.getRGB(),
052: back.getRGB() });
053: GC gc = new GC(image);
054:
055: ImageData imageData = image.getImageData();
056: int w = imageData.width;
057: int h = imageData.height;
058:
059: gc.setBackground(back);
060: gc.fillRectangle(0, 0, w, h);
061:
062: gc.setBackground(fill);
063: gc.fillOval(w / 2 - width / 2, h / 2 - width / 2, width, width);
064:
065: return descriptor(imageData, image, gc);
066: }
067:
068: public static ImageDescriptor text(int color, int width) {
069: Display display = PlatformUI.getWorkbench().getDisplay();
070: Color line = display.getSystemColor(color);
071: Color back = display
072: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
073:
074: Image image = image(display, new RGB[] { line.getRGB(),
075: back.getRGB() });
076: GC gc = new GC(image);
077:
078: ImageData imageData = image.getImageData();
079: int w = imageData.width;
080: int h = imageData.height;
081:
082: gc.setBackground(back);
083: gc.fillRectangle(0, 0, w, h);
084:
085: gc.setForeground(line);
086: gc.setLineWidth(width);
087:
088: gc.drawLine(2, 2, 2, 4);
089: gc.drawLine(2, 2, w - 2, 2);
090: gc.drawLine(w - 2, 2, h - 2, 4);
091: gc.drawLine(8, 2, 8, h - 2);
092: gc.drawLine(4, h - 2, w - 4, h - 2);
093:
094: return descriptor(imageData, image, gc);
095: }
096:
097: public static void disable(ImageDescriptor descriptor) {
098: ImageData imageData = descriptor.getImageData();
099: //PaletteData paletteData = imageData.palette;
100:
101: for (int i = 2; i < DEFAULT_WIDTH - 2; i++) {
102: imageData.setPixel(i, i, 0);
103: }
104: }
105:
106: public static ImageDescriptor line(int color, int width) {
107:
108: Display display = PlatformUI.getWorkbench().getDisplay();
109: Color line = display.getSystemColor(color);
110: Color back = display
111: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
112:
113: Image image = image(display, new RGB[] { line.getRGB(),
114: back.getRGB() });
115: GC gc = new GC(image);
116:
117: ImageData imageData = image.getImageData();
118: int w = imageData.width;
119: int h = imageData.height;
120:
121: gc.setBackground(back);
122: gc.fillRectangle(0, 0, w, h);
123:
124: gc.setBackground(line);
125: gc.setForeground(line);
126: gc.setLineWidth(width);
127: gc.drawLine(1, h / 2, w - 1, h / 2);
128:
129: return descriptor(imageData, image, gc);
130: }
131:
132: public static ImageDescriptor polygon(int lineColor, int fillColor,
133: int width) {
134: Display display = PlatformUI.getWorkbench().getDisplay();
135: Color line = display.getSystemColor(lineColor);
136: Color fill = display.getSystemColor(fillColor);
137: Color back = display
138: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
139:
140: Image image = image(display, new RGB[] { line.getRGB(),
141: fill.getRGB(), back.getRGB() });
142: GC gc = new GC(image);
143:
144: ImageData imageData = image.getImageData();
145: int w = imageData.width;
146: int h = imageData.height;
147:
148: gc.setBackground(back);
149: gc.fillRectangle(0, 0, w, h);
150:
151: gc.setBackground(fill);
152: gc.fillRectangle(2, 2, w - 4, h - 4);
153:
154: gc.setForeground(line);
155: gc.setLineWidth(width);
156: gc.drawRectangle(2, 2, w - 4, w - 4);
157:
158: return descriptor(imageData, image, gc);
159: }
160:
161: public static ImageDescriptor raster(int lineColor, int fillColor,
162: int width) {
163: Display display = PlatformUI.getWorkbench().getDisplay();
164: Color line = display.getSystemColor(lineColor);
165: Color fill = display.getSystemColor(fillColor);
166: Color back = display
167: .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
168:
169: Image image = image(display, new RGB[] { line.getRGB(),
170: fill.getRGB(), back.getRGB() });
171: GC gc = new GC(image);
172:
173: ImageData imageData = image.getImageData();
174: int w = imageData.width;
175: int h = imageData.height;
176:
177: gc.setBackground(back);
178: gc.fillRectangle(0, 0, w, h);
179:
180: gc.setBackground(fill);
181: gc.fillRectangle(2, 2, w - 4, h - 4);
182:
183: gc.setForeground(line);
184: gc.setLineWidth(width);
185: gc.drawRectangle(2, 2, w - 4, w - 4);
186:
187: return descriptor(imageData, image, gc);
188: }
189: }
|