1 /*******************************************************************************
2  *  Copyright (c) 2005, 2019 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import java.util.ArrayList;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jdt.core.*;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.pde.core.build.IBuildEntry;
23 import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
24 import org.eclipse.pde.internal.core.builders.PDEBuilderHelper;
25 import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
26 import org.eclipse.pde.internal.core.text.build.Build;
27 import org.eclipse.pde.internal.core.text.build.BuildEntry;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 
30 public class AddSourceBuildEntryResolution extends BuildEntryMarkerResolution {
31 
AddSourceBuildEntryResolution(int type, IMarker marker)32 	public AddSourceBuildEntryResolution(int type, IMarker marker) {
33 		super(type, marker);
34 	}
35 
36 	@Override
getLabel()37 	public String getLabel() {
38 		return NLS.bind(PDEUIMessages.AddSourceBuildEntryResolution_label, fEntry);
39 	}
40 
41 	@Override
createChange(Build build)42 	protected void createChange(Build build) {
43 		try {
44 			fEntry = (String) marker.getAttribute(PDEMarkerFactory.BK_BUILD_ENTRY);
45 			fToken = (String) marker.getAttribute(PDEMarkerFactory.BK_BUILD_TOKEN);
46 		} catch (CoreException e) {
47 		}
48 		try {
49 			BuildEntry buildEntry = (BuildEntry) build.getEntry(fEntry);
50 			boolean unlistedOnly = true;
51 			if (buildEntry == null) {
52 				buildEntry = new BuildEntry(fEntry, build.getModel());
53 				unlistedOnly = false;
54 			}
55 			String[] unlisted = getSourcePaths(build, unlistedOnly);
56 			for (String token : unlisted) {
57 				if (token == null){
58 					break;
59 				}
60 				buildEntry.addToken(token);
61 			}
62 		} catch (CoreException e) {
63 		}
64 	}
65 
getSourcePaths(Build build, boolean unlistedOnly)66 	private String[] getSourcePaths(Build build, boolean unlistedOnly) {
67 		IProject project = build.getModel().getUnderlyingResource().getProject();
68 		try {
69 			if (project.hasNature(JavaCore.NATURE_ID)) {
70 				ArrayList<IBuildEntry> sourceEntries = new ArrayList<>();
71 				IBuildEntry[] entries = build.getBuildEntries();
72 				if (unlistedOnly) {
73 					for (IBuildEntry entry : entries) {
74 						if (entry.getName().startsWith(IBuildPropertiesConstants.PROPERTY_SOURCE_PREFIX)) {
75 							sourceEntries.add(entry);
76 						}
77 					}
78 				}
79 
80 				IJavaProject jp = JavaCore.create(project);
81 				IClasspathEntry[] cpes = jp.getRawClasspath();
82 				return PDEBuilderHelper.getUnlistedClasspaths(sourceEntries, project, cpes);
83 			}
84 		} catch (CoreException e) {
85 		}
86 		return null;
87 	}
88 }
89