1 /*******************************************************************************
2 * Copyright (c) 2003, 2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * IBM Corporation - initial API and implementation
12 *******************************************************************************/
13 package org
.eclipse
.egit
.ui
.internal
;
15 import org
.eclipse
.core
.resources
.IFile
;
16 import org
.eclipse
.core
.resources
.IResource
;
17 import org
.eclipse
.core
.resources
.mapping
.ResourceMapping
;
18 import org
.eclipse
.core
.resources
.mapping
.ResourceMappingContext
;
19 import org
.eclipse
.core
.resources
.mapping
.ResourceTraversal
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.egit
.ui
.Activator
;
22 import org
.eclipse
.osgi
.util
.NLS
;
23 import org
.eclipse
.ui
.IEditorPart
;
24 import org
.eclipse
.ui
.ISaveableFilter
;
25 import org
.eclipse
.ui
.IWorkbenchPart
;
26 import org
.eclipse
.ui
.Saveable
;
27 import org
.eclipse
.ui
.ide
.ResourceUtil
;
30 * Copied from org.eclipse.ui.ide.IDE.SaveFilter, see
31 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=386609
32 * Replace with the above when we can depend on Eclipse 4.3.
35 * A saveable filter that selects savables that contain resources that
36 * are descendants of the roots of the filter.
40 class SaveFilter
implements ISaveableFilter
{
41 private final IResource
[] roots
;
45 * @param roots the save roots
47 public SaveFilter(IResource
[] roots
) {
52 * @see org.eclipse.ui.ISaveableFilter#select(org.eclipse.ui.Saveable, org.eclipse.ui.IWorkbenchPart[])
55 public boolean select(Saveable saveable
,
56 IWorkbenchPart
[] containingParts
) {
57 if (isDescendantOfRoots(saveable
)) {
60 // For backwards compatibility, we need to check the parts
61 for (int i
= 0; i
< containingParts
.length
; i
++) {
62 IWorkbenchPart workbenchPart
= containingParts
[i
];
63 if (workbenchPart
instanceof IEditorPart
) {
64 IEditorPart editorPart
= (IEditorPart
) workbenchPart
;
65 if (isEditingDescendantOf(editorPart
)) {
74 * Return whether the given saveable contains any resources that
75 * are descendants of the root resources.
76 * @param saveable the saveable
77 * @return whether the given saveable contains any resources that
78 * are descendants of the root resources
80 private boolean isDescendantOfRoots(Saveable saveable
) {
81 // First, try and adapt the saveable to a resource mapping.
82 ResourceMapping mapping
= ResourceUtil
.getResourceMapping(saveable
);
83 if (mapping
!= null) {
85 ResourceTraversal
[] traversals
= mapping
.getTraversals(
86 ResourceMappingContext
.LOCAL_CONTEXT
, null);
87 for (int i
= 0; i
< traversals
.length
; i
++) {
88 ResourceTraversal traversal
= traversals
[i
];
89 IResource
[] resources
= traversal
.getResources();
90 for (int j
= 0; j
< resources
.length
; j
++) {
91 IResource resource
= resources
[j
];
92 if (isDescendantOfRoots(resource
)) {
97 } catch (CoreException e
) {
102 "An internal error occurred while determining the resources for {0}", saveable
.getName()), e
); //$NON-NLS-1$
105 // If there is no mapping, try to adapt to a resource or file directly
106 IFile file
= ResourceUtil
.getFile(saveable
);
108 return isDescendantOfRoots(file
);
115 * Return whether the given resource is either equal to or a descendant of
116 * one of the given roots.
118 * @param resource the resource to be tested
119 * @return whether the given resource is either equal to or a descendant of
120 * one of the given roots
122 private boolean isDescendantOfRoots(IResource resource
) {
123 for (int l
= 0; l
< roots
.length
; l
++) {
124 IResource root
= roots
[l
];
125 if (root
.getFullPath().isPrefixOf(resource
.getFullPath())) {
133 * Return whether the given dirty editor part is editing resources that are
134 * descendants of the given roots.
136 * @param part the dirty editor part
137 * @return whether the given dirty editor part is editing resources that are
138 * descendants of the given roots
140 private boolean isEditingDescendantOf(IEditorPart part
) {
141 IFile file
= ResourceUtil
.getFile(part
.getEditorInput());
143 return isDescendantOfRoots(file
);