KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.team.internal.ccvs.core.*;
27 import org.eclipse.team.internal.ccvs.core.client.*;
28 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
29 import org.eclipse.team.internal.ccvs.core.client.Command.QuietOption;
30 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
31 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
32 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
33 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
34 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
35 import org.eclipse.team.internal.ccvs.ui.*;
36 import org.eclipse.team.internal.ccvs.ui.Policy;
37 import org.eclipse.team.internal.ccvs.ui.wizards.RestoreFromRepositoryWizard;
38
39 /**
40  * @author Administrator
41  *
42  * To change this generated comment edit the template variable "typecomment":
43  * Window>Preferences>Java>Templates.
44  * To enable and disable the creation of type comments go to
45  * Window>Preferences>Java>Code Generation.
46  */

47 public class RestoreFromRepositoryAction extends WorkspaceTraversalAction {
48
49     /*
50      * This class handles the output from "cvs log -R ..." where -R
51      * indicates that only the RCS file name is to be returned. Files
52      * that have been deleted will be in the Attic. The Attic may also
53      * contains files that exist on a branch but not in HEAD
54      */

55     class AtticLogListener extends CommandOutputListener {
56         private static final String JavaDoc RCS_FILE_POSTFIX = ",v"; //$NON-NLS-1$
57
private static final String JavaDoc LOGGING_PREFIX = "Logging "; //$NON-NLS-1$
58
ICVSFolder currentFolder;
59         List JavaDoc atticFiles = new ArrayList JavaDoc();
60         
61         public IStatus messageLine(
62                     String JavaDoc line,
63                     ICVSRepositoryLocation location,
64                     ICVSFolder commandRoot,
65                     IProgressMonitor monitor) {
66             
67             // Extract the file name and path from the RCS path
68
// String filePath = line.substring(index);
69
// Find all RCS file names that contain "Attic"
70
int start = line.lastIndexOf(Session.SERVER_SEPARATOR);
71             if (start != -1) {
72                 String JavaDoc fileName = line.substring(start + 1);
73                 if (fileName.endsWith(RCS_FILE_POSTFIX)) {
74                     fileName = fileName.substring(0, fileName.length() - RCS_FILE_POSTFIX.length());
75                 }
76                 if (currentFolder != null) {
77                     try {
78                         ICVSFile file = currentFolder.getFile(fileName);
79                         if (!file.exists())
80                             atticFiles.add(file);
81                     } catch (CVSException e) {
82                         return e.getStatus();
83                     }
84                 }
85             }
86             return OK;
87         }
88         
89         public IStatus errorLine(
90             String JavaDoc line,
91             ICVSRepositoryLocation location,
92             ICVSFolder commandRoot,
93             IProgressMonitor monitor) {
94             
95             CVSRepositoryLocation repo = (CVSRepositoryLocation)location;
96             String JavaDoc folderPath = repo.getServerMessageWithoutPrefix(line, SERVER_PREFIX);
97             if (folderPath != null) {
98                 if (folderPath.startsWith(LOGGING_PREFIX)) {
99                     folderPath = folderPath.substring(LOGGING_PREFIX.length());
100                     try {
101                         currentFolder = commandRoot.getFolder(folderPath);
102                     } catch (CVSException e) {
103                         return e.getStatus();
104                     }
105                     return OK;
106                 }
107             }
108             return super.errorLine(line, location, commandRoot, monitor);
109         }
110
111         public ICVSFile[] getAtticFilePaths() {
112             return (ICVSFile[]) atticFiles.toArray(new ICVSFile[atticFiles.size()]);
113         }
114     }
115     
116     /**
117      * @see org.eclipse.team.internal.ccvs.ui.actions.CVSAction#execute(org.eclipse.jface.action.IAction)
118      */

119     protected void execute(IAction action) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
120         IContainer resource = (IContainer)getSelectedResources()[0];
121         ICVSFile[] files = fetchDeletedFiles(resource);
122         if (files == null) return;
123         if (files.length == 0) {
124             MessageDialog.openInformation(getShell(), CVSUIMessages.RestoreFromRepositoryAction_noFilesTitle, NLS.bind(CVSUIMessages.RestoreFromRepositoryAction_noFilesMessage, new String JavaDoc[] { resource.getName() })); //
125
return;
126         }
127         RestoreFromRepositoryWizard wizard = new RestoreFromRepositoryWizard(resource, files);
128         WizardDialog dialog = new ResizableWizardDialog(getShell(), wizard);
129         dialog.setMinimumPageSize(350, 250);
130         dialog.open();
131     }
132
133     /**
134      * @see org.eclipse.team.internal.ui.actions.TeamAction#isEnabled()
135      */

136     public boolean isEnabled() {
137         IResource[] resources = getSelectedResources();
138         if (resources.length != 1) return false;
139         if (resources[0].getType() == IResource.FILE) return false;
140         ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor((IContainer)resources[0]);
141         try {
142             if (!folder.isCVSFolder()) return false;
143         } catch (CVSException e) {
144             return isEnabledForException(e);
145         }
146         return true;
147     }
148     
149     private ICVSFile[] fetchDeletedFiles(final IContainer parent) {
150         final ICVSFile[][] files = new ICVSFile[1][0];
151         files[0] = null;
152         try {
153             run(new IRunnableWithProgress() {
154                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
155                     try {
156                         ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(parent);
157                         FolderSyncInfo info = folder.getFolderSyncInfo();
158                         ICVSRepositoryLocation location = KnownRepositories.getInstance().getRepository(info.getRoot());
159                         files[0] = fetchFilesInAttic(location, folder, monitor);
160                     } catch (CVSException e) {
161                         throw new InvocationTargetException JavaDoc(e);
162                     }
163                 }
164             }, true, PROGRESS_DIALOG);
165         } catch (InvocationTargetException JavaDoc e) {
166             handle(e);
167         } catch (InterruptedException JavaDoc e) {
168             return null;
169         }
170         return files[0];
171     }
172     
173     /*
174      * Fetch the RCS paths (minus the Attic segment) of all files in the Attic.
175      * This path includes the repository root path.
176      */

177     private ICVSFile[] fetchFilesInAttic(ICVSRepositoryLocation location, ICVSFolder parent, IProgressMonitor monitor) throws CVSException {
178         monitor = Policy.monitorFor(monitor);
179         monitor.beginTask(null, 100);
180         AtticLogListener listener = new AtticLogListener();
181         Session session = new Session(location, parent, true /* output to console */);
182         session.open(Policy.subMonitorFor(monitor, 10), false /* read-only */);
183         try {
184             QuietOption quietness = CVSProviderPlugin.getPlugin().getQuietness();
185             try {
186                 CVSProviderPlugin.getPlugin().setQuietness(Command.VERBOSE);
187                 IStatus status = Command.LOG.execute(
188                     session,
189                     Command.NO_GLOBAL_OPTIONS,
190                     new LocalOption[] { Log.RCS_FILE_NAMES_ONLY },
191                     new ICVSResource[] { parent }, listener,
192                     Policy.subMonitorFor(monitor, 90));
193                 if (status.getCode() == CVSStatus.SERVER_ERROR) {
194                     throw new CVSServerException(status);
195                 }
196             } finally {
197                 CVSProviderPlugin.getPlugin().setQuietness(quietness);
198                 monitor.done();
199             }
200         } finally {
201             session.close();
202         }
203         return listener.getAtticFilePaths();
204     }
205 }
206
Popular Tags