]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputViewPart.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceInputViewPart.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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  *******************************************************************************/\r
12 package org.simantics.ui.workbench;\r
13 \r
14 import org.eclipse.swt.widgets.Composite;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.event.ChangeEvent;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.layer0.Layer0;\r
20 import org.simantics.utils.ui.ErrorLogger;\r
21 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
22 \r
23 /**\r
24  * This class inherits the Graph access setup behavior from\r
25  * GraphAccessViewPart and in addition assumes that the view receives a\r
26  * Resource ID as a parameter in the secondary ID of the view part.\r
27  * \r
28  * <p>\r
29  * To use this class all you need to do is call super.createPartControl in your\r
30  * own createPartControl implementation. This will make sure a {@link Graph}\r
31  * will be available directly after that for initializing the UI and its\r
32  * contents.\r
33  * </p>\r
34  * \r
35  * <p>\r
36  * To open a ResourceInputViewPart use:\r
37  * {@link ResourceViewPartUtils#activateViewForResource(String, org.simantics.db.Session, Resource)}</code>\r
38  * which should give the view the specified resource id as its secondary id.\r
39  * </p>\r
40  * \r
41  * @author Tuukka Lehtonen\r
42  */\r
43 public abstract class ResourceInputViewPart extends GraphAccessViewPart {\r
44 \r
45     private static final String NO_NAME        = "(no name)";\r
46 \r
47     private static final String MULTIPLE_NAMES = "(multiple names)";\r
48 \r
49 \r
50     private ResourceInput       mainInput;\r
51 \r
52     private Resource            inputResource;\r
53 \r
54     private String              inputName      = NO_NAME;\r
55 \r
56 \r
57     //----------------------------------------------------------------------\r
58     // Getters\r
59 \r
60     public Resource getInputResource() {\r
61         return inputResource;\r
62     }\r
63 \r
64     public String getInputName() {\r
65         return inputName;\r
66     }\r
67 \r
68 \r
69     //----------------------------------------------------------------------\r
70     // Event handlers & initialization\r
71 \r
72     /**\r
73      * Must be called after initializeGraph.\r
74      */\r
75     private void initializeInput() {\r
76         // Get the main input from the view's secondary ID.\r
77         String secondaryId = getViewSite().getSecondaryId();\r
78         if (secondaryId == null)\r
79             // No input given!\r
80             return;\r
81 \r
82         mainInput = ResourceInput.unmarshall(secondaryId);\r
83         try {\r
84             inputResource = mainInput.toResource(getSession());\r
85         } catch (DatabaseException e) {\r
86             ErrorLogger.defaultLogError(e);\r
87         }\r
88     }\r
89 \r
90 \r
91     /**\r
92      * Initializes graph data access and view resource ID input structures.\r
93      * \r
94      * <p>\r
95      * This method is automatically called by\r
96      * {@link #createPartControl(Composite)}. Override to perform own\r
97      * graph-related initializations but be absolutely sure to call super the\r
98      * first thing. Clients must not directly call this method.\r
99      * </p>\r
100      */\r
101     @Override\r
102     protected void initialize() {\r
103         super.initialize();\r
104         initializeInput();\r
105     }\r
106 \r
107     /**\r
108      * @return <code>true</code> if the input resource still exists, i.e.\r
109      *         there are triples with the subject of the input resource.\r
110      */\r
111     protected boolean inputExists(ReadGraph g) throws DatabaseException {\r
112         if (inputResource == null)\r
113             return false;\r
114         return g.hasStatement(inputResource);\r
115     }\r
116 \r
117     @Override\r
118     protected void cleanup() {\r
119         inputResource = null;\r
120         super.cleanup();\r
121     }\r
122 \r
123 \r
124     /**\r
125      * @return the main input structure of this view which identifies a single\r
126      *         input resource\r
127      */\r
128     public ResourceInput getMainInput() {\r
129         return mainInput;\r
130     }\r
131 \r
132 \r
133     //----------------------------------------------------------------------\r
134     // Override these if necessary:\r
135 \r
136     @Override\r
137     protected String getTitleText() {\r
138         return getInputName();\r
139     }\r
140 \r
141     @Override\r
142     protected String getTitleTooltip() {\r
143         return getInputName();\r
144     }\r
145 \r
146 \r
147     /**\r
148      * Extends {@link #update(GraphChangeEvent)} to handle disappearing inputs\r
149      * by hiding the view.\r
150      * \r
151      * @param event\r
152      *            the received change event\r
153      */\r
154     @Override\r
155     protected void update(ChangeEvent event) throws DatabaseException {\r
156         ReadGraph g = event.getGraph();\r
157 \r
158         // If the input no longer exists, the editor should be closed since the\r
159         // situation is analogous to having an IEditorPart input, such as a\r
160         // file, deleted suddenly.\r
161         if (!inputExists(g)) {\r
162             WorkbenchUtils.hideView(getViewSite().getWorkbenchWindow(), this);\r
163             return;\r
164         }\r
165 \r
166         updateModelCache(g);\r
167         reload(g);\r
168     }\r
169 \r
170     private void updateModelCache(ReadGraph graph) {\r
171         try {\r
172                 Layer0 L0 = Layer0.getInstance(graph);\r
173             inputName = graph.getPossibleRelatedValue(inputResource, L0.HasName);\r
174             if (inputName == null)\r
175                 inputName = NO_NAME;\r
176         } catch (DatabaseException e) {\r
177             inputName = MULTIPLE_NAMES;\r
178         }\r
179     }\r
180 \r
181 }\r