]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/EditorNaming.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / EditorNaming.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.simantics.Simantics;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.common.request.PossibleIndexRoot;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.variable.Variable;\r
20 import org.simantics.db.layer0.variable.Variables;\r
21 import org.simantics.project.IProject;\r
22 \r
23 /**\r
24  * An IProject service for naming editors while working on a project.\r
25  * \r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public final class EditorNaming {\r
29 \r
30     /**\r
31      * @param graph\r
32      * @param resource\r
33      * @return\r
34      * @throws DatabaseException\r
35      */\r
36     public static IEditorNamingService getNamingService(ReadGraph graph, Resource resource) throws DatabaseException {\r
37         IEditorNamingService ems = graph.getPossibleAdapter(resource, IEditorNamingService.class);\r
38         if (ems == null) {\r
39             Resource model = graph.syncRequest(new PossibleIndexRoot(resource));\r
40             if (model != null) {\r
41                 ems = graph.getPossibleAdapter(model, IEditorNamingService.class);\r
42             }\r
43         }\r
44 \r
45         // Last resort.\r
46         if (ems == null) {\r
47             IProject p = Simantics.peekProject();\r
48             ems = p.getHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE);\r
49         }\r
50 \r
51         return ems;\r
52     }\r
53 \r
54     /**\r
55      * @param graph\r
56      * @param variable\r
57      * @return\r
58      * @throws DatabaseException\r
59      */\r
60     public static IEditorNamingService getNamingService(ReadGraph graph, Variable variable) throws DatabaseException {\r
61         IEditorNamingService ems = null;\r
62 \r
63         Resource r = variable.getPossibleRepresents(graph);\r
64         if (r != null)\r
65             ems = graph.getPossibleAdapter(r, IEditorNamingService.class);\r
66 \r
67         if (ems == null) {\r
68             try {\r
69                 Resource model = Variables.getModel(graph, variable);\r
70                 ems = graph.getPossibleAdapter(model, IEditorNamingService.class);\r
71             } catch (DatabaseException e) {\r
72                 // Ignore missing model, in this case the variable is lost anyway.\r
73             }\r
74         }\r
75 \r
76         // Last resort.\r
77         if (ems == null) {\r
78             IProject p = Simantics.peekProject();\r
79             if (p != null) {\r
80                 ems = p.getHint(IEditorNamingService.KEY_EDITOR_NAMING_SERVICE);\r
81             }\r
82         }\r
83 \r
84         return ems;\r
85     }\r
86 \r
87 }\r