1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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.ArrayList;
15 import java.util.Collection;
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;
26 * @author Tuukka Lehtonen
28 public final class PropertyPageUtil {
30 public static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;
32 public static String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {
33 boolean sameTypes = true;
37 final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);
41 // Check if all the input resource are of the same type.
42 Collection<Resource> types = null;
43 for (ResourceArray ra : ras) {
48 types = graph.getPrincipalTypes(ra.resources[0]);
50 Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);
59 // If the resource no longer exists, provide default name only.
60 if (!graph.hasStatement(ras[0].resources[0])) {
64 String name = safeGetName(graph, ras[0].resources[0]);
66 name += " [" + ras.length +"]";
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]))
78 names.add(safeGetName(graph, ras[i].resources[0]));
80 if (names.isEmpty()) {
85 names.add("+ " + missing + " more...");
87 String name = names.toString();
91 } catch (Throwable t) {
97 private static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {
98 return NameUtils.getSafeName(g, r);
100 // //System.out.println("safeGetName " + r);
101 // return g.adapt(r, String.class);
102 // } catch (AdaptionException e) {
103 // return NameUtils.getSafeName(g, r);