X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fimage%2Fimpl%2FImageProxy.java;h=4d4c636ac5878dbb1d3d83aa03413870ef06960e;hp=abad207eac1facafc2426207e4899a28fb761f93;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 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 index abad207ea..4d4c636ac 100644 --- 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 @@ -1,136 +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(); - } - -} - +/******************************************************************************* + * 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(); + } + +} +