]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/PropertyPageUtil.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / PropertyPageUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.eclipse.jface.viewers.ISelection;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.ResourceArray;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public final class PropertyPageUtil {\r
29 \r
30     public static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;\r
31 \r
32     public static String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {\r
33         boolean sameTypes = true;\r
34 \r
35         try {\r
36 \r
37             final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);\r
38             if (ras.length == 0)\r
39                 return null;\r
40 \r
41             // Check if all the input resource are of the same type.\r
42             Collection<Resource> types = null;\r
43             for (ResourceArray ra : ras) {\r
44                 if (ra.isEmpty()) {\r
45                     return null;\r
46                 }\r
47                 if (types == null) {\r
48                     types = graph.getPrincipalTypes(ra.resources[0]);\r
49                 } else {\r
50                     Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);\r
51                     ts.removeAll(types);\r
52                     if (!ts.isEmpty()) {\r
53                         sameTypes = false;\r
54                         break;\r
55                     }\r
56                 }\r
57             }\r
58             if (sameTypes) {\r
59                 // If the resource no longer exists, provide default name only.\r
60                 if (!graph.hasStatement(ras[0].resources[0])) {\r
61                     return null;\r
62                 }\r
63 \r
64                 String name = safeGetName(graph, ras[0].resources[0]);\r
65                 if (ras.length > 1)\r
66                     name += " [" + ras.length +"]";\r
67                 return name;\r
68             } else {\r
69                 Collection<String> names = new ArrayList<String>(ras.length);\r
70                 boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;\r
71                 int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);\r
72                 int missing = ras.length - end;\r
73                 for (int i = 0; i < end; ++i) {\r
74                     // If the resource no longer exists, provide default name only.\r
75                     if (!graph.hasStatement(ras[i].resources[0]))\r
76                         continue;\r
77 \r
78                     names.add(safeGetName(graph, ras[i].resources[0]));\r
79                 }\r
80                 if (names.isEmpty()) {\r
81                     return null;\r
82                 }\r
83 \r
84                 if (truncate)\r
85                     names.add("+ " + missing + " more...");\r
86 \r
87                 String name = names.toString();\r
88                 return name;\r
89             }\r
90 \r
91         } catch (Throwable t) {\r
92             t.printStackTrace();\r
93             return null;\r
94         }\r
95    }\r
96 \r
97     private static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {\r
98         return NameUtils.getSafeName(g, r);\r
99 //        try {\r
100 //            //System.out.println("safeGetName " + r);\r
101 //            return g.adapt(r, String.class);\r
102 //        } catch (AdaptionException e) {\r
103 //            return NameUtils.getSafeName(g, r);\r
104 //        }\r
105     }\r
106 \r
107 }