63b82c03c9c22fcd4fdb740ec26f78d059edd537
[sdc.git] /
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import React from 'react';
18
19 import Common from '../../common/Common';
20 import Logger from '../../common/Logger';
21 import Diagram from '../diagram/Diagram';
22 import Dialog from '../dialog/Dialog';
23 import Editor from '../editor/Editor';
24 import Export from '../export/Export';
25 import Overlay from '../overlay/Overlay';
26
27 /**
28  * Application controller, also a view.
29  */
30 export default class Application extends React.Component {
31
32   /**
33    * Construct application view.
34    * @param props element properties.
35    * @param context react context.
36    */
37   constructor(props, context) {
38     super(props, context);
39
40     this.sequencer = Common.assertNotNull(props.sequencer);
41     this.model = this.sequencer.getModel();
42     this.metamodel = this.sequencer.getMetamodel();
43     this.options = props.options;
44     Logger.setLevel(this.options.unwrap().log.level);
45
46     // Bindings.
47
48     this.showInfoDialog = this.showInfoDialog.bind(this);
49     this.showEditDialog = this.showEditDialog.bind(this);
50     this.showConfirmDialog = this.showConfirmDialog.bind(this);
51     this.hideOverlay = this.hideOverlay.bind(this);
52     this.onMouseMove = this.onMouseMove.bind(this);
53     this.onMouseUp = this.onMouseUp.bind(this);
54   }
55
56   // ///////////////////////////////////////////////////////////////////////////////////////////////
57
58   /**
59    * Get application options.
60    * @returns JSON options, see Options.js.
61    */
62   getOptions() {
63     return this.options.unwrap();
64   }
65
66   // ///////////////////////////////////////////////////////////////////////////////////////////////
67
68   /**
69    * Set diagram name.
70    * @param n diagram (human-readable) name.
71    */
72   setName(n) {
73     this.diagram.setName(n);
74   }
75
76   // ///////////////////////////////////////////////////////////////////////////////////////////////
77
78   /**
79    * Set diagram model.
80    * @param model diagram instance.
81    */
82   setModel(model) {
83
84     Common.assertNotNull(model);
85
86     this.model = model;
87
88     if (this.editor) {
89       this.editor.render();
90     }
91
92     if (this.diagram) {
93       this.diagram.render();
94     }
95   }
96
97   // ///////////////////////////////////////////////////////////////////////////////////////////////
98
99   /**
100    * Get Model wrapper.
101    * @returns Model.
102    */
103   getModel() {
104     return this.model;
105   }
106
107   // ///////////////////////////////////////////////////////////////////////////////////////////////
108
109   /**
110    * Get SVG element.
111    * @returns {*}
112    */
113   getSVG() {
114     return this.diagram.getSVG();
115   }
116
117   // ///////////////////////////////////////////////////////////////////////////////////////////////
118
119   /**
120    * Get top-level widget. Provides the demo toolbar with access to the public API.
121    * @returns {*}
122    */
123   getSequencer() {
124     return this.sequencer;
125   }
126
127   // ///////////////////////////////////////////////////////////////////////////////////////////////
128
129   /**
130    * Present info dialog.
131    * @param msg info message.
132    */
133   showInfoDialog(msg) {
134     this.dialog.showInfoDialog(msg);
135   }
136
137   // ///////////////////////////////////////////////////////////////////////////////////////////////
138
139   /**
140    * Present error dialog.
141    * @param msg error message.
142    */
143   showErrorDialog(msg) {
144     this.dialog.showErrorDialog(msg);
145   }
146
147   // ///////////////////////////////////////////////////////////////////////////////////////////////
148
149   /**
150    * Present confirmation dialog.
151    * @param msg info message.
152    * @param cb callback function to be invoked on OK.
153    */
154   showConfirmDialog(msg, cb) {
155     Common.assertType(cb, 'Function');
156     this.dialog.showConfirmDialog(msg, cb);
157   }
158
159   // ///////////////////////////////////////////////////////////////////////////////////////////////
160
161   /**
162    * Present edit (textarea) dialog.
163    * @param msg prompt.
164    * @param text current edit text.
165    * @param cb callback function to be invoked on OK, taking the updated text
166    * as an argument.
167    */
168   showEditDialog(msg, text, cb) {
169     this.dialog.showEditDialog(msg, text, cb);
170   }
171
172   // ///////////////////////////////////////////////////////////////////////////////////////////////
173
174   /**
175    * Select lifeline by ID.
176    * @param id lifeline ID.
177    */
178   selectLifeline(id) {
179     if (this.editor) {
180       this.editor.selectLifeline(id);
181     }
182     if (this.diagram) {
183       this.diagram.selectLifeline(id);
184     }
185   }
186
187   // ///////////////////////////////////////////////////////////////////////////////////////////////
188
189   /**
190    * Select message by ID.
191    * @param id message ID.
192    */
193   selectMessage(id) {
194     if (this.editor) {
195       this.editor.selectMessage(id);
196     }
197     if (this.diagram) {
198       this.diagram.selectMessage(id);
199     }
200   }
201
202   // ///////////////////////////////////////////////////////////////////////////////////////////////
203
204   /**
205    * (Re)render just the diagram.
206    */
207   renderDiagram() {
208     this.diagram.redraw();
209   }
210
211   // ///////////////////////////////////////////////////////////////////////////////////////////////
212
213   /**
214    * Show overlay between application and modal dialog.
215    */
216   showOverlay() {
217     if (this.overlay) {
218       this.overlay.setVisible(true);
219     }
220   }
221
222   // ///////////////////////////////////////////////////////////////////////////////////////////////
223
224   /**
225    * Hide overlay between application and modal dialog.
226    */
227   hideOverlay() {
228     if (this.overlay) {
229       this.overlay.setVisible(false);
230     }
231   }
232
233   // ///////////////////////////////////////////////////////////////////////////////////////////////
234
235   /**
236    * Capture mouse move events, for resize.
237    * @param event move event.
238    */
239   onMouseMove(event) {
240     if (this.editor) {
241       this.editor.onMouseMove(event);
242     }
243   }
244
245   // ///////////////////////////////////////////////////////////////////////////////////////////////
246
247   /**
248    * Propagate mouse event to the editor that manages the resize.
249    */
250   onMouseUp() {
251     if (this.editor) {
252       this.editor.onMouseUp();
253     }
254   }
255
256   // ///////////////////////////////////////////////////////////////////////////////////////////////
257
258   /**
259    * Render current model state.
260    */
261   render() {
262
263     return (
264
265       <div className="asdcs-control" onMouseMove={this.onMouseMove} onMouseUp={this.onMouseUp}>
266
267         <Editor application={this} ref={(r) => { this.editor = r; }} />
268         <Diagram application={this} ref={(r) => { this.diagram = r; }} />
269         <Dialog application={this} ref={(r) => { this.dialog = r; }} />
270         <Export />
271         <Overlay application={this} ref={(r) => { this.overlay = r; }} />
272
273       </div>
274     );
275   }
276
277 }
278
279 /** React properties. */
280 Application.propTypes = {
281   options: React.PropTypes.object.isRequired,
282   sequencer: React.PropTypes.object.isRequired,
283 };