001: package net.refractions.udig.mapgraphic.internal;
002:
003: import java.awt.Color;
004: import java.awt.Composite;
005: import java.awt.Font;
006: import java.awt.FontMetrics;
007: import java.awt.Graphics;
008: import java.awt.Graphics2D;
009: import java.awt.GraphicsConfiguration;
010: import java.awt.Image;
011: import java.awt.Paint;
012: import java.awt.Polygon;
013: import java.awt.Rectangle;
014: import java.awt.RenderingHints;
015: import java.awt.Shape;
016: import java.awt.Stroke;
017: import java.awt.RenderingHints.Key;
018: import java.awt.font.FontRenderContext;
019: import java.awt.font.GlyphVector;
020: import java.awt.geom.AffineTransform;
021: import java.awt.image.BufferedImage;
022: import java.awt.image.BufferedImageOp;
023: import java.awt.image.ImageObserver;
024: import java.awt.image.RenderedImage;
025: import java.awt.image.renderable.RenderableImage;
026: import java.io.IOException;
027: import java.text.AttributedCharacterIterator;
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: import net.refractions.udig.mapgraphic.MapGraphic;
034: import net.refractions.udig.mapgraphic.MapGraphicContext;
035: import net.refractions.udig.mapgraphic.MapGraphicPlugin;
036: import net.refractions.udig.project.internal.render.impl.RendererImpl;
037: import net.refractions.udig.project.render.ICompositeRenderContext;
038: import net.refractions.udig.project.render.IMultiLayerRenderer;
039: import net.refractions.udig.project.render.IRenderContext;
040: import net.refractions.udig.project.render.IRenderer;
041: import net.refractions.udig.project.render.RenderException;
042:
043: import org.eclipse.core.runtime.IProgressMonitor;
044: import org.eclipse.core.runtime.IStatus;
045: import org.eclipse.core.runtime.Status;
046: import org.eclipse.core.runtime.jobs.Job;
047:
048: /**
049: * Renderer for MapGraphic layers
050: */
051: public class MapGraphicRenderer extends RendererImpl implements
052: IMultiLayerRenderer {
053:
054: @Override
055: public String getName() {
056: return super .getName();
057: }
058:
059: /**
060: * @see net.refractions.udig.project.internal.render.impl.RendererImpl#render(java.awt.Graphics2D, IProgressMonitor)
061: * @param destination
062: */
063: @Override
064: public void render(Graphics2D destination, IProgressMonitor monitor) {
065: List<IOException> exceptions = new ArrayList<IOException>();
066:
067: final NonDisposableGraphics graphics = new NonDisposableGraphics(
068: destination);
069:
070: Shape clip = graphics.getClip();
071: AffineTransform transform = graphics.getTransform();
072: Color background = graphics.getBackground();
073: Color foreground = graphics.getColor();
074: Font font = graphics.getFont();
075: Composite composite = graphics.getComposite();
076: Paint paint = graphics.getPaint();
077: Map<Object, Object> hints = new HashMap<Object, Object>(
078: graphics.getRenderingHints());
079: Stroke stroke = graphics.getStroke();
080:
081: for (IRenderContext l : getContext().getContexts()) {
082: try {
083: if (!l.getLayer().isVisible())
084: continue;
085: MapGraphic mg = l.getGeoResource().resolve(
086: MapGraphic.class, null);
087: MapGraphicContext mgContext = new MapGraphicContextImpl(
088: l, graphics);
089: mg.draw(mgContext);
090: } catch (IOException e) {
091: exceptions.add(e);
092: } finally {
093: graphics.setBackground(background);
094: graphics.setClip(clip);
095: graphics.setColor(foreground);
096: graphics.setComposite(composite);
097: graphics.setFont(font);
098: graphics.setPaint(paint);
099: graphics.setRenderingHints(hints);
100: graphics.setStroke(stroke);
101: graphics.setTransform(transform);
102: }
103: setState(RENDERING);
104: }
105: if (!exceptions.isEmpty()) {
106: //XXX: externalize this message
107: RenderException exception = new RenderException(
108: exceptions.size()
109: + " exceptions we raised while drawing map graphics", exceptions.get(0)); //$NON-NLS-1$
110: exception.fillInStackTrace();
111: }
112: }
113:
114: /**
115: * @see net.refractions.udig.project.internal.render.impl.RendererImpl#getContext()
116: */
117: @Override
118: public synchronized ICompositeRenderContext getContext() {
119: return (ICompositeRenderContext) super .getContext();
120: }
121:
122: @Override
123: public synchronized void setContext(IRenderContext newContext) {
124: super .setContext(newContext);
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see net.refractions.udig.project.render.Renderer#render(com.vividsolutions.jts.geom.Envelope)
131: */
132: @Override
133: public void render(IProgressMonitor monitor) {
134: render(getContext().getImage().createGraphics(), monitor);
135: setState(DONE);
136: }
137:
138: Job refreshJob = new Job("RefreshJob") { //$NON-NLS-1$
139: @Override
140: protected IStatus run(IProgressMonitor monitor) {
141: getContext().clearImage();
142: try {
143:
144: render(monitor);
145: } catch (Throwable e) {
146: MapGraphicPlugin.log(null, e);
147: }
148: return Status.OK_STATUS;
149: }
150:
151: };
152:
153: public void refreshImage() throws RenderException {
154: refreshJob.schedule();
155: }
156:
157: @Override
158: public boolean isCacheable() {
159: return false;
160: }
161:
162: private class NonDisposableGraphics extends Graphics2D {
163: final Graphics2D graphics;
164:
165: public NonDisposableGraphics(final Graphics2D graphics) {
166: super ();
167: this .graphics = graphics;
168: }
169:
170: public void addRenderingHints(Map<?, ?> hints) {
171: graphics.addRenderingHints(hints);
172: }
173:
174: public void clearRect(int x, int y, int width, int height) {
175: graphics.clearRect(x, y, width, height);
176: }
177:
178: public void clip(Shape s) {
179: graphics.clip(s);
180: }
181:
182: public void clipRect(int x, int y, int width, int height) {
183: graphics.clipRect(x, y, width, height);
184: }
185:
186: public void copyArea(int x, int y, int width, int height,
187: int dx, int dy) {
188: graphics.copyArea(x, y, width, height, dx, dy);
189: }
190:
191: public Graphics create() {
192: return graphics.create();
193: }
194:
195: public Graphics create(int x, int y, int width, int height) {
196: return graphics.create(x, y, width, height);
197: }
198:
199: public void dispose() {
200: graphics.dispose();
201: }
202:
203: public void draw(Shape s) {
204: graphics.draw(s);
205: }
206:
207: public void draw3DRect(int x, int y, int width, int height,
208: boolean raised) {
209: graphics.draw3DRect(x, y, width, height, raised);
210: }
211:
212: public void drawArc(int x, int y, int width, int height,
213: int startAngle, int arcAngle) {
214: graphics.drawArc(x, y, width, height, startAngle, arcAngle);
215: }
216:
217: public void drawBytes(byte[] data, int offset, int length,
218: int x, int y) {
219: graphics.drawBytes(data, offset, length, x, y);
220: }
221:
222: public void drawChars(char[] data, int offset, int length,
223: int x, int y) {
224: graphics.drawChars(data, offset, length, x, y);
225: }
226:
227: public void drawGlyphVector(GlyphVector g, float x, float y) {
228: graphics.drawGlyphVector(g, x, y);
229: }
230:
231: public void drawImage(BufferedImage img, BufferedImageOp op,
232: int x, int y) {
233: graphics.drawImage(img, op, x, y);
234: }
235:
236: public boolean drawImage(Image img, AffineTransform xform,
237: ImageObserver obs) {
238: return graphics.drawImage(img, xform, obs);
239: }
240:
241: public boolean drawImage(Image img, int x, int y,
242: Color bgcolor, ImageObserver observer) {
243: return graphics.drawImage(img, x, y, bgcolor, observer);
244: }
245:
246: public boolean drawImage(Image img, int x, int y,
247: ImageObserver observer) {
248: return graphics.drawImage(img, x, y, observer);
249: }
250:
251: public boolean drawImage(Image img, int x, int y, int width,
252: int height, Color bgcolor, ImageObserver observer) {
253: return graphics.drawImage(img, x, y, width, height,
254: bgcolor, observer);
255: }
256:
257: public boolean drawImage(Image img, int x, int y, int width,
258: int height, ImageObserver observer) {
259: return graphics.drawImage(img, x, y, width, height,
260: observer);
261: }
262:
263: public boolean drawImage(Image img, int dx1, int dy1, int dx2,
264: int dy2, int sx1, int sy1, int sx2, int sy2,
265: Color bgcolor, ImageObserver observer) {
266: return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1,
267: sy1, sx2, sy2, bgcolor, observer);
268: }
269:
270: public boolean drawImage(Image img, int dx1, int dy1, int dx2,
271: int dy2, int sx1, int sy1, int sx2, int sy2,
272: ImageObserver observer) {
273: return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1,
274: sy1, sx2, sy2, observer);
275: }
276:
277: public void drawLine(int x1, int y1, int x2, int y2) {
278: graphics.drawLine(x1, y1, x2, y2);
279: }
280:
281: public void drawOval(int x, int y, int width, int height) {
282: graphics.drawOval(x, y, width, height);
283: }
284:
285: public void drawPolygon(int[] xPoints, int[] yPoints,
286: int nPoints) {
287: graphics.drawPolygon(xPoints, yPoints, nPoints);
288: }
289:
290: public void drawPolygon(Polygon p) {
291: graphics.drawPolygon(p);
292: }
293:
294: public void drawPolyline(int[] xPoints, int[] yPoints,
295: int nPoints) {
296: graphics.drawPolyline(xPoints, yPoints, nPoints);
297: }
298:
299: public void drawRect(int x, int y, int width, int height) {
300: graphics.drawRect(x, y, width, height);
301: }
302:
303: public void drawRenderableImage(RenderableImage img,
304: AffineTransform xform) {
305: graphics.drawRenderableImage(img, xform);
306: }
307:
308: public void drawRenderedImage(RenderedImage img,
309: AffineTransform xform) {
310: graphics.drawRenderedImage(img, xform);
311: }
312:
313: public void drawRoundRect(int x, int y, int width, int height,
314: int arcWidth, int arcHeight) {
315: graphics.drawRoundRect(x, y, width, height, arcWidth,
316: arcHeight);
317: }
318:
319: public void drawString(AttributedCharacterIterator iterator,
320: float x, float y) {
321: graphics.drawString(iterator, x, y);
322: }
323:
324: public void drawString(AttributedCharacterIterator iterator,
325: int x, int y) {
326: graphics.drawString(iterator, x, y);
327: }
328:
329: public void drawString(String s, float x, float y) {
330: graphics.drawString(s, x, y);
331: }
332:
333: public void drawString(String str, int x, int y) {
334: graphics.drawString(str, x, y);
335: }
336:
337: public void fill(Shape s) {
338: graphics.fill(s);
339: }
340:
341: public void fill3DRect(int x, int y, int width, int height,
342: boolean raised) {
343: graphics.fill3DRect(x, y, width, height, raised);
344: }
345:
346: public void fillArc(int x, int y, int width, int height,
347: int startAngle, int arcAngle) {
348: graphics.fillArc(x, y, width, height, startAngle, arcAngle);
349: }
350:
351: public void fillOval(int x, int y, int width, int height) {
352: graphics.fillOval(x, y, width, height);
353: }
354:
355: public void fillPolygon(int[] xPoints, int[] yPoints,
356: int nPoints) {
357: graphics.fillPolygon(xPoints, yPoints, nPoints);
358: }
359:
360: public void fillPolygon(Polygon p) {
361: graphics.fillPolygon(p);
362: }
363:
364: public void fillRect(int x, int y, int width, int height) {
365: graphics.fillRect(x, y, width, height);
366: }
367:
368: public void fillRoundRect(int x, int y, int width, int height,
369: int arcWidth, int arcHeight) {
370: graphics.fillRoundRect(x, y, width, height, arcWidth,
371: arcHeight);
372: }
373:
374: public void finalize() {
375: graphics.finalize();
376: }
377:
378: public Color getBackground() {
379: return graphics.getBackground();
380: }
381:
382: public Shape getClip() {
383: return graphics.getClip();
384: }
385:
386: public Rectangle getClipBounds() {
387: return graphics.getClipBounds();
388: }
389:
390: public Rectangle getClipBounds(Rectangle r) {
391: return graphics.getClipBounds(r);
392: }
393:
394: public Rectangle getClipRect() {
395: return graphics.getClipRect();
396: }
397:
398: public Color getColor() {
399: return graphics.getColor();
400: }
401:
402: public Composite getComposite() {
403: return graphics.getComposite();
404: }
405:
406: public GraphicsConfiguration getDeviceConfiguration() {
407: return graphics.getDeviceConfiguration();
408: }
409:
410: public Font getFont() {
411: return graphics.getFont();
412: }
413:
414: public FontMetrics getFontMetrics() {
415: return graphics.getFontMetrics();
416: }
417:
418: public FontMetrics getFontMetrics(Font f) {
419: return graphics.getFontMetrics(f);
420: }
421:
422: public FontRenderContext getFontRenderContext() {
423: return graphics.getFontRenderContext();
424: }
425:
426: public Paint getPaint() {
427: return graphics.getPaint();
428: }
429:
430: public Object getRenderingHint(Key hintKey) {
431: return graphics.getRenderingHint(hintKey);
432: }
433:
434: public RenderingHints getRenderingHints() {
435: return graphics.getRenderingHints();
436: }
437:
438: public Stroke getStroke() {
439: return graphics.getStroke();
440: }
441:
442: public AffineTransform getTransform() {
443: return graphics.getTransform();
444: }
445:
446: public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
447: return graphics.hit(rect, s, onStroke);
448: }
449:
450: public boolean hitClip(int x, int y, int width, int height) {
451: return graphics.hitClip(x, y, width, height);
452: }
453:
454: public void rotate(double theta, double x, double y) {
455: graphics.rotate(theta, x, y);
456: }
457:
458: public void rotate(double theta) {
459: graphics.rotate(theta);
460: }
461:
462: public void scale(double sx, double sy) {
463: graphics.scale(sx, sy);
464: }
465:
466: public void setBackground(Color color) {
467: graphics.setBackground(color);
468: }
469:
470: public void setClip(int x, int y, int width, int height) {
471: graphics.setClip(x, y, width, height);
472: }
473:
474: public void setClip(Shape clip) {
475: graphics.setClip(clip);
476: }
477:
478: public void setColor(Color c) {
479: graphics.setColor(c);
480: }
481:
482: public void setComposite(Composite comp) {
483: graphics.setComposite(comp);
484: }
485:
486: public void setFont(Font font) {
487: graphics.setFont(font);
488: }
489:
490: public void setPaint(Paint paint) {
491: graphics.setPaint(paint);
492: }
493:
494: public void setPaintMode() {
495: graphics.setPaintMode();
496: }
497:
498: public void setRenderingHint(Key hintKey, Object hintValue) {
499: graphics.setRenderingHint(hintKey, hintValue);
500: }
501:
502: public void setRenderingHints(Map<?, ?> hints) {
503: graphics.setRenderingHints(hints);
504: }
505:
506: public void setStroke(Stroke s) {
507: graphics.setStroke(s);
508: }
509:
510: public void setTransform(AffineTransform Tx) {
511: graphics.setTransform(Tx);
512: }
513:
514: public void setXORMode(Color c1) {
515: graphics.setXORMode(c1);
516: }
517:
518: public void shear(double shx, double shy) {
519: graphics.shear(shx, shy);
520: }
521:
522: public String toString() {
523: return graphics.toString();
524: }
525:
526: public void transform(AffineTransform Tx) {
527: graphics.transform(Tx);
528: }
529:
530: public void translate(double tx, double ty) {
531: graphics.translate(tx, ty);
532: }
533:
534: public void translate(int x, int y) {
535: graphics.translate(x, y);
536: }
537:
538: }
539:
540: }
|