X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fprofile%2FUpdater.java;h=2e79ef2160808332339bd79d72d97d4edec14fca;hb=00a0eae4da98b701fa38a6813b1743b754c5c99e;hp=f229aa9b0df20a46c5db73bd49b35b328a92ac32;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Updater.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Updater.java index f229aa9b0..2e79ef216 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Updater.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Updater.java @@ -1,92 +1,92 @@ -package org.simantics.diagram.profile; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.simantics.diagram.elements.DiagramNodeUtil; -import org.simantics.g2d.canvas.ICanvasContext; -import org.simantics.scenegraph.INode; -import org.simantics.scenegraph.g2d.G2DNode; -import org.simantics.utils.threads.ThreadUtils; - -class Updater implements Runnable { - - private static Updater INSTANCE; - - Map requesters = new HashMap(); - - AtomicBoolean state = new AtomicBoolean(false); - - private static long time = System.nanoTime(); - - private Updater() { - } - - @Override - public void run() { - - // Stop this if nothing is to be done, need synchonization since this is not AWT - synchronized(requesters) { - if(requesters.isEmpty()) { - state.set(false); - throw new RuntimeException(); - } - } - - // TODO: in unit tests this should not be AWT - ThreadUtils.AWT_EDT.execute(new Runnable() { - - @Override - public void run() { - - time = System.nanoTime(); - - synchronized(requesters) { - HashSet ctxSet = new HashSet(); - for(ICanvasContext ctx : requesters.values()) { - if(ctx != null) { - if(ctxSet.add(ctx)) ctx.getContentContext().setDirty(); - } - } - } - - } - - }); - - } - - public void register(INode node) { - // We use ths size of this map to determine whether updates are needed, this is done in AWT thread - synchronized(requesters) { - if(requesters.size() == 0) { - if(state.compareAndSet(false, true)) { - ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(this, 0, 500, TimeUnit.MILLISECONDS); - } - } - ICanvasContext context = DiagramNodeUtil.getPossibleCanvasContext((G2DNode)node); - requesters.put(node, context); - } - } - - public void unregister(INode node) { - synchronized(requesters) { - requesters.remove(node); - } - } - - public long getTime() { - return time; - } - - public static Updater getInstance() { - if(INSTANCE == null) { - INSTANCE = new Updater(); - } - return INSTANCE; - } - +package org.simantics.diagram.profile; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.simantics.diagram.elements.DiagramNodeUtil; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.scenegraph.INode; +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.utils.threads.ThreadUtils; + +class Updater implements Runnable { + + private static Updater INSTANCE; + + Map requesters = new HashMap(); + + AtomicBoolean state = new AtomicBoolean(false); + + private static long time = System.nanoTime(); + + private Updater() { + } + + @Override + public void run() { + + // Stop this if nothing is to be done, need synchonization since this is not AWT + synchronized(requesters) { + if(requesters.isEmpty()) { + state.set(false); + throw new RuntimeException(); + } + } + + // TODO: in unit tests this should not be AWT + ThreadUtils.AWT_EDT.execute(new Runnable() { + + @Override + public void run() { + + time = System.nanoTime(); + + synchronized(requesters) { + HashSet ctxSet = new HashSet(); + for(ICanvasContext ctx : requesters.values()) { + if(ctx != null) { + if(ctxSet.add(ctx)) ctx.getContentContext().setDirty(); + } + } + } + + } + + }); + + } + + public void register(INode node) { + // We use the size of this map to determine whether updates are needed, this is done in AWT thread + synchronized(requesters) { + if(requesters.size() == 0) { + if(state.compareAndSet(false, true)) { + ThreadUtils.getNonBlockingWorkExecutor().scheduleWithFixedDelay(this, 0, 500, TimeUnit.MILLISECONDS); + } + } + ICanvasContext context = DiagramNodeUtil.getPossibleCanvasContext((G2DNode)node); + requesters.put(node, context); + } + } + + public void unregister(INode node) { + synchronized(requesters) { + requesters.remove(node); + } + } + + public long getTime() { + return time; + } + + public static Updater getInstance() { + if(INSTANCE == null) { + INSTANCE = new Updater(); + } + return INSTANCE; + } + } \ No newline at end of file