KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.*;
14
15 import org.eclipse.core.internal.resources.mapping.*;
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.team.core.subscribers.Subscriber;
21 import org.eclipse.team.core.synchronize.FastSyncInfoFilter;
22 import org.eclipse.team.core.synchronize.SyncInfo;
23 import org.eclipse.team.core.synchronize.FastSyncInfoFilter.SyncInfoDirectionFilter;
24 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
25 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
26 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
27 import org.eclipse.team.internal.ui.dialogs.*;
28
29 public abstract class UncommittedChangesDialog extends MappingSelectionDialog {
30     
31     public static final class UncommittedFilter implements IResourceMappingResourceFilter {
32         public boolean select(IResource resource,
33                 ResourceMapping mapping, ResourceTraversal traversal)
34                 throws CoreException {
35             SyncInfo info = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().getSyncInfo(resource);
36             return (info != null && getResourceFilter().select(info));
37         }
38     }
39     
40     public static FastSyncInfoFilter getResourceFilter() {
41         // Return a filter that selects outgoing changes
42
return new SyncInfoDirectionFilter(new int[] { SyncInfo.OUTGOING, SyncInfo.CONFLICTING });
43     }
44     
45     private final ResourceMapping[] allMappings;
46     
47     
48     public UncommittedChangesDialog(Shell parentShell, String JavaDoc dialogTitle, ResourceMapping[] mappings, IProgressMonitor monitor) {
49         super(parentShell, dialogTitle, getMatchingMappings(mappings, CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(), getResourceFilter(), monitor), new UncommittedFilter());
50         allMappings = mappings;
51     }
52
53
54     protected String JavaDoc getResourceListMessage(ResourceMapping mapping) {
55         if (mapping == null) {
56             return CVSUIMessages.UncommittedChangesDialog_2; //$NON-NLS-1$
57
} else {
58             String JavaDoc label = ResourceMappingResourceDisplayArea.getLabel(mapping);
59             if (mapping.getModelObject() instanceof IFile) {
60                 return NLS.bind(CVSUIMessages.UncommittedChangesDialog_4, new String JavaDoc[] { label }); //$NON-NLS-1$
61
}
62             return NLS.bind(CVSUIMessages.UncommittedChangesDialog_3, new String JavaDoc[] { label }); //$NON-NLS-1$
63
}
64     }
65
66     /**
67      * Prompt for any mappings that match the given filter in order to allow the
68      * user to explicitly include/exclude those mappings.
69      * @return the mappings that either didn't match the filter or were selected by the user
70      */

71     public ResourceMapping[] promptToSelectMappings() {
72         ResourceMapping[] matchingMappings = getMappings();
73         if (matchingMappings.length > 0) {
74             int code = open();
75             if (code == OK) {
76                 Set result = new HashSet();
77                 result.addAll(Arrays.asList(allMappings));
78                 result.removeAll(Arrays.asList(matchingMappings));
79                 result.addAll(Arrays.asList(getCheckedMappings()));
80                 return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
81             }
82             return new ResourceMapping[0];
83         } else {
84             // No mappings match the filter so return them all
85
return allMappings;
86         }
87     }
88
89     private static ResourceMapping[] getMatchingMappings(ResourceMapping[] mappings, final Subscriber subscriber, final FastSyncInfoFilter resourceFilter, IProgressMonitor monitor) {
90         Set result = new HashSet();
91         for (int i = 0; i < mappings.length; i++) {
92             ResourceMapping mapping = mappings[i];
93             if (matchesFilter(mapping, subscriber, resourceFilter, monitor)) {
94                 result.add(mapping);
95             }
96         }
97         return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
98     }
99
100     private static boolean matchesFilter(ResourceMapping mapping, final Subscriber subscriber, final FastSyncInfoFilter resourceFilter, IProgressMonitor monitor) {
101         try {
102             mapping.accept(ResourceMappingContext.LOCAL_CONTEXT, new IResourceVisitor() {
103                 public boolean visit(IResource resource) throws CoreException {
104                     SyncInfo info = subscriber.getSyncInfo(resource);
105                     if (info != null && resourceFilter.select(info)) {
106                         throw new CoreException(Status.OK_STATUS);
107                     }
108                     return true;
109                 }
110             }, monitor);
111         } catch (CoreException e) {
112             if (e.getStatus().isOK()) {
113                 return true;
114             }
115             CVSUIPlugin.log(e);
116         }
117         return false;
118     }
119
120
121     public ResourceMapping[] getAllMappings() {
122         return allMappings;
123     }
124     
125     protected boolean includeCancelButton() {
126         if (super.includeCancelButton()) {
127             return getAllMappings().length > 1;
128         }
129         return false;
130     }
131     
132 }
133
Popular Tags