-
Notifications
You must be signed in to change notification settings - Fork 154
/
Workbench.java
3566 lines (3140 loc) · 116 KB
/
Workbench.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Francis Upton - <francisu@ieee.org> - Bug 217777
* Tristan Hume - <trishume@gmail.com> - Bug 2369
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 422533, 440136, 445724, 366708, 418661, 456897, 472654, 481516, 486543
* Terry Parker <tparker@google.com> - Bug 416673
* Sergey Prigogin <eclipse.sprigogin@gmail.com> - Bug 438324
* Snjezana Peco <snjeza.peco@gmail.com> - Bug 405542
* Andrey Loskutov <loskutov@gmx.de> - Bug 372799
* Mickael Istria (Red Hat Inc.) - Bug 469918
* Patrik Suzzi <psuzzi@gmail.com> - Bug 487297
* Daniel Kruegler <daniel.kruegler@gmail.com> - Bug 520926
* Christian Georgi (SAP SE) - Bug 540440
* Paul Pazderski <paul-eclipse@ppazderski.de> - Bug 550950
*******************************************************************************/
package org.eclipse.ui.internal;
import com.ibm.icu.util.ULocale;
import com.ibm.icu.util.ULocale.Category;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.EventManager;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.commands.contexts.ContextManager;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionDelta;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IRegistryChangeListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.e4.core.contexts.ContextFunction;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.InjectionException;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.internal.workbench.E4Workbench;
import org.eclipse.e4.ui.internal.workbench.E4XMIResource;
import org.eclipse.e4.ui.internal.workbench.renderers.swt.IUpdateService;
import org.eclipse.e4.ui.internal.workbench.swt.E4Application;
import org.eclipse.e4.ui.internal.workbench.swt.IEventLoopAdvisor;
import org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MBindingContext;
import org.eclipse.e4.ui.model.application.commands.MBindingTable;
import org.eclipse.e4.ui.model.application.commands.MCategory;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
import org.eclipse.e4.ui.model.application.commands.impl.CommandsFactoryImpl;
import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.model.application.ui.basic.impl.BasicFactoryImpl;
import org.eclipse.e4.ui.model.application.ui.menu.MTrimContribution;
import org.eclipse.e4.ui.services.EContextService;
import org.eclipse.e4.ui.workbench.IModelResourceHandler;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.ISaveHandler;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.ExternalActionManager;
import org.eclipse.jface.action.ExternalActionManager.CommandCallback;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.bindings.BindingManager;
import org.eclipse.jface.bindings.IBindingManagerListener;
import org.eclipse.jface.databinding.swt.DisplayRealm;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.ModalContext;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.BidiUtils;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.service.runnable.StartupMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.graphics.DeviceData;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IDecoratorManager;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.ILocalWorkingSetManager;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.ISaveableFilter;
import org.eclipse.ui.ISaveablesLifecycleListener;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.ISourceProvider;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.Saveable;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.commands.ICommandImageService;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.commands.IWorkbenchCommandSupport;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.contexts.IWorkbenchContextSupport;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.help.IWorkbenchHelpSystem;
import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
import org.eclipse.ui.internal.WorkbenchWindow.WWinPartServiceSaveHandler;
import org.eclipse.ui.internal.actions.CommandAction;
import org.eclipse.ui.internal.activities.ws.WorkbenchActivitySupport;
import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;
import org.eclipse.ui.internal.commands.CommandImageManager;
import org.eclipse.ui.internal.commands.CommandImageService;
import org.eclipse.ui.internal.commands.CommandService;
import org.eclipse.ui.internal.commands.WorkbenchCommandSupport;
import org.eclipse.ui.internal.contexts.ActiveContextSourceProvider;
import org.eclipse.ui.internal.contexts.ContextService;
import org.eclipse.ui.internal.contexts.WorkbenchContextSupport;
import org.eclipse.ui.internal.dialogs.PropertyPageContributorManager;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityPart;
import org.eclipse.ui.internal.e4.compatibility.E4Util;
import org.eclipse.ui.internal.handlers.LegacyHandlerService;
import org.eclipse.ui.internal.help.WorkbenchHelpSystem;
import org.eclipse.ui.internal.intro.IIntroRegistry;
import org.eclipse.ui.internal.intro.IntroDescriptor;
import org.eclipse.ui.internal.keys.BindingService;
import org.eclipse.ui.internal.keys.show.ShowKeysListener;
import org.eclipse.ui.internal.menus.FocusControlSourceProvider;
import org.eclipse.ui.internal.menus.WorkbenchMenuService;
import org.eclipse.ui.internal.misc.Policy;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.internal.misc.UIStats;
import org.eclipse.ui.internal.model.ContributionService;
import org.eclipse.ui.internal.progress.ProgressManager;
import org.eclipse.ui.internal.progress.ProgressManagerUtil;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
import org.eclipse.ui.internal.registry.ViewDescriptor;
import org.eclipse.ui.internal.services.EvaluationService;
import org.eclipse.ui.internal.services.IServiceLocatorCreator;
import org.eclipse.ui.internal.services.IWorkbenchLocationService;
import org.eclipse.ui.internal.services.MenuSourceProvider;
import org.eclipse.ui.internal.services.ServiceLocator;
import org.eclipse.ui.internal.services.ServiceLocatorCreator;
import org.eclipse.ui.internal.services.SourceProviderService;
import org.eclipse.ui.internal.services.WorkbenchLocationService;
import org.eclipse.ui.internal.splash.EclipseSplashHandler;
import org.eclipse.ui.internal.splash.SplashHandlerFactory;
import org.eclipse.ui.internal.testing.ContributionInfoMessages;
import org.eclipse.ui.internal.testing.WorkbenchTestable;
import org.eclipse.ui.internal.themes.ColorDefinition;
import org.eclipse.ui.internal.themes.FontDefinition;
import org.eclipse.ui.internal.themes.ThemeElementHelper;
import org.eclipse.ui.internal.themes.WorkbenchThemeManager;
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.intro.IIntroManager;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.model.IContributionService;
import org.eclipse.ui.operations.IWorkbenchOperationSupport;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.services.IDisposable;
import org.eclipse.ui.services.IEvaluationService;
import org.eclipse.ui.services.IServiceScopes;
import org.eclipse.ui.services.ISourceProviderService;
import org.eclipse.ui.splash.AbstractSplashHandler;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.ui.swt.IFocusService;
import org.eclipse.ui.testing.ContributionInfo;
import org.eclipse.ui.themes.ITheme;
import org.eclipse.ui.themes.IThemeManager;
import org.eclipse.ui.views.IViewDescriptor;
import org.eclipse.ui.views.IViewRegistry;
import org.eclipse.ui.wizards.IWizardRegistry;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.SynchronousBundleListener;
import org.osgi.util.tracker.ServiceTracker;
/**
* The workbench class represents the top of the Eclipse user interface. Its
* primary responsibility is the management of workbench windows, dialogs,
* wizards, and other workbench-related windows.
* <p>
* Note that any code that is run during the creation of a workbench instance
* should not required access to the display.
* </p>
*/
public final class Workbench extends EventManager implements IWorkbench, org.eclipse.e4.ui.workbench.IWorkbench {
public static final String WORKBENCH_AUTO_SAVE_JOB = "Workbench Auto-Save Job"; //$NON-NLS-1$
private static final String WORKBENCH_AUTO_SAVE_BACKGROUND_JOB = "Workbench Auto-Save Background Job"; //$NON-NLS-1$
public static final String MEMENTO_KEY = "memento"; //$NON-NLS-1$
public static final String EDITOR_TAG = "Editor"; //$NON-NLS-1$
private static final String PROP_VM = "eclipse.vm"; //$NON-NLS-1$
private static final String PROP_VMARGS = "eclipse.vmargs"; //$NON-NLS-1$
private static final String PROP_COMMANDS = "eclipse.commands"; //$NON-NLS-1$
public static final String PROP_EXIT_CODE = "eclipse.exitcode"; //$NON-NLS-1$
private static final String CMD_DATA = "-data"; //$NON-NLS-1$
private static final String CMD_VMARGS = "-vmargs"; //$NON-NLS-1$
private static final class StartupProgressBundleListener implements SynchronousBundleListener {
private final SubMonitor subMonitor;
private Display displayForStartupListener;
StartupProgressBundleListener(IProgressMonitor progressMonitor, Display display) {
displayForStartupListener = display;
subMonitor = SubMonitor.convert(progressMonitor);
subMonitor.setTaskName(NLS.bind(WorkbenchMessages.Startup_Loading, Platform.getProduct().getName()));
}
@Override
public void bundleChanged(BundleEvent event) {
int eventType = event.getType();
if (eventType == BundleEvent.STARTED) {
subMonitor.setWorkRemaining(5).worked(1);
spinEventQueueToUpdateSplash(displayForStartupListener);
}
}
}
/**
* Family for the early startup job.
*/
public static final String EARLY_STARTUP_FAMILY = "earlyStartup"; //$NON-NLS-1$
public static final String DEFAULT_WORKBENCH_STATE_FILENAME = "workbench.xml"; //$NON-NLS-1$
/**
* Holds onto the only instance of Workbench.
*/
private static Workbench instance;
/**
* The testable object facade.
*
* @since 3.0
*/
private static WorkbenchTestable testableObject;
/**
* Signals that the workbench should create a splash implementation when
* instantiated. Initial value is <code>true</code>.
*
* @since 3.3
*/
private static boolean createSplash = true;
/**
* The splash handler.
*/
private static AbstractSplashHandler splash;
/**
* The display used for all UI interactions with this workbench.
*
* @since 3.0
*/
private Display display;
private boolean workbenchAutoSave = true;
private EditorHistory editorHistory;
private boolean runEventLoop = true;
private boolean isStarting = true;
private boolean isClosing = false;
/**
* A boolean field to indicate whether all the workbench windows have been
* closed or not.
*/
private boolean windowsClosed = false;
/**
* PlatformUI return code
*/
private int returnCode = PlatformUI.RETURN_UNSTARTABLE;
/**
* Advisor providing application-specific configuration and customization of the
* workbench.
*
* @since 3.0
*/
private WorkbenchAdvisor advisor;
/**
* Object for configuring the workbench. Lazily initialized to an instance
* unique to the workbench instance.
*
* @since 3.0
*/
private WorkbenchConfigurer workbenchConfigurer;
// for dynamic UI
/**
* ExtensionEventHandler handles extension life-cycle events.
*/
private ExtensionEventHandler extensionEventHandler;
/**
* A count of how many large updates are going on. This tracks nesting of
* requests to disable services during a large update -- similar to the
* <code>setRedraw</code> functionality on <code>Control</code>. When this value
* becomes greater than zero, services are disabled. When this value becomes
* zero, services are enabled. Please see <code>largeUpdateStart()</code> and
* <code>largeUpdateEnd()</code>.
*/
private int largeUpdates = 0;
/**
* The service locator maintained by the workbench. These services are
* initialized during workbench during the <code>init</code> method.
*/
private final ServiceLocator serviceLocator;
/**
* Listener list for registered IWorkbenchListeners .
*/
private ListenerList<IWorkbenchListener> workbenchListeners = new ListenerList<>(ListenerList.IDENTITY);
private ServiceRegistration workbenchService;
private MApplication application;
private IEclipseContext e4Context;
private IEventBroker eventBroker;
private IExtensionRegistry registry;
boolean initializationDone = false;
private WorkbenchWindow windowBeingCreated = null;
private Listener backForwardListener;
private Job autoSaveJob;
private String id;
private ServiceRegistration<?> e4WorkbenchService;
// flag used to identify if the application model needs to be saved
private boolean applicationModelChanged = false;
private IWorkbenchWindow windowWhileInit;
/**
* Creates a new workbench.
*
* @param display the display to be used for all UI interactions with the
* workbench
* @param advisor the application-specific advisor that configures and
* specializes this workbench instance
* @since 3.0
*/
private Workbench(Display display, final WorkbenchAdvisor advisor, MApplication app, IEclipseContext appContext) {
this.advisor = Objects.requireNonNull(advisor);
this.display = Objects.requireNonNull(display);
application = app;
e4Context = appContext;
this.id = createId();
if (instance != null && instance.isRunning()) {
throw new IllegalStateException(WorkbenchMessages.Workbench_CreatingWorkbenchTwice);
}
Workbench.instance = this;
StartupThreading.setDisplay(display);
eventBroker = e4Context.get(IEventBroker.class);
registry = e4Context.get(IExtensionRegistry.class);
appContext.set(Workbench.class, this);
appContext.set(IWorkbench.class, this);
appContext.set(IEventLoopAdvisor.class, new IEventLoopAdvisor() {
@Override
public void eventLoopIdle(Display display) {
advisor.eventLoopIdle(display);
}
@Override
public void eventLoopException(Throwable exception) {
advisor.eventLoopException(exception);
}
});
// for dynamic UI [This seems to be for everything that isn't handled by
// some
// subclass of RegistryManager. I think that when an extension is moved
// to the
// RegistryManager implementation, then it should be removed from the
// list in
// ExtensionEventHandler#appear.
// I've found that the new wizard extension in particular is a poor
// choice to
// use as an example, since the result of reading the registry is not
// cached
// -- so it is re-read each time. The only real contribution of this
// dialog is
// to show the user a nice dialog describing the addition.]
extensionEventHandler = new ExtensionEventHandler(this);
registry.addRegistryChangeListener(extensionEventHandler);
IServiceLocatorCreator slc = new ServiceLocatorCreator();
serviceLocator = (ServiceLocator) slc.createServiceLocator(null, null, () -> {
final Display display1 = getDisplay();
if (display1 != null && !display1.isDisposed()) {
MessageDialog.openInformation(null, WorkbenchMessages.Workbench_NeedsClose_Title,
WorkbenchMessages.Workbench_NeedsClose_Message);
close(PlatformUI.RETURN_RESTART, true);
}
}, appContext);
serviceLocator.registerService(IServiceLocatorCreator.class, slc);
serviceLocator.registerService(IWorkbenchLocationService.class,
new WorkbenchLocationService(IServiceScopes.WORKBENCH_SCOPE, this, null, null, null, null, 0));
}
/**
* Returns the one and only instance of the workbench, if there is one.
*
* @return the workbench, or <code>null</code> if the workbench has not been
* created, or has been created and already completed
*/
public static Workbench getInstance() {
return instance;
}
/**
* Creates the workbench and associates it with the the given display and
* workbench advisor, and runs the workbench UI. This entails processing and
* dispatching events until the workbench is closed or restarted.
* <p>
* This method is intended to be called by <code>PlatformUI</code>. Fails if the
* workbench UI has already been created.
* </p>
* <p>
* The display passed in must be the default display.
* </p>
*
* @param display the display to be used for all UI interactions with the
* workbench
* @param advisor the application-specific advisor that configures and
* specializes the workbench
* @return return code {@link PlatformUI#RETURN_OK RETURN_OK}for normal exit;
* {@link PlatformUI#RETURN_RESTART RETURN_RESTART}if the workbench was
* terminated with a call to {@link IWorkbench#restart
* IWorkbench.restart}; other values reserved for future use
*/
public static int createAndRunWorkbench(final Display display, final WorkbenchAdvisor advisor) {
final int[] returnCode = new int[1];
Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
boolean showProgress = PrefUtil.getAPIPreferenceStore()
.getBoolean(IWorkbenchPreferenceConstants.SHOW_PROGRESS_ON_STARTUP);
final String nlExtensions = Platform.getNLExtensions();
if (nlExtensions.length() > 0) {
ULocale.setDefault(Category.FORMAT,
new ULocale(ULocale.getDefault(Category.FORMAT).getBaseName() + nlExtensions));
}
System.setProperty(org.eclipse.e4.ui.workbench.IWorkbench.XMI_URI_ARG,
"org.eclipse.ui.workbench/LegacyIDE.e4xmi"); //$NON-NLS-1$
Object obj = getApplication(Platform.getCommandLineArgs());
IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();
if (!store.isDefault(IPreferenceConstants.LAYOUT_DIRECTION)) {
int orientation = store.getInt(IPreferenceConstants.LAYOUT_DIRECTION);
Window.setDefaultOrientation(orientation);
}
if (obj instanceof E4Application) {
E4Application e4app = (E4Application) obj;
E4Workbench e4Workbench = e4app.createE4Workbench(getApplicationContext(), display);
MApplication appModel = e4Workbench.getApplication();
IEclipseContext context = e4Workbench.getContext();
// create the workbench instance
Workbench workbench = new Workbench(display, advisor, appModel, context);
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(Constants.SERVICE_RANKING, Integer.valueOf(Integer.MAX_VALUE - 1));
ServiceRegistration<?> registration[] = new ServiceRegistration[1];
StartupMonitor startupMonitor = new StartupMonitor() {
@Override
public void applicationRunning() {
registration[0].unregister(); // unregister ourself
// fire part visibility events now that we're up
for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
IWorkbenchPage page = window.getActivePage();
if (page != null) {
((WorkbenchPage) page).fireInitialPartVisibilityEvents();
}
}
}
@Override
public void update() {
// do nothing - we come into the picture far too late
// for this to be relevant
}
};
registration[0] = FrameworkUtil.getBundle(WorkbenchPlugin.class).getBundleContext()
.registerService(StartupMonitor.class.getName(), startupMonitor,
properties);
// listener for updating the splash screen
SynchronousBundleListener bundleListener = null;
createSplash = WorkbenchPlugin.isSplashHandleSpecified();
if (createSplash) {
// prime the splash nice and early
workbench.createSplashWrapper();
// Bug 539376, 427393, 455162: show the splash screen after
// the image is loaded. See IDEApplication#checkInstanceLocation
// where the splash shell got hidden to avoid empty shell
AbstractSplashHandler handler = getSplash();
if (handler != null) {
Shell splashShell = handler.getSplash();
if (splashShell != null && !splashShell.isDisposed()) {
splashShell.setVisible(true);
splashShell.forceActive();
}
}
spinEventQueueToUpdateSplash(display);
if (handler != null && showProgress) {
IProgressMonitor progressMonitor = SubMonitor.convert(handler.getBundleProgressMonitor());
bundleListener = new Workbench.StartupProgressBundleListener(progressMonitor, display);
WorkbenchPlugin.getDefault().addBundleListener(bundleListener);
}
}
setSearchContribution(appModel, true);
// run the legacy workbench once
returnCode[0] = workbench.runUI();
if (returnCode[0] == PlatformUI.RETURN_OK) {
// run the e4 event loop and instantiate ... well, stuff
if (bundleListener != null) {
WorkbenchPlugin.getDefault().removeBundleListener(bundleListener);
}
e4Workbench.createAndRunUI(e4Workbench.getApplication());
}
if (returnCode[0] != PlatformUI.RETURN_UNSTARTABLE) {
setSearchContribution(appModel, false);
e4app.saveModel();
}
// if a restart was triggered via E4Workbench the return
// code needs to be set appropriately
if (e4Workbench.isRestart()) {
returnCode[0] = PlatformUI.RETURN_RESTART;
} else {
e4Workbench.close();
returnCode[0] = workbench.returnCode;
}
}
});
return returnCode[0];
}
private static void setSearchContribution(MApplication app, boolean enabled) {
for (MTrimContribution contribution : app.getTrimContributions()) {
if ("org.eclipse.ui.ide.application.trimcontribution.QuickAccess".contains(contribution //$NON-NLS-1$
.getElementId())) {
// allows us to handle the case where someone opens a workspace
// with Luna and then with Kepler
contribution.setToBeRendered(enabled);
}
}
}
private static ServiceTracker instanceAppContext;
static IApplicationContext getApplicationContext() {
if (instanceAppContext == null) {
instanceAppContext = new ServiceTracker(WorkbenchPlugin.getDefault().getBundleContext(),
IApplicationContext.class.getName(), null);
instanceAppContext.open();
}
return (IApplicationContext) instanceAppContext.getService();
}
static Object getApplication(String[] args) {
// Find the name of the application as specified by the PDE JUnit
// launcher.
// If no application is specified, the 3.0 default workbench application
// is returned.
IExtension extension = Platform.getExtensionRegistry().getExtension(Platform.PI_RUNTIME,
Platform.PT_APPLICATIONS, "org.eclipse.e4.ui.workbench.swt.E4Application"); //$NON-NLS-1$
Assert.isNotNull(extension);
// If the extension does not have the correct grammar, return null.
// Otherwise, return the application object.
try {
IConfigurationElement[] elements = extension.getConfigurationElements();
if (elements.length > 0) {
IConfigurationElement[] runs = elements[0].getChildren("run"); //$NON-NLS-1$
if (runs.length > 0) {
Object runnable;
runnable = runs[0].createExecutableExtension("class");//$NON-NLS-1$
if (runnable instanceof IApplication)
return runnable;
}
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Creates the <code>Display</code> to be used by the workbench.
*
* @return the display
*/
public static Display createDisplay() {
// setup the application name used by SWT to lookup resources on some
// platforms
String applicationName = WorkbenchPlugin.getDefault().getAppName();
if (applicationName != null) {
Display.setAppName(applicationName);
}
// create the display
Display newDisplay = Display.getCurrent();
if (newDisplay == null) {
if (Policy.DEBUG_SWT_GRAPHICS || Policy.DEBUG_SWT_DEBUG) {
DeviceData data = new DeviceData();
if (Policy.DEBUG_SWT_GRAPHICS) {
data.tracking = true;
}
if (Policy.DEBUG_SWT_DEBUG) {
data.debug = true;
}
newDisplay = new Display(data);
} else {
newDisplay = new Display();
}
}
// workaround for 1GEZ9UR and 1GF07HN
newDisplay.setWarnings(false);
// Set the priority higher than normal so as to be higher
// than the JobManager.
Thread.currentThread().setPriority(Math.min(Thread.MAX_PRIORITY, Thread.NORM_PRIORITY + 1));
initializeImages();
return newDisplay;
}
/**
* Create the splash wrapper and set it to work.
*
* @since 3.3
*/
private void createSplashWrapper() {
SafeRunnable run = new SafeRunnable() {
Image background = null;
@Override
public void run() throws Exception {
String splashLoc = System.getProperty("org.eclipse.equinox.launcher.splash.location"); //$NON-NLS-1$
background = loadSplashScreenImage(display, splashLoc);
// create the splash
getSplash();
if (splash == null) {
createSplash = false;
return;
}
Shell splashShell = splash.getSplash();
if (splashShell == null) {
splashShell = WorkbenchPlugin.getSplashShell(display);
if (splashShell == null)
return;
if (background != null)
splashShell.setBackgroundImage(background);
}
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(Constants.SERVICE_RANKING, Integer.valueOf(Integer.MAX_VALUE));
BundleContext context = WorkbenchPlugin.getDefault().getBundleContext();
final ServiceRegistration<?> registration[] = new ServiceRegistration[1];
StartupMonitor startupMonitor = new StartupMonitor() {
@Override
public void applicationRunning() {
if (background != null)
background.dispose();
registration[0].unregister(); // unregister ourself
if (splash != null)
splash.dispose();
WorkbenchPlugin.unsetSplashShell(display);
}
@Override
public void update() {
// do nothing - we come into the picture far too late
// for this to be relevant
}
};
registration[0] = context.registerService(StartupMonitor.class.getName(), startupMonitor, properties);
splash.init(splashShell);
}
@Override
public void handleException(Throwable e) {
StatusManager.getManager()
.handle(StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, "Could not instantiate splash", e)); //$NON-NLS-1$
createSplash = false;
splash = null;
if (background != null)
background.dispose();
}
};
SafeRunner.run(run);
}
// Ensure that the splash screen is rendered
private static void spinEventQueueToUpdateSplash(final Display display) {
if (!display.isDisposed() && display.getThread() == Thread.currentThread()) {
int safetyCounter = 0;
while (display.readAndDispatch() && safetyCounter++ < 100) {
// process until the queue is empty or until we hit the safetyCounter limit
}
}
}
/**
* Load an image from a filesystem path.
*
* @param splashLoc the location to load from
* @return the image or <code>null</code>
* @since 3.3
*/
private static Image loadSplashScreenImage(Display display, String splashLoc) {
Image background = null;
if (splashLoc != null) {
try (InputStream input = new BufferedInputStream(new FileInputStream(splashLoc))) {
background = getImage(display, input);
} catch (SWTException | IOException e) {
StatusManager.getManager().handle(StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, e));
}
}
return background;
}
private static Image getImage(Display display, InputStream input) {
Image image = new Image(display, input);
if (Util.isMac()) {
/*
* Due to a bug in MacOS Sonoma
* (https://github.com/eclipse-platform/eclipse.platform.swt/issues/772) ,Splash
* Screen gets flipped.As a workaround the image is flipped and returned.
*/
if (System.getProperty("os.version").startsWith("14")) { //$NON-NLS-1$ //$NON-NLS-2$
GC gc = new GC(image);
Transform tr = new Transform(display);
tr.setElements(1, 0, 0, -1, 0, 0);
gc.setTransform(tr);
gc.drawImage(image, 0, -(image.getBounds().height));
tr.dispose();
gc.dispose();
}
}
return image;
}
/**
* Return the splash handler for this application. If none is specifically
* provided the default Eclipse implementation is returned.
*
* @return the splash handler for this application or <code>null</code>
* @since 3.3
*/
private static AbstractSplashHandler getSplash() {
if (!createSplash)
return null;
if (splash == null) {
IProduct product = Platform.getProduct();
if (product != null)
splash = SplashHandlerFactory.findSplashHandlerFor(product);
if (splash == null)
splash = new EclipseSplashHandler();
}
return splash;
}
/**
* Returns the testable object facade, for use by the test harness.
*
* @return the testable object facade
* @since 3.0
*/
public static WorkbenchTestable getWorkbenchTestable() {
if (testableObject == null) {
testableObject = new WorkbenchTestable();
}
return testableObject;
}
@Override
public void addWorkbenchListener(IWorkbenchListener listener) {
workbenchListeners.add(listener);
}
@Override
public void removeWorkbenchListener(IWorkbenchListener listener) {
workbenchListeners.remove(listener);
}
/**
* Fire workbench preShutdown event, stopping at the first one to veto
*
* @param forced flag indicating whether the shutdown is being forced
* @return <code>true</code> to allow the workbench to proceed with shutdown,
* <code>false</code> to veto a non-forced shutdown
* @since 3.2
*/
boolean firePreShutdown(final boolean forced) {
for (final IWorkbenchListener l : workbenchListeners) {
final boolean[] result = new boolean[] { false };
SafeRunnable.run(new SafeRunnable() {
@Override
public void run() {
result[0] = l.preShutdown(Workbench.this, forced);
}
});
if (!result[0]) {
return false;
}
}
return true;
}
/**
* Fire workbench postShutdown event.
*
* @since 3.2
*/
void firePostShutdown() {
for (final IWorkbenchListener l : workbenchListeners) {
SafeRunnable.run(new SafeRunnable() {
@Override
public void run() {
l.postShutdown(Workbench.this);
}
});
}
}
@Override
public void addWindowListener(IWindowListener l) {
addListenerObject(l);
}
@Override
public void removeWindowListener(IWindowListener l) {
removeListenerObject(l);
}
/**
* Fire window opened event.
*
* @param window The window which just opened; should not be <code>null</code>.
*/
protected void fireWindowOpened(final IWorkbenchWindow window) {
Object list[] = getListeners();
for (Object element : list) {
final IWindowListener l = (IWindowListener) element;
SafeRunner.run(new SafeRunnable() {
@Override
public void run() {
l.windowOpened(window);
}
});
}
}
/**
* Fire window closed event.