KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > FetchMembersOperation


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.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder;
21 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderMemberFetcher;
22 import org.eclipse.team.internal.ccvs.ui.*;
23 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
24 import org.eclipse.team.internal.ccvs.ui.Policy;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.progress.IElementCollector;
27
28 /**
29  * Fetch the members of a remote folder in the background, passing incremental
30  * results through an IElementCollector.
31  */

32 public class FetchMembersOperation extends RemoteOperation {
33
34     /* internal uzse only */ IElementCollector collector;
35     /* internal uzse only */ RemoteFolderFilter filter = new RemoteFolderFilter();
36     
37     public static class RemoteFolderFilter {
38         public ICVSRemoteResource[] filter(ICVSRemoteResource[] resource) {
39             return resource;
40         }
41     }
42     
43     public class InternalRemoteFolderMemberFetcher extends RemoteFolderMemberFetcher {
44         long sendIncrement = 100;
45         List JavaDoc unsent = new ArrayList JavaDoc();
46         long intervalStart;
47         protected InternalRemoteFolderMemberFetcher(RemoteFolder parentFolder, CVSTag tag) {
48             super(parentFolder, tag);
49         }
50         protected void parentDoesNotExist() {
51             super.parentDoesNotExist();
52             // Indicate that there are no children
53
collector.add(new Object JavaDoc[0], getProgressMonitor());
54         }
55         protected RemoteFolder recordFolder(String JavaDoc name) {
56             RemoteFolder folder = super.recordFolder(name);
57             unsent.add(folder);
58             if (isTimeToSend()) {
59                 sendFolders();
60             }
61             return folder;
62         }
63         private boolean isTimeToSend() {
64             long currentTime = System.currentTimeMillis();
65             return ((currentTime - intervalStart) > sendIncrement) || unsent.size() > sendIncrement;
66         }
67         protected IStatus performUpdate(IProgressMonitor progress, CVSTag tag) throws CVSException {
68             intervalStart = System.currentTimeMillis();
69             IStatus status = super.performUpdate(progress, tag);
70             sendFolders();
71             return status;
72         }
73         protected void updateFileRevisions(ICVSFile[] files, IProgressMonitor monitor) throws CVSException {
74             super.updateFileRevisions(files, monitor);
75             sendFiles();
76         }
77         private void sendFolders() {
78             updateParentFolderChildren();
79             collector.add(filter.filter((ICVSRemoteFolder[]) unsent.toArray(new ICVSRemoteFolder[unsent.size()])), getProgressMonitor());
80             unsent.clear();
81             intervalStart = System.currentTimeMillis();
82         }
83         private void sendFiles() {
84             collector.add(getFiles(), getProgressMonitor());
85             unsent.clear();
86         }
87         private IProgressMonitor getProgressMonitor() {
88             return null;
89         }
90     }
91
92     public FetchMembersOperation(IWorkbenchPart part, ICVSRemoteFolder folder, IElementCollector collector) {
93         super(part, new ICVSRemoteResource[] { folder });
94         this.collector = collector;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
99      */

100     protected void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
101         ICVSRemoteFolder remote = getRemoteFolder();
102         if (remote.getClass().equals(RemoteFolder.class)) {
103             monitor = Policy.monitorFor(monitor);
104             boolean isRoot = remote.getName().equals(ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME);
105             monitor.beginTask(null, 100 + (isRoot ? 30 : 0));
106             RemoteFolderMemberFetcher fetcher = new InternalRemoteFolderMemberFetcher((RemoteFolder)remote, remote.getTag());
107             fetcher.fetchMembers(Policy.subMonitorFor(monitor, 100));
108             if (isRoot) {
109                 ICVSRemoteResource[] modules = CVSUIPlugin.getPlugin()
110                     .getRepositoryManager()
111                     .getRepositoryRootFor(remote.getRepository())
112                     .getDefinedModules(remote.getTag(), Policy.subMonitorFor(monitor, 25));
113                 collector.add(filter.filter(modules), Policy.subMonitorFor(monitor, 5));
114             }
115         } else {
116             monitor = Policy.monitorFor(monitor);
117             try {
118                 monitor.beginTask(null, 100);
119                 ICVSRemoteResource[] children = remote.members(Policy.subMonitorFor(monitor, 95));
120                 collector.add(children, Policy.subMonitorFor(monitor, 5));
121             } catch (TeamException e) {
122                 throw CVSException.wrapException(e);
123             } finally {
124                 monitor.done();
125             }
126         }
127
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
132      */

133     protected String JavaDoc getTaskName() {
134         return NLS.bind(CVSUIMessages.FetchMembersOperation_0, new String JavaDoc[] { getRemoteFolder().getName() });
135     }
136
137     private ICVSRemoteFolder getRemoteFolder() {
138         return (ICVSRemoteFolder)getRemoteResources()[0];
139     }
140
141     public void setFilter(RemoteFolderFilter filter) {
142         this.filter = filter;
143     }
144
145 }
146
Popular Tags