Skip to main content
summaryrefslogtreecommitdiffstats
blob: efb33606d6b8ba48792d490193e5df4ab2158000 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.actions;

import java.util.*;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ui.Utils;

/**
 * This class represents a selection for a set of CVS actions.
 */
public class CVSActionSelectionProperties {
    
    // Use a weak hash map so that the properties ae collected when the selection is no longer referenced
	private static Map<IStructuredSelection, CVSActionSelectionProperties> selectionProperties = new WeakHashMap<>();
    
    private Object[] selection;
	private Map<String, Object> properties = new HashMap<>();
    
    private static final String SELECTED_RESOURCES = "selectedResources"; //$NON-NLS-1$
    private static final String NONOVERLAPPING_SELECTED_RESOURCES = "nonoverlappingSelectedResources"; //$NON-NLS-1$
    private static final String CVS_RESOURCE_MAP = "cvsResourceMap"; //$NON-NLS-1$
    
    public static CVSActionSelectionProperties getProperties(IStructuredSelection selection) {
        if (selection == null) return null;
        CVSActionSelectionProperties props = selectionProperties.get(selection);
        if (props == null) {
            props = new CVSActionSelectionProperties(selection);
            selectionProperties.put(selection, props);
        }
        return props;
    }
    
    public CVSActionSelectionProperties(IStructuredSelection selection) {
        // Copy the selection so that the WeakHashMap will not think the seleciton used for the key is still in use
        this.selection = selection.toArray();
    }

    public void put(String key, Object value) {
        properties.put(key, value);
    }
    
    public Object get(String key) {
        return properties.get(key);
    }
    
    public IResource[] getAllSelectedResources() {
        IResource[] resources = (IResource[])get(SELECTED_RESOURCES);
        if (resources == null) {
            resources = getResources(selection);
            put(SELECTED_RESOURCES, resources);
        }
        return resources;
    }

    /*
     * This method gets the resources from the given objects.
     * It does so in a manner that is consistent with how the workbench does it.
     * Tha is, it first uses IContributionResourceAdapter, then IResource,
     * then ResourceMapping.
     */
    private IResource[] getResources(Object[] objects) {
        return Utils.getContributedResources(objects);
    }
    
   public IResource[] getNonoverlappingSelectedResources() {
        IResource[] resources = (IResource[])get(NONOVERLAPPING_SELECTED_RESOURCES);
        if (resources == null) {
            resources = getNonOverlapping(getAllSelectedResources());
            put (NONOVERLAPPING_SELECTED_RESOURCES, resources);
        }
        return resources;
    }
        
    public ICVSResource getCVSResourceFor(IResource resource) {
        Map map = (Map)get(CVS_RESOURCE_MAP);
        if (map == null) {
            map = new HashMap();
            put(CVS_RESOURCE_MAP, map);
        }
        ICVSResource cvsResource = (ICVSResource)map.get(resource);
        if (cvsResource == null) {
            cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
            map.put(resource, cvsResource);
        }
        return cvsResource;
    }
    
    /**
     * Method getNonOverlapping ensures that a resource is not covered more than once.
     * @param resources
     * @return IResource[]
     */
    public static IResource[] getNonOverlapping(IResource[] resources) {
        if (resources == null || resources.length == 0 || resources.length == 1) {
            return resources;
        }
        // Sort the resources so the shortest paths are first
		List<IResource> sorted = new ArrayList<>();
        sorted.addAll(Arrays.asList(resources));
		Collections.sort(sorted, new Comparator<IResource>() {
            @Override
			public int compare(IResource resource0, IResource resource1) {
                return resource0.getFullPath().segmentCount() - resource1.getFullPath().segmentCount();
            }
            @Override
			public boolean equals(Object arg0) {
                return false;
            }
        });
        // Collect all non-overlapping resources
		List<IPath> coveredPaths = new ArrayList<>();
        for (Iterator iter = sorted.iterator(); iter.hasNext();) {
            IResource resource = (IResource) iter.next();
            IPath resourceFullPath = resource.getFullPath();
            boolean covered = false;
            for (Iterator it = coveredPaths.iterator(); it.hasNext();) {
                IPath path = (IPath) it.next();
                if(path.isPrefixOf(resourceFullPath)) {
                    covered = true;
                }
            }
            if (covered) {
                // if the resource is covered by a parent, remove it
                iter.remove();
            } else {
                // if the resource is a non-covered folder, add it to the covered paths
                if (resource.getType() == IResource.FOLDER) {
                    coveredPaths.add(resource.getFullPath());
                }
            }
        }
        return sorted.toArray(new IResource[sorted.size()]);
    }
    
}

Back to the top