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