-
Notifications
You must be signed in to change notification settings - Fork 187
/
LaunchBarManager.java
1062 lines (989 loc) · 33.1 KB
/
LaunchBarManager.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) 2014, 2018 QNX Software Systems 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:
* Doug Schaefer
*******************************************************************************/
package org.eclipse.launchbar.core.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchMode;
import org.eclipse.launchbar.core.DefaultLaunchDescriptor;
import org.eclipse.launchbar.core.ILaunchBarListener;
import org.eclipse.launchbar.core.ILaunchBarManager;
import org.eclipse.launchbar.core.ILaunchConfigurationProvider;
import org.eclipse.launchbar.core.ILaunchDescriptor;
import org.eclipse.launchbar.core.ILaunchDescriptorType;
import org.eclipse.launchbar.core.ILaunchObjectProvider;
import org.eclipse.launchbar.core.target.ILaunchTarget;
import org.eclipse.launchbar.core.target.ILaunchTargetListener;
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
import org.eclipse.launchbar.core.target.launch.ITargetedLaunch;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
/**
* The brains of the launch bar.
*/
public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListener {
private final List<ILaunchBarListener> listeners = new LinkedList<>();
// The launch object providers
private final List<ILaunchObjectProvider> objectProviders = new ArrayList<>();
// The descriptor types
private final Map<String, LaunchDescriptorTypeInfo> descriptorTypes = new HashMap<>();
// Descriptor types ordered from highest priority to lowest
private List<LaunchDescriptorTypeInfo> orderedDescriptorTypes;
// the extended info for loaded descriptor types
private final Map<ILaunchDescriptorType, LaunchDescriptorTypeInfo> descriptorTypeInfo = new HashMap<>();
private final Map<String, List<LaunchConfigProviderInfo>> configProviders = new HashMap<>();
// Descriptors in MRU order, key is desc type id and desc name.
private final Map<Pair<String, String>, ILaunchDescriptor> descriptors = new LinkedHashMap<>();
// Map of launch objects to launch descriptors
private final Map<Object, ILaunchDescriptor> objectDescriptorMap = new HashMap<>();
private ILaunchTargetManager launchTargetManager;
private ILaunchDescriptor activeLaunchDesc;
private ILaunchMode activeLaunchMode;
private ILaunchTarget activeLaunchTarget;
// private static final String PREF_ACTIVE_CONFIG_DESC = "activeConfigDesc";
private static final String PREF_ACTIVE_LAUNCH_MODE = "activeLaunchMode"; //$NON-NLS-1$
private static final String PREF_ACTIVE_LAUNCH_TARGET = "activeLaunchTarget"; //$NON-NLS-1$
private static final String PREF_CONFIG_DESC_ORDER = "configDescList"; //$NON-NLS-1$
private static final String PREF_TRACK_LAUNCHES = "trackLaunches"; //$NON-NLS-1$
boolean initialized = false;
public LaunchBarManager() {
this(true);
}
// called from unit tests to ensure everything is inited
LaunchBarManager(boolean doInit) {
launchTargetManager = getLaunchTargetManager();
launchTargetManager.addListener(this);
if (doInit) {
new Job(Messages.LaunchBarManager_0) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
init();
return Status.OK_STATUS;
} catch (CoreException e) {
return e.getStatus();
}
}
}.schedule();
}
}
// To allow override by tests
IExtensionPoint getExtensionPoint() throws CoreException {
return Platform.getExtensionRegistry().getExtensionPoint(Activator.PLUGIN_ID, "launchBarContributions"); //$NON-NLS-1$
}
// To allow override by tests
ILaunchManager getLaunchManager() {
return DebugPlugin.getDefault().getLaunchManager();
}
ILaunchTargetManager getLaunchTargetManager() {
return Activator.getService(ILaunchTargetManager.class);
}
// When testing, call this after setting up the mocks.
void init() throws CoreException {
try {
// Fetch the desc order before the init messes it up
IEclipsePreferences store = getPreferenceStore();
String configDescIds = store.get(PREF_CONFIG_DESC_ORDER, ""); //$NON-NLS-1$
// Load up the types
loadExtensions();
// Hook up the existing launch configurations and listen
ILaunchManager launchManager = getLaunchManager();
for (ILaunchConfiguration configuration : launchManager.getLaunchConfigurations()) {
launchConfigurationAdded(configuration);
}
launchManager.addLaunchConfigurationListener(this);
// Reorder the descriptors based on the preference
if (!configDescIds.isEmpty()) {
String[] split = configDescIds.split(","); //$NON-NLS-1$
ILaunchDescriptor last = null;
for (String id : split) {
Pair<String, String> key = toId(id);
synchronized (descriptors) {
ILaunchDescriptor desc = descriptors.get(key);
if (desc != null) {
descriptors.remove(key);
descriptors.put(key, desc);
last = desc;
}
}
}
// Set the active desc, with MRU, it should be the last one
if (last != null) {
setActiveLaunchDescriptor(last);
}
}
launchManager.addLaunchListener(new ILaunchListener() {
@Override
public void launchRemoved(ILaunch launch) {
// ignore
}
@Override
public void launchAdded(ILaunch launch) {
if (!getPreferenceStore().getBoolean(PREF_TRACK_LAUNCHES, true))
return;
ILaunchConfiguration lc = launch.getLaunchConfiguration();
String mode = launch.getLaunchMode();
ILaunchTarget target = null;
if (launch instanceof ITargetedLaunch) {
target = ((ITargetedLaunch) launch).getLaunchTarget();
}
try {
setActive(lc, mode, target);
} catch (CoreException e) {
Activator.log(e);
}
}
@Override
public void launchChanged(ILaunch launch) {
ILaunchConfiguration lc = launch.getLaunchConfiguration();
ILaunchTarget target = null;
if (launch instanceof ITargetedLaunch) {
target = ((ITargetedLaunch) launch).getLaunchTarget();
}
if (target == null)
return;
if (launchDescriptorMatches(activeLaunchDesc, lc, target)) {
// active launch delegate may have changed target
try {
setActiveLaunchTarget(target);
} catch (CoreException e) {
Activator.log(e);
}
}
}
});
} finally {
initialized = true;
}
fireActiveLaunchDescriptorChanged();
fireActiveLaunchTargetChanged();
fireActiveLaunchModeChanged();
fireLaunchTargetsChanged();
}
private void loadExtensions() throws CoreException {
IExtensionPoint point = getExtensionPoint();
IExtension[] extensions = point.getExtensions();
// Load up the types
for (IExtension extension : extensions) {
for (IConfigurationElement element : extension.getConfigurationElements()) {
try {
String elementName = element.getName();
if (elementName.equals("descriptorType")) { //$NON-NLS-1$
LaunchDescriptorTypeInfo typeInfo = new LaunchDescriptorTypeInfo(element);
descriptorTypes.put(typeInfo.getId(), typeInfo);
if (configProviders.get(typeInfo.getId()) == null) {
// Make sure we initialize the list
configProviders.put(typeInfo.getId(), new ArrayList<LaunchConfigProviderInfo>());
}
} else if (elementName.equals("configProvider")) { //$NON-NLS-1$
LaunchConfigProviderInfo info = new LaunchConfigProviderInfo(element);
List<LaunchConfigProviderInfo> providers = configProviders.get(info.getDescriptorTypeId());
if (providers == null) {
providers = new ArrayList<>();
configProviders.put(info.getDescriptorTypeId(), providers);
}
providers.add(info);
}
} catch (Exception e) {
Activator.log(e);
}
}
}
// Sort things
orderedDescriptorTypes = new ArrayList<>(descriptorTypes.values());
Collections.sort(orderedDescriptorTypes, (o1, o2) -> {
int p1 = o1.getPriority();
int p2 = o2.getPriority();
if (p1 < p2) {
return 1;
} else if (p1 > p2) {
return -1;
} else {
return 0;
}
});
for (List<LaunchConfigProviderInfo> providers : configProviders.values()) {
Collections.sort(providers, (o1, o2) -> {
int p1 = o1.getPriority();
int p2 = o2.getPriority();
if (p1 < p2) {
return 1;
} else if (p1 > p2) {
return -1;
} else {
return 0;
}
});
}
// Now that all the types are loaded, the object providers which now
// populate the descriptors
for (IExtension extension : extensions) {
for (IConfigurationElement element : extension.getConfigurationElements()) {
try {
String elementName = element.getName();
if (elementName.equals("objectProvider")) { //$NON-NLS-1$
ILaunchObjectProvider objectProvider = (ILaunchObjectProvider) element
.createExecutableExtension("class"); //$NON-NLS-1$
objectProviders.add(objectProvider);
objectProvider.init(this);
}
} catch (Exception e) {
// exceptions during extension loading, log and move on
Activator.log(e);
}
}
}
}
private String toString(Pair<String, String> key) {
return key.getFirst() + ":" + key.getSecond(); //$NON-NLS-1$
}
private Pair<String, String> toId(String key) {
int i = key.indexOf(':');
if (i < 0) {
return null;
}
return new Pair<>(key.substring(0, i), key.substring(i + 1));
}
@Override
public String getDescriptorTypeId(ILaunchDescriptorType type) {
return descriptorTypeInfo.get(type).getId();
}
private Pair<String, String> getDescriptorId(ILaunchDescriptor descriptor) {
return new Pair<>(getDescriptorTypeId(descriptor.getType()), descriptor.getName());
}
private Pair<String, String> getTargetId(ILaunchTarget target) {
return new Pair<>(target.getTypeId(), target.getId());
}
private void addDescriptor(Object launchObject, ILaunchDescriptor descriptor) throws CoreException {
Pair<String, String> descriptorId = getDescriptorId(descriptor);
synchronized (descriptors) {
descriptors.put(descriptorId, descriptor);
}
objectDescriptorMap.put(launchObject, descriptor);
setActiveLaunchDescriptor(descriptor);
}
@Override
public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor, ILaunchTarget target)
throws CoreException {
if (descriptor == null)
return null;
if (descriptor instanceof DefaultLaunchDescriptor) {
// With the default descriptor, we already have the config, just return the type
// Doesn't matter what the target is, that's dealt with at launch time
ILaunchConfiguration config = descriptor.getAdapter(ILaunchConfiguration.class);
return config.getType();
}
for (LaunchConfigProviderInfo providerInfo : configProviders.get(getDescriptorTypeId(descriptor.getType()))) {
if (providerInfo.enabled(descriptor) && providerInfo.enabled(target)) {
ILaunchConfigurationProvider provider = providerInfo.getProvider();
if (provider != null && provider.supports(descriptor, target)) {
ILaunchConfigurationType type = provider.getLaunchConfigurationType(descriptor, target);
if (type != null) {
return type;
}
}
}
}
// not found
return null;
}
@Override
public ILaunchDescriptor launchObjectAdded(Object launchObject) {
Activator.trace("launch object added " + launchObject); //$NON-NLS-1$
ILaunchDescriptor desc = objectDescriptorMap.get(launchObject);
if (desc != null) {
return desc;
}
for (LaunchDescriptorTypeInfo descriptorInfo : orderedDescriptorTypes) {
try {
if (descriptorInfo.enabled(launchObject)) {
ILaunchDescriptorType type = descriptorInfo.getType();
// For newly loaded types, this is the first time we see
// them
// Add it to the info map.
descriptorTypeInfo.put(type, descriptorInfo);
desc = type.getDescriptor(launchObject);
if (desc != null) {
addDescriptor(launchObject, desc);
return desc;
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
return null;
}
@Override
public void launchObjectRemoved(Object launchObject) throws CoreException {
Activator.trace("launch object removed " + launchObject); //$NON-NLS-1$
ILaunchDescriptor descriptor = objectDescriptorMap.remove(launchObject);
if (descriptor != null) {
Pair<String, String> descriptorId = getDescriptorId(descriptor);
synchronized (descriptors) {
descriptors.remove(descriptorId);
}
if (descriptor.equals(activeLaunchDesc)) {
setActiveLaunchDescriptor(getLastUsedDescriptor());
}
for (LaunchConfigProviderInfo providerInfo : configProviders
.get(getDescriptorTypeId(descriptor.getType()))) {
if (providerInfo.enabled(descriptor)) {
providerInfo.getProvider().launchDescriptorRemoved(descriptor);
}
}
}
}
@Override
public void launchObjectChanged(Object launchObject) throws CoreException {
// TODO deal with object renames here, somehow
ILaunchDescriptor origDesc = objectDescriptorMap.get(launchObject);
if (origDesc == null) {
// See if anyone wants it now
launchObjectAdded(launchObject);
return;
}
// check if descriptor still wants it
ILaunchDescriptorType origDescType = origDesc.getType();
try {
ILaunchDescriptor newDesc = origDescType.getDescriptor(launchObject);
if (newDesc == null) {
// nope, give it back to the pool
objectDescriptorMap.remove(launchObject);
launchObjectAdded(launchObject);
} else if (!newDesc.equals(origDesc)) {
// record the new descriptor
objectDescriptorMap.put(launchObject, newDesc);
}
} catch (Throwable e) {
Activator.log(e);
}
}
private ILaunchDescriptor getLastUsedDescriptor() {
if (descriptors.size() == 0)
return null;
ILaunchDescriptor[] descs;
synchronized (descriptors) {
descs = descriptors.values().toArray(new ILaunchDescriptor[descriptors.size()]);
}
return descs[descs.length - 1];
}
@Override
public ILaunchDescriptor[] getLaunchDescriptors() {
// return descriptor in usage order (most used first). UI can sort them
// later as it wishes
ArrayList<ILaunchDescriptor> values = new ArrayList<>();
synchronized (descriptors) {
values.addAll(descriptors.values());
}
Collections.reverse(values);
return values.toArray(new ILaunchDescriptor[values.size()]);
}
@Override
public ILaunchDescriptor getActiveLaunchDescriptor() {
return activeLaunchDesc;
}
private void setActive(ILaunchConfiguration config, String mode, ILaunchTarget target) throws CoreException {
ILaunchDescriptor descriptor = getLaunchDescriptor(config, target);
if (descriptor == null)
return; // not found
// we do not call setActiveLaunchTarget because it will cause
// mode/target switch and cause flickering
boolean changeDesc = activeLaunchDesc != descriptor;
boolean changeTarget = target != null && activeLaunchTarget != target;
if (changeDesc) {
doSetActiveLaunchDescriptor(descriptor);
// store in persistent storage
storeActiveDescriptor(activeLaunchDesc);
}
if (changeTarget) {
activeLaunchTarget = target;
storeLaunchTarget(activeLaunchDesc, target);
}
ILaunchMode[] supportedModes = getLaunchModes();
for (ILaunchMode launchMode : supportedModes) {
if (launchMode.getIdentifier().equals(mode)) {
setActiveLaunchMode(launchMode);
break;
}
}
// send delayed notification about descriptor change
if (changeDesc) {
fireActiveLaunchDescriptorChanged();
}
if (changeTarget) {
fireActiveLaunchTargetChanged(); // notify target listeners
}
}
@Override
public void setActiveLaunchDescriptor(ILaunchDescriptor descriptor) throws CoreException {
Activator.trace("set active descriptor " + descriptor); //$NON-NLS-1$
if (activeLaunchDesc == descriptor) {
// Sync since targets could be changed since last time (and modes
// theoretically too)
syncActiveTarget();
syncActiveMode();
Activator.trace("resync for " + descriptor); //$NON-NLS-1$
return;
}
if (descriptor != null) {
boolean isContained;
synchronized (descriptors) {
isContained = descriptors.containsValue(descriptor);
}
if (!isContained) {
throw new IllegalStateException(Messages.LaunchBarManager_1);
}
}
if (descriptor == null) {
// do not set to null unless no descriptors
descriptor = getLastUsedDescriptor();
}
doSetActiveLaunchDescriptor(descriptor);
// store in persistent storage
storeActiveDescriptor(activeLaunchDesc);
// Send notifications
fireActiveLaunchDescriptorChanged();
// Set active target
syncActiveTarget();
// Set active mode
syncActiveMode();
}
private void doSetActiveLaunchDescriptor(ILaunchDescriptor descriptor) {
activeLaunchDesc = descriptor;
if (descriptor != null) {
// keeps most used descriptor last
Pair<String, String> id = getDescriptorId(descriptor);
synchronized (descriptors) {
descriptors.remove(id);
descriptors.put(id, descriptor);
}
}
}
private void storeActiveDescriptor(ILaunchDescriptor descriptor) {
Activator.trace("new active config is stored " + descriptor); //$NON-NLS-1$
// Store the desc order, active one is the last one
StringBuffer buff = new StringBuffer();
// TODO: this can be very long string
ArrayList<Pair<String, String>> keys = new ArrayList<>();
synchronized (descriptors) {
keys.addAll(descriptors.keySet());
}
for (Pair<String, String> key : keys) {
if (buff.length() > 0) {
buff.append(',');
}
buff.append(toString(key));
}
setPreference(getPreferenceStore(), PREF_CONFIG_DESC_ORDER, buff.toString());
}
private void syncActiveTarget() throws CoreException {
if (activeLaunchDesc == null) {
setActiveLaunchTarget(null);
return;
}
// last stored target from persistent storage
String activeTargetId = getPerDescriptorStore().get(PREF_ACTIVE_LAUNCH_TARGET, null);
if (activeTargetId != null) {
Pair<String, String> id = toId(activeTargetId);
ILaunchTarget storedTarget = launchTargetManager.getLaunchTarget(id.getFirst(), id.getSecond());
if (storedTarget != null && supportsTarget(activeLaunchDesc, storedTarget)) {
setActiveLaunchTarget(storedTarget);
return;
}
} else {
// current active target, check if it is supported
if (activeLaunchTarget != null && activeLaunchTarget != ILaunchTarget.NULL_TARGET
&& supportsTarget(activeLaunchDesc, activeLaunchTarget)) {
setActiveLaunchTarget(activeLaunchTarget);
return;
}
}
// default target for descriptor
setActiveLaunchTarget(getDefaultLaunchTarget(activeLaunchDesc));
}
private void syncActiveMode() throws CoreException {
if (activeLaunchDesc == null) {
setActiveLaunchMode(null);
return;
}
ILaunchMode foundMode = null;
// last desc mode id
String storedModeId = getPerDescriptorStore().get(PREF_ACTIVE_LAUNCH_MODE, null);
String lastActiveModeId = activeLaunchMode == null ? null : activeLaunchMode.getIdentifier();
// this is based on active desc and target which are already set
ILaunchMode[] supportedModes = getLaunchModes();
if (supportedModes.length > 0) { // mna, what if no modes are supported?
String modeNames[] = new String[] { storedModeId, lastActiveModeId, "run", //$NON-NLS-1$
"debug", //$NON-NLS-1$
supportedModes[0].getIdentifier() };
for (int i = 0; i < modeNames.length; i++) {
foundMode = getLaunchManager().getLaunchMode(modeNames[i]);
if (supportsMode(foundMode))
break;
}
}
setActiveLaunchMode(foundMode);
}
private boolean supportsMode(ILaunchMode mode) throws CoreException {
// check that active descriptor supports the given mode
if (mode == null)
return false;
ILaunchMode[] supportedModes = getLaunchModes();
for (int j = 0; j < supportedModes.length; j++) {
ILaunchMode lm = supportedModes[j];
if (lm.equals(mode))
return true;
}
return false;
}
private void setPreference(Preferences store, String prefId, String value) {
if (value != null) {
store.put(prefId, value);
} else {
store.remove(prefId);
}
try {
store.flush();
} catch (BackingStoreException e) {
Activator.log(e);
}
}
private Preferences getPerDescriptorStore() {
return getPerDescriptorStore(activeLaunchDesc);
}
private Preferences getPerDescriptorStore(ILaunchDescriptor launchDesc) {
if (launchDesc == null)
return getPreferenceStore();
String string;
try {
string = toString(getDescriptorId(launchDesc));
} catch (Exception e) {
Activator.log(e);
string = launchDesc.getName();
}
return getPreferenceStore().node(string);
}
// package private so tests can access it
IEclipsePreferences getPreferenceStore() {
return InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
}
private interface FireAction {
void run(ILaunchBarListener listener);
}
private void fireEvent(FireAction action) {
Collection<ILaunchBarListener> l;
synchronized (listeners) {
l = new ArrayList<>(listeners);
}
for (ILaunchBarListener listener : l) {
action.run(listener);
}
}
private void fireActiveLaunchDescriptorChanged() {
if (!initialized)
return;
fireEvent(listener -> {
try {
listener.activeLaunchDescriptorChanged(activeLaunchDesc);
} catch (Exception e) {
Activator.log(e);
}
});
}
@Override
public ILaunchMode[] getLaunchModes() throws CoreException {
ILaunchConfigurationType configType = getLaunchConfigurationType(activeLaunchDesc, activeLaunchTarget);
if (configType == null)
return new ILaunchMode[0];
List<ILaunchMode> modeList = new ArrayList<>();
ILaunchMode[] modes = getLaunchManager().getLaunchModes();
for (ILaunchMode mode : modes) {
if (configType.supportsMode(mode.getIdentifier())) {
modeList.add(mode);
}
}
return modeList.toArray(new ILaunchMode[modeList.size()]);
}
@Override
public ILaunchMode getActiveLaunchMode() {
return activeLaunchMode;
}
/**
* Sets the preferred mode for the given descriptor
*
* @param desc
* @param mode
* @throws CoreException
*/
public void setLaunchMode(ILaunchDescriptor desc, ILaunchMode mode) throws CoreException {
if (desc == activeLaunchDesc) {
setActiveLaunchMode(mode);
} else {
storeLaunchMode(desc, mode);
}
}
@Override
public void setActiveLaunchMode(ILaunchMode mode) throws CoreException {
if (activeLaunchMode == mode) {
// we have to modify listeners here because same mode does not mean
// same launch group. ModeSelector has to update.
fireActiveLaunchModeChanged(); // notify listeners
return;
}
if (activeLaunchDesc != null && mode != null && !supportsMode(mode))
throw new IllegalStateException(Messages.LaunchBarManager_2);
// change mode
activeLaunchMode = mode;
storeLaunchMode(activeLaunchDesc, mode);
fireActiveLaunchModeChanged(); // notify listeners
}
private void fireActiveLaunchModeChanged() {
if (!initialized)
return;
fireEvent(listener -> {
try {
listener.activeLaunchModeChanged(activeLaunchMode);
} catch (Exception e) {
Activator.log(e);
}
});
}
private void storeLaunchMode(ILaunchDescriptor desc, ILaunchMode mode) {
if (mode != null) {
// per desc store, desc can null if will be stored globally
setPreference(getPerDescriptorStore(desc), PREF_ACTIVE_LAUNCH_MODE, mode.getIdentifier());
}
}
@Override
public ILaunchTarget[] getLaunchTargets(ILaunchDescriptor descriptor) {
if (descriptor == null)
return launchTargetManager.getLaunchTargets();
List<ILaunchTarget> targets = new ArrayList<>();
for (ILaunchTarget target : launchTargetManager.getLaunchTargets()) {
if (supportsTarget(descriptor, target)) {
targets.add(target);
}
}
if (supportsNullTarget(descriptor)) {
targets.add(ILaunchTarget.NULL_TARGET);
}
return targets.toArray(new ILaunchTarget[targets.size()]);
}
boolean supportsTarget(ILaunchDescriptor descriptor, ILaunchTarget target) {
String descriptorTypeId = getDescriptorTypeId(descriptor.getType());
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descriptorTypeId)) {
try {
if (providerInfo.enabled(descriptor) && providerInfo.enabled(target)) {
if (providerInfo.getProvider().supports(descriptor, target)) {
return true;
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
return false;
}
boolean supportsNullTarget(ILaunchDescriptor descriptor) {
String descriptorTypeId = getDescriptorTypeId(descriptor.getType());
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descriptorTypeId)) {
if (providerInfo.enabled(descriptor) && providerInfo.supportsNullTarget()) {
return true;
}
}
return false;
}
@Override
public ILaunchTarget getActiveLaunchTarget() {
return activeLaunchTarget;
}
/**
* Sets preferred target for launch descriptor
*
* @param desc
* @param target
* @throws CoreException
*/
public void setLaunchTarget(ILaunchDescriptor desc, ILaunchTarget target) throws CoreException {
if (desc == activeLaunchDesc) {
setActiveLaunchTarget(target);
} else {
storeLaunchTarget(desc, target);
}
}
@Override
public void setActiveLaunchTarget(ILaunchTarget target) throws CoreException {
if (target == null)
target = ILaunchTarget.NULL_TARGET;
if (activeLaunchTarget == target) {
return;
}
activeLaunchTarget = target;
storeLaunchTarget(activeLaunchDesc, target);
syncActiveMode();
fireActiveLaunchTargetChanged(); // notify listeners
}
private void storeLaunchTarget(ILaunchDescriptor desc, ILaunchTarget target) {
if (target == null) {
// Don't store if it's null. Not sure we're null any more anyway.
return;
}
// per desc store, desc can be null means it store globally
setPreference(getPerDescriptorStore(desc), PREF_ACTIVE_LAUNCH_TARGET, toString(getTargetId(target)));
}
private void fireActiveLaunchTargetChanged() {
if (!initialized)
return;
fireEvent(listener -> {
try {
listener.activeLaunchTargetChanged(activeLaunchTarget);
} catch (Exception e) {
Activator.log(e);
}
});
}
private ILaunchTarget getDefaultLaunchTarget(ILaunchDescriptor descriptor) {
ILaunchTarget[] targets = getLaunchTargets(descriptor);
// chances are that better target is most recently added, rather then
// the oldest
return targets.length == 0 ? ILaunchTarget.NULL_TARGET : targets[targets.length - 1];
}
@Override
public ILaunchConfiguration getActiveLaunchConfiguration() throws CoreException {
ILaunchConfiguration configuration = getLaunchConfiguration(activeLaunchDesc, activeLaunchTarget);
// This is the only concrete time we have the mapping from launch
// configuration to launch target. Record it in the target manager for
// the launch delegates to use.
if (configuration != null) {
launchTargetManager.setDefaultLaunchTarget(configuration, activeLaunchTarget);
}
return configuration;
}
@Override
public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
throws CoreException {
if (descriptor == null) {
return null;
}
if (descriptor instanceof DefaultLaunchDescriptor) {
if (!descriptor.getType().supportsTargets()) {
return descriptor.getAdapter(ILaunchConfiguration.class);
}
}
String descTypeId = getDescriptorTypeId(descriptor.getType());
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeId)) {
try {
if (providerInfo.enabled(descriptor) && providerInfo.enabled(target)) {
ILaunchConfigurationProvider provider = providerInfo.getProvider();
// between multiple provider who support this descriptor we
// need to find one that supports this target
if (provider.supports(descriptor, target)) {
ILaunchConfiguration config = provider.getLaunchConfiguration(descriptor, target);
if (config != null) {
return config;
}
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
return null;
}
@Override
public void addListener(ILaunchBarListener listener) {
if (listener == null)
return;
synchronized (listeners) {
if (!listeners.contains(listener)) // cannot add duplicates
listeners.add(listener);
}
}
@Override
public void removeListener(ILaunchBarListener listener) {
if (listener == null)
return;
synchronized (listener) {
listeners.remove(listener);
}
}
@Override
public void launchConfigurationAdded(ILaunchConfiguration configuration) {
for (LaunchDescriptorTypeInfo descTypeInfo : orderedDescriptorTypes) {
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeInfo.getId())) {
try {
if (providerInfo.enabled(configuration)) {
if (providerInfo.getProvider().launchConfigurationAdded(configuration)) {
return;
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
}
// No one clamed it, add it as a launch object
launchObjectAdded(configuration);
}
@Override
public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
try {
launchObjectRemoved(configuration);
} catch (Throwable e) {
Activator.log(e);
}
for (LaunchDescriptorTypeInfo descTypeInfo : orderedDescriptorTypes) {
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeInfo.getId())) {
try {
if (providerInfo.enabled(configuration)) {
if (providerInfo.getProvider().launchConfigurationRemoved(configuration)) {
return;
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
}
}
@Override
public void launchConfigurationChanged(ILaunchConfiguration configuration) {
// we have to ignore notifications from working copies, otherwise
// we will get thousand of events and we don't track working copies
// (add/remove events are not sent for WCs)
if (configuration.isWorkingCopy())
return;
for (LaunchDescriptorTypeInfo descTypeInfo : orderedDescriptorTypes) {
for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeInfo.getId())) {
try {
if (providerInfo.enabled(configuration)) {
ILaunchConfigurationProvider provider = providerInfo.getProvider();
if (provider != null) {
if (provider.launchConfigurationChanged(configuration)) {
return;
}
}
}
} catch (Throwable e) {
Activator.log(e);
}
}
}
}
public void dispose() {
ILaunchManager launchManager = getLaunchManager();
launchManager.removeLaunchConfigurationListener(this);
for (ILaunchObjectProvider o : objectProviders) {
try {
o.dispose();
} catch (Exception e) {
Activator.log(e);
}
}
}
private void fireLaunchTargetsChanged() {
if (!initialized)
return;
fireEvent(listener -> {
try {
listener.launchTargetsChanged();
} catch (Exception e) {
Activator.log(e);
}
});
}
@Override
public void launchTargetAdded(ILaunchTarget target) {