]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledButton.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ModelledButton.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.browsing.ui.swt;
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.jface.resource.LocalResourceManager;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.simantics.Simantics;
26 import org.simantics.browsing.ui.common.ErrorLogger;
27 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;
28 import org.simantics.browsing.ui.swt.widgets.Button;
29 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
30 import org.simantics.databoard.Bindings;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.management.ISessionContext;
35 import org.simantics.db.request.Read;
36
37 public class ModelledButton implements ModelledControl {
38
39         final private Resource configuration;
40         
41         public ModelledButton(Resource configuration) {
42                 this.configuration = configuration;
43         }
44         
45         @Override
46         public Control create(Composite parent, final IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) throws DatabaseException {
47
48         LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
49                 
50                 Button button = new Button(parent, support, SWT.READ_ONLY);
51                 
52         GridData gridData = Simantics.getSession().syncRequest(new Read<GridData>() {
53
54             @Override
55             public GridData perform(ReadGraph graph) throws DatabaseException {
56                 BrowsingResource br = BrowsingResource.getInstance(graph);
57                 GridData data = new GridData(SWT.LEFT, SWT.CENTER, false, false);                
58                 Integer span = graph.getPossibleRelatedValue(configuration, br.Control_HorizontalSpan, Bindings.INTEGER);
59                 if(span != null) data.horizontalSpan = span;
60                 Boolean grabExcessHorizontalSpace = graph.getPossibleRelatedValue(configuration, br.Control_HorizontalGrab, Bindings.BOOLEAN);
61                 if(grabExcessHorizontalSpace != null) data.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
62                 return data;
63             }
64
65         });
66         
67         button.setLayoutData(gridData);
68                 
69                 String text = Simantics.getSession().syncRequest(new Read<String>() {
70
71                         @Override
72                         public String perform(ReadGraph graph) throws DatabaseException {
73                                 BrowsingResource br = BrowsingResource.getInstance(graph);
74                                 return graph.getPossibleRelatedValue(configuration, br.Button_Text);
75                         }
76                         
77                 });
78
79                 if(text != null) button.setText(text);
80
81                 String tooltip = Simantics.getSession().syncRequest(new Read<String>() {
82
83                         @Override
84                         public String perform(ReadGraph graph) throws DatabaseException {
85                                 BrowsingResource br = BrowsingResource.getInstance(graph);
86                                 return graph.getPossibleRelatedValue(configuration, br.Button_Tooltip);
87                         }
88                         
89                 });
90
91                 if(tooltip != null) button.setTooltipText(tooltip);
92                 
93                 ModelledIcon icon = Simantics.getSession().syncRequest(new Read<ModelledIcon>() {
94
95                         @Override
96                         public ModelledIcon perform(ReadGraph graph) throws DatabaseException {
97                                 BrowsingResource br = BrowsingResource.getInstance(graph);
98                                 Resource icon = graph.getPossibleObject(configuration, br.Button_Icon);
99                                 if(icon == null) return null;
100                                 else return graph.adapt(icon, ModelledIcon.class);
101                         }
102                         
103                 });
104                 
105                 if(icon != null) {
106                         ImageDescriptor iconDescriptor = icon.create();
107                         if (iconDescriptor != null) {
108                             Image image = resourceManager.createImage(iconDescriptor);
109                             button.setImage(image);
110                         } else {
111                             ErrorLogger.defaultLogWarning("Could not load icon " + icon, null);
112                         }
113                 }
114                 
115                 ModelledAction modelledAction = Simantics.getSession().syncRequest(new Read<ModelledAction>() {
116
117                         @Override
118                         public ModelledAction perform(ReadGraph graph) throws DatabaseException {
119                                 BrowsingResource br = BrowsingResource.getInstance(graph);
120                                 Resource action = graph.getPossibleObject(configuration, br.Button_Action);
121                                 if(action == null) return null;
122                                 return graph.adapt(action, ModelledAction.class);
123                         }
124                         
125                 });
126
127                 final Runnable actionRunnable = modelledAction != null ? modelledAction.create(site, context, support) : null; 
128                 
129                 button.addSelectionListener(new SelectionListener() {
130
131                         @Override
132                         public void widgetSelected(SelectionEvent e) {
133                                 if(actionRunnable != null) actionRunnable.run();
134                         }
135
136                         @Override
137                         public void widgetDefaultSelected(SelectionEvent e) {
138                                 widgetSelected(e);
139                         }
140                         
141                 });
142                 
143                 return button.getWidget();
144                 
145         }
146         
147 }