ISourceContainer[] getSourceContainers(String location, String id) throws CoreException { ISourceContainer[] containers = (ISourceContainer[]) fSourceContainerMap.get(location); if (containers != null) { return containers; } ArrayList result = new ArrayList(); ModelEntry entry = MonitorRegistry.findEntry(id); boolean match = false; IMonitorModelBase[] models = entry.getWorkspaceModels(); for (int i = 0; i < models.length; i++) { if (isPerfectMatch(models[i], new Path(location))) { IResource resource = models[i].getUnderlyingResource(); // if the plug-in matches a workspace model, // add the project and any libraries not coming via a container // to the list of source containers, in that order if (resource != null) { addProjectSourceContainers(resource.getProject(), result); } match = true; break; } } if (!match) { File file = new File(location); if (file.isFile()) { // in case of linked plug-in projects that map to an external JARd plug-in, // use source container that maps to the library in the linked project. ISourceContainer container = getArchiveSourceContainer(location); if (container != null) { containers = new ISourceContainer[] {container}; fSourceContainerMap.put(location, containers); return containers; } } models = entry.getExternalModels(); for (int i = 0; i < models.length; i++) { if (isPerfectMatch(models[i], new Path(location))) { // try all source zips found in the source code locations IClasspathEntry[] entries = MDEClasspathContainer.getExternalEntries(models[i]); for (int j = 0; j < entries.length; j++) { IRuntimeClasspathEntry rte = convertClasspathEntry(entries[j]); if (rte != null) result.add(rte); } break; } } } IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[]) result.toArray(new IRuntimeClasspathEntry[result.size()]); containers = JavaRuntime.getSourceContainers(entries); fSourceContainerMap.put(location, containers); return containers; }
private void createBuildPathChange(IResource[] sourceResources, CompositeChange rootChange) throws ModelException { IResource[] uniqueSourceResources = removeDuplicateResources(sourceResources); for (IResource element : uniqueSourceResources) { // only container need handle build/include path. if (element instanceof IContainer) { IProject project = element.getProject(); // if moving to another project if (RefactoringUtility.getResource(fMainDestinationPath).getProject() != project) { removeBuildPath(element, project); IPath path = element.getFullPath().removeLastSegments(1); RenameBuildAndIcludePathChange biChange = new RenameBuildAndIcludePathChange( path, path, element.getName(), "", oldBuildEntries, //$NON-NLS-1$ newBuildEntries, oldIncludePath, newIncludePathEntries); if (newBuildEntries.size() > 0 || newIncludePathEntries.size() > 0) { rootChange.add(biChange); } } else { updateBuildPath(element, project); RenameBuildAndIcludePathChange biChange = new RenameBuildAndIcludePathChange( element.getFullPath().removeLastSegments(1), fMainDestinationPath, element.getName(), element.getName(), oldBuildEntries, newBuildEntries, oldIncludePath, newIncludePathEntries); if (newBuildEntries.size() > 0 || newIncludePathEntries.size() > 0) { rootChange.add(biChange); } } } } }
/* Return the list of projects for which it requires a resource delta. This builder's project * is implicitly included and need not be specified. Builders must re-specify the list * of interesting projects every time they are run as this is not carried forward * beyond the next build. Missing projects should be specified but will be ignored until * they are added to the workspace. */ private IProject[] getRequiredProjects(boolean includeBinaryPrerequisites) { if (this.javaProject == null || this.workspaceRoot == null) return new IProject[0]; ArrayList projects = new ArrayList(); ExternalFoldersManager externalFoldersManager = JavaModelManager.getExternalManager(); try { IClasspathEntry[] entries = this.javaProject.getExpandedClasspath(); for (int i = 0, l = entries.length; i < l; i++) { IClasspathEntry entry = entries[i]; IPath path = entry.getPath(); IProject p = null; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: p = this.workspaceRoot.getProject( path.lastSegment()); // missing projects are considered too if (((ClasspathEntry) entry).isOptional() && !JavaProject.hasJavaNature(p)) // except if entry is optional p = null; break; case IClasspathEntry.CPE_LIBRARY: if (includeBinaryPrerequisites && path.segmentCount() > 0) { // some binary resources on the class path can come from projects that are not // included in the project references IResource resource = this.workspaceRoot.findMember(path.segment(0)); if (resource instanceof IProject) { p = (IProject) resource; } else { resource = externalFoldersManager.getFolder(path); if (resource != null) p = resource.getProject(); } } } if (p != null && !projects.contains(p)) projects.add(p); } } catch (JavaModelException e) { return new IProject[0]; } IProject[] result = new IProject[projects.size()]; projects.toArray(result); return result; }
/** * Ensure that the sync info for all the provided resources has been loaded. If an out-of-sync * resource is found, prompt to refresh all the projects involved. */ protected boolean ensureSyncInfoLoaded(IResource[] resources) throws CVSException { boolean keepTrying = true; while (keepTrying) { try { EclipseSynchronizer.getInstance().ensureSyncInfoLoaded(resources, getActionDepth()); keepTrying = false; } catch (CVSException e) { if (e.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { // determine the projects of the resources involved Set projects = new HashSet(); for (int i = 0; i < resources.length; i++) { IResource resource = resources[i]; projects.add(resource.getProject()); } // prompt to refresh if (promptToRefresh( getShell(), (IResource[]) projects.toArray(new IResource[projects.size()]), e.getStatus())) { for (Iterator iter = projects.iterator(); iter.hasNext(); ) { IProject project = (IProject) iter.next(); try { project.refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException coreException) { throw CVSException.wrapException(coreException); } } } else { return false; } } else { throw e; } } } return true; }
public void setCharsetFor(IPath resourcePath, String newCharset) throws CoreException { // for the workspace root we just set a preference in the instance scope if (resourcePath.segmentCount() == 0) { IEclipsePreferences resourcesPreferences = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); if (newCharset != null) resourcesPreferences.put(ResourcesPlugin.PREF_ENCODING, newCharset); else resourcesPreferences.remove(ResourcesPlugin.PREF_ENCODING); try { resourcesPreferences.flush(); } catch (BackingStoreException e) { IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); String message = Messages.resources_savingEncoding; throw new ResourceException( IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); } return; } // for all other cases, we set a property in the corresponding project IResource resource = workspace.getRoot().findMember(resourcePath); if (resource != null) { try { // disable the listener so we don't react to changes made by ourselves Preferences encodingSettings = getPreferences( resource.getProject(), true, resource.isDerived(IResource.CHECK_ANCESTORS)); if (newCharset == null || newCharset.trim().length() == 0) encodingSettings.remove(getKeyFor(resourcePath)); else encodingSettings.put(getKeyFor(resourcePath), newCharset); flushPreferences(encodingSettings, true); } catch (BackingStoreException e) { IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); String message = Messages.resources_savingEncoding; throw new ResourceException( IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); } } }
/** * The action is enabled for the appropriate resources. This method checks that: * * <ol> * <li>there is no overlap between a selected file and folder (overlapping folders is allowed * because of logical vs. physical mapping problem in views) * <li>the state of the resources match the conditions provided by: * <ul> * <li>isEnabledForIgnoredResources() * <li>isEnabledForManagedResources() * <li>isEnabledForUnManagedResources() (i.e. not ignored and not managed) * </ul> * </ol> * * @see TeamAction#isEnabled() */ public boolean isEnabled() { // allow the super to decide enablement. if the super doesn't know it will return false. boolean enabled = super.isEnabled(); if (enabled) return true; // invoke the inherited method so that overlaps are maintained IResource[] resources = getSelectedResourcesWithOverlap(); // disable if no resources are selected if (resources.length == 0) return false; // disable properly for single resource enablement if (!isEnabledForMultipleResources() && resources.length != 1) return false; // validate enabled for each resource in the selection List folderPaths = new ArrayList(); List filePaths = new ArrayList(); for (int i = 0; i < resources.length; i++) { IResource resource = resources[i]; // only enable for accessible resources if (resource.getType() == IResource.PROJECT) { if (!resource.isAccessible()) return false; } // no CVS actions are enabled if the selection contains a linked resource if (CVSWorkspaceRoot.isLinkedResource(resource)) return false; // only enable for resources in a project shared with CVS if (RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) == null) { return false; } // collect files and folders separately to check for overlap later IPath resourceFullPath = resource.getFullPath(); if (resource.getType() == IResource.FILE) { filePaths.add(resourceFullPath); } else { folderPaths.add(resourceFullPath); } // ensure that resource management state matches what the action requires ICVSResource cvsResource = getCVSResourceFor(resource); try { if (!isEnabledForCVSResource(cvsResource)) { return false; } } catch (CVSException e) { if (!isEnabledForException(e)) return false; } } // Ensure that there is no overlap between files and folders // NOTE: folder overlap must be allowed because of logical vs. physical if (!folderPaths.isEmpty()) { for (Iterator fileIter = filePaths.iterator(); fileIter.hasNext(); ) { IPath resourcePath = (IPath) fileIter.next(); for (Iterator it = folderPaths.iterator(); it.hasNext(); ) { IPath folderPath = (IPath) it.next(); if (folderPath.isPrefixOf(resourcePath)) { return false; } } } } return true; }
private void processEntryChanges( IResourceDelta projectDelta, Map<IProject, Boolean> projectsToSave) { // check each resource with user-set encoding to see if it has // been moved/deleted or if derived state has been changed IProject currentProject = (IProject) projectDelta.getResource(); Preferences projectRegularPrefs = getPreferences(currentProject, false, false, true); Preferences projectDerivedPrefs = getPreferences(currentProject, false, true, true); Map<Boolean, String[]> affectedResourcesMap = new HashMap<>(); try { // no regular preferences for this project if (projectRegularPrefs == null) affectedResourcesMap.put(Boolean.FALSE, new String[0]); else affectedResourcesMap.put(Boolean.FALSE, projectRegularPrefs.keys()); // no derived preferences for this project if (projectDerivedPrefs == null) affectedResourcesMap.put(Boolean.TRUE, new String[0]); else affectedResourcesMap.put(Boolean.TRUE, projectDerivedPrefs.keys()); } catch (BackingStoreException e) { // problems with the project scope... we will miss the changes (but will log) String message = Messages.resources_readingEncoding; Policy.log( new ResourceStatus( IResourceStatus.FAILED_GETTING_CHARSET, currentProject.getFullPath(), message, e)); return; } for (Iterator<Boolean> it = affectedResourcesMap.keySet().iterator(); it.hasNext(); ) { Boolean isDerived = it.next(); String[] affectedResources = affectedResourcesMap.get(isDerived); Preferences projectPrefs = isDerived.booleanValue() ? projectDerivedPrefs : projectRegularPrefs; for (int i = 0; i < affectedResources.length; i++) { IResourceDelta memberDelta = projectDelta.findMember(new Path(affectedResources[i])); // no changes for the given resource if (memberDelta == null) continue; if (memberDelta.getKind() == IResourceDelta.REMOVED) { boolean shouldDisableCharsetDeltaJobForCurrentProject = false; // remove the setting for the original location - save its value though String currentValue = projectPrefs.get(affectedResources[i], null); projectPrefs.remove(affectedResources[i]); if ((memberDelta.getFlags() & IResourceDelta.MOVED_TO) != 0) { IPath movedToPath = memberDelta.getMovedToPath(); IResource resource = workspace.getRoot().findMember(movedToPath); if (resource != null) { Preferences encodingSettings = getPreferences( resource.getProject(), true, resource.isDerived(IResource.CHECK_ANCESTORS)); if (currentValue == null || currentValue.trim().length() == 0) encodingSettings.remove(getKeyFor(movedToPath)); else encodingSettings.put(getKeyFor(movedToPath), currentValue); IProject targetProject = workspace.getRoot().getProject(movedToPath.segment(0)); if (targetProject.equals(currentProject)) // if the file was moved inside the same project disable charset listener shouldDisableCharsetDeltaJobForCurrentProject = true; else projectsToSave.put(targetProject, Boolean.FALSE); } } projectsToSave.put( currentProject, Boolean.valueOf(shouldDisableCharsetDeltaJobForCurrentProject)); } } if (moveSettingsIfDerivedChanged( projectDelta, currentProject, projectPrefs, affectedResources)) { // if settings were moved between preferences files disable charset listener so we don't // react to changes made by ourselves projectsToSave.put(currentProject, Boolean.TRUE); } } }