001: /**
002: * <copyright></copyright> $Id$
003: */package net.refractions.udig.project.internal.render.impl;
004:
005: import java.awt.Color;
006: import java.awt.Graphics2D;
007: import java.awt.Point;
008: import java.awt.Rectangle;
009: import java.awt.geom.AffineTransform;
010: import java.awt.image.BufferedImage;
011:
012: import net.refractions.udig.catalog.IGeoResource;
013: import net.refractions.udig.project.ILayer;
014: import net.refractions.udig.project.ProjectBlackboardConstants;
015: import net.refractions.udig.project.internal.Layer;
016: import net.refractions.udig.project.internal.ProjectPlugin;
017: import net.refractions.udig.project.internal.Trace;
018: import net.refractions.udig.project.internal.impl.AbstractContextImpl;
019: import net.refractions.udig.project.internal.render.RenderContext;
020: import net.refractions.udig.project.internal.render.SelectionLayer;
021: import net.refractions.udig.project.render.ILabelPainter;
022:
023: import org.geotools.data.DefaultQuery;
024: import org.geotools.data.Query;
025: import org.geotools.filter.Filter;
026: import org.geotools.filter.FilterFactory;
027: import org.geotools.filter.FilterFactoryFinder;
028: import org.geotools.filter.FilterType;
029: import org.geotools.filter.IllegalFilterException;
030:
031: /**
032: * The default implementation of the RenderContext interface.
033: *
034: * @author Jesse
035: * @since 1.0.0
036: */
037: public class RenderContextImpl extends AbstractContextImpl implements
038: RenderContext {
039:
040: /**
041: * The cached value of the '{@link #getImage() <em>Image</em>}' attribute.
042: *
043: * @see #getImage()
044: */
045: protected volatile BufferedImage image = null;
046:
047: public static final BufferedImage dummyImage = new BufferedImage(1,
048: 1, BufferedImage.TYPE_4BYTE_ABGR);
049:
050: private static final String LABEL_PAINTER = "LABEL_PAINTER";
051:
052: /**
053: * The cached value of the '{@link #getLayerInternal() <em>Layer Internal</em>}' reference.
054: *
055: * @see #getLayerInternal()
056: */
057: protected Layer layerInternal = null;
058:
059: /**
060: * The cached value of the '{@link #getGeoResourceInternal() <em>Geo Resource Internal</em>}'
061: * attribute.
062: *
063: * @see #getGeoResourceInternal()
064: */
065: protected IGeoResource geoResourceInternal = null;
066:
067: private boolean selection;
068:
069: public RenderContextImpl() {
070: super ();
071: }
072:
073: public RenderContextImpl(boolean selection) {
074: super ();
075: this .selection = selection;
076: }
077:
078: public RenderContextImpl(RenderContextImpl impl) {
079: super (impl);
080: setGeoResourceInternal(impl.getGeoResourceInternal());
081: setLayerInternal(impl.getLayerInternal());
082: }
083:
084: public BufferedImage getImage() {
085: int width = getMapDisplay().getWidth(), height = getMapDisplay()
086: .getHeight();
087: return getImage(width, height);
088: }
089:
090: public synchronized BufferedImage getImage(int width, int height) {
091:
092: if (width < 1 || height < 1)
093: return dummyImage;
094:
095: if (image == null || image.getWidth() < width
096: || image.getHeight() < height) {
097: synchronized (this ) {
098: if (image == null || image.getWidth() < width
099: || image.getHeight() < height) {
100: image = new BufferedImage(width, height,
101: BufferedImage.TYPE_INT_ARGB);
102: }
103:
104: }
105: }
106: return image;
107: }
108:
109: public Query getQuery(ILayer layer) {
110: return layer.getQuery(selection);
111: }
112:
113: /**
114: * <p>
115: * Default behavior is to return 0 if no layers are associated otherwise returns the zorder of
116: * the first layer in the list.
117: * </p>
118: *
119: * @return Default behavior is to return 0 if no layers are associated otherwise returns the
120: * zorder of the first layer in the list. <!-- end-user-doc -->
121: */
122: public int getZorder() {
123: if (getLayer() == null) {
124: return 0;
125: }
126: return getLayer().getZorder();
127: }
128:
129: public boolean isVisible() {
130: return getLayer().isVisible();
131: }
132:
133: public Layer getLayerInternal() {
134: return layerInternal;
135: }
136:
137: public void setLayerInternal(Layer newLayerInternal) {
138: layerInternal = newLayerInternal;
139: }
140:
141: public IGeoResource getGeoResourceInternal() {
142: return geoResourceInternal;
143: }
144:
145: public void setGeoResourceInternal(
146: IGeoResource newGeoResourceInternal) {
147: geoResourceInternal = newGeoResourceInternal;
148: }
149:
150: public String toString() {
151: StringBuffer result = new StringBuffer();
152: result.append("[layer: "); //$NON-NLS-1$
153: if (layerInternal instanceof SelectionLayer)
154: result.append("Selection "); //$NON-NLS-1$
155: result
156: .append(layerInternal == null ? "null" : layerInternal.getName()); //$NON-NLS-1$
157: result.append(", geoResource: "); //$NON-NLS-1$
158: result
159: .append(geoResourceInternal == null ? "null" : geoResourceInternal.getIdentifier().toString()); //$NON-NLS-1$
160: result.append(']');
161: return result.toString();
162: }
163:
164: public boolean hasContent(Point screenLocation) {
165: BufferedImage image = getImage();
166:
167: int alpha = image.getAlphaRaster().getSample(screenLocation.x,
168: screenLocation.y, 0);
169:
170: return alpha > 0;
171:
172: // ColorModel cm = image.getColorModel();
173: // SampleModel sm = image.getSampleModel();
174: // WritableRaster raster = image.getRaster();
175: //
176: //
177: // Object array;
178: // switch( sm.getTransferType() ){
179: // case DataBuffer.TYPE_BYTE:
180: // array = raster.getPixel( screenLocation.x, screenLocation.y, new
181: // byte[
182: // raster.getNumBands() ] );
183: // break;
184: // case DataBuffer.TYPE_USHORT:
185: // array = raster.getPixel( screenLocation.x, screenLocation.y, new
186: // short[
187: // raster.getNumBands() ] );
188: // break;
189: // case DataBuffer.TYPE_INT:
190: // array = raster.getPixel( screenLocation.x, screenLocation.y, new int[
191: // raster.getNumBands() ] );
192: // break;
193: // default:
194: // return false; // I give up
195: // }
196: // cm.getAlpha( array );
197: // return false;
198: }
199:
200: public BufferedImage copyImage(Rectangle rectangle) {
201: return null;
202: }
203:
204: public int compareTo(RenderContext o) {
205: if (o == null)
206: return 1;
207: if (o == this )
208: return 0;
209: int result = getLayer().compareTo(o.getLayer());
210:
211: // don't have same rendermanager then they are not the same.
212: if (result == 0 && getRenderManager() != o.getRenderManager())
213: return 1;
214: return result;
215: }
216:
217: public void init(RenderContext renderContext) {
218: setMapInternal(renderContext.getMapInternal());
219: setRenderManagerInternal(renderContext
220: .getRenderManagerInternal());
221: setLayerInternal(renderContext.getLayerInternal());
222: setGeoResourceInternal(renderContext.getGeoResourceInternal());
223: }
224:
225: public void clearImage() {
226: clearImage(new Rectangle(0, 0, getMapDisplay().getWidth(),
227: getMapDisplay().getHeight()));
228: }
229:
230: public Query getFeatureQuery() {
231: Query query = getLayer().getQuery(
232: getLayer() instanceof SelectionLayer);
233: if (query.getFilter() == Filter.ALL)
234: return query;
235: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
236: Object editFilter = getLayer().getBlackboard().get(
237: ProjectBlackboardConstants.MAP__RENDERING_FILTER);
238: if (!(editFilter instanceof Filter))
239: return query;
240:
241: Filter newFilter;
242: try {
243: if (query.getFilter() == Filter.NONE) {
244: newFilter = ff.createLogicFilter((Filter) editFilter,
245: FilterType.LOGIC_NOT);
246: } else {
247: editFilter = ff.createLogicFilter((Filter) editFilter,
248: FilterType.LOGIC_NOT);
249: newFilter = ff.createLogicFilter(query.getFilter(),
250: (Filter) editFilter, FilterType.LOGIC_AND);
251: }
252: } catch (IllegalFilterException e) {
253: return query;
254: }
255: return new DefaultQuery(query.getTypeName(), query
256: .getNamespace(), newFilter, query.getMaxFeatures(),
257: query.getPropertyNames(), query.getHandle());
258: }
259:
260: public synchronized void clearImage(Rectangle paintArea) {
261: if (ProjectPlugin.isDebugging(Trace.RENDER)) {
262: ProjectPlugin.trace(getClass(), "", null); //$NON-NLS-1$
263: }
264: Graphics2D graphics = getImage().createGraphics();
265: graphics.setBackground(new Color(0, 0, 0, 0));
266: graphics.setTransform(new AffineTransform());
267: graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
268: paintArea.height);
269: graphics.dispose();
270: }
271:
272: public void setStatus(int status) {
273: if (layerInternal != null
274: && !(layerInternal instanceof SelectionLayer))
275: layerInternal.setStatus(status);
276: }
277:
278: public ILayer getLayer() {
279: return getLayerInternal();
280: }
281:
282: public IGeoResource getGeoResource() {
283: return getGeoResourceInternal();
284: }
285:
286: public int getStatus() {
287: if (getLayer() == null)
288: return -1;
289: return getLayer().getStatus();
290: }
291:
292: public String getStatusMessage() {
293: if (getLayer() == null)
294: return null;
295: return getLayer().getStatusMessage();
296: }
297:
298: public void setStatusMessage(String message) {
299: if (getLayerInternal() != null)
300: getLayerInternal().setStatusMessage(message);
301: }
302:
303: public void dispose() {
304: image = null;
305: }
306:
307: public RenderContextImpl copy() {
308: return new RenderContextImpl(this );
309: }
310:
311: @Override
312: public int hashCode() {
313: final int PRIME = 31;
314: int result = super .hashCode();
315: result = PRIME
316: * result
317: + ((geoResourceInternal == null) ? 0
318: : geoResourceInternal.hashCode());
319: result = PRIME
320: * result
321: + ((layerInternal == null) ? 0 : layerInternal
322: .hashCode());
323: return result;
324: }
325:
326: @Override
327: public boolean equals(Object obj) {
328: if (!(obj instanceof RenderContextImpl))
329: return false;
330: if (this == obj)
331: return true;
332: if (!super .equals(obj))
333: return false;
334: if (getClass() != obj.getClass())
335: return false;
336: final RenderContextImpl other = (RenderContextImpl) obj;
337: if (geoResourceInternal == null) {
338: if (other.geoResourceInternal != null)
339: return false;
340: } else if (!geoResourceInternal
341: .equals(other.geoResourceInternal))
342: return false;
343: if (layerInternal == null) {
344: if (other.layerInternal != null)
345: return false;
346: } else if (!layerInternal.equals(other.layerInternal))
347: return false;
348: return true;
349: }
350:
351: public synchronized ILabelPainter getLabelPainter() {
352: ILabelPainter labelPainter = (ILabelPainter) getMap()
353: .getBlackboard().get(LABEL_PAINTER);
354: if (labelPainter == null) {
355: labelPainter = new UDIGLabelCache();
356: getMap().getBlackboard().put(LABEL_PAINTER, labelPainter);
357: }
358: return labelPainter;
359: }
360: } // RenderContextImpl
|