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