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