Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e89dc52a16c7e862ee89330cb2ec942e2f4b0a1 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2011 Intel 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:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.tcmodification;

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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IResourceInfo;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.internal.core.Builder;
import org.eclipse.cdt.managedbuilder.internal.core.FolderInfo;
import org.eclipse.cdt.managedbuilder.internal.core.IRealBuildObjectAssociation;
import org.eclipse.cdt.managedbuilder.internal.core.ResourceInfo;
import org.eclipse.cdt.managedbuilder.internal.core.Tool;
import org.eclipse.cdt.managedbuilder.internal.core.ToolChain;
import org.eclipse.cdt.managedbuilder.internal.core.ToolChainModificationHelper;
import org.eclipse.cdt.managedbuilder.internal.core.ToolListModificationInfo;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.ToolChainModificationManager.ConflictMatch;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.ToolChainModificationManager.ConflictMatchSet;
import org.eclipse.cdt.managedbuilder.tcmodification.CompatibilityStatus;
import org.eclipse.cdt.managedbuilder.tcmodification.IModificationOperation;
import org.eclipse.cdt.managedbuilder.tcmodification.IToolListModification;
import org.eclipse.cdt.managedbuilder.tcmodification.IToolModification;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public abstract class ToolListModification implements IToolListModification {
	//	private Tool []fTools;
	private HashSet<String> fInputExtsSet = new HashSet<>();
	private ResourceInfo fRcInfo;
	private LinkedHashMap<Tool, IToolModification> fProjCompInfoMap = new LinkedHashMap<>();
	private HashMap<Tool, IToolModification> fSysCompInfoMap = new HashMap<>();
	private Tool[] fAllSysTools;
	private HashSet<ITool> fFilteredOutSysTools;
	//	private LinkedHashMap fRealToToolMap = new LinkedHashMap();
	//	private boolean fSysInfoMapInited;
	private PerTypeMapStorage<IRealBuildObjectAssociation, Set<IPath>> fCompleteObjectStorage;
	protected TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> fCompletePathMapStorage;
	private HashSet<Tool> fAddCapableTools;
	private Map<Tool, Tool> fFilteredOutTools;

	private ToolListModificationInfo fModificationInfo;

	private class ModificationOperation implements IModificationOperation {
		private ITool fReplacement;
		private IToolModification fModification;

		ModificationOperation(IToolModification modification, ITool tool) {
			fModification = modification;
			fReplacement = tool;
		}

		@Override
		public ITool getReplacementTool() {
			return fReplacement;
		}

		@Override
		public IToolModification getToolModification() {
			return fModification;
		}
	}

	private class FilteredTool implements IToolModification {
		private ITool fTool;
		private boolean fIsProject;

		public FilteredTool(ITool tool, boolean isProject) {
			fTool = tool;
			fIsProject = isProject;
		}

		@Override
		public CompatibilityStatus getCompatibilityStatus() {
			return fIsProject ? CompatibilityStatus.OK_COMPATIBILITY_STATUS
					: new CompatibilityStatus(IStatus.ERROR,
							Messages.getString("ToolListModification.ToolIsIncompatible"), null); //$NON-NLS-1$
		}

		@Override
		public IModificationOperation[] getSupportedOperations() {
			return new IModificationOperation[0];
		}

		@Override
		public ITool getTool() {
			return fTool;
		}

		@Override
		public boolean isCompatible() {
			return fIsProject;
		}

		@Override
		public boolean isProjectTool() {
			return fIsProject;
		}

		@Override
		public IResourceInfo getResourceInfo() {
			return ToolListModification.this.getResourceInfo();
		}

	}

	public class ProjToolCompatibilityStatusInfo implements IToolModification {
		private ToolCompatibilityInfoElement fCurrentElement;
		private Map<Tool, ToolCompatibilityInfoElement> fCompatibleTools;
		private Map<Tool, ToolCompatibilityInfoElement> fInCompatibleTools;
		private IModificationOperation[] fOperations;
		private Tool fSelectedTool;
		private Tool fRealTool;
		private boolean fInited;
		private Set<Tool> fExtConflictTools;

		ProjToolCompatibilityStatusInfo(Tool tool) {
			fSelectedTool = tool;
			fRealTool = (Tool) ManagedBuildManager.getRealTool(tool);
		}

		private ToolCompatibilityInfoElement getCurrentElement() {
			checkInitCompatibleTools();
			return fCurrentElement;
		}

		@Override
		public CompatibilityStatus getCompatibilityStatus() {
			return getCurrentElement().getCompatibilityStatus();
		}

		@Override
		public boolean isCompatible() {
			return getCurrentElement().isCompatible();
		}

		@Override
		public boolean isProjectTool() {
			return true;
		}

		public Map<Tool, ToolCompatibilityInfoElement> getCompatibleTools() {
			checkInitCompatibleTools();
			return fCompatibleTools;
		}

		private void checkInitCompatibleTools() {
			if (fInited)
				return;

			if (DbgTcmUtil.DEBUG)
				DbgTcmUtil.println("calculating compatibility for tool " + fRealTool.getUniqueRealName()); //$NON-NLS-1$

			PerTypeMapStorage<? extends IRealBuildObjectAssociation, Set<IPath>> storage = getCompleteObjectStore();
			Tool tool = fRealTool;
			Set<IPath> rmSet = getToolApplicabilityPathSet(tool, true);
			@SuppressWarnings("unchecked")
			Map<Tool, Set<IPath>> toolMap = (Map<Tool, Set<IPath>>) storage
					.getMap(IRealBuildObjectAssociation.OBJECT_TOOL, false);
			try {
				if (rmSet != null && rmSet.size() != 0)
					TcModificationUtil.removePaths(toolMap, tool, rmSet);

				if (DbgTcmUtil.DEBUG)
					DbgTcmUtil.dumpStorage(storage);

				@SuppressWarnings("unchecked")
				ConflictMatchSet conflicts = ToolChainModificationManager.getInstance().getConflictInfo(
						IRealBuildObjectAssociation.OBJECT_TOOL,
						(PerTypeMapStorage<IRealBuildObjectAssociation, Set<IPath>>) storage);

				fCompatibleTools = new HashMap<>();
				fInCompatibleTools = new HashMap<>();
				Tool sysTools[] = getTools(false, true);
				@SuppressWarnings("unchecked")
				Map<Tool, List<ConflictMatch>> conflictMap = (Map<Tool, List<ConflictMatch>>) conflicts.fObjToConflictListMap;
				for (int i = 0; i < sysTools.length; i++) {
					Tool t = sysTools[i];
					List<ConflictMatch> l = conflictMap.get(t);
					ToolCompatibilityInfoElement el = new ToolCompatibilityInfoElement(this, t, l);
					if (el.isCompatible()) {
						fCompatibleTools.put(t, el);
					} else {
						fInCompatibleTools.put(t, el);
					}
				}

				Tool t = fRealTool;
				List<ConflictMatch> l = conflictMap.get(t);
				fCurrentElement = new ToolCompatibilityInfoElement(this, t, l);
			} finally {
				if (rmSet != null && rmSet.size() != 0)
					TcModificationUtil.addPaths(toolMap, tool, rmSet);
			}
			fInited = true;
		}

		private Set<Tool> getConflictingTools() {
			if (fExtConflictTools == null) {
				Tool[] tmp = new Tool[1];
				tmp[0] = fSelectedTool;
				tmp = filterTools(tmp);
				if (tmp.length == 0)
					fExtConflictTools = Collections.emptySet();
				else
					fExtConflictTools = getExtensionConflictToolSet(fSelectedTool, filterTools(getTools(true, false)));
			}
			return fExtConflictTools;
		}

		@Override
		public IModificationOperation[] getSupportedOperations() {
			if (fOperations == null) {
				checkInitCompatibleTools();
				if (fFilteredOutTools.containsKey(fRealTool)) {
					if (canRemove(fRealTool)) {
						fOperations = new ModificationOperation[] { new ModificationOperation(this, null) };
					} else {
						fOperations = new ModificationOperation[0];
					}
				} else {
					List<ModificationOperation> opList = new ArrayList<>(
							fCompatibleTools.size() + 1);
					Set<Tool> keySet = fCompatibleTools.keySet();
					for (Tool tool : keySet) {
						if (tool == fRealTool)
							continue;

						if (canReplace(fRealTool, tool))
							opList.add(new ModificationOperation(this, tool));
					}

					if (fCompatibleTools.size() == 0 || canRemove(fRealTool)) {
						opList.add(new ModificationOperation(this, null));
					}

					fOperations = opList.toArray(new ModificationOperation[opList.size()]);
				}
			}
			return fOperations.clone();
		}

		@Override
		public ITool getTool() {
			return fSelectedTool;
		}

		public ITool getRealTool() {
			return fRealTool;
		}

		@Override
		public IResourceInfo getResourceInfo() {
			return fRcInfo;
		}

		public void clearCompatibilityInfo() {
			fInited = false;
			fCompatibleTools = null;
			fInCompatibleTools = null;
			fOperations = null;
			fCurrentElement = null;
			fExtConflictTools = null;
		}

	}

	protected Set<String> getInputExtsSet() {
		return fInputExtsSet;
	}

	public class SysToolCompatibilityStatusInfo implements IToolModification {
		private IModificationOperation[] fOperations;
		private Tool fSelectedTool;
		private Tool fRealTool;
		private CompatibilityStatus fStatus;

		SysToolCompatibilityStatusInfo(Tool tool) {
			fSelectedTool = tool;
			fRealTool = (Tool) ManagedBuildManager.getRealTool(tool);
		}

		//		private ToolCompatibilityInfoElement getCurrentElement(){
		//			if(fCurrentElement == null){
		//				checkInitCompatibleTools();
		//				ToolCompatibilityInfoElement info = (ToolCompatibilityInfoElement)fCompatibleTools.get(fRealTool);
		//				if(info == null)
		//					info = (ToolCompatibilityInfoElement)fInCompatibleTools.get(fRealTool);
		//				fCurrentElement = info;
		//			}
		//			return fCurrentElement;
		//		}

		@Override
		public CompatibilityStatus getCompatibilityStatus() {
			if (fStatus == null) {
				int severity;
				String message;
				if (!isCompatible()) {
					severity = IStatus.ERROR;
					message = Messages.getString("ToolListModification.ToolIsIncompatible"); //$NON-NLS-1$
				} else {
					severity = IStatus.OK;
					message = ""; //$NON-NLS-1$
				}
				fStatus = new CompatibilityStatus(severity, message, null);
			}
			return fStatus;
		}

		@Override
		public boolean isCompatible() {
			return getSupportedOperationsArray().length > 0;
		}

		@Override
		public boolean isProjectTool() {
			return false;
		}

		public IModificationOperation[] getSupportedOperationsArray() {
			if (fOperations == null) {
				Set<Tool> addCompatibleSysToolsSet = getAddCompatibleSysTools();
				if (addCompatibleSysToolsSet.contains(fRealTool) && canAdd(fRealTool)) {
					fOperations = new ModificationOperation[] { new ModificationOperation(this, null) };
				} else {
					Map<Tool, IToolModification> projMap = getMap(true);
					List<ModificationOperation> opList = new ArrayList<>(projMap.size());
					for (IToolModification tm : projMap.values()) {
						ProjToolCompatibilityStatusInfo info = (ProjToolCompatibilityStatusInfo) tm;
						if (info.getCompatibleTools().containsKey(fRealTool)
								&& !fFilteredOutTools.containsKey(info.fRealTool)
								&& canReplace(info.fSelectedTool, this.fSelectedTool)) {
							opList.add(new ModificationOperation(this, info.fSelectedTool));
						}
					}
					fOperations = opList.toArray(new ModificationOperation[opList.size()]);
				}
			}
			return fOperations;

		}

		@Override
		public IModificationOperation[] getSupportedOperations() {
			return getSupportedOperationsArray().clone();
		}

		@Override
		public ITool getTool() {
			return fSelectedTool;
		}

		public ITool getRealTool() {
			return fRealTool;
		}

		@Override
		public IResourceInfo getResourceInfo() {
			return fRcInfo;
		}

		public void clearCompatibilityInfo() {
			fOperations = null;
			fStatus = null;
		}
	}

	private Set<Tool> getAddCompatibleSysTools() {
		if (fAddCapableTools == null) {
			fAddCapableTools = new HashSet<>(Arrays.asList(getAllSysTools()));
			PerTypeMapStorage<IRealBuildObjectAssociation, Set<IPath>> storage = getCompleteObjectStore();
			ConflictMatchSet conflicts = ToolChainModificationManager.getInstance()
					.getConflictInfo(IRealBuildObjectAssociation.OBJECT_TOOL, storage);
			fAddCapableTools.removeAll(conflicts.fObjToConflictListMap.keySet());
		}
		return fAddCapableTools;
	}

	public ToolListModification(ResourceInfo rcInfo, ITool[] tools) {
		fRcInfo = rcInfo;
		clearToolInfo(tools);
	}

	public ToolListModification(ResourceInfo rcInfo, ToolListModification base) {
		fRcInfo = rcInfo;
		Tool[] initialTools = (Tool[]) rcInfo.getTools();
		@SuppressWarnings("unchecked")
		Map<Tool, Tool> initRealToToolMap = (Map<Tool, Tool>) TcModificationUtil.getRealToObjectsMap(initialTools,
				null);
		Tool[] updatedTools = base.getTools(true, false);
		@SuppressWarnings("unchecked")
		Map<Tool, Tool> updatedRealToToolMap = (Map<Tool, Tool>) TcModificationUtil.getRealToObjectsMap(updatedTools,
				null);
		Set<Entry<Tool, Tool>> entrySet = updatedRealToToolMap.entrySet();
		for (Entry<Tool, Tool> entry : entrySet) {
			Tool real = entry.getKey();
			Tool initial = initRealToToolMap.get(real);
			if (initial != null) {
				entry.setValue(initial);
			} else {
				Tool updated = entry.getValue();
				if (!updated.isExtensionBuildObject()) {
					updated = (Tool) updated.getExtensionObject();
					entry.setValue(updated);
				}
			}
		}

		updatedRealToToolMap.values().toArray(updatedTools);
		clearToolInfo(updatedTools);

		//		if(base.fInputExtsSet != null)
		//			fInputExtsSet = (HashSet)base.fInputExtsSet.clone();
		//		private LinkedHashMap fProjCompInfoMap = new LinkedHashMap();
		//		private HashMap fSysCompInfoMap = new HashMap();
		if (base.fAllSysTools != null)
			fAllSysTools = base.fAllSysTools.clone();

		if (base.fFilteredOutSysTools != null) {
			@SuppressWarnings("unchecked")
			HashSet<ITool> clone = (HashSet<ITool>) base.fFilteredOutSysTools.clone();
			fFilteredOutSysTools = clone;
		}

		if (base.fCompleteObjectStorage != null) {
			fCompleteObjectStorage = TcModificationUtil.cloneRealToolToPathSet(base.fCompleteObjectStorage);
		}

		if (base.fCompletePathMapStorage != null) {
			fCompletePathMapStorage = base.fCompletePathMapStorage;
		}
		//		if(base.fAddCapableTools)
		//			fAddCapableTools = (HashSet)base.fAddCapableTools.clone();
		//private Map fFilteredOutTools;

		//		private ToolListModificationInfo fModificationInfo;
	}

	@Override
	public IResourceInfo getResourceInfo() {
		return fRcInfo;
	}

	protected abstract boolean canRemove(ITool realTool);

	protected abstract boolean canReplace(Tool fromTool, Tool toTool);

	protected abstract boolean canAdd(Tool tool);

	protected abstract Set<IPath> getToolApplicabilityPathSet(Tool realTool, boolean isProject);

	protected abstract Set<Tool> getExtensionConflictToolSet(Tool tool, Tool[] toos);

	protected abstract Tool[] filterTools(Tool[] tools);

	public class ToolCompatibilityInfoElement {
		private Tool fRealTool;
		private List<ConflictMatch> fErrComflictMatchList;
		private CompatibilityStatus fStatus;
		private ProjToolCompatibilityStatusInfo fStatusInfo;

		ToolCompatibilityInfoElement(ProjToolCompatibilityStatusInfo statusInfo, Tool realTool,
				List<ConflictMatch> errConflictList) {
			fStatusInfo = statusInfo;
			fRealTool = realTool;
			if (errConflictList != null && errConflictList.size() != 0)
				fErrComflictMatchList = errConflictList;
		}

		public CompatibilityStatus getCompatibilityStatus() {
			if (fStatus == null) {
				int severity;
				String message;
				ConflictSet conflicts;
				if (!isCompatible()) {
					severity = IStatus.ERROR;
					message = Messages.getString("ToolListModification.ToolIsIncompatible"); //$NON-NLS-1$
					conflicts = new ConflictSet(fRealTool, fErrComflictMatchList, fStatusInfo.getConflictingTools());
				} else {
					severity = IStatus.OK;
					message = ""; //$NON-NLS-1$
					conflicts = null;
				}
				fStatus = new CompatibilityStatus(severity, message, conflicts);
			}
			return fStatus;
		}

		public boolean isCompatible() {
			return noCompatibilityConflicts()
					&& (fRealTool != fStatusInfo.getRealTool() || fStatusInfo.getConflictingTools().size() == 0);
		}

		public boolean noCompatibilityConflicts() {
			return fErrComflictMatchList == null;
		}
	}

	private Tool[] getAllSysTools() {
		if (fAllSysTools == null) {
			ITool[] allSys = ManagedBuildManager.getRealTools();
			fAllSysTools = filterTools((Tool[]) allSys);
			HashSet<ITool> set = new HashSet<>(Arrays.asList(allSys));
			set.removeAll(Arrays.asList(fAllSysTools));
			fFilteredOutSysTools = set;
		}
		return fAllSysTools;
	}

	@Override
	public final void apply() throws CoreException {
		TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> initialMap = TcModificationUtil
				.createPathMap(fRcInfo.getParent());
		TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> cur = getCompletePathMapStorage();
		TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> result = TcModificationUtil
				.createResultingChangesMap(cur, initialMap);
		apply(result);
	}

	private void apply(TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> resultingChangeMap)
			throws CoreException {
		//the order matters here: we first should process tool-chain than a builder and then tools
		int types[] = new int[] { IRealBuildObjectAssociation.OBJECT_TOOLCHAIN,
				IRealBuildObjectAssociation.OBJECT_BUILDER, IRealBuildObjectAssociation.OBJECT_TOOL, };

		int type;
		IConfiguration cfg = fRcInfo.getParent();
		Set<Entry<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>>> entrySet = resultingChangeMap.entrySet();
		for (Entry<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> entry : entrySet) {
			IPath path = entry.getKey();
			ResourceInfo rcInfo = (ResourceInfo) cfg.getResourceInfo(path, true);
			if (rcInfo == null) {
				rcInfo = (FolderInfo) cfg.createFolderInfo(path);
			}
			PerTypeSetStorage<IRealBuildObjectAssociation> storage = entry.getValue();
			for (int i = 0; i < types.length; i++) {
				type = types[i];
				Set<IRealBuildObjectAssociation> set = storage.getSet(type, false);
				if (set != null) {
					apply(rcInfo, type, set);
				}
			}
		}
	}

	private void apply(ResourceInfo rcInfo, int type, Set<? extends IRealBuildObjectAssociation> set)
			throws CoreException {
		switch (type) {
		case IRealBuildObjectAssociation.OBJECT_TOOL:
			ToolListModificationInfo info = rcInfo == fRcInfo ? getModificationInfo()
					: ToolChainModificationHelper.getModificationInfo(rcInfo, rcInfo.getTools(),
							set.toArray(new Tool[set.size()]));
			info.apply();
			break;
		case IRealBuildObjectAssociation.OBJECT_TOOLCHAIN:
			if (rcInfo.isFolderInfo()) {
				if (set.size() != 0) {
					ToolChain tc = (ToolChain) set.iterator().next();
					try {
						((FolderInfo) rcInfo).changeToolChain(tc, CDataUtil.genId(tc.getId()), null);
					} catch (BuildException e) {
						throw new CoreException(new Status(IStatus.ERROR,
								ManagedBuilderCorePlugin.getUniqueIdentifier(), e.getLocalizedMessage(), e));
					}
				}
			}
			break;
		case IRealBuildObjectAssociation.OBJECT_BUILDER:
			if (rcInfo.isRoot()) {
				if (set.size() != 0) {
					Builder b = (Builder) set.iterator().next();
					rcInfo.getParent().changeBuilder(b, CDataUtil.genId(b.getId()), null);
				}
			}
			break;
		}
	}

	private ToolListModificationInfo getModificationInfo() {
		if (fModificationInfo == null) {
			fModificationInfo = ToolChainModificationHelper.getModificationInfo(fRcInfo, fRcInfo.getTools(),
					getTools(true, false));
		}
		return fModificationInfo;
	}

	@Override
	public IToolModification[] getProjectToolModifications() {
		Map<Tool, IToolModification> map = getMap(true);
		return map.values().toArray(new ProjToolCompatibilityStatusInfo[map.size()]);
	}

	@Override
	public ITool[] getProjectTools() {
		return getTools(true, false);
	}

	@Override
	public IToolModification[] getSystemToolModifications() {
		Map<Tool, IToolModification> map = getMap(false);
		return map.values().toArray(new SysToolCompatibilityStatusInfo[map.size()]);
	}

	@Override
	public IToolModification getToolModification(ITool tool) {
		Tool rt = (Tool) ManagedBuildManager.getRealTool(tool);
		boolean isProj = isProjectTool(rt);
		Map<Tool, IToolModification> map = getMap(isProj);
		IToolModification m = map.get(rt);
		if (m == null) {
			ITool realTool = ManagedBuildManager.getRealTool(tool);
			boolean projFiltered = fFilteredOutTools.keySet().contains(realTool);
			m = new FilteredTool(tool, projFiltered);
		}
		return m;
	}

	//	private Map<Tool, IToolModification> getMap(Tool tool){
	//		return getMap(isProjectTool(tool));
	//	}

	private Map<Tool, IToolModification> getMap(boolean proj) {
		return proj ? fProjCompInfoMap : fSysCompInfoMap;
	}

	private Tool[] getTools(boolean proj, boolean real) {
		if (proj) {
			if (real)
				return fProjCompInfoMap.keySet().toArray(new Tool[fProjCompInfoMap.size()]);

			Tool[] tools = new Tool[fProjCompInfoMap.size()];
			int i = 0;
			Collection<IToolModification> infos = fProjCompInfoMap.values();
			for (IToolModification info : infos) {
				tools[i++] = ((ProjToolCompatibilityStatusInfo) info).fSelectedTool;
			}
			return tools;
		}

		return fSysCompInfoMap.keySet().toArray(new Tool[fSysCompInfoMap.size()]);
	}

	private boolean isProjectTool(Tool tool) {
		return fProjCompInfoMap.containsKey(tool.getRealBuildObject());
	}

	@Override
	public void changeProjectTools(ITool removeTool, ITool addTool) {
		Map<ITool, ITool> map = createRealToToolMap();
		Tool realAdded = (Tool) ManagedBuildManager.getRealTool(addTool);
		Tool realRemoved = (Tool) ManagedBuildManager.getRealTool(removeTool);
		boolean removed = realRemoved != null ? map.remove(realRemoved) != null : false;
		boolean added = realAdded != null ? map.put(realAdded, addTool) == null : false;
		if (!added && !removed)
			return;

		Set<IPath> rmSet = null;
		Set<IPath> addSet = null;
		if (removed) {
			rmSet = getToolApplicabilityPathSet(realRemoved, true);
		}
		if (added) {
			if (rmSet == null)
				addSet = getToolApplicabilityPathSet(realAdded, false);
			else
				addSet = rmSet;
		}

		List<ITool> list = new ArrayList<>();
		list.addAll(map.values());
		clearToolInfo(map.values().toArray(new Tool[map.size()]));

		PerTypeMapStorage<? extends IRealBuildObjectAssociation, Set<IPath>> storage = getCompleteObjectStore();
		@SuppressWarnings("unchecked")
		Map<Tool, Set<IPath>> toolMap = (Map<Tool, Set<IPath>>) storage.getMap(IRealBuildObjectAssociation.OBJECT_TOOL,
				true);
		if (rmSet != null)
			TcModificationUtil.removePaths(toolMap, realRemoved, rmSet);
		if (addSet != null)
			TcModificationUtil.addPaths(toolMap, realAdded, addSet);
	}

	private HashMap<ITool, ITool> createRealToToolMap(/*boolean includeFilteredOut*/) {
		HashMap<ITool, ITool> map = new HashMap<>();
		Set<Entry<Tool, IToolModification>> entries = fProjCompInfoMap.entrySet();
		for (Entry<Tool, IToolModification> entry : entries) {
			map.put(entry.getKey(), entry.getValue().getTool());
		}
		//		if(includeFilteredOut){
		//			for(Iterator iter = fFilteredOutTools.entrySet().iterator(); iter.hasNext(); ){
		//				Map.Entry entry = (Map.Entry)iter.next();
		//				map.put(entry.getKey(), entry.getValue());
		//			}
		//		}

		return map;
	}

	protected void clearToolCompatibilityInfo() {
		for (IToolModification info : fProjCompInfoMap.values()) {
			((ProjToolCompatibilityStatusInfo) info).clearCompatibilityInfo();
		}

		for (IToolModification info : fSysCompInfoMap.values()) {
			((SysToolCompatibilityStatusInfo) info).clearCompatibilityInfo();
		}
	}

	protected void clearToolInfo(ITool[] tools) {
		Tool[] filteredTools;
		Tool[] allTools;
		if (tools instanceof Tool[]) {
			allTools = (Tool[]) tools;
		} else {
			allTools = new Tool[tools.length];
			System.arraycopy(tools, 0, allTools, 0, tools.length);
		}

		filteredTools = filterTools(allTools);
		@SuppressWarnings("unchecked")
		Map<Tool, Tool> filteredMap = (Map<Tool, Tool>) TcModificationUtil.getRealToObjectsMap(filteredTools, null);
		@SuppressWarnings("unchecked")
		Map<Tool, Tool> allMap = (Map<Tool, Tool>) TcModificationUtil.getRealToObjectsMap(allTools, null);
		allMap.keySet().removeAll(filteredMap.keySet());
		fFilteredOutTools = allMap;
		//		tools = filteredTools;

		fModificationInfo = null;
		fInputExtsSet.clear();
		fProjCompInfoMap.clear();
		for (int i = 0; i < tools.length; i++) {
			ITool tool = tools[i];
			Tool realTool = (Tool) ManagedBuildManager.getRealTool(tool);
			fProjCompInfoMap.put(realTool, new ProjToolCompatibilityStatusInfo((Tool) tool));
			if (!fFilteredOutTools.containsKey(realTool))
				fInputExtsSet.addAll(Arrays.asList(tool.getPrimaryInputExtensions()));
		}
		fSysCompInfoMap.clear();
		Tool[] sysTools = getAllSysTools();
		for (int i = 0; i < sysTools.length; i++) {
			Tool sysTool = sysTools[i];
			if (!fProjCompInfoMap.containsKey(sysTool)) {
				fSysCompInfoMap.put(sysTool, new SysToolCompatibilityStatusInfo(sysTool));
			}
		}
		fAddCapableTools = null;
		fCompletePathMapStorage = null;
	}

	protected PerTypeMapStorage<IRealBuildObjectAssociation, Set<IPath>> getCompleteObjectStore() {
		if (fCompleteObjectStorage == null) {
			fCompleteObjectStorage = TcModificationUtil.createRealToolToPathSet(fRcInfo.getParent(), null, false);
			if (DbgTcmUtil.DEBUG) {
				DbgTcmUtil.println("dumping complete storage"); //$NON-NLS-1$
				DbgTcmUtil.dumpStorage(fCompleteObjectStorage);
			}
		}
		return fCompleteObjectStorage;
	}

	protected TreeMap<IPath, PerTypeSetStorage<IRealBuildObjectAssociation>> getCompletePathMapStorage() {
		if (fCompletePathMapStorage == null) {
			fCompletePathMapStorage = TcModificationUtil.createPathMap(getCompleteObjectStore());
		}
		return fCompletePathMapStorage;
	}

}

Back to the top