1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 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 47 public class RestoreFromRepositoryAction extends WorkspaceTraversalAction { 48 49 55 class AtticLogListener extends CommandOutputListener { 56 private static final String RCS_FILE_POSTFIX = ",v"; private static final String LOGGING_PREFIX = "Logging "; ICVSFolder currentFolder; 59 List atticFiles = new ArrayList (); 60 61 public IStatus messageLine( 62 String line, 63 ICVSRepositoryLocation location, 64 ICVSFolder commandRoot, 65 IProgressMonitor monitor) { 66 67 int start = line.lastIndexOf(Session.SERVER_SEPARATOR); 71 if (start != -1) { 72 String 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 line, 91 ICVSRepositoryLocation location, 92 ICVSFolder commandRoot, 93 IProgressMonitor monitor) { 94 95 CVSRepositoryLocation repo = (CVSRepositoryLocation)location; 96 String 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 119 protected void execute(IAction action) throws InvocationTargetException , InterruptedException { 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 [] { resource.getName() })); 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 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 , InterruptedException { 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 (e); 162 } 163 } 164 }, true, PROGRESS_DIALOG); 165 } catch (InvocationTargetException e) { 166 handle(e); 167 } catch (InterruptedException e) { 168 return null; 169 } 170 return files[0]; 171 } 172 173 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 ); 182 session.open(Policy.subMonitorFor(monitor, 10), false ); 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 |