]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfilesView.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / ProfilesView.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.diagram.profile.view;
13
14 import org.eclipse.ui.IWorkbenchPart;
15 import org.simantics.Simantics;
16 import org.simantics.databoard.util.URIStringUtils;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.ResourceRead;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.management.ISessionContext;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.ui.workbench.IPropertyPage;
26 import org.simantics.views.swt.ModelledView;
27
28 /**
29  * @author Antti Villberg
30  */
31 public class ProfilesView extends ModelledView {
32
33     @Override
34     protected String configurationURI() {
35         return DiagramResource.URIs.ProfilesView;
36     }
37
38     @Override
39     protected void inputChanged(IWorkbenchPart part, Object input) {
40         //System.out.println("input: " + input);
41         updateViewDescription(input);
42         super.inputChanged(part, input);
43     }
44
45     private void updateViewDescription(Object input) {
46         if (input instanceof Resource) {
47             Resource r = (Resource) input;
48             ISessionContext ctx = getSessionContext();
49             if (ctx != null) {
50                 try {
51                     String desc = ctx.getSession().syncRequest(new ResourceRead<String>(r) {
52                         @Override
53                         public String perform(ReadGraph graph) throws DatabaseException {
54                             Resource r = resource;
55                             Resource config = getConfiguration(graph, r);
56                             if (config != null)
57                                 r = config;
58
59                             return formName(graph, r);
60                         }
61
62                         private String formName(ReadGraph graph, Resource r) throws DatabaseException {
63                             String name = NameUtils.getSafeName(graph, r);
64                             final Resource project = Simantics.getProjectResource();
65                             String projectUri = project != null ? graph.getPossibleURI(project) : "";
66                             String uri = graph.getPossibleURI(r);
67                             if (uri != null) {
68                                 if (uri.startsWith(projectUri))
69                                     uri = uri.substring(projectUri.length());
70                                 uri = URIStringUtils.unescape(uri); 
71                             }
72                             return uri != null ? name + " (" + uri + ")" : name;
73                         }
74
75                         private Resource getConfiguration(ReadGraph graph, Resource r) throws DatabaseException {
76                             DiagramResource DIA = DiagramResource.getInstance(graph);
77                             Resource diagram = graph.getPossibleObject(r, DIA.RuntimeDiagram_HasConfiguration);
78                             if (diagram == null)
79                                 return r;
80                             ModelingResources MOD = ModelingResources.getInstance(graph);
81                             Resource config = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
82                             if (config == null)
83                                 return diagram;
84                             return config;
85                         }
86                     });
87                     setContentDescription(desc);
88                 } catch (DatabaseException e) {
89                     setContentDescription(e.getMessage());
90                 }
91             }
92             setVisible(true);
93         } else {
94             setContentDescription("Profiles not available.");
95             setVisible(false);
96         }
97     }
98
99     @Override
100     protected IPropertyPage getPropertyPage() {
101         //return new PropertyPage(getSite());
102         return null;
103     }
104
105 }