1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.Collections;
17 import org.eclipse.jface.resource.FontDescriptor;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.NodeQueryManager;
21 import org.simantics.browsing.ui.common.ColumnKeys;
22 import org.simantics.browsing.ui.content.LabelDecorator;
23 import org.simantics.browsing.ui.content.PrunedChildrenResult;
26 * A label decorator factory that shows a [children shown/total] suffix for a
27 * node context when it is expanded.
29 * @author Tuukka Lehtonen
31 public class TreeTruncationDecoratorFactory {
33 protected int withStyle;
34 protected Set<String> decoratedColumns;
36 public TreeTruncationDecoratorFactory(int withStyle) {
37 this(withStyle, Collections.singleton(ColumnKeys.SINGLE));
40 public TreeTruncationDecoratorFactory(int withStyle, Set<String> decoratedColumns) {
41 this.withStyle = withStyle;
42 this.decoratedColumns = decoratedColumns;
45 public LabelDecorator create(NodeQueryManager manager, NodeContext context) {
46 boolean expanded = manager.query(context, BuiltinKeys.IS_EXPANDED);
50 PrunedChildrenResult prunedChildren = manager.query(context, BuiltinKeys.PRUNED_CHILDREN);
51 NodeContext[] finalChildren = manager.query(context, BuiltinKeys.FINAL_CHILDREN);
52 final int prunedLength = prunedChildren.getPrunedChildren().length;
53 final int finalLength = finalChildren.length;
54 if (prunedLength == finalLength) {
55 if (finalLength >= 10) {
56 return new LabelDecorator.Stub() {
58 public String decorateLabel(String label, String column, int itemIndex) {
59 if (decoratedColumns.contains(column))
60 return label + " [" + finalLength + "]";
64 public <F> F decorateFont(F font, String column, int itemIndex) {
65 //if (decoratedColumns.contains(column))
66 // return (F) ((FontDescriptor) font).withStyle(withStyle);
74 return new LabelDecorator.Stub() {
76 public String decorateLabel(String label, String column, int itemIndex) {
77 if (decoratedColumns.contains(column))
78 return label + " [showing " + finalLength + "/" + prunedLength + "]";
81 @SuppressWarnings("unchecked")
83 public <F> F decorateFont(F font, String column, int itemIndex) {
84 if (decoratedColumns.contains(column))
85 return (F) ((FontDescriptor) font).withStyle(withStyle);