X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FLazyGraphLabeler.java;h=cf410444ed7753fc9a4d6df73882211fdc811f9e;hp=73fe008c241c10828f9874e384000d14276c8470;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java index 73fe008c2..cf410444e 100644 --- a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java @@ -1,119 +1,119 @@ -/******************************************************************************* - * 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.browsing.ui.graph.impl; - -import java.util.Collections; -import java.util.Map; - -import org.simantics.browsing.ui.BuiltinKeys.LabelerKey; -import org.simantics.browsing.ui.DataSource; -import org.simantics.browsing.ui.NodeContext; -import org.simantics.browsing.ui.PrimitiveQueryUpdater; -import org.simantics.browsing.ui.common.labelers.LabelerContent; -import org.simantics.browsing.ui.common.labelers.LabelerStub; -import org.simantics.browsing.ui.graph.impl.request.ResourceQuery; -import org.simantics.db.ReadGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.procedure.Listener; -import org.simantics.utils.ui.ErrorLogger; - -public abstract class LazyGraphLabeler extends LabelerStub { - - protected final PrimitiveQueryUpdater updater; - private final ResourceQuery labelQuery; - private final Listener procedure; - - public Object getIdentity(LabelerKey key) { - return key; - } - - public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) { - this.updater = updater; - - labelQuery = new ResourceQuery(getIdentity(key), context) { - - @Override - public LabelerContent perform(ReadGraph graph) throws DatabaseException { - try { - int cat = category(graph); - Map lbls = labels(graph); - // Make sure that null is not returned. - if (lbls == null) - throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it"); - return new LabelerContent(cat, lbls); - } catch (DatabaseException e) { - throw e; - } catch (Throwable t) { - ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t); - return LabelerContent.NO_CONTENT; - } - } - - @Override - public String toString() { - return LazyGraphLabeler.this + " with context " + context; - } - - }; - - procedure = new Listener() { - - @Override - public void execute(LabelerContent result) { - setContent(result); - updater.scheduleReplace(context, key, LazyGraphLabeler.this); - } - - @Override - public boolean isDisposed() { - return updater.isDisposed(); - } - - @Override - public void exception(Throwable t) { - System.err.println("LazyGraphLabeler2: "); - t.printStackTrace(); - } - - }; - } - - @Override - public Map getLabels() { - - if (content == LabelerContent.NO_CONTENT) { - - final DataSource source = updater.getDataSource(ReadGraph.class); - assert(source != null); - source.schedule(graph -> graph.asyncRequest(labelQuery, procedure)); - - } - - if(content == null) return Collections.emptyMap(); - - return content.labels; - - } - - // OVERRIDE - - public abstract Map labels(ReadGraph graph) throws DatabaseException; - - public int category(ReadGraph graph) throws DatabaseException { - - if(content == null) return 0; - - return content.category; - - } - -} +/******************************************************************************* + * 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.browsing.ui.graph.impl; + +import java.util.Collections; +import java.util.Map; + +import org.simantics.browsing.ui.BuiltinKeys.LabelerKey; +import org.simantics.browsing.ui.DataSource; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.PrimitiveQueryUpdater; +import org.simantics.browsing.ui.common.labelers.LabelerContent; +import org.simantics.browsing.ui.common.labelers.LabelerStub; +import org.simantics.browsing.ui.graph.impl.request.ResourceQuery; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Listener; +import org.simantics.utils.ui.ErrorLogger; + +public abstract class LazyGraphLabeler extends LabelerStub { + + protected final PrimitiveQueryUpdater updater; + private final ResourceQuery labelQuery; + private final Listener procedure; + + public Object getIdentity(LabelerKey key) { + return key; + } + + public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) { + this.updater = updater; + + labelQuery = new ResourceQuery(getIdentity(key), context) { + + @Override + public LabelerContent perform(ReadGraph graph) throws DatabaseException { + try { + int cat = category(graph); + Map lbls = labels(graph); + // Make sure that null is not returned. + if (lbls == null) + throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it"); + return new LabelerContent(cat, lbls); + } catch (DatabaseException e) { + throw e; + } catch (Throwable t) { + ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t); + return LabelerContent.NO_CONTENT; + } + } + + @Override + public String toString() { + return LazyGraphLabeler.this + " with context " + context; + } + + }; + + procedure = new Listener() { + + @Override + public void execute(LabelerContent result) { + setContent(result); + updater.scheduleReplace(context, key, LazyGraphLabeler.this); + } + + @Override + public boolean isDisposed() { + return updater.isDisposed(); + } + + @Override + public void exception(Throwable t) { + System.err.println("LazyGraphLabeler2: "); + t.printStackTrace(); + } + + }; + } + + @Override + public Map getLabels() { + + if (content == LabelerContent.NO_CONTENT) { + + final DataSource source = updater.getDataSource(ReadGraph.class); + assert(source != null); + source.schedule(graph -> graph.asyncRequest(labelQuery, procedure)); + + } + + if(content == null) return Collections.emptyMap(); + + return content.labels; + + } + + // OVERRIDE + + public abstract Map labels(ReadGraph graph) throws DatabaseException; + + public int category(ReadGraph graph) throws DatabaseException { + + if(content == null) return 0; + + return content.category; + + } + +}