]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/ModelledDialogs.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / ModelledDialogs.java
1 package org.simantics.views.swt;
2
3 import java.util.Collections;
4 import java.util.Map;
5
6 import org.eclipse.jface.dialogs.MessageDialog;
7 import org.eclipse.jface.layout.GridDataFactory;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.ui.PlatformUI;
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.mutable.Variant;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.VirtualGraph;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.primitiverequest.PossibleObject;
21 import org.simantics.db.common.primitiverequest.RelatedValue2;
22 import org.simantics.db.common.primitiverequest.ResourceByURI;
23 import org.simantics.db.common.primitiverequest.SingleObject;
24 import org.simantics.db.common.request.WriteResultRequest;
25 import org.simantics.db.common.utils.Logger;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.layer0.variable.Variables;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.scenegraph.loader.ScenegraphVariable;
31 import org.simantics.scenegraph.ontology.ScenegraphResources;
32 import org.simantics.utils.threads.SWTThread;
33 import org.simantics.views.ontology.ViewsResources;
34 import org.simantics.views.swt.client.base.SWTRoot;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class ModelledDialogs {
40
41         public static void scheduleMessage(final String uri) {
42                 scheduleMessage(uri, Collections.<String, Variant>emptyMap());
43         }
44
45         public static void scheduleMessage(final String uri, final Map<String, Variant> parameters) {
46
47                 SWTThread.getThreadAccess().asyncExec(new Runnable() {
48
49                         @Override
50                         public void run() {
51                                 openMessage(uri, parameters);
52                         }
53
54                 });
55
56         }
57         
58         private static int getStyle(Resource type) throws DatabaseException {
59                 ViewsResources VIEWS = ViewsResources.getInstance(Simantics.getSession());
60                 if(VIEWS.MessageDialog_Type_Error.equals(type)) return MessageDialog.ERROR;
61                 else if(VIEWS.MessageDialog_Type_Information.equals(type)) return MessageDialog.INFORMATION;
62                 else if(VIEWS.MessageDialog_Type_Question.equals(type)) return MessageDialog.QUESTION;
63                 else if(VIEWS.MessageDialog_Type_QuestionWithCancel.equals(type)) return MessageDialog.QUESTION_WITH_CANCEL;
64                 else if(VIEWS.MessageDialog_Type_Confirm.equals(type)) return MessageDialog.CONFIRM;
65                 else if(VIEWS.MessageDialog_Type_Warning.equals(type)) return MessageDialog.WARNING;
66                 else throw new DatabaseException("MessageDialog model needs to define a dialog type");
67         }
68                 
69         
70         public static int openMessage(final String uri) {
71                 return openMessage(uri, Collections.<String, Variant>emptyMap());
72         }
73
74         private static SWTRoot createCustomArea(final Resource customArea, final Map<String, Variant> parameters) throws DatabaseException {
75                 
76                 if(customArea == null) return null;
77
78                 final SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, null);
79                 
80                 Variable context = Simantics.getSession().sync(new WriteResultRequest<Variable>(Simantics.getSession().getService(VirtualGraph.class)) {
81
82                         @Override
83                         public Variable perform(WriteGraph graph) throws DatabaseException {
84
85                                 Layer0 L0 = Layer0.getInstance(graph);
86                                 ScenegraphResources SG = ScenegraphResources.getInstance(graph);
87                                 Resource runtime = graph.newResource();
88                                 graph.claim(runtime, L0.InstanceOf, null, SG.Runtime);
89                                 Variable base = Variables.getVariable(graph, customArea);
90                                 String uri = base.getURI(graph);
91                                 graph.claimLiteral(runtime, SG.Runtime_HasVariable, uri, Bindings.STRING);
92
93                                 return new ScenegraphVariable(base, customArea, runtime, loader.getRoot(), parameters);
94
95                         }
96
97                 });
98
99                 return loader.load(Simantics.getSession(), context);
100                 
101         }
102         
103         public static int openMessage(final String uri, final Map<String, Variant> parameters) {
104
105                 //assert SWTThread.getThreadAccess().currentThreadAccess();
106                 
107                 try {
108
109                         Session session = Simantics.getSession();
110                         ViewsResources VIEWS = ViewsResources.getInstance(session);
111                         final Resource configuration = session.sync(new ResourceByURI(uri));
112                         final Resource typeResource = session.sync(new SingleObject(configuration, VIEWS.MessageDialog_HasType));
113                         final int type = getStyle(typeResource);
114                         final String dialogTitle = session.sync(new RelatedValue2<String>(configuration, VIEWS.MessageDialog_title, Bindings.STRING));
115                         final String dialogMessage = session.sync(new RelatedValue2<String>(configuration, VIEWS.MessageDialog_message, Bindings.STRING));
116                         final String[] buttonLabels = session.sync(new RelatedValue2<String[]>(configuration, VIEWS.MessageDialog_buttonLabels, Bindings.STRING_ARRAY));
117                         final Integer defaultButton = session.sync(new RelatedValue2<Integer>(configuration, VIEWS.MessageDialog_defaultButton, Bindings.INTEGER));
118                         final Resource customArea = session.sync(new PossibleObject(configuration, VIEWS.MessageDialog_HasCustomArea));
119
120                         final SWTRoot root = createCustomArea(customArea, parameters);
121
122                         class Dialog extends MessageDialog {
123
124                                 public Dialog() {
125                                         super(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 
126                                                         dialogTitle, null, 
127                                                         dialogMessage, 
128                                                         type, buttonLabels, defaultButton);
129                                 }
130
131                                 @Override
132                                 protected boolean isResizable() {
133                                         return true;
134                                 }
135
136                                 @Override
137                                 protected Control createCustomArea(Composite parent) {
138                                         if(root != null) {
139                                                 Composite customArea = new Composite(parent, SWT.NONE);
140                                                 customArea.setLayout(new GridLayout());
141                                                 GridDataFactory.fillDefaults().grab(true, true).minSize(50, 50).applyTo(customArea);
142                                                 root.createControls(customArea);
143                                                 return customArea;
144                                         } else {
145                                                 return super.createCustomArea(parent);
146                                         }
147                                 }
148
149                         }
150
151                         return new Dialog().open();
152
153                 } catch (DatabaseException e) {
154                         Logger.defaultLogError(e);
155                 }
156                 
157                 return -1;
158
159         }
160         
161         
162         
163
164 }