X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fimage%2Fimpl%2FImageProxy.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fimage%2Fimpl%2FImageProxy.java;h=abad207eac1facafc2426207e4899a28fb761f93;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/ImageProxy.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/ImageProxy.java new file mode 100644 index 000000000..abad207ea --- /dev/null +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/ImageProxy.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g2d.image.impl; + +import java.awt.Shape; +import java.awt.geom.Rectangle2D; +import java.lang.ref.WeakReference; +import java.util.EnumSet; + +import org.simantics.g2d.image.Image; +import org.simantics.scenegraph.Node; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.ListenerList; + +/** + * ImageProxy keeps strong reference to an original image, but weak reference + * from original image to proxy image. + *

+ * Listeners attached to ImageProxy relays events from the original image. + * + * @author Toni Kalajainen + */ +public class ImageProxy implements Image { + + ListenerList list = null; + + /** Strong reference to the original image */ + Image source; + Node node = null; + /** Listener for the original image */ + WeakImageListener sourceListener; + + public ImageProxy(Image original) { + if (original==null) throw new IllegalArgumentException("null arg"); + this.source = original; + } + + protected void notifyChanged() + { + ImageListener[] listeners; + synchronized(this) { + if (list==null) return; + listeners = list.getListeners(); + } + for (ImageListener l : listeners) + l.onContentChangedNotification(this); + } + + @Override + public synchronized void addImageListener(ImageListener listener) { + if (list==null) list = new ListenerList(ImageListener.class); + if (sourceListener==null) { + sourceListener = new WeakImageListener(this); + source.addImageListener(sourceListener); + } + list.add(listener); + } + + @Override + public synchronized void removeImageListener(ImageListener listener) { + if (list==null) return; + list.remove(listener); + if (list.isEmpty()) { + source.removeImageListener(sourceListener); + } + } + + static class WeakImageListener implements ImageListener { + WeakReference ref; + public WeakImageListener(ImageProxy img) + { + ref = new WeakReference(img); + } + + @Override + public void onContentChangedNotification(Image image) { + ImageProxy i = ref.get(); + if (i == null) { + image.removeImageListener(this); + } else { + i.notifyChanged(); + } + } + } + + @Override + public Rectangle2D getBounds() { + return source.getBounds(); + } + + @Override + public EnumSet getFeatures() { + return source.getFeatures(); + } + + @Override + public Shape getOutline() { + return source.getOutline(); + } + + @Override + public Node init(G2DParentNode parent) { + this.node = source.init(parent); + return this.node; + } + + public synchronized void setSource(Image newSource) { + if (newSource==null) throw new IllegalArgumentException("null"); + if (source == newSource) return; + Image oldSource = source; + if(node != null) { + node.remove(); // FIXME + node = null; + } + + source = newSource; + + if (sourceListener!=null) { + oldSource.removeImageListener(sourceListener); + newSource.addImageListener(sourceListener); + } + + notifyChanged(); + } + +} +