org.apache.batik.gvt.AbstractGraphicsNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of batik-gvt Show documentation
Show all versions of batik-gvt Show documentation
Batik Graphics Vector Tree (GVT)
The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.gvt;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.event.EventListenerList;
import org.apache.batik.ext.awt.RenderingHintsKeyExt;
import org.apache.batik.ext.awt.image.renderable.ClipRable;
import org.apache.batik.ext.awt.image.renderable.Filter;
import org.apache.batik.gvt.event.GraphicsNodeChangeEvent;
import org.apache.batik.gvt.event.GraphicsNodeChangeListener;
import org.apache.batik.gvt.filter.GraphicsNodeRable;
import org.apache.batik.gvt.filter.GraphicsNodeRable8Bit;
import org.apache.batik.gvt.filter.Mask;
import org.apache.batik.util.HaltingThread;
/**
* A partial implementation of the GraphicsNode
interface.
*
* @author Thierry Kormann
* @author Emmanuel Tissandier
* @author Thomas DeWeese
* @version $Id$
*/
public abstract class AbstractGraphicsNode implements GraphicsNode {
/**
* The listeners list.
*/
protected EventListenerList listeners;
/**
* The transform of this graphics node.
*/
protected AffineTransform transform;
/**
* The inverse transform for this node, i.e., from parent node
* to this node.
*/
protected AffineTransform inverseTransform;
/**
* The compositing operation to be used when a graphics node is
* painted on top of another one.
*/
protected Composite composite;
/**
* This flag bit indicates whether or not this graphics node is visible.
*/
protected boolean isVisible = true;
/**
* The clipping filter for this graphics node.
*/
protected ClipRable clip;
/**
* The rendering hints that control the quality to use when rendering
* this graphics node.
*/
protected RenderingHints hints;
/**
* The parent of this graphics node.
*/
protected CompositeGraphicsNode parent;
/**
* The root of the GVT tree.
*/
protected RootGraphicsNode root;
/**
* The mask of this graphics node.
*/
protected Mask mask;
/**
* The filter of this graphics node.
*/
protected Filter filter;
/**
* Indicates how this graphics node reacts to events.
*/
protected int pointerEventType = VISIBLE_PAINTED;
/**
* The GraphicsNodeRable for this node.
*/
protected WeakReference graphicsNodeRable;
/**
* The GraphicsNodeRable for this node with all filtering applied
*/
protected WeakReference enableBackgroundGraphicsNodeRable;
/**
* A Weak Reference to this.
*/
protected WeakReference weakRef;
/**
* Internal Cache: node bounds
*/
private Rectangle2D bounds;
protected GraphicsNodeChangeEvent changeStartedEvent = null;
protected GraphicsNodeChangeEvent changeCompletedEvent = null;
/**
* Constructs a new graphics node.
*/
protected AbstractGraphicsNode() {}
/**
* Returns a canonical WeakReference to this GraphicsNode.
* This is suitable for use as a key value in a hash map
*/
public WeakReference getWeakReference() {
if (weakRef == null)
weakRef = new WeakReference(this);
return weakRef;
}
//
// Properties methods
//
/**
* Returns the type that describes how this graphics node reacts to events.
*
* @return VISIBLE_PAINTED | VISIBLE_FILL | VISIBLE_STROKE | VISIBLE |
* PAINTED | FILL | STROKE | ALL | NONE
*/
public int getPointerEventType() {
return pointerEventType;
}
/**
* Sets the type that describes how this graphics node reacts to events.
*
* @param pointerEventType VISIBLE_PAINTED | VISIBLE_FILL | VISIBLE_STROKE |
* VISIBLE | PAINTED | FILL | STROKE | ALL | NONE
*/
public void setPointerEventType(int pointerEventType) {
this.pointerEventType = pointerEventType;
}
/**
* Sets the transform of this node.
*
* @param newTransform the new transform of this node
*/
public void setTransform(AffineTransform newTransform) {
fireGraphicsNodeChangeStarted();
this.transform = newTransform;
if(transform.getDeterminant() != 0){
try{
inverseTransform = transform.createInverse();
}catch(NoninvertibleTransformException e){
// Should never happen.
throw new RuntimeException( e.getMessage() );
}
} else {
// The transform is not invertible. Use the same
// transform.
inverseTransform = transform;
}
if (parent != null)
parent.invalidateGeometryCache();
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the transform of this node or null if any.
*/
public AffineTransform getTransform() {
return transform;
}
/**
* Returns the inverse transform for this node.
*/
public AffineTransform getInverseTransform(){
return inverseTransform;
}
/**
* Returns the concatenated transform of this node. That is, this
* node's transform preconcatenated with it's parent's transforms.
*/
public AffineTransform getGlobalTransform(){
AffineTransform ctm = new AffineTransform();
GraphicsNode node = this;
while (node != null) {
if(node.getTransform() != null){
ctm.preConcatenate(node.getTransform());
}
node = node.getParent();
}
return ctm;
}
/**
* Sets the composite of this node.
*
* @param newComposite the composite of this node
*/
public void setComposite(Composite newComposite) {
fireGraphicsNodeChangeStarted();
invalidateGeometryCache();
this.composite = newComposite;
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the composite of this node or null if any.
*/
public Composite getComposite() {
return composite;
}
/**
* Sets if this node is visible or not depending on the specified value.
*
* @param isVisible If true this node is visible
*/
public void setVisible(boolean isVisible) {
fireGraphicsNodeChangeStarted();
this.isVisible = isVisible;
invalidateGeometryCache();
fireGraphicsNodeChangeCompleted();
}
/**
* Returns true if this node is visible, false otherwise.
*/
public boolean isVisible() {
return isVisible;
}
public void setClip(ClipRable newClipper) {
if ((newClipper == null) && (this.clip == null))
return; // No change still no clip.
fireGraphicsNodeChangeStarted();
invalidateGeometryCache();
this.clip = newClipper;
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the clipping filter of this node or null if any.
*/
public ClipRable getClip() {
return clip;
}
/**
* Maps the specified key to the specified value in the rendering hints of
* this node.
*
* @param key the key of the hint to be set
* @param value the value indicating preferences for the specified
* hint category.
*/
public void setRenderingHint(RenderingHints.Key key, Object value) {
fireGraphicsNodeChangeStarted();
if (this.hints == null) {
this.hints = new RenderingHints(key, value);
} else {
hints.put(key, value);
}
fireGraphicsNodeChangeCompleted();
}
/**
* Copies all of the mappings from the specified Map to the
* rendering hints of this node.
*
* @param hints the rendering hints to be set
*/
public void setRenderingHints(Map hints) {
fireGraphicsNodeChangeStarted();
if (this.hints == null) {
this.hints = new RenderingHints(hints);
} else {
this.hints.putAll(hints);
}
fireGraphicsNodeChangeCompleted();
}
/**
* Sets the rendering hints of this node.
*
* @param newHints the new rendering hints of this node
*/
public void setRenderingHints(RenderingHints newHints) {
fireGraphicsNodeChangeStarted();
hints = newHints;
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the rendering hints of this node or null if any.
*/
public RenderingHints getRenderingHints() {
return hints;
}
/**
* Sets the mask of this node.
*
* @param newMask the new mask of this node
*/
public void setMask(Mask newMask) {
if ((newMask == null) && (mask == null))
return; // No change still no mask.
fireGraphicsNodeChangeStarted();
invalidateGeometryCache();
mask = newMask;
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the mask of this node or null if any.
*/
public Mask getMask() {
return mask;
}
/**
* Sets the filter of this node.
*
* @param newFilter the new filter of this node
*/
public void setFilter(Filter newFilter) {
if ((newFilter == null) && (filter == null))
return; // No change still no filter.
fireGraphicsNodeChangeStarted();
invalidateGeometryCache();
filter = newFilter;
fireGraphicsNodeChangeCompleted();
}
/**
* Returns the filter of this node or null if any.
*/
public Filter getFilter() {
return filter;
}
/**
* Returns the GraphicsNodeRable for this node. This
* GraphicsNodeRable is the Renderable (Filter) before any of the
* filter operations have been applied.
*/
public Filter getGraphicsNodeRable(boolean createIfNeeded) {
GraphicsNodeRable ret = null;
if (graphicsNodeRable != null) {
ret = (GraphicsNodeRable)graphicsNodeRable.get();
if (ret != null) return ret;
}
if (createIfNeeded) {
ret = new GraphicsNodeRable8Bit(this);
graphicsNodeRable = new WeakReference(ret);
}
return ret;
}
/**
* Returns the GraphicsNodeRable for this node. This
* GraphicsNodeRable is the Renderable (Filter) after all of the
* filter operations have been applied.
*/
public Filter getEnableBackgroundGraphicsNodeRable
(boolean createIfNeeded) {
GraphicsNodeRable ret = null;
if (enableBackgroundGraphicsNodeRable != null) {
ret = (GraphicsNodeRable)enableBackgroundGraphicsNodeRable.get();
if (ret != null) return ret;
}
if (createIfNeeded) {
ret = new GraphicsNodeRable8Bit(this);
ret.setUsePrimitivePaint(false);
enableBackgroundGraphicsNodeRable = new WeakReference(ret);
}
return ret;
}
//
// Drawing methods
//
/**
* Paints this node.
*
* @param g2d the Graphics2D to use
*/
public void paint(Graphics2D g2d){
if ((composite != null) &&
(composite instanceof AlphaComposite)) {
AlphaComposite ac = (AlphaComposite)composite;
if (ac.getAlpha() < 0.001)
return; // No point in drawing
}
Rectangle2D bounds = getBounds();
if (bounds == null) return;
// Set up graphic context. It is important to setup the
// transform first, because the clip is defined in this node's
// user space.
Composite defaultComposite = null;
AffineTransform defaultTransform = null;
RenderingHints defaultHints = null;
Graphics2D baseG2d = null;
if (clip != null) {
baseG2d = g2d;
g2d = (Graphics2D)g2d.create();
if (hints != null)
g2d.addRenderingHints(hints);
if (transform != null)
g2d.transform(transform);
if (composite != null)
g2d.setComposite(composite);
g2d.clip(clip.getClipPath());
} else {
if (hints != null) {
defaultHints = g2d.getRenderingHints();
g2d.addRenderingHints(hints);
}
if (transform != null) {
defaultTransform = g2d.getTransform();
g2d.transform(transform);
}
if (composite != null) {
defaultComposite = g2d.getComposite();
g2d.setComposite(composite);
}
}
Shape curClip = g2d.getClip();
g2d.setRenderingHint(RenderingHintsKeyExt.KEY_AREA_OF_INTEREST,
curClip);
// Check if any painting is needed at all. Get the clip (in user space)
// and see if it intersects with this node's bounds (in user space).
boolean paintNeeded = true;
Shape g2dClip = curClip; //g2d.getClip();
if (g2dClip != null) {
Rectangle2D cb = g2dClip.getBounds2D();
if(!bounds.intersects(cb.getX(), cb.getY(),
cb.getWidth(), cb.getHeight()))
paintNeeded = false;
}
// Only paint if needed.
if (paintNeeded){
boolean antialiasedClip = false;
if ((clip != null) && clip.getUseAntialiasedClip()) {
antialiasedClip = isAntialiasedClip(g2d.getTransform(),
g2d.getRenderingHints(),
clip.getClipPath());
}
boolean useOffscreen = isOffscreenBufferNeeded();
useOffscreen |= antialiasedClip;
if (!useOffscreen) {
// Render on this canvas.
primitivePaint(g2d);
} else {
Filter filteredImage = null;
if(filter == null){
filteredImage = getGraphicsNodeRable(true);
}
else {
// traceFilter(filter, "=====>> ");
filteredImage = filter;
}
if (mask != null) {
if (mask.getSource() != filteredImage){
mask.setSource(filteredImage);
}
filteredImage = mask;
}
if (clip != null && antialiasedClip) {
if (clip.getSource() != filteredImage){
clip.setSource(filteredImage);
}
filteredImage = clip;
}
baseG2d = g2d;
// Only muck with the clip on a 'child'
// graphics 2D otherwise when we restore the
// clip it might 'wander' by a pixel.
g2d = (Graphics2D)g2d.create();
if(antialiasedClip){
// Remove hard edged clip
g2d.setClip(null);
}
Rectangle2D filterBounds = filteredImage.getBounds2D();
g2d.clip(filterBounds);
org.apache.batik.ext.awt.image.GraphicsUtil.drawImage
(g2d, filteredImage);
g2d.dispose();
g2d = baseG2d;
baseG2d = null;// Don't leave null we need g2d restored...
}
}
// Restore default rendering attributes
if (baseG2d != null) {
g2d.dispose();
} else {
if (defaultHints != null)
g2d.setRenderingHints(defaultHints);
if (defaultTransform != null)
g2d.setTransform(defaultTransform);
if (defaultComposite != null) {
g2d.setComposite(defaultComposite);
}
}
}
/**
* DEBUG: Trace filter chain
*/
private void traceFilter(Filter filter, String prefix){
System.out.println(prefix + filter.getClass().getName());
System.out.println(prefix + filter.getBounds2D());
List sources = filter.getSources();
int nSources = sources != null ? sources.size() : 0;
prefix += "\t";
for(int i=0; i