X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FDefaultIsExpandedProcessor.java;h=368d5924401305771668707a58d5ee5c48b2676f;hb=HEAD;hp=7c2ecfdc7e3f5b3635b31502682af4aec576f98a;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsExpandedProcessor.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsExpandedProcessor.java index 7c2ecfdc7..368d59244 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsExpandedProcessor.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsExpandedProcessor.java @@ -1,149 +1,149 @@ -/******************************************************************************* - * 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.swt; - -import gnu.trove.map.hash.THashMap; -import gnu.trove.set.hash.THashSet; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Event; -import org.eclipse.swt.widgets.Listener; -import org.eclipse.swt.widgets.Tree; -import org.simantics.browsing.ui.BuiltinKeys; -import org.simantics.browsing.ui.GraphExplorer; -import org.simantics.browsing.ui.NodeContext; -import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey; -import org.simantics.browsing.ui.PrimitiveQueryUpdater; -import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor; -import org.simantics.browsing.ui.common.processors.IsExpandedProcessor; -import org.simantics.browsing.ui.common.processors.ProcessorLifecycle; - -/** - * @author Tuukka Lehtonen - */ -public class DefaultIsExpandedProcessor extends AbstractPrimitiveQueryProcessor implements -IsExpandedProcessor, ProcessorLifecycle { - - /** - * The set of currently expanded node contexts. - */ - private final Set expanded = new THashSet(); - private final Map expandedQueries = new THashMap(); - - private Tree tree; - - public DefaultIsExpandedProcessor() { - } - - @Override - public Object getIdentifier() { - return BuiltinKeys.IS_EXPANDED; - } - - @Override - public String toString() { - return "IsExpandedProcessor"; - } - - @Override - public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey key) { - boolean isExpanded = expanded.contains(context); - //System.out.println("isExpanded(" + updater + ", " + context + "): " + isExpanded); - expandedQueries.put(context, updater); - return Boolean.valueOf(isExpanded); - } - - @Override - public Collection getExpanded() { - return new HashSet(expanded); - } - - @Override - public boolean getExpanded(NodeContext context) { - return this.expanded.contains(context); - } - - @Override - public boolean setExpanded(NodeContext context, boolean expanded) { - return _setExpanded(context, expanded); - } - - @Override - public boolean replaceExpanded(NodeContext context, boolean expanded) { - return nodeStatusChanged(context, expanded); - } - - private boolean _setExpanded(NodeContext context, boolean expanded) { - if (expanded) { - return this.expanded.add(context); - } else { - return this.expanded.remove(context); - } - } - - Listener treeListener = new Listener() { - @Override - public void handleEvent(Event event) { - NodeContext context = (NodeContext) event.item.getData(); - switch (event.type) { - case SWT.Expand: - nodeStatusChanged(context, true); - break; - case SWT.Collapse: - nodeStatusChanged(context, false); - break; - } - } - }; - - protected boolean nodeStatusChanged(NodeContext context, boolean expanded) { - boolean result = _setExpanded(context, expanded); - PrimitiveQueryUpdater updater = expandedQueries.get(context); - if (updater != null) - updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded); - return result; - } - - @Override - public void attached(GraphExplorer explorer) { - Object control = explorer.getControl(); - if (control instanceof Tree) { - this.tree = (Tree) control; - tree.addListener(SWT.Expand, treeListener); - tree.addListener(SWT.Collapse, treeListener); - } else { - System.out.println("WARNING: " + getClass().getSimpleName() + " attached to unsupported control: " + control); - } - } - - @Override - public void clear() { - expanded.clear(); - expandedQueries.clear(); - } - - @Override - public void detached(GraphExplorer explorer) { - clear(); - if (tree != null) { - tree.removeListener(SWT.Expand, treeListener); - tree.removeListener(SWT.Collapse, treeListener); - tree = null; - } - } - +/******************************************************************************* + * 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.swt; + +import gnu.trove.map.hash.THashMap; +import gnu.trove.set.hash.THashSet; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Tree; +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey; +import org.simantics.browsing.ui.PrimitiveQueryUpdater; +import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor; +import org.simantics.browsing.ui.common.processors.IsExpandedProcessor; +import org.simantics.browsing.ui.common.processors.ProcessorLifecycle; + +/** + * @author Tuukka Lehtonen + */ +public class DefaultIsExpandedProcessor extends AbstractPrimitiveQueryProcessor implements +IsExpandedProcessor, ProcessorLifecycle { + + /** + * The set of currently expanded node contexts. + */ + private final Set expanded = new THashSet(); + private final Map expandedQueries = new THashMap(); + + private Tree tree; + + public DefaultIsExpandedProcessor() { + } + + @Override + public Object getIdentifier() { + return BuiltinKeys.IS_EXPANDED; + } + + @Override + public String toString() { + return "IsExpandedProcessor"; + } + + @Override + public Boolean query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey key) { + boolean isExpanded = expanded.contains(context); + //System.out.println("isExpanded(" + updater + ", " + context + "): " + isExpanded); + expandedQueries.put(context, updater); + return Boolean.valueOf(isExpanded); + } + + @Override + public Collection getExpanded() { + return new HashSet(expanded); + } + + @Override + public boolean getExpanded(NodeContext context) { + return this.expanded.contains(context); + } + + @Override + public boolean setExpanded(NodeContext context, boolean expanded) { + return _setExpanded(context, expanded); + } + + @Override + public boolean replaceExpanded(NodeContext context, boolean expanded) { + return nodeStatusChanged(context, expanded); + } + + private boolean _setExpanded(NodeContext context, boolean expanded) { + if (expanded) { + return this.expanded.add(context); + } else { + return this.expanded.remove(context); + } + } + + Listener treeListener = new Listener() { + @Override + public void handleEvent(Event event) { + NodeContext context = (NodeContext) event.item.getData(); + switch (event.type) { + case SWT.Expand: + nodeStatusChanged(context, true); + break; + case SWT.Collapse: + nodeStatusChanged(context, false); + break; + } + } + }; + + protected boolean nodeStatusChanged(NodeContext context, boolean expanded) { + boolean result = _setExpanded(context, expanded); + PrimitiveQueryUpdater updater = expandedQueries.get(context); + if (updater != null) + updater.scheduleReplace(context, BuiltinKeys.IS_EXPANDED, expanded); + return result; + } + + @Override + public void attached(GraphExplorer explorer) { + Object control = explorer.getControl(); + if (control instanceof Tree) { + this.tree = (Tree) control; + tree.addListener(SWT.Expand, treeListener); + tree.addListener(SWT.Collapse, treeListener); + } else { + System.out.println("WARNING: " + getClass().getSimpleName() + " attached to unsupported control: " + control); + } + } + + @Override + public void clear() { + expanded.clear(); + expandedQueries.clear(); + } + + @Override + public void detached(GraphExplorer explorer) { + clear(); + if (tree != null) { + tree.removeListener(SWT.Expand, treeListener); + tree.removeListener(SWT.Collapse, treeListener); + tree = null; + } + } + } \ No newline at end of file