KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > actions > CVSActionSelectionProperties


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.WeakHashMap JavaDoc;
22
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.team.internal.ccvs.core.ICVSResource;
27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
28 import org.eclipse.team.internal.ui.Utils;
29
30 /**
31  * This class represents a selection for a set of CVS actions.
32  */

33 public class CVSActionSelectionProperties {
34     
35     // Use a weak hash map so that the properties ae collected when the selection is no longer referenced
36
private static Map JavaDoc selectionProperties = new WeakHashMap JavaDoc();
37     
38     private Object JavaDoc[] selection;
39     private Map JavaDoc properties = new HashMap JavaDoc();
40     
41     private static final String JavaDoc SELECTED_RESOURCES = "selectedResources"; //$NON-NLS-1$
42
private static final String JavaDoc NONOVERLAPPING_SELECTED_RESOURCES = "nonoverlappingSelectedResources"; //$NON-NLS-1$
43
private static final String JavaDoc CVS_RESOURCE_MAP = "cvsResourceMap"; //$NON-NLS-1$
44

45     public static CVSActionSelectionProperties getProperties(IStructuredSelection selection) {
46         if (selection == null) return null;
47         CVSActionSelectionProperties props = (CVSActionSelectionProperties)selectionProperties.get(selection);
48         if (props == null) {
49             props = new CVSActionSelectionProperties(selection);
50             selectionProperties.put(selection, props);
51         }
52         return props;
53     }
54     
55     public CVSActionSelectionProperties(IStructuredSelection selection) {
56         // Copy the selection so that the WeakHashMap will not think the seleciton used for the key is still in use
57
this.selection = selection.toArray();
58     }
59
60     public void put(String JavaDoc key, Object JavaDoc value) {
61         properties.put(key, value);
62     }
63     
64     public Object JavaDoc get(String JavaDoc key) {
65         return properties.get(key);
66     }
67     
68     public IResource[] getAllSelectedResources() {
69         IResource[] resources = (IResource[])get(SELECTED_RESOURCES);
70         if (resources == null) {
71             resources = getResources(selection);
72             put(SELECTED_RESOURCES, resources);
73         }
74         return resources;
75     }
76
77     /*
78      * This method gets the resources from the given objects.
79      * It does so in a manner that is consistent with how the workbench does it.
80      * Tha is, it first uses IContributionResourceAdapter, then IResource,
81      * then ResourceMapping.
82      */

83     private IResource[] getResources(Object JavaDoc[] objects) {
84         return Utils.getContributedResources(objects);
85     }
86     
87    public IResource[] getNonoverlappingSelectedResources() {
88         IResource[] resources = (IResource[])get(NONOVERLAPPING_SELECTED_RESOURCES);
89         if (resources == null) {
90             resources = getNonOverlapping(getAllSelectedResources());
91             put (NONOVERLAPPING_SELECTED_RESOURCES, resources);
92         }
93         return resources;
94     }
95         
96     public ICVSResource getCVSResourceFor(IResource resource) {
97         Map JavaDoc map = (Map JavaDoc)get(CVS_RESOURCE_MAP);
98         if (map == null) {
99             map = new HashMap JavaDoc();
100             put(CVS_RESOURCE_MAP, map);
101         }
102         ICVSResource cvsResource = (ICVSResource)map.get(resource);
103         if (cvsResource == null) {
104             cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
105             map.put(resource, cvsResource);
106         }
107         return cvsResource;
108     }
109     
110     /**
111      * Method getNonOverlapping ensures that a resource is not covered more than once.
112      * @param resources
113      * @return IResource[]
114      */

115     public static IResource[] getNonOverlapping(IResource[] resources) {
116         if (resources == null || resources.length == 0 || resources.length == 1) {
117             return resources;
118         }
119         // Sort the resources so the shortest paths are first
120
List JavaDoc sorted = new ArrayList JavaDoc();
121         sorted.addAll(Arrays.asList(resources));
122         Collections.sort(sorted, new Comparator JavaDoc() {
123             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
124                 IResource resource0 = (IResource) arg0;
125                 IResource resource1 = (IResource) arg1;
126                 return resource0.getFullPath().segmentCount() - resource1.getFullPath().segmentCount();
127             }
128             public boolean equals(Object JavaDoc arg0) {
129                 return false;
130             }
131         });
132         // Collect all non-overlapping resources
133
List JavaDoc coveredPaths = new ArrayList JavaDoc();
134         for (Iterator JavaDoc iter = sorted.iterator(); iter.hasNext();) {
135             IResource resource = (IResource) iter.next();
136             IPath resourceFullPath = resource.getFullPath();
137             boolean covered = false;
138             for (Iterator JavaDoc it = coveredPaths.iterator(); it.hasNext();) {
139                 IPath path = (IPath) it.next();
140                 if(path.isPrefixOf(resourceFullPath)) {
141                     covered = true;
142                 }
143             }
144             if (covered) {
145                 // if the resource is covered by a parent, remove it
146
iter.remove();
147             } else {
148                 // if the resource is a non-covered folder, add it to the covered paths
149
if (resource.getType() == IResource.FOLDER) {
150                     coveredPaths.add(resource.getFullPath());
151                 }
152             }
153         }
154         return (IResource[]) sorted.toArray(new IResource[sorted.size()]);
155     }
156     
157 }
158
Popular Tags