StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / SaveFilter.java
blob2fc638ca2acb75b4ab12916157f988fe16d0b630
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
10 * Contributors:
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.
34 /**
35 * A saveable filter that selects savables that contain resources that
36 * are descendants of the roots of the filter.
37 * @since 3.3
40 class SaveFilter implements ISaveableFilter {
41 private final IResource[] roots;
43 /**
44 * Create the filter
45 * @param roots the save roots
47 public SaveFilter(IResource[] roots) {
48 this.roots = roots;
51 /* (non-Javadoc)
52 * @see org.eclipse.ui.ISaveableFilter#select(org.eclipse.ui.Saveable, org.eclipse.ui.IWorkbenchPart[])
54 @Override
55 public boolean select(Saveable saveable,
56 IWorkbenchPart[] containingParts) {
57 if (isDescendantOfRoots(saveable)) {
58 return true;
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)) {
66 return true;
70 return false;
73 /**
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) {
84 try {
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)) {
93 return true;
97 } catch (CoreException e) {
98 Activator
99 .logError(
101 .bind(
102 "An internal error occurred while determining the resources for {0}", saveable.getName()), e); //$NON-NLS-1$
104 } else {
105 // If there is no mapping, try to adapt to a resource or file directly
106 IFile file = ResourceUtil.getFile(saveable);
107 if (file != null) {
108 return isDescendantOfRoots(file);
111 return false;
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())) {
126 return true;
129 return false;
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());
142 if (file != null) {
143 return isDescendantOfRoots(file);
145 return false;