--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class PropertyPageUtil {\r
+\r
+ public static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;\r
+\r
+ public static String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {\r
+ boolean sameTypes = true;\r
+\r
+ try {\r
+\r
+ final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);\r
+ if (ras.length == 0)\r
+ return null;\r
+\r
+ // Check if all the input resource are of the same type.\r
+ Collection<Resource> types = null;\r
+ for (ResourceArray ra : ras) {\r
+ if (ra.isEmpty()) {\r
+ return null;\r
+ }\r
+ if (types == null) {\r
+ types = graph.getPrincipalTypes(ra.resources[0]);\r
+ } else {\r
+ Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);\r
+ ts.removeAll(types);\r
+ if (!ts.isEmpty()) {\r
+ sameTypes = false;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (sameTypes) {\r
+ // If the resource no longer exists, provide default name only.\r
+ if (!graph.hasStatement(ras[0].resources[0])) {\r
+ return null;\r
+ }\r
+\r
+ String name = safeGetName(graph, ras[0].resources[0]);\r
+ if (ras.length > 1)\r
+ name += " [" + ras.length +"]";\r
+ return name;\r
+ } else {\r
+ Collection<String> names = new ArrayList<String>(ras.length);\r
+ boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;\r
+ int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);\r
+ int missing = ras.length - end;\r
+ for (int i = 0; i < end; ++i) {\r
+ // If the resource no longer exists, provide default name only.\r
+ if (!graph.hasStatement(ras[i].resources[0]))\r
+ continue;\r
+\r
+ names.add(safeGetName(graph, ras[i].resources[0]));\r
+ }\r
+ if (names.isEmpty()) {\r
+ return null;\r
+ }\r
+\r
+ if (truncate)\r
+ names.add("+ " + missing + " more...");\r
+\r
+ String name = names.toString();\r
+ return name;\r
+ }\r
+\r
+ } catch (Throwable t) {\r
+ t.printStackTrace();\r
+ return null;\r
+ }\r
+ }\r
+\r
+ private static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {\r
+ return NameUtils.getSafeName(g, r);\r
+// try {\r
+// //System.out.println("safeGetName " + r);\r
+// return g.adapt(r, String.class);\r
+// } catch (AdaptionException e) {\r
+// return NameUtils.getSafeName(g, r);\r
+// }\r
+ }\r
+\r
+}
\ No newline at end of file