1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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.browsing.ui.swt;
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;
37 public class ModelledButton implements ModelledControl {
39 final private Resource configuration;
41 public ModelledButton(Resource configuration) {
42 this.configuration = configuration;
46 public Control create(Composite parent, final IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) throws DatabaseException {
48 LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
50 Button button = new Button(parent, support, SWT.READ_ONLY);
52 GridData gridData = Simantics.getSession().syncRequest(new Read<GridData>() {
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;
67 button.setLayoutData(gridData);
69 String text = Simantics.getSession().syncRequest(new Read<String>() {
72 public String perform(ReadGraph graph) throws DatabaseException {
73 BrowsingResource br = BrowsingResource.getInstance(graph);
74 return graph.getPossibleRelatedValue(configuration, br.Button_Text);
79 if(text != null) button.setText(text);
81 String tooltip = Simantics.getSession().syncRequest(new Read<String>() {
84 public String perform(ReadGraph graph) throws DatabaseException {
85 BrowsingResource br = BrowsingResource.getInstance(graph);
86 return graph.getPossibleRelatedValue(configuration, br.Button_Tooltip);
91 if(tooltip != null) button.setTooltipText(tooltip);
93 ModelledIcon icon = Simantics.getSession().syncRequest(new Read<ModelledIcon>() {
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);
106 ImageDescriptor iconDescriptor = icon.create();
107 if (iconDescriptor != null) {
108 Image image = resourceManager.createImage(iconDescriptor);
109 button.setImage(image);
111 ErrorLogger.defaultLogWarning("Could not load icon " + icon, null);
115 ModelledAction modelledAction = Simantics.getSession().syncRequest(new Read<ModelledAction>() {
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);
127 final Runnable actionRunnable = modelledAction != null ? modelledAction.create(site, context, support) : null;
129 button.addSelectionListener(new SelectionListener() {
132 public void widgetSelected(SelectionEvent e) {
133 if(actionRunnable != null) actionRunnable.run();
137 public void widgetDefaultSelected(SelectionEvent e) {
143 return button.getWidget();