1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.ui;
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;
34 * @author Antti Villberg
36 public class IssueView2 extends ModelledView {
39 protected String configurationURI() {
40 return IssueUIResource.URIs.IssueView;
44 protected void inputChanged(IWorkbenchPart part, Object input) {
45 //System.out.println("input: " + input);
46 updateViewDescription(input);
47 super.inputChanged(part, input);
50 private void updateViewDescription(Object input) {
51 if (input instanceof Resource) {
52 Resource r = (Resource) input;
53 ISessionContext ctx = getSessionContext();
56 String desc = ctx.getSession().syncRequest(new ResourceRead<String>(r) {
58 public String perform(ReadGraph graph) throws DatabaseException {
59 Resource r = resource;
60 Resource config = getConfiguration(graph, r);
64 return formName(graph, r);
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) : ""; //$NON-NLS-1$
71 String uri = graph.getPossibleURI(r);
73 if (uri.startsWith(projectUri))
74 uri = uri.substring(projectUri.length());
76 return uri != null ? name + " (" + uri + ")" : name; //$NON-NLS-1$ //$NON-NLS-2$
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);
84 ModelingResources MOD = ModelingResources.getInstance(graph);
85 Resource config = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
91 setContentDescription(desc);
92 } catch (DatabaseException e) {
93 setContentDescription(e.getMessage());
98 setContentDescription(Messages.IssueView2_IssuesNotAvailable);
104 protected IPropertyPage getPropertyPage() {
105 //return new PropertyPage(getSite());
109 @SuppressWarnings({ "unchecked", "deprecation" })
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);
119 private BrowseContext tryGetBrowseContext(Control control) {
120 return SWTUtils.tryGetObject(control, c -> {
121 return c instanceof IAdaptable
122 ? (BrowseContext) ((IAdaptable) c).getAdapter(BrowseContext.class)
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);