001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui.renderer;
033:
034: import java.awt.Graphics2D;
035: import javax.swing.SwingUtilities;
036: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
037: import com.vividsolutions.jump.workbench.ui.WorkbenchFrame;
038:
039: public abstract class ImageCachingRenderer implements Renderer {
040: protected volatile boolean cancelled = false;
041: private Object contentID;
042: protected volatile ThreadSafeImage image = null;
043: protected LayerViewPanel panel;
044: protected volatile boolean rendering = false;
045:
046: public ImageCachingRenderer(Object contentID, LayerViewPanel panel) {
047: this .contentID = contentID;
048: this .panel = panel;
049: }
050:
051: public void clearImageCache() {
052: image = null;
053: }
054:
055: public boolean isRendering() {
056: return rendering;
057: }
058:
059: public Object getContentID() {
060: return contentID;
061: }
062:
063: protected ThreadSafeImage getImage() {
064: return image;
065: }
066:
067: public void copyTo(Graphics2D graphics) {
068: //Some subclasses override #getImage [Jon Aquino]
069: if (getImage() == null) {
070: return;
071: }
072: getImage().copyTo(graphics, null);
073: }
074:
075: public Runnable createRunnable() {
076: if (image != null) {
077: return null;
078: }
079: //Rendering starts as soon as the #createRunnable request is made,
080: //to get the animated clock icons going. [Jon Aquino]
081: rendering = true;
082: cancelled = false;
083: return new Runnable() {
084: public void run() {
085: try {
086: if (cancelled) {
087: //This short-circuit exit made a big speed increase
088: // (21 March 2003). [Jon Aquino]
089: return;
090: }
091: image = new ThreadSafeImage(panel);
092: try {
093: renderHook(image);
094: } catch (Throwable t) {
095: panel.getContext().warnUser(
096: WorkbenchFrame.toMessage(t));
097: t.printStackTrace(System.err);
098: return;
099: }
100: //Don't wait for the RenderingManager's 1-second
101: // repaint-timer. [Jon Aquino]
102: SwingUtilities.invokeLater(new Runnable() {
103: public void run() {
104: panel.super Repaint();
105: }
106: });
107: } finally {
108: rendering = false;
109: }
110: }
111: };
112: }
113:
114: protected abstract void renderHook(ThreadSafeImage image)
115: throws Exception;
116:
117: public void cancel() {
118: cancelled = true;
119: }
120: }
|