]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledDialog.java
Added Chart Preferences action to time series chart editor context menu
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ModelledDialog.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.dialogs.Dialog;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.simantics.Simantics;
21 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.management.ISessionContext;
28 import org.simantics.db.request.Read;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class ModelledDialog implements ModelledAction {
34
35         public class DialogImpl extends Dialog {
36
37             private final WidgetSupport          widgetSupport;
38             private Runnable               finishAction;
39             
40             private final String title;
41
42             public DialogImpl(Shell shell, WidgetSupport support, String title) {
43                 super(shell);
44                 this.title = title;
45                 this.widgetSupport = support;
46             }
47
48             @Override
49             protected void configureShell(Shell shell) {
50                 super.configureShell(shell);
51                 if (title != null) {
52                                 shell.setText(title);
53                         }
54             }
55             
56             @Override
57             protected boolean isResizable() {
58                 return true;
59             }
60             
61             @Override
62             protected void okPressed() {
63                 if(finishAction != null) finishAction.run();
64                 super.okPressed();
65             }
66             
67                 protected Control createDialogArea(Composite parent) {
68                         
69                         Composite composite = (Composite)super.createDialogArea(parent);
70                         
71                         try {
72                         
73                                 ModelledControl modelledControl = Simantics.getSession().syncRequest(new Read<ModelledControl>() {
74                                         @Override
75                                         public ModelledControl perform(ReadGraph graph) throws DatabaseException {
76                                                 BrowsingResource BRO = BrowsingResource.getInstance(graph);
77                                                 Resource controlResource = graph.getPossibleObject(configuration, BRO.Dialog_Control);
78                                                 return graph.adapt(controlResource, ModelledControl.class);
79                                         }
80                                 });
81                                 
82                                 ModelledAction finishAction = Simantics.getSession().syncRequest(new Read<ModelledAction>() {
83                                         @Override
84                                         public ModelledAction perform(ReadGraph graph) throws DatabaseException {
85                                                 BrowsingResource BRO = BrowsingResource.getInstance(graph);
86                                                 Resource actionResource = graph.getPossibleObject(configuration, BRO.Dialog_FinishAction);
87                                                 return graph.adapt(actionResource, ModelledAction.class);
88                                         }
89                                 }); 
90                                 
91                                 this.finishAction = finishAction.create(null, null, widgetSupport);
92                                 
93                                 return modelledControl.create(composite, null, null, widgetSupport);
94                         
95                         } catch (DatabaseException e) {
96                                 
97                                 e.printStackTrace();
98                                 
99                         }
100                         
101                         return composite;
102                         
103                 }
104                 
105                 @Override
106                 protected Control createContents(Composite parent) {
107                         return super.createContents(parent);
108                 }
109                 
110         }
111         
112         
113         final Resource configuration;
114         
115     public ModelledDialog(Resource configuration) {
116         this.configuration = configuration;
117     }
118
119         @Override
120         public Runnable create(IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
121
122                 final String title = Simantics.getSession().syncRequest(new Read<String>() {
123
124                         @Override
125                         public String perform(ReadGraph graph) throws DatabaseException {
126                                 BrowsingResource br = BrowsingResource.getInstance(graph);
127                                 return graph.getPossibleRelatedValue(configuration, br.Dialog_Title, Bindings.STRING);
128                         }
129                         
130                 });
131                 
132                 return new Runnable() {
133
134                         @Override
135                         public void run() {
136                                 
137                                 DialogImpl dialog = new DialogImpl(Display.getCurrent().getActiveShell(), support, title);
138                             dialog.create();
139                             support.update();
140                             dialog.open();                              
141                                 
142                         }
143                         
144                 };
145                 
146         }
147
148 }