1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\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
26 * @author Tuukka Lehtonen
\r
28 public final class PropertyPageUtil {
\r
30 public static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;
\r
32 public static String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {
\r
33 boolean sameTypes = true;
\r
37 final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);
\r
38 if (ras.length == 0)
\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
47 if (types == null) {
\r
48 types = graph.getPrincipalTypes(ra.resources[0]);
\r
50 Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);
\r
51 ts.removeAll(types);
\r
52 if (!ts.isEmpty()) {
\r
59 // If the resource no longer exists, provide default name only.
\r
60 if (!graph.hasStatement(ras[0].resources[0])) {
\r
64 String name = safeGetName(graph, ras[0].resources[0]);
\r
66 name += " [" + ras.length +"]";
\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
78 names.add(safeGetName(graph, ras[i].resources[0]));
\r
80 if (names.isEmpty()) {
\r
85 names.add("+ " + missing + " more...");
\r
87 String name = names.toString();
\r
91 } catch (Throwable t) {
\r
92 t.printStackTrace();
\r
97 private static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {
\r
98 return NameUtils.getSafeName(g, r);
\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