]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/TitleWithParentNameRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / TitleWithParentNameRequest.java
1 /*******************************************************************************\r
2  * Copyright (c) 2014 Association for Decentralized Information Management\r
3  * in 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  *     Semantum Oy - initial API and implementation\r
12  *******************************************************************************/\r
13 package org.simantics.ui.workbench;\r
14 \r
15 import org.simantics.databoard.Bindings;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.common.request.UnaryRead;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 /**\r
23  * A simple request that takes an IResourceEditorInput as input and calculates\r
24  * the following editor title from it:\r
25  * <code>input-name (input-parent-name)</code>.\r
26  * \r
27  * @author Hannu Niemist&ouml;\r
28  * @author Tuukka Lehtonen\r
29  * @see TitleRequest\r
30  */\r
31 public class TitleWithParentNameRequest extends UnaryRead<IResourceEditorInput, String> {\r
32 \r
33     public TitleWithParentNameRequest(IResourceEditorInput input) {\r
34         super(input);\r
35     }\r
36 \r
37     @Override\r
38     public String perform(ReadGraph graph) throws DatabaseException {\r
39         Layer0 L0 = Layer0.getInstance(graph);\r
40         Resource r = parameter.getResource();\r
41         Resource parent = graph.getPossibleObject(r, L0.PartOf);\r
42         String name = safeName(graph, r, "No name", L0);\r
43         String parentName = safeName(graph, parent, "No parent", L0);\r
44         return name + " (" + parentName + ")";\r
45     }\r
46 \r
47     protected String safeName(ReadGraph graph, Resource r, String noNameValue, Layer0 L0) throws DatabaseException {\r
48         if (r != null) {\r
49             String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);\r
50             if (name != null) {\r
51                 return name;\r
52             }\r
53         }\r
54         return noNameValue;\r
55     }\r
56     \r
57 }\r