]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/IssueView2.java
162eeff87d5913862e9ff2fa7d5a799c3831dccf
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / IssueView2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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.issues.ui;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.GraphExplorer;
19 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.utils.NameUtils;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.management.ISessionContext;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.issues.ui.ontology.IssueUIResource;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.ui.workbench.IPropertyPage;
30 import org.simantics.utils.ui.SWTUtils;
31 import org.simantics.views.swt.ModelledView;
32
33 /**
34  * @author Antti Villberg
35  */
36 public class IssueView2 extends ModelledView {
37
38     @Override
39     protected String configurationURI() {
40         return IssueUIResource.URIs.IssueView;
41     }
42
43     @Override
44     protected void inputChanged(IWorkbenchPart part, Object input) {
45         //System.out.println("input: " + input);
46         updateViewDescription(input);
47         super.inputChanged(part, input);
48     }
49
50     private void updateViewDescription(Object input) {
51         if (input instanceof Resource) {
52             Resource r = (Resource) input;
53             ISessionContext ctx = getSessionContext();
54             if (ctx != null) {
55                 try {
56                     String desc = ctx.getSession().syncRequest(new ResourceRead<String>(r) {
57                         @Override
58                         public String perform(ReadGraph graph) throws DatabaseException {
59                             Resource r = resource;
60                             Resource config = getConfiguration(graph, r);
61                             if (config != null)
62                                 r = config;
63
64                             return formName(graph, r);
65                         }
66
67                         private String formName(ReadGraph graph, Resource r) throws DatabaseException {
68                             String name = NameUtils.getSafeName(graph, r);
69                             final Resource project = Simantics.getProjectResource();
70                             String projectUri = project != null ? graph.getPossibleURI(project) : "";
71                             String uri = graph.getPossibleURI(r);
72                             if (uri != null) {
73                                 if (uri.startsWith(projectUri))
74                                     uri = uri.substring(projectUri.length());
75                             }
76                             return uri != null ? name + " (" + uri + ")" : name;
77                         }
78
79                         private Resource getConfiguration(ReadGraph graph, Resource r) throws DatabaseException {
80                             DiagramResource DIA = DiagramResource.getInstance(graph);
81                             Resource diagram = graph.getPossibleObject(r, DIA.RuntimeDiagram_HasConfiguration);
82                             if (diagram == null)
83                                 return r;
84                             ModelingResources MOD = ModelingResources.getInstance(graph);
85                             Resource config = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
86                             if (config == null)
87                                 return diagram;
88                             return config;
89                         }
90                     });
91                     setContentDescription(desc);
92                 } catch (DatabaseException e) {
93                     setContentDescription(e.getMessage());
94                 }
95             }
96             setVisible(true);
97         } else {
98             setContentDescription("Issues not available.");
99             setVisible(false);
100         }
101     }
102
103     @Override
104     protected IPropertyPage getPropertyPage() {
105         //return new PropertyPage(getSite());
106         return null;
107     }
108
109     @SuppressWarnings({ "unchecked", "deprecation" })
110     @Override
111     public <T> T getAdapter(Class<T> adapter) {
112         if (GraphExplorer.class == adapter)
113             return (T) tryGetExplorer(container);
114         if (BrowseContext.class == adapter)
115             return (T) tryGetBrowseContext(container);
116         return super.getAdapter(adapter);
117     }
118
119     private BrowseContext tryGetBrowseContext(Control control) {
120         return SWTUtils.tryGetObject(control, c -> {
121             return c instanceof IAdaptable
122                     ? (BrowseContext) ((IAdaptable) c).getAdapter(BrowseContext.class)
123                             : null;
124         });
125     }
126
127     private GraphExplorer tryGetExplorer(Control control) {
128         return SWTUtils.tryGetObject(control, c -> {
129             return c.isDisposed() ? null
130                     : (GraphExplorer) c.getData(GraphExplorer.KEY_GRAPH_EXPLORER);
131         });
132     }
133
134 }