1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 import java.io.BufferedInputStream ; 14 import java.io.InputStream ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.compare.*; 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.operation.IRunnableWithProgress; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.viewers.*; 27 import org.eclipse.osgi.util.NLS; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.SelectionAdapter; 30 import org.eclipse.swt.events.SelectionEvent; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Widget; 35 import org.eclipse.team.core.TeamException; 36 import org.eclipse.team.core.variants.IResourceVariant; 37 import org.eclipse.team.internal.ccvs.core.*; 38 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 39 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 40 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 41 import org.eclipse.team.internal.ccvs.ui.*; 42 import org.eclipse.team.internal.ccvs.ui.Policy; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.model.WorkbenchLabelProvider; 45 import org.eclipse.ui.views.navigator.ResourceSorter; 46 47 50 public class RestoreFromRepositoryFileSelectionPage extends CVSWizardPage { 51 private TreeViewer fileTree; 52 private CompareViewerPane fileSelectionPane; 53 private CompareViewerPane revisionSelectionPane; 54 private CheckboxTableViewer revisionsTable; 55 private CompareViewerSwitchingPane fileContentPane; 56 57 private HistoryTableProvider historyTableProvider; 58 private AdaptableHierarchicalResourceList treeInput = new AdaptableHierarchicalResourceList(ResourcesPlugin.getWorkspace().getRoot(), new IResource[0]); 59 60 private IContainer folder; 61 private IFile selectedFile; 62 private ILogEntry selectedRevision; 63 private Map entriesCache = new HashMap (); 64 private Map filesToRestore = new HashMap (); 65 66 private static final int WIZARD_WIDTH = 550; 67 68 class HistoryInput implements ITypedElement, IEncodedStreamContentAccessor, IModificationDate { 69 IFile file; 70 ILogEntry logEntry; 71 72 HistoryInput(IFile file, ILogEntry logEntry) { 73 this.file= file; 74 this.logEntry = logEntry; 75 } 76 public InputStream getContents() throws CoreException { 77 IStorage s = getStorageFromLogEntry(logEntry); 78 if (s == null) return null; 79 return new BufferedInputStream (s.getContents()); 80 } 81 public String getName() { 82 return file.getName(); 83 } 84 public String getType() { 85 return file.getFileExtension(); 86 } 87 public Image getImage() { 88 return CompareUI.getImage(file); 89 } 90 public long getModificationDate() { 91 return logEntry.getDate().getTime(); 92 } 93 public String getCharset() throws CoreException { 94 IStorage s = getStorageFromLogEntry(logEntry); 95 if (s instanceof IEncodedStorage) { 96 return ((IEncodedStorage)s).getCharset(); 97 } 98 return null; 99 } 100 } 101 102 109 public RestoreFromRepositoryFileSelectionPage( 110 String pageName, 111 String title, 112 ImageDescriptor titleImage, 113 String description) { 114 super(pageName, title, titleImage, description); 115 } 116 117 120 public void createControl(Composite parent) { 121 Composite composite= createComposite(parent, 1, false); 122 setControl(composite); 123 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.RESTORE_FROM_REPOSITORY_FILE_SELECTION_PAGE); 125 126 Splitter vsplitter= new Splitter(composite, SWT.VERTICAL); 128 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL 129 | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL); 130 data.widthHint = WIZARD_WIDTH; 132 vsplitter.setLayoutData(data); 133 134 Splitter hsplitter= new Splitter(vsplitter, SWT.HORIZONTAL); 136 137 fileSelectionPane = new CompareViewerPane(hsplitter, SWT.BORDER | SWT.FLAT); 139 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL); 140 fileSelectionPane.setLayoutData(data); 141 fileTree = createFileSelectionTree(fileSelectionPane); 142 143 revisionSelectionPane = new CompareViewerPane(hsplitter, SWT.BORDER | SWT.FLAT); 145 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL); 146 revisionSelectionPane.setLayoutData(data); 147 historyTableProvider = new HistoryTableProvider(); 148 revisionsTable = createRevisionSelectionTable(revisionSelectionPane, historyTableProvider); 149 revisionSelectionPane.setText(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_emptyRevisionPane); 150 151 fileContentPane = new CompareViewerSwitchingPane(vsplitter, SWT.BORDER | SWT.FLAT) { 153 protected Viewer getViewer(Viewer oldViewer, Object input) { 154 return CompareUI.findContentViewer(oldViewer, input, this, null); 155 } 156 }; 157 158 initializeValues(); 159 updateWidgetEnablements(); 160 Dialog.applyDialogFont(parent); 161 } 162 163 protected CheckboxTableViewer createRevisionSelectionTable(CompareViewerPane composite, HistoryTableProvider tableProvider) { 164 CheckboxTableViewer table = tableProvider.createCheckBoxTable(composite); 165 table.setContentProvider(new IStructuredContentProvider() { 166 public Object [] getElements(Object inputElement) { 167 ILogEntry[] entries = getSelectedEntries(); 168 if (entries != null) return entries; 169 return new Object [0]; 170 } 171 public void dispose() { 172 } 173 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 174 } 175 }); 176 table.setInput(this); 177 table.getTable().addSelectionListener( 178 new SelectionAdapter() { 179 public void widgetSelected(SelectionEvent e) { 180 if (e.detail == SWT.CHECK) return; 182 handleRevisionSelection(e.item); 183 } 184 } 185 ); 186 table.addCheckStateListener(new ICheckStateListener() { 187 public void checkStateChanged(CheckStateChangedEvent event) { 188 handleRevisionChecked(event); 189 } 190 }); 191 composite.setContent(table.getControl()); 192 return table; 193 } 194 195 protected TreeViewer createFileSelectionTree(CompareViewerPane composite) { 196 TreeViewer tree = new TreeViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 197 tree.setUseHashlookup(true); 198 tree.setContentProvider(treeInput.getTreeContentProvider()); 199 tree.setLabelProvider( 200 new DecoratingLabelProvider( 201 new WorkbenchLabelProvider() { 202 protected String decorateText(String input, Object element) { 203 String text; 204 if (element instanceof IFolder && element.equals(folder)) { 205 text = super.decorateText(folder.getProjectRelativePath().toString(), element); 206 } else { 207 ILogEntry entry = (ILogEntry)filesToRestore.get(element); 208 text = super.decorateText(input, element); 209 if (entry != null) { 210 text = NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_fileToRestore, new String [] { text, entry.getRevision() }); 211 } 212 } 213 return text; 214 } 215 }, 216 CVSUIPlugin.getPlugin().getWorkbench().getDecoratorManager().getLabelDecorator())); 217 tree.setSorter(new ResourceSorter(ResourceSorter.NAME)); 218 tree.setInput(treeInput); 219 220 GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL); 221 tree.getTree().setLayoutData(data); 222 tree.addPostSelectionChangedListener(new ISelectionChangedListener() { 223 public void selectionChanged(SelectionChangedEvent event) { 224 handleFileSelection(event); 225 } 226 }); 227 composite.setContent(tree.getControl()); 228 return tree; 229 } 230 231 234 private void updateWidgetEnablements() { 235 236 if (filesToRestore.isEmpty()) { 237 setPageComplete(false); 238 setErrorMessage(null); 239 return; 240 } 241 242 for (Iterator iter = filesToRestore.keySet().iterator(); iter.hasNext();) { 243 IFile file = (IFile) iter.next(); 244 if (file.exists()) { 245 setPageComplete(false); 246 setErrorMessage(NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_fileExists, new String [] { file.getName() })); 247 return; 248 } 249 250 ILogEntry entry = (ILogEntry) filesToRestore.get(file); 251 if (entry.isDeletion()) { 252 setPageComplete(false); 253 setErrorMessage(NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_revisionIsDeletion, new String [] { entry.getRevision(), file.getName() })); 254 return; 255 } 256 } 257 setPageComplete(true); 258 setErrorMessage(null); 259 } 260 261 264 private void initializeValues() { 265 refresh(); 266 } 267 268 272 public void setInput(IContainer folder, ICVSFile[] files) { 273 if (folder.equals(this.folder)) return; 274 this.folder = folder; 275 setTreeInput(folder, files); 276 initializeValues(); 277 updateWidgetEnablements(); 278 } 279 280 283 private void setTreeInput(IContainer folder, ICVSFile[] cvsFiles) { 284 reset(); 285 IResource[] files = new IResource[cvsFiles.length]; 286 for (int i = 0; i < cvsFiles.length; i++) { 287 files[i] = cvsFiles[i].getIResource(); 288 } 289 treeInput.setResources(files); 290 treeInput.setRoot(folder.getParent()); 293 refresh(); 294 } 295 296 private void reset() { 297 this.selectedFile = null; 298 this.selectedRevision = null; 299 treeInput.setResources(null); 300 filesToRestore = new HashMap (); 301 if (fileContentPane != null && !fileContentPane.isDisposed()) { 302 fileContentPane.setInput(null); 303 } 304 updateWidgetEnablements(); 305 } 306 307 310 private void refresh() { 311 if (folder == null) return; 312 313 if (fileSelectionPane != null && !fileSelectionPane.isDisposed()) { 314 fileSelectionPane.setText(NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_fileSelectionPaneTitle, new String [] { folder.getProject().getName() })); 315 fileSelectionPane.setImage(CompareUI.getImage(folder.getProject())); 316 } 317 318 if (revisionSelectionPane != null && !revisionSelectionPane.isDisposed()) { 319 if (selectedFile == null) { 320 revisionSelectionPane.setText(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_emptyRevisionPane); 321 revisionSelectionPane.setImage(null); 322 } 323 } 324 325 if (fileContentPane != null && !fileContentPane.isDisposed()) { 327 fileContentPane.setInput(null); 328 } 329 330 if (fileTree != null) { 332 fileTree.setExpandedState(folder, true); 334 fileTree.refresh(); 335 } 336 if (revisionsTable != null) 337 revisionsTable.refresh(); 338 } 339 340 343 private void setLogEntryTableInput(ILogEntry[] entries) { 344 this.selectedRevision = null; 345 revisionsTable.refresh(); 347 ILogEntry selectedEntry = (ILogEntry)filesToRestore.get(selectedFile); 349 if (selectedEntry != null) { 350 revisionsTable.setChecked(selectedEntry, true); 351 } 352 for (int i = 0; i < entries.length; i++) { 354 ILogEntry entry = entries[i]; 355 if (entry.isDeletion()) { 356 revisionsTable.setGrayed(entry, true); 357 } 358 } 359 revisionSelectionPane.setText(NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_revisionSelectionPaneTitle, new String [] { selectedFile.getName() })); 361 revisionSelectionPane.setImage(CompareUI.getImage(selectedFile)); 362 fileContentPane.setInput(null); 364 } 365 366 private void handleFileSelection(SelectionChangedEvent event) { 367 ISelection selection = event.getSelection(); 368 if (selection == null || selection.isEmpty()) { 369 clearSelection(); 370 } else { 371 if (selection instanceof IStructuredSelection) { 372 IStructuredSelection structuredSelection = (IStructuredSelection) selection; 373 IResource resource = (IResource)structuredSelection.getFirstElement(); 374 if (resource instanceof IFile) { 375 handleFileSelection((IFile) resource); 376 } else { 377 clearSelection(); 378 } 379 } 380 } 381 } 382 383 387 private void handleFileSelection(IFile file) { 388 if (this.selectedFile == file) return; 389 this.selectedFile = file; 390 if (entriesCache.get(file) == null) { 391 try { 392 393 ICVSFolder parent = CVSWorkspaceRoot.getCVSFolderFor(file.getParent()); 395 FolderSyncInfo info = parent.getFolderSyncInfo(); 396 ICVSRepositoryLocation location = KnownRepositories.getInstance().getRepository(info.getRoot()); 397 final ICVSRemoteFile remoteFile = location.getRemoteFile(new Path(null, info.getRepository()).append(file.getName()).toString(), CVSTag.DEFAULT); 398 399 getContainer().run(true, true, new IRunnableWithProgress() { 401 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 402 try { 403 ILogEntry[] entries = remoteFile.getLogEntries(monitor); 405 entriesCache.put(selectedFile, entries); 407 } catch (TeamException e) { 408 throw new InvocationTargetException (e); 409 } 410 } 411 }); 412 } catch (CVSException e) { 413 setErrorMessage( 414 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC) 415 .getMessage()); 416 return; 417 } catch (InvocationTargetException e) { 418 setErrorMessage( 419 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC) 420 .getMessage()); 421 return; 422 } catch (InterruptedException e) { 423 return; 424 } 425 } 426 427 setLogEntryTableInput(getSelectedEntries()); 429 } 430 431 private ILogEntry[] getSelectedEntries() { 432 return (ILogEntry[])entriesCache.get(selectedFile); 433 } 434 435 private IStorage getStorageFromLogEntry(final ILogEntry logEntry) { 436 final IStorage[] s = new IStorage[] { null }; 437 try { 438 getContainer().run(true, true, new IRunnableWithProgress() { 439 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 440 try { 441 ICVSRemoteFile remoteFile = logEntry.getRemoteFile(); 442 s[0] = ((IResourceVariant)remoteFile).getStorage(monitor); 443 } catch (TeamException e) { 444 throw new InvocationTargetException (e); 445 } 446 } 447 }); 448 } catch (InvocationTargetException e) { 449 setErrorMessage( 450 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC) 451 .getMessage()); 452 return null; 453 } catch (InterruptedException e) { 454 return null; 455 } 456 return s[0]; 457 } 458 459 private void handleRevisionChecked(CheckStateChangedEvent event) { 460 if (event.getChecked()) { 461 revisionsTable.setCheckedElements(new Object [] {event.getElement()}); 462 filesToRestore.put(selectedFile, event.getElement()); 463 } 464 if (revisionsTable.getCheckedElements().length == 0) { 465 filesToRestore.remove(selectedFile); 466 } 467 fileTree.refresh(); 468 updateWidgetEnablements(); 469 } 470 471 475 private void handleRevisionSelection(Widget w) { 476 if (fileContentPane != null && !fileContentPane.isDisposed()) { 477 Object o= w.getData(); 478 if (o instanceof ILogEntry) { 479 ILogEntry selected = (ILogEntry) o; 480 if (this.selectedRevision == selected) return; 481 this.selectedRevision = selected; 482 if (selected.isDeletion()) { 483 fileContentPane.setInput(null); 484 } else { 485 fileContentPane.setInput(new HistoryInput(selectedFile, selected)); 486 fileContentPane.setText(getEditionLabel(selectedFile, selected)); 487 fileContentPane.setImage(CompareUI.getImage(selectedFile)); 488 } 489 } else { 490 fileContentPane.setInput(null); 491 } 492 } 493 } 494 500 private String getEditionLabel(IFile selectedFile, ILogEntry selected) { 501 return NLS.bind(CVSUIMessages.RestoreFromRepositoryFileSelectionPage_fileContentPaneTitle, (new Object [] { selectedFile.getName(), selected.getRevision(), selectedFile.getFullPath().makeRelative().removeLastSegments(1).toString() })); 502 } 503 504 public boolean restoreSelectedFiles() { 505 try { 506 getContainer().run(true, true, new IRunnableWithProgress() { 507 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 508 try { 509 monitor.beginTask(null, 100 * filesToRestore.size()); 510 for (Iterator iter = filesToRestore.keySet().iterator();iter.hasNext();) { 511 IFile file = (IFile) iter.next(); 512 ILogEntry entry = (ILogEntry)filesToRestore.get(file); 513 ensureParentExists(file); 514 file.create(entry.getRemoteFile().getContents(Policy.subMonitorFor(monitor, 50)), false, Policy.subMonitorFor(monitor, 50)); 515 } 516 } catch (TeamException e) { 517 throw new InvocationTargetException (e); 518 } catch (CoreException e) { 519 throw new InvocationTargetException (e); 520 } finally { 521 monitor.done(); 522 } 523 } 524 }); 525 } catch (InvocationTargetException e) { 526 setErrorMessage( 527 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC) 528 .getMessage()); 529 return false; 530 } catch (InterruptedException e) { 531 return false; 532 } 533 return true; 534 } 535 536 540 private void ensureParentExists(IResource resource) throws CoreException { 541 IContainer parent = resource.getParent(); 542 if (!parent.exists() && parent.getType() == IResource.FOLDER) { 543 ensureParentExists(parent); 544 ((IFolder)parent).create(false, true, null); 545 } 546 } 547 548 private void clearSelection() { 549 this.selectedFile = null; 550 this.selectedRevision = null; 551 refresh(); 552 } 553 } 554 | Popular Tags |