]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/TreeTruncationDecoratorFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / TreeTruncationDecoratorFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.Collections;
15 import java.util.Set;
16
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;
24
25 /**
26  * A label decorator factory that shows a [children shown/total] suffix for a
27  * node context when it is expanded.
28  * 
29  * @author Tuukka Lehtonen
30  */
31 public class TreeTruncationDecoratorFactory {
32
33     protected int         withStyle;
34     protected Set<String> decoratedColumns;
35
36     public TreeTruncationDecoratorFactory(int withStyle) {
37         this(withStyle, Collections.singleton(ColumnKeys.SINGLE));
38     }
39
40     public TreeTruncationDecoratorFactory(int withStyle, Set<String> decoratedColumns) {
41         this.withStyle = withStyle;
42         this.decoratedColumns = decoratedColumns;
43     }
44
45     public LabelDecorator create(NodeQueryManager manager, NodeContext context) {
46         boolean expanded = manager.query(context, BuiltinKeys.IS_EXPANDED);
47         if (!expanded)
48             return null;
49
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() {
57                     @Override
58                     public String decorateLabel(String label, String column, int itemIndex) {
59                         if (decoratedColumns.contains(column))
60                             return label + " [" + finalLength + "]";
61                         return null;
62                     }
63                     @Override
64                     public <F> F decorateFont(F font, String column, int itemIndex) {
65                         //if (decoratedColumns.contains(column))
66                         //    return (F) ((FontDescriptor) font).withStyle(withStyle);
67                         return null;
68                     }
69                 };
70             }
71             return null;
72         }
73
74         return new LabelDecorator.Stub() {
75             @Override
76             public String decorateLabel(String label, String column, int itemIndex) {
77                 if (decoratedColumns.contains(column))
78                     return label + " [showing " + finalLength + "/" + prunedLength + "]";
79                 return null;
80             }
81             @SuppressWarnings("unchecked")
82             @Override
83             public <F> F decorateFont(F font, String column, int itemIndex) {
84                 if (decoratedColumns.contains(column))
85                     return (F) ((FontDescriptor) font).withStyle(withStyle);
86                 return null;
87             }
88         };
89     }
90
91 }