-
Notifications
You must be signed in to change notification settings - Fork 155
/
WorkbenchPage.java
5554 lines (4912 loc) · 178 KB
/
WorkbenchPage.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, 2020 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
* Christian Janz - <christian.janz@gmail.com> Fix for Bug 385592
* Marc-Andre Laperle (Ericsson) - Fix for Bug 413590
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 431340, 431348, 426535, 433234, 431868, 472654
* Cornel Izbasa <cizbasa@info.uvt.ro> - Bug 442214
* Andrey Loskutov <loskutov@gmx.de> - Bug 411639, 372799, 466230
* Dirk Fauth <dirk.fauth@googlemail.com> - Bug 473063
* Stefan Prieschl <stefan.prieschl@gmail.com> - Bug 374132
* Paul Pazderski <paul-eclipse@ppazderski.de> - Bug 549361
* Christoph Läubrich - Bug 538151
* Dennis Hendriks - Bug 576877
*******************************************************************************/
package org.eclipse.ui.internal;
import static java.util.Collections.singletonList;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Predicate;
import org.eclipse.core.runtime.Adapters;
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.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.internal.workbench.ModelServiceImpl;
import org.eclipse.e4.ui.internal.workbench.PartServiceImpl;
import org.eclipse.e4.ui.internal.workbench.UIExtensionTracker;
import org.eclipse.e4.ui.model.application.MApplication;
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.MGenericStack;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.model.application.ui.basic.MWindowElement;
import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.UIEvents.EventTags;
import org.eclipse.e4.ui.workbench.addons.minmax.TrimStack;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.dialogs.IPageChangeProvider;
import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.internal.provisional.action.ICoolBarManager2;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorLauncher;
import org.eclipse.ui.IEditorMatchingStrategy;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.INavigationHistory;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableEditor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IReusableEditor;
import org.eclipse.ui.ISaveablePart;
import org.eclipse.ui.ISaveablePart2;
import org.eclipse.ui.ISaveablesLifecycleListener;
import org.eclipse.ui.ISaveablesSource;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IShowEditorInput;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.MultiPartInitException;
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.contexts.IContextService;
import org.eclipse.ui.internal.dialogs.cpd.CustomizePerspectiveDialog;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityPart;
import org.eclipse.ui.internal.e4.compatibility.CompatibilityView;
import org.eclipse.ui.internal.e4.compatibility.ModeledPageLayout;
import org.eclipse.ui.internal.e4.compatibility.SelectionService;
import org.eclipse.ui.internal.menus.MenuHelper;
import org.eclipse.ui.internal.misc.ExternalEditor;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.internal.misc.UIListenerLogging;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.EditorDescriptor;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
import org.eclipse.ui.internal.registry.PerspectiveRegistry;
import org.eclipse.ui.internal.registry.ViewDescriptor;
import org.eclipse.ui.internal.tweaklets.TabBehaviour;
import org.eclipse.ui.internal.tweaklets.Tweaklets;
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.part.IShowInSource;
import org.eclipse.ui.part.ShowInContext;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.ui.views.IStickyViewDescriptor;
import org.eclipse.ui.views.IViewDescriptor;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
/**
* A collection of views and editors in a workbench.
*/
public class WorkbenchPage implements IWorkbenchPage {
private static final String ATT_AGGREGATE_WORKING_SET_ID = "aggregateWorkingSetId"; //$NON-NLS-1$
private static final int WINDOW_SCOPE = EModelService.OUTSIDE_PERSPECTIVE | EModelService.IN_ANY_PERSPECTIVE
| EModelService.IN_SHARED_AREA;
class E4PartListener implements org.eclipse.e4.ui.workbench.modeling.IPartListener {
@Override
public void partActivated(MPart part) {
// update the workbench window's current selection with the active
// part's selection
IWorkbenchPart workbenchPart = getWorkbenchPart(part);
selectionService.updateSelection(workbenchPart);
updateActivations(part);
firePartActivated(part);
selectionService.notifyListeners(workbenchPart);
}
@Override
public void partBroughtToTop(MPart part) {
updateBroughtToTop(part);
firePartBroughtToTop(part);
}
@Override
public void partDeactivated(MPart part) {
firePartDeactivated(part);
Object client = part.getObject();
if (client instanceof CompatibilityPart) {
IWorkbenchPart workbenchPart = getWrappedPart((CompatibilityPart) client);
if (workbenchPart == null) {
return;
}
IWorkbenchPartSite site = workbenchPart.getSite();
// if it's an editor, we only want to disable the actions
((PartSite) site).deactivateActionBars(site instanceof ViewSite);
}
WorkbenchWindow wwindow = (WorkbenchWindow) getWorkbenchWindow();
if (!wwindow.isClosing()) {
wwindow.getStatusLineManager().update(false);
}
}
@Override
public void partHidden(MPart part) {
firePartHidden(part);
}
@Override
public void partVisible(MPart part) {
firePartVisible(part);
}
}
ArrayList<MPart> activationList = new ArrayList<>();
/**
* Cached perspective stack for this workbench page.
*/
private MPerspectiveStack _perspectiveStack;
/** Ids of parts used as Show In targets, maintained in MRU order */
private List<String> mruShowInPartIds = new ArrayList<>();
/**
* Deactivate the last editor's action bars if another type of editor has // *
* been activated.
*
* @param part the part that is being activated
*/
private void deactivateLastEditor(MPart part) {
Object client = part.getObject();
// we only care if the currently activated part is an editor
if (client instanceof CompatibilityEditor) {
IWorkbenchPart activePart = getWrappedPart((CompatibilityEditor) client);
if (activePart == null) {
return;
}
String activeId = activePart.getSite().getId();
// find another editor that was last activated
for (MPart previouslyActive : activationList) {
if (previouslyActive != part) {
Object object = previouslyActive.getObject();
if (object instanceof CompatibilityEditor) {
IWorkbenchPart workbenchPart = getWrappedPart((CompatibilityEditor) object);
if (workbenchPart == null) {
continue;
}
EditorSite site = (EditorSite) workbenchPart.getSite();
String lastId = site.getId();
// if not the same, hide the other editor's action bars
if (lastId != null && !lastId.equals(activeId)) {
site.deactivateActionBars(true);
}
break;
}
}
}
}
}
private void updateActivations(MPart part) {
if (activationList.size() > 1) {
deactivateLastEditor(part);
}
activationList.remove(part);
activationList.add(0, part);
updateActivePartSources(part);
updateActiveEditorSources(part);
Object client = part.getObject();
if (client instanceof CompatibilityPart) {
IWorkbenchPart workbenchPart = getWrappedPart((CompatibilityPart) client);
if (workbenchPart != null) {
PartSite site = (PartSite) workbenchPart.getSite();
site.activateActionBars(true);
IActionBars actionBars = site.getActionBars();
if (actionBars instanceof EditorActionBars) {
((EditorActionBars) actionBars).partChanged(workbenchPart);
}
}
}
((WorkbenchWindow) getWorkbenchWindow()).getStatusLineManager().update(false);
IWorkbenchPart workbenchPart = getWorkbenchPart(part);
actionSwitcher.updateActivePart(workbenchPart);
}
private void updateActivePartSources(MPart part) {
IWorkbenchPart workbenchPart = getWorkbenchPart(part);
IContextService cs = legacyWindow.getService(IContextService.class);
try {
cs.deferUpdates(true);
if (workbenchPart == null) {
window.getContext().set(ISources.ACTIVE_PART_NAME, null);
window.getContext().set(ISources.ACTIVE_PART_ID_NAME, null);
window.getContext().set(ISources.ACTIVE_SITE_NAME, null);
} else {
window.getContext().set(ISources.ACTIVE_PART_NAME, workbenchPart);
window.getContext().set(ISources.ACTIVE_PART_ID_NAME, workbenchPart.getSite().getId());
window.getContext().set(ISources.ACTIVE_SITE_NAME, workbenchPart.getSite());
}
} finally {
cs.deferUpdates(false);
}
}
private void updateActionSets(Perspective oldPersp, Perspective newPersp) {
// Update action sets
IContextService service = legacyWindow.getService(IContextService.class);
try {
service.deferUpdates(true);
if (newPersp != null) {
for (IActionSetDescriptor descriptor : newPersp.getAlwaysOnActionSets()) {
actionSets.showAction(descriptor);
}
for (IActionSetDescriptor descriptor : newPersp.getAlwaysOffActionSets()) {
actionSets.maskAction(descriptor);
}
}
if (oldPersp != null) {
for (IActionSetDescriptor descriptor : oldPersp.getAlwaysOnActionSets()) {
actionSets.hideAction(descriptor);
}
for (IActionSetDescriptor descriptor : oldPersp.getAlwaysOffActionSets()) {
actionSets.unmaskAction(descriptor);
}
}
} finally {
service.deferUpdates(false);
}
}
private IWorkbenchPart getWorkbenchPart(MPart part) {
if (part != null) {
Object clientObject = part.getObject();
if (clientObject instanceof CompatibilityPart) {
return ((CompatibilityPart) clientObject).getPart();
} else if (clientObject != null) {
if (part.getTransientData().get(E4PartWrapper.E4_WRAPPER_KEY) instanceof E4PartWrapper) {
return (IWorkbenchPart) part.getTransientData().get(E4PartWrapper.E4_WRAPPER_KEY);
}
ViewReference viewReference = getViewReference(part);
if (viewReference != null) {
E4PartWrapper legacyPart = E4PartWrapper.getE4PartWrapper(part);
try {
viewReference.initialize(legacyPart);
} catch (PartInitException e) {
WorkbenchPlugin.log(e);
}
part.getTransientData().put(E4PartWrapper.E4_WRAPPER_KEY, legacyPart);
return legacyPart;
}
}
}
return null;
}
private void updateActiveEditorSources(MPart part) {
IEditorPart editor = getEditor(part);
window.getContext().set(ISources.ACTIVE_EDITOR_ID_NAME, editor == null ? null : editor.getSite().getId());
window.getContext().set(ISources.ACTIVE_EDITOR_NAME, editor);
window.getContext().set(ISources.ACTIVE_EDITOR_INPUT_NAME, editor == null ? null : editor.getEditorInput());
if (editor != null) {
navigationHistory.markEditor(editor);
}
actionSwitcher.updateTopEditor(editor);
}
public void updateShowInSources(MPart part) {
IWorkbenchPart workbenchPart = getWorkbenchPart(part);
ShowInContext context = getContext(workbenchPart);
if (context != null) {
window.getContext().set(ISources.SHOW_IN_INPUT, context.getInput());
window.getContext().set(ISources.SHOW_IN_SELECTION, context.getSelection());
}
}
private IShowInSource getShowInSource(IWorkbenchPart sourcePart) {
return Adapters.adapt(sourcePart, IShowInSource.class);
}
private ShowInContext getContext(IWorkbenchPart sourcePart) {
IShowInSource source = getShowInSource(sourcePart);
if (source != null) {
ShowInContext context = source.getShowInContext();
if (context != null) {
return context;
}
} else if (sourcePart instanceof IEditorPart) {
Object input = ((IEditorPart) sourcePart).getEditorInput();
ISelectionProvider sp = sourcePart.getSite().getSelectionProvider();
ISelection sel = sp == null ? null : sp.getSelection();
return new ShowInContext(input, sel);
}
return null;
}
private IEditorPart getEditor(MPart part) {
if (part != null) {
Object clientObject = part.getObject();
if (clientObject instanceof CompatibilityEditor) {
return ((CompatibilityEditor) clientObject).getEditor();
}
}
return getActiveEditor();
}
private void updateBroughtToTop(MPart part) {
updateActiveEditorSources(part);
IWorkbenchPart workbenchPart = getWorkbenchPart(part);
if (workbenchPart instanceof IEditorPart) {
navigationHistory.markEditor((IEditorPart) workbenchPart);
}
MElementContainer<?> parent = part.getParent();
if (parent == null) {
MPlaceholder placeholder = part.getCurSharedRef();
if (placeholder == null) {
return;
}
parent = placeholder.getParent();
}
if (parent instanceof MPartStack) {
int newIndex = lastIndexOfContainer(parent);
// New index can be -1 if there is no last index
if (newIndex >= 0 && part == activationList.get(newIndex)) {
return;
}
activationList.remove(part);
if (newIndex >= 0 && newIndex < activationList.size() - 1) {
activationList.add(newIndex, part);
} else {
activationList.add(part);
}
}
}
private int lastIndexOfContainer(MElementContainer<?> parent) {
for (int i = 0; i < activationList.size(); i++) {
MPart mPart = activationList.get(i);
MElementContainer<MUIElement> container = mPart.getParent();
if (container == parent) {
return i;
} else if (container == null) {
MPlaceholder placeholder = mPart.getCurSharedRef();
if (placeholder != null && placeholder.getParent() == parent) {
return i;
}
}
}
return -1;
}
private List<ViewReference> viewReferences = new ArrayList<>();
private List<EditorReference> editorReferences = new ArrayList<>();
private List<IPerspectiveDescriptor> sortedPerspectives = new ArrayList<>();
private ListenerList<IPartListener> partListenerList = new ListenerList<>();
private ListenerList<IPartListener2> partListener2List = new ListenerList<>();
/**
* A listener that forwards page change events to our part listeners.
*/
private IPageChangedListener pageChangedListener = event -> {
for (final IPartListener2 listener : partListener2List) {
if (listener instanceof IPageChangedListener) {
SafeRunner.run(new SafeRunnable() {
@Override
public void run() throws Exception {
((IPageChangedListener) listener).pageChanged(event);
}
});
}
}
};
private E4PartListener e4PartListener = new E4PartListener();
protected WorkbenchWindow legacyWindow;
private IAdaptable input;
private IWorkingSet workingSet;
private AggregateWorkingSet aggregateWorkingSet;
private Composite composite;
private ListenerList<IPropertyChangeListener> propertyChangeListeners = new ListenerList<>();
private IActionBars actionBars;
private ActionSetManager actionSets;
private NavigationHistory navigationHistory = new NavigationHistory(this);
/**
* If we're in the process of activating a part, this points to the new part.
* Otherwise, this is null.
*/
private IWorkbenchPartReference partBeingActivated = null;
private IWorkingSet[] workingSets = new IWorkingSet[0];
private IPropertyChangeListener workingSetPropertyChangeListener = event -> {
String property = event.getProperty();
if (IWorkingSetManager.CHANGE_WORKING_SET_REMOVE.equals(property)) {
if (event.getOldValue().equals(workingSet)) {
setWorkingSet(null);
}
// room for optimization here
List<IWorkingSet> newList = new ArrayList<>(Arrays.asList(workingSets));
if (newList.remove(event.getOldValue())) {
setWorkingSets(newList.toArray(new IWorkingSet[newList.size()]));
}
}
};
private ActionSwitcher actionSwitcher = new ActionSwitcher();
private IExtensionTracker tracker;
// Deferral count... delays disposing parts and sending certain events if
// nonzero
private int deferCount = 0;
private String aggregateWorkingSetId;
private LargeFileLimitsPreferenceHandler largeFileLimitsPreferenceHandler;
/**
* Manages editor contributions and action set part associations.
*/
private class ActionSwitcher {
private IWorkbenchPart activePart;
private IEditorPart topEditor;
private List<IActionSetDescriptor> oldActionSets = new ArrayList<>();
/**
* Updates the contributions given the new part as the active part.
*
* @param newPart the new active part, may be <code>null</code>
*/
public void updateActivePart(IWorkbenchPart newPart) {
if (activePart == newPart) {
return;
}
boolean isNewPartAnEditor = newPart instanceof IEditorPart;
if (isNewPartAnEditor) {
String oldId = null;
if (topEditor != null) {
oldId = topEditor.getSite().getId();
}
String newId = newPart.getSite().getId();
// if the active part is an editor and the new editor
// is the same kind of editor, then we don't have to do
// anything
if (activePart == topEditor && newId.equals(oldId)) {
activePart = newPart;
topEditor = (IEditorPart) newPart;
return;
}
// remove the contributions of the old editor
// if it is a different kind of editor
if (oldId != null && !oldId.equals(newId)) {
deactivateContributions(topEditor, true);
}
// if a view was the active part, disable its contributions
if (activePart != null && activePart != topEditor) {
deactivateContributions(activePart, true);
}
// show (and enable) the contributions of the new editor
// if it is a different kind of editor or if the
// old active part was a view
if (!newId.equals(oldId) || activePart != topEditor) {
activateContributions(newPart, true);
}
} else if (newPart == null) {
if (activePart != null) {
// remove all contributions
deactivateContributions(activePart, true);
}
} else {
// new part is a view
// if old active part is a view, remove all contributions,
// but if old part is an editor only disable
if (activePart != null) {
deactivateContributions(activePart, activePart instanceof IViewPart);
}
activateContributions(newPart, true);
}
List<IActionSetDescriptor> newActionSets = null;
if (isNewPartAnEditor || (activePart == topEditor && newPart == null)) {
newActionSets = calculateActionSets(newPart, null);
} else {
newActionSets = calculateActionSets(newPart, topEditor);
}
if (!updateActionSets(newActionSets)) {
updateActionBars();
}
if (isNewPartAnEditor) {
topEditor = (IEditorPart) newPart;
} else if (activePart == topEditor && newPart == null) {
// since we removed all the contributions, we clear the top
// editor
topEditor = null;
}
activePart = newPart;
}
/**
* Updates the contributions given the new part as the topEditor.
*
* @param newEditor the new top editor, may be <code>null</code>
*/
public void updateTopEditor(IEditorPart newEditor) {
if (topEditor == newEditor) {
return;
}
if (activePart == topEditor) {
updateActivePart(newEditor);
return;
}
String oldId = null;
if (topEditor != null) {
oldId = topEditor.getSite().getId();
}
String newId = null;
if (newEditor != null) {
newId = newEditor.getSite().getId();
}
if (oldId == null ? newId == null : oldId.equals(newId)) {
// we don't have to change anything
topEditor = newEditor;
return;
}
// Remove the contributions of the old editor
if (topEditor != null) {
deactivateContributions(topEditor, true);
}
// Show (disabled) the contributions of the new editor
if (newEditor != null) {
activateContributions(newEditor, false);
}
List<IActionSetDescriptor> newActionSets = calculateActionSets(activePart, newEditor);
if (!updateActionSets(newActionSets)) {
updateActionBars();
}
topEditor = newEditor;
}
/**
* Activates the contributions of the given part. If <code>enable</code> is
* <code>true</code> the contributions are visible and enabled, otherwise they
* are disabled.
*
* @param part the part whose contributions are to be activated
* @param enable <code>true</code> the contributions are to be enabled, not just
* visible.
*/
private void activateContributions(IWorkbenchPart part, boolean enable) {
PartSite site = (PartSite) part.getSite();
site.activateActionBars(enable);
}
/**
* Deactivates the contributions of the given part. If <code>remove</code> is
* <code>true</code> the contributions are removed, otherwise they are disabled.
*
* @param part the part whose contributions are to be deactivated
* @param remove <code>true</code> the contributions are to be removed, not just
* disabled.
*/
private void deactivateContributions(IWorkbenchPart part, boolean remove) {
PartSite site = (PartSite) part.getSite();
if (site != null) {
site.deactivateActionBars(remove);
}
}
/**
* Calculates the action sets to show for the given part and editor
*
* @param part the active part, may be <code>null</code>
* @param editor the current editor, may be <code>null</code>, may be the active
* part
* @return the new action sets
*/
private List<IActionSetDescriptor> calculateActionSets(IWorkbenchPart part, IEditorPart editor) {
List<IActionSetDescriptor> newActionSets = new ArrayList<>();
if (part != null) {
IActionSetDescriptor[] partActionSets = WorkbenchPlugin.getDefault().getActionSetRegistry()
.getActionSetsFor(part.getSite().getId());
newActionSets.addAll(Arrays.asList(partActionSets));
}
if (editor != null && editor != part) {
IActionSetDescriptor[] editorActionSets = WorkbenchPlugin.getDefault().getActionSetRegistry()
.getActionSetsFor(editor.getSite().getId());
newActionSets.addAll(Arrays.asList(editorActionSets));
}
return newActionSets;
}
/**
* Updates the actions we are showing for the active part and current editor.
*
* @param newActionSets the action sets to show
* @return <code>true</code> if the action sets changed
*/
private boolean updateActionSets(List<IActionSetDescriptor> newActionSets) {
if (oldActionSets.equals(newActionSets)) {
return false;
}
IContextService service = legacyWindow.getService(IContextService.class);
try {
service.deferUpdates(true);
// show the new
for (IActionSetDescriptor newActionSet : newActionSets) {
actionSets.showAction(newActionSet);
}
// hide the old
for (IActionSetDescriptor oldActionSet : oldActionSets) {
actionSets.hideAction(oldActionSet);
}
oldActionSets = newActionSets;
} finally {
service.deferUpdates(false);
}
Perspective persp = getActivePerspective();
if (persp == null) {
return false;
}
legacyWindow.updateActionSets(); // this calls updateActionBars
legacyWindow.firePerspectiveChanged(WorkbenchPage.this, getPerspective(), CHANGE_ACTION_SET_SHOW);
return true;
}
}
private EPartService partService;
private SelectionService selectionService;
private MApplication application;
private MWindow window;
private EModelService modelService;
private IEventBroker broker;
/**
* An event handler that listens for an MArea's widget being set so that we can
* install DND support into its control.
*/
private EventHandler widgetHandler = event -> {
Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
Object newValue = event.getProperty(UIEvents.EventTags.NEW_VALUE);
if (element instanceof MArea) {
// If it's an MArea in this window install the DND handling
if (modelService.findElements(window, null, MArea.class).contains(element)) {
if (newValue instanceof Control) {
installAreaDropSupport((Control) newValue);
}
}
} else if (element instanceof MPart && newValue == null) {
// If it's a 'e4' part then remove the reference for it
MPart changedPart = (MPart) element;
Object impl = changedPart.getObject();
if (impl != null && !(impl instanceof CompatibilityPart)) {
EditorReference eRef = getEditorReference(changedPart);
if (eRef != null)
editorReferences.remove(eRef);
ViewReference vRef = getViewReference(changedPart);
if (vRef != null)
viewReferences.remove(vRef);
}
}
};
@Inject
@Optional
private void handleMinimizedStacks(@UIEventTopic(UIEvents.ApplicationElement.TOPIC_TAGS) Event event) {
Object changedObj = event.getProperty(EventTags.ELEMENT);
if (!(changedObj instanceof MToolControl))
return;
final MToolControl minimizedStack = (MToolControl) changedObj;
if (!(minimizedStack.getObject() instanceof TrimStack))
return;
TrimStack ts = (TrimStack) minimizedStack.getObject();
if (!(ts.getMinimizedElement() instanceof MPartStack))
return;
MPartStack stack = (MPartStack) ts.getMinimizedElement();
MUIElement stackSel = stack.getSelectedElement();
MPart thePart = null;
if (stackSel instanceof MPart) {
thePart = (MPart) stackSel;
} else if (stackSel instanceof MPlaceholder) {
MPlaceholder ph = (MPlaceholder) stackSel;
if (ph.getRef() instanceof MPart) {
thePart = (MPart) ph.getRef();
}
}
if (thePart == null)
return;
if (UIEvents.isADD(event)) {
if (UIEvents.contains(event, UIEvents.EventTags.NEW_VALUE,
org.eclipse.e4.ui.workbench.addons.minmax.TrimStack.MINIMIZED_AND_SHOWING)) {
firePartVisible(thePart);
}
} else if (UIEvents.isREMOVE(event)) {
if (UIEvents.contains(event, UIEvents.EventTags.OLD_VALUE,
org.eclipse.e4.ui.workbench.addons.minmax.TrimStack.MINIMIZED_AND_SHOWING)) {
firePartHidden(thePart);
}
}
}
/**
* Boolean field to determine whether DND support has been added to the shared
* area yet.
*
* @see #installAreaDropSupport(Control)
*/
private boolean dndSupportInstalled = false;
/**
* Constructs a page. <code>restoreState(IMemento)</code> should be called to
* restore this page from data stored in a persistance file.
*
* @param w the parent window
* @param input the page input
*/
public WorkbenchPage(WorkbenchWindow w, IAdaptable input) {
super();
init(w, input);
}
/**
* Allow access to the UI model that this page is managing
*
* @return the MWindow element for this page
*/
public MWindow getWindowModel() {
return window;
}
/**
* Activates a part. The part will be brought to the front and given focus.
*
* @param part the part to activate
*/
@Override
public void activate(IWorkbenchPart part) {
if (part == null || !certifyPart(part) || legacyWindow.isClosing()) {
return;
}
MPart mpart = findPart(part);
if (mpart != null) {
partService.activate(mpart);
actionSwitcher.updateActivePart(part);
}
}
/**
* Adds an IPartListener to the part service.
*/
@Override
public void addPartListener(IPartListener l) {
partListenerList.add(l);
}
/**
* Adds an IPartListener to the part service.
*/
@Override
public void addPartListener(IPartListener2 l) {
partListener2List.add(l);
}
/**
* Implements IWorkbenchPage
*
* @see org.eclipse.ui.IWorkbenchPage#addPropertyChangeListener(IPropertyChangeListener)
* @since 2.0
* @deprecated individual views should store a working set if needed and
* register a property change listener directly with the working set
* manager to receive notification when the view working set is
* removed.
*/
@Deprecated
@Override
public void addPropertyChangeListener(IPropertyChangeListener listener) {
propertyChangeListeners.add(listener);
}
@Override
public void addSelectionListener(ISelectionListener listener) {
selectionService.addSelectionListener(listener);
}
@Override
public void addSelectionListener(String partId, ISelectionListener listener) {
selectionService.addSelectionListener(partId, listener);
}