-
Notifications
You must be signed in to change notification settings - Fork 4
/
LayeredDisplayView.java
471 lines (401 loc) · 12 KB
/
LayeredDisplayView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*******************************************************************************************************
*
* LayeredDisplayView.java, in ummisco.gama.ui.experiment, is part of the source code of the GAMA modeling and
* simulation platform (v.1.8.2).
*
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.ui.views.displays;
import static msi.gama.common.preferences.GamaPreferences.Displays.CORE_DISPLAY_BORDER;
import static msi.gama.common.preferences.GamaPreferences.Runtime.CORE_SYNC;
import java.awt.Color;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import msi.gama.common.interfaces.IDisplaySurface;
import msi.gama.common.interfaces.IDisposable;
import msi.gama.common.interfaces.IGamaView;
import msi.gama.common.interfaces.ILayerManager;
import msi.gama.kernel.experiment.ITopLevelAgent;
import msi.gama.outputs.IDisplayOutput;
import msi.gama.outputs.LayeredDisplayOutput;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.ui.resources.GamaColors;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.utils.ViewsHelper;
import ummisco.gama.ui.utils.WorkbenchHelper;
import ummisco.gama.ui.views.GamaViewPart;
import ummisco.gama.ui.views.toolbar.GamaToolbar2;
import ummisco.gama.ui.views.toolbar.IToolbarDecoratedView;
/**
* The Class LayeredDisplayView.
*/
public abstract class LayeredDisplayView extends GamaViewPart
implements IToolbarDecoratedView.Pausable, IToolbarDecoratedView.Zoomable, IGamaView.Display {
static {
DEBUG.ON();
}
/** The real index. */
protected int realIndex = -1;
/** The form. */
private SashForm form;
/** The surface composite. */
public Composite surfaceComposite;
/** The decorator. */
public LayeredDisplayDecorator decorator;
/** The synchronizer. */
public final LayeredDisplaySynchronizer synchronizer = new LayeredDisplaySynchronizer();
/** The disposed. */
public volatile boolean disposed = false;
/** The in init phase. */
protected volatile boolean inInitPhase = true;
/** The closing. */
private volatile boolean closing = false;
/** The central panel. */
protected CentralPanel centralPanel;
@Override
public void setIndex(final int index) { realIndex = index; }
@Override
public int getIndex() { return realIndex; }
/**
* Instantiates a new layered display view.
*/
public LayeredDisplayView() {
}
/**
* Gets the sash.
*
* @return the sash
*/
public SashForm getSash() { return form; }
@Override
public boolean containsPoint(final int x, final int y) {
if (super.containsPoint(x, y)) return true;
final Point o = getSurfaceComposite().toDisplay(0, 0);
final Point s = getSurfaceComposite().getSize();
return new Rectangle(o.x, o.y, s.x, s.y).contains(x, y);
}
@Override
public void init(final IViewSite site) throws PartInitException {
super.init(site);
decorator = new LayeredDisplayDecorator(this);
if (getOutput() != null) {
getOutput().getData().addListener(decorator);
setPartName(getOutput().getName());
}
}
@Override
public void addOutput(final IDisplayOutput out) {
super.addOutput(out);
if (out instanceof LayeredDisplayOutput) {
final IScope scope = out.getScope();
if (scope != null && scope.getSimulation() != null) {
final ITopLevelAgent root = scope.getRoot();
final Color color = root.getColor();
this.setTitleImage(GamaIcons.createTempColorIcon(GamaColors.get(color)));
}
}
}
/**
* Checks if is open GL.
*
* @return true, if is open GL
*/
public boolean isOpenGL() {
return false;
// if (outputs.isEmpty()) return false;
// return getOutput().getData().isOpenGL();
}
/**
* Gets the display manager.
*
* @return the display manager
*/
public ILayerManager getDisplayManager() { return getDisplaySurface().getManager(); }
/**
* Gets the surface composite.
*
* @return the surface composite
*/
public Composite getSurfaceComposite() { return surfaceComposite; }
/**
* The Class CentralPanel.
*/
public class CentralPanel extends Composite implements InnerComponent {
/**
* Instantiates a new central panel.
*
* @param parent
* the parent
* @param style
* the style
*/
public CentralPanel() {
super(form, CORE_DISPLAY_BORDER.getValue() ? SWT.BORDER : SWT.NONE);
setLayout(emptyLayout());
setLayoutData(fullData());
setParentComposite(this);
form.setMaximizedControl(this);
}
/**
* Gets the view.
*
* @return the view
*/
@Override
public LayeredDisplayView getView() { return LayeredDisplayView.this; }
}
@Override
public void ownCreatePartControl(final Composite c) {
if (getOutput() == null) return;
c.setLayout(emptyLayout());
// First create the sashform
form = new SashForm(c, SWT.HORIZONTAL);
form.setLayoutData(fullData());
form.setBackground(IGamaColors.WHITE.color());
form.setSashWidth(8);
decorator.createSidePanel(form);
centralPanel = new CentralPanel();
createSurfaceComposite(centralPanel);
surfaceComposite.setLayoutData(fullData());
getOutput().setSynchronized(getOutput().isSynchronized() || CORE_SYNC.getValue());
decorator.createDecorations(form);
c.requestLayout();
}
/**
* Gets the multi listener.
*
* @return the multi listener
*/
public IDisposable getMultiListener() {
return new SWTLayeredDisplayMultiListener(decorator, getDisplaySurface());
}
/**
* Empty layout.
*
* @return the grid layout
*/
Layout emptyLayout() {
// return new FillLayout(SWT.VERTICAL);
final GridLayout gl = new GridLayout(1, true);
gl.horizontalSpacing = 0;
gl.marginHeight = 0;
gl.marginWidth = 0;
gl.verticalSpacing = 0;
return gl;
}
/**
* Full data.
*
* @return the grid data
*/
GridData fullData() {
return new GridData(SWT.FILL, SWT.FILL, true, true);
}
//
// @Override
// public void setFocus() {
// if (getParentComposite() != null && !getParentComposite().isDisposed()
// && !getParentComposite().isFocusControl()) {
// getParentComposite().forceFocus();
// }
// }
/**
* Creates the surface composite.
*
* @param parent
* the parent
* @return the composite
*/
protected abstract Composite createSurfaceComposite(Composite parent);
@Override
public LayeredDisplayOutput getOutput() { return (LayeredDisplayOutput) super.getOutput(); }
@Override
public IDisplaySurface getDisplaySurface() {
final LayeredDisplayOutput out = getOutput();
if (out != null) return out.getSurface();
return null;
}
@Override
public void widgetDisposed(final DisposeEvent e) {
if (disposed) return;
final LayeredDisplayOutput output = getOutput();
if (output != null) {
output.getData().listeners.clear();
final IDisplaySurface s = output.getSurface();
if (isOpenGL() && s != null) {
s.dispose();
output.setSurface(null);
}
output.releaseView();
output.setSurface(null);
}
disposed = true;
if (surfaceComposite != null) {
try {
surfaceComposite.dispose();
} catch (final RuntimeException ex) {
}
}
synchronizer.authorizeViewUpdate();
// }
if (updateThread != null) {
updateThread.interrupt();
DEBUG.OUT("Update thread disposed for " + getTitle());
updateThread = null;
}
if (decorator != null) { decorator.dispose(); }
super.widgetDisposed(e);
}
@Override
public void pauseChanged() {
decorator.updateOverlay();
}
/**
* Force overlay visibility.
*
* @return true, if successful
*/
public boolean forceOverlayVisibility() {
return false;
}
@Override
public void synchronizeChanged() {
decorator.updateOverlay();
}
@Override
public void zoomIn() {
if (getDisplaySurface() != null) { getDisplaySurface().zoomIn(); }
}
@Override
public void zoomOut() {
if (getDisplaySurface() != null) { getDisplaySurface().zoomOut(); }
}
@Override
public void zoomFit() {
if (getDisplaySurface() != null) { getDisplaySurface().zoomFit(); }
}
@Override
public Control[] getZoomableControls() { return new Control[] { getParentComposite() }; }
@Override
protected GamaUIJob createUpdateJob() {
return new GamaUIJob() {
@Override
protected UpdatePriority jobPriority() {
return UpdatePriority.HIGHEST;
}
@Override
public IStatus runInUIThread(final IProgressMonitor monitor) {
return Status.OK_STATUS;
}
};
}
/** The update thread. */
Thread updateThread;
/**
* Inits the update thread.
*/
private void initUpdateThread() {
updateThread = new Thread(() -> {
final IDisplaySurface surface = getDisplaySurface();
synchronizer.waitForSurfaceToBeRealized();
while (!disposed && !surface.isDisposed()) {
try {
synchronizer.waitForViewUpdateAuthorisation();
surface.updateDisplay(false);
if (surface.getData().isAutosave()) { takeSnapshot(); }
inInitPhase = false;
} catch (Exception e) {
DEBUG.OUT("Error when updating " + this.getTitle() + ": " + e.getMessage());
}
}
});
}
@Override
public void update(final IDisplayOutput out) {
if (updateThread == null || !updateThread.isAlive()) {
initUpdateThread();
synchronizer.setSurface(getDisplaySurface());
updateThread.start();
}
synchronizer.authorizeViewUpdate();
if (!inInitPhase && out.isSynchronized()) { synchronizer.waitForRenderingToBeFinished(); }
}
@Override
public boolean zoomWhenScrolling() {
return true;
}
@Override
public void removeOutput(final IDisplayOutput output) {
if (output == null) return;
if (output == getOutput() && isFullScreen()) { WorkbenchHelper.run(this::toggleFullScreen); }
output.dispose();
outputs.remove(output);
if (outputs.isEmpty()) {
synchronizer.authorizeViewUpdate();
close(GAMA.getRuntimeScope());
}
}
@Override
public boolean isFullScreen() { return decorator.isFullScreen(); }
@Override
public void toggleSideControls() {
decorator.toggleSideControls();
}
@Override
public void toggleOverlay() {
decorator.toggleOverlay();
}
@Override
public void toggleFullScreen() {
decorator.toggleFullScreen();
}
@Override
public void createToolItems(final GamaToolbar2 tb) {
super.createToolItems(tb);
decorator.createToolItems(tb);
}
@Override
public void showOverlay(final boolean show) {
decorator.overlay.setVisible(show);
}
@Override
public void takeSnapshot() {
SnapshotMaker.getInstance().doSnapshot(getDisplaySurface(), WorkbenchHelper.displaySizeOf(surfaceComposite));
}
/**
* Gets the interaction control.
*
* @return the interaction control
*/
public Control getInteractionControl() { return getZoomableControls()[0]; }
@Override
public void close(final IScope scope) {
if (closing) return;
closing = true;
WorkbenchHelper.asyncRun(() -> {
try {
if (getDisplaySurface() != null) { getDisplaySurface().dispose(); }
ViewsHelper.hideView(this);
} catch (final Exception e) {}
});
}
}