]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/PropertyPageUtil.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / PropertyPageUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * 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.ArrayList;
15 import java.util.Collection;
16
17 import org.eclipse.jface.viewers.ISelection;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.ResourceArray;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.ui.utils.ResourceAdaptionUtils;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public final class PropertyPageUtil {
29
30     public static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;
31
32     public static String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {
33         boolean sameTypes = true;
34
35         try {
36
37             final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);
38             if (ras.length == 0)
39                 return null;
40
41             // Check if all the input resource are of the same type.
42             Collection<Resource> types = null;
43             for (ResourceArray ra : ras) {
44                 if (ra.isEmpty()) {
45                     return null;
46                 }
47                 if (types == null) {
48                     types = graph.getPrincipalTypes(ra.resources[0]);
49                 } else {
50                     Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);
51                     ts.removeAll(types);
52                     if (!ts.isEmpty()) {
53                         sameTypes = false;
54                         break;
55                     }
56                 }
57             }
58             if (sameTypes) {
59                 // If the resource no longer exists, provide default name only.
60                 if (!graph.hasStatement(ras[0].resources[0])) {
61                     return null;
62                 }
63
64                 String name = safeGetName(graph, ras[0].resources[0]);
65                 if (ras.length > 1)
66                     name += " [" + ras.length +"]";
67                 return name;
68             } else {
69                 Collection<String> names = new ArrayList<String>(ras.length);
70                 boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;
71                 int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);
72                 int missing = ras.length - end;
73                 for (int i = 0; i < end; ++i) {
74                     // If the resource no longer exists, provide default name only.
75                     if (!graph.hasStatement(ras[i].resources[0]))
76                         continue;
77
78                     names.add(safeGetName(graph, ras[i].resources[0]));
79                 }
80                 if (names.isEmpty()) {
81                     return null;
82                 }
83
84                 if (truncate)
85                     names.add("+ " + missing + " more...");
86
87                 String name = names.toString();
88                 return name;
89             }
90
91         } catch (Throwable t) {
92             t.printStackTrace();
93             return null;
94         }
95    }
96
97     private static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {
98         return NameUtils.getSafeName(g, r);
99 //        try {
100 //            //System.out.println("safeGetName " + r);
101 //            return g.adapt(r, String.class);
102 //        } catch (AdaptionException e) {
103 //            return NameUtils.getSafeName(g, r);
104 //        }
105     }
106
107 }