1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.lang.reflect.InvocationTargetException ; 17 18 import org.eclipse.compare.*; 19 import org.eclipse.compare.structuremergeviewer.*; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jface.action.*; 24 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.jface.viewers.*; 27 import org.eclipse.osgi.util.NLS; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.*; 30 import org.eclipse.team.core.TeamException; 31 import org.eclipse.team.internal.ccvs.core.*; 32 import org.eclipse.team.internal.ccvs.core.client.Command; 33 import org.eclipse.team.internal.ccvs.core.client.Update; 34 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 35 import org.eclipse.team.internal.ccvs.ui.actions.CVSAction; 36 import org.eclipse.team.internal.ccvs.ui.operations.UpdateOperation; 37 import org.eclipse.team.internal.ui.Utils; 38 import org.eclipse.team.ui.ISaveableWorkbenchPart; 39 import org.eclipse.ui.*; 40 import org.eclipse.ui.actions.WorkspaceModifyOperation; 41 42 public class CVSCompareRevisionsInput extends CompareEditorInput implements ISaveableWorkbenchPart { 43 IFile resource; 44 ILogEntry[] editions; 45 TableViewer viewer; 46 Action getRevisionAction; 47 Action getContentsAction; 48 Shell shell; 49 50 private HistoryTableProvider historyTableProvider; 52 53 57 class TypedBufferedContent extends ResourceNode { 58 public TypedBufferedContent(IFile resource) { 59 super(resource); 60 } 61 protected InputStream createStream() throws CoreException { 62 return ((IFile)getResource()).getContents(); 63 } 64 public void setContent(byte[] contents) { 65 if (contents == null) contents = new byte[0]; 66 final InputStream is = new ByteArrayInputStream (contents); 67 IRunnableWithProgress runnable = new IRunnableWithProgress() { 68 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 69 try { 70 IFile file = resource; 71 if (is != null) { 72 if (!file.exists()) { 73 file.create(is, false, monitor); 74 } else { 75 file.setContents(is, false, true, monitor); 76 } 77 } else { 78 file.delete(false, true, monitor); 79 } 80 } catch (CoreException e) { 81 throw new InvocationTargetException (e); 82 } 83 } 84 }; 85 try { 86 new ProgressMonitorDialog(shell).run(false, false, runnable); 87 } catch (InvocationTargetException e) { 88 CVSUIPlugin.openError(null, null, null, e); 89 } catch (InterruptedException e) { 90 } 92 fireContentChanged(); 93 } 94 public ITypedElement replace(ITypedElement child, ITypedElement other) { 95 return null; 96 } 97 public void fireChange() { 98 fireContentChanged(); 99 } 100 } 101 102 105 class ResourceRevisionNode extends ResourceEditionNode { 106 ILogEntry entry; 107 public ResourceRevisionNode(ILogEntry entry) { 108 super(entry.getRemoteFile()); 109 this.entry = entry; 110 } 111 public ILogEntry getLogEntry() { 112 return entry; 113 } 114 public String getName() { 115 String revisionName = entry.getRevision(); 116 if (revisionName != null) { 117 IResource resource = CVSCompareRevisionsInput.this.resource; 118 try { 119 ICVSRemoteFile currentEdition = (ICVSRemoteFile) CVSWorkspaceRoot.getRemoteResourceFor(resource); 120 if (currentEdition != null && currentEdition.getRevision().equals(revisionName)) { 121 NLS.bind(CVSUIMessages.currentRevision, new String [] { revisionName }); } else { 123 return revisionName; 124 } 125 } catch (TeamException e) { 126 handle(e); 127 } 128 } 129 return super.getName(); 130 } 131 } 132 133 136 class VersionCompareDiffNode extends DiffNode implements IAdaptable { 137 public VersionCompareDiffNode(ITypedElement left, ITypedElement right) { 138 super(left, right); 139 } 140 public String getName() { 141 return getRight().getName(); 142 } 143 public Object getAdapter(Class adapter) { 144 if (adapter == ILogEntry.class) { 145 return ((ResourceRevisionNode)getRight()).getLogEntry(); 146 } 147 return null; 148 } 149 public void fireContentChanges() { 150 fireChange(); 151 } 152 } 153 156 class VersionCompareContentProvider implements IStructuredContentProvider { 157 public void dispose() { 158 } 159 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 160 } 161 public Object [] getElements(Object inputElement) { 162 if (inputElement instanceof DiffContainer) { 163 return ((DiffContainer)inputElement).getChildren(); 164 } 165 return null; 166 } 167 } 168 169 public CVSCompareRevisionsInput(IFile resource, ILogEntry[] editions) { 170 super(new CompareConfiguration()); 171 this.resource = resource; 172 this.editions = editions; 173 updateCurrentEdition(); 174 initializeActions(); 175 } 176 177 180 public Control createContents(Composite parent) { 181 Control c = super.createContents(parent); 182 c.setLayoutData(new GridData(GridData.FILL_BOTH)); 183 return c; 184 } 185 186 public Viewer createDiffViewer(Composite parent) { 187 this.shell = parent.getShell(); 188 viewer = getHistoryTableProvider().createTable(parent); 189 Table table = viewer.getTable(); 190 table.setData(CompareUI.COMPARE_VIEWER_TITLE, getTitle()); 192 viewer.setContentProvider(new VersionCompareContentProvider()); 193 194 MenuManager mm = new MenuManager(); 195 mm.setRemoveAllWhenShown(true); 196 mm.addMenuListener( 197 new IMenuListener() { 198 public void menuAboutToShow(IMenuManager mm) { 199 mm.add(getContentsAction); 200 mm.add(getRevisionAction); 201 } 202 } 203 ); 204 table.setMenu(mm.createContextMenu(table)); 205 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 206 public void selectionChanged(SelectionChangedEvent event) { 207 ISelection selection = event.getSelection(); 208 if (!(selection instanceof IStructuredSelection)) { 209 getRevisionAction.setEnabled(false); 210 getContentsAction.setEnabled(false); 211 return; 212 } 213 IStructuredSelection ss = (IStructuredSelection)selection; 214 getRevisionAction.setEnabled(ss.size() == 1); 215 getContentsAction.setEnabled(ss.size() == 1); 216 } 217 }); 218 219 PlatformUI.getWorkbench().getHelpSystem().setHelp(table, IHelpContextIds.COMPARE_REVISIONS_VIEW); 221 return viewer; 222 } 223 224 private void initLabels() { 225 CompareConfiguration cc = getCompareConfiguration(); 226 cc.setLeftEditable(true); 227 cc.setRightEditable(false); 228 String resourceName = resource.getName(); 229 String leftLabel = NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_workspace, (new Object [] {resourceName})); cc.setLeftLabel(leftLabel); 231 String rightLabel = NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_repository, (new Object [] {resourceName})); cc.setRightLabel(rightLabel); 233 } 234 private void initializeActions() { 235 getRevisionAction = new Action(CVSUIMessages.HistoryView_getRevisionAction) { public void run() { 237 try { 238 final IStructuredSelection selection = (IStructuredSelection)viewer.getSelection(); 239 new ProgressMonitorDialog(shell).run(true, true, new WorkspaceModifyOperation(null) { 240 protected void execute(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 241 if (selection.size() != 1) return; 242 VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement(); 243 ResourceEditionNode right = (ResourceEditionNode)node.getRight(); 244 ICVSRemoteResource edition = right.getRemoteResource(); 245 try { 248 CVSTag revisionTag = new CVSTag(((ICVSRemoteFile)edition).getRevision(), CVSTag.VERSION); 249 if(CVSAction.checkForMixingTags(shell, new IResource[] {resource}, revisionTag)) { 250 new UpdateOperation( 251 null, 252 new IResource[] {resource}, 253 new Command.LocalOption[] {Update.IGNORE_LOCAL_CHANGES}, 254 revisionTag) 255 .run(monitor); 256 getHistoryTableProvider().setFile((ICVSFile)edition); 257 } 258 } catch (TeamException e) { 259 throw new InvocationTargetException (e); 260 } 261 } 262 }); 263 } catch (InterruptedException e) { 264 return; 266 } catch (InvocationTargetException e) { 267 handle(e); 268 } 269 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection(); 271 if (selection.size() != 1) return; 272 VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement(); 273 TypedBufferedContent left = (TypedBufferedContent)node.getLeft(); 274 left.fireChange(); 275 Display.getCurrent().syncExec(new Runnable () { 277 public void run() { 278 viewer.refresh(); 279 } 280 }); 281 } 282 }; 283 getContentsAction = new Action(CVSUIMessages.HistoryView_getContentsAction) { public void run() { 285 try { 286 replaceLocalWithCurrentlySelectedRevision(); 287 } catch (CoreException e) { 288 Utils.handle(e); 289 } 290 } 291 }; 292 } 293 protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 294 initLabels(); 295 DiffNode diffRoot = new DiffNode(Differencer.NO_CHANGE); 296 ITypedElement left = new TypedBufferedContent(resource); 297 for (int i = 0; i < editions.length; i++) { 298 ITypedElement right = new ResourceRevisionNode(editions[i]); 299 diffRoot.add(new VersionCompareDiffNode(left, right)); 300 } 301 return diffRoot; 302 } 303 private void updateCurrentEdition() { 304 try { 305 getHistoryTableProvider().setFile((ICVSFile) CVSWorkspaceRoot.getRemoteResourceFor(resource)); 306 } catch (TeamException e) { 307 handle(e); 308 } 309 } 310 private void handle(Exception e) { 311 setMessage(CVSUIPlugin.openError(shell, null, null, e, CVSUIPlugin.LOG_NONTEAM_EXCEPTIONS).getMessage()); 312 } 313 317 public HistoryTableProvider getHistoryTableProvider() { 318 if (historyTableProvider == null) { 319 historyTableProvider = new HistoryTableProvider(); 320 } 321 return historyTableProvider; 322 } 323 324 325 328 public void saveChanges(IProgressMonitor pm) throws CoreException { 329 super.saveChanges(pm); 330 } 331 332 public void replaceLocalWithCurrentlySelectedRevision() throws CoreException { 333 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection(); 334 if (selection.size() != 1) return; 335 VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement(); 336 ResourceRevisionNode right = (ResourceRevisionNode)node.getRight(); 337 TypedBufferedContent left = (TypedBufferedContent)node.getLeft(); 338 left.setContent(Utils.readBytes(right.getContents())); 339 } 340 341 public Viewer getViewer() { 342 return viewer; 343 } 344 345 346 349 public String getTitle() { 350 return NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_compareResourceAndVersions, (new Object [] {resource.getFullPath().toString()})); } 352 353 356 public void doSave(IProgressMonitor monitor) { 357 try { 358 saveChanges(monitor); 359 } catch (CoreException e) { 360 Utils.handle(e); 361 } 362 } 363 364 367 public void doSaveAs() { 368 } 370 371 374 public boolean isDirty() { 375 return isSaveNeeded(); 376 } 377 378 381 public boolean isSaveAsAllowed() { 382 return true; 383 } 384 385 388 public boolean isSaveOnCloseNeeded() { 389 return true; 390 } 391 392 395 public void addPropertyListener(IPropertyListener listener) { 396 } 398 399 402 public void createPartControl(Composite parent) { 403 createContents(parent); 404 } 405 406 409 public void dispose() { 410 } 411 412 415 public IWorkbenchPartSite getSite() { 416 return null; 417 } 418 419 422 public String getTitleToolTip() { 423 return null; 424 } 425 426 429 public void removePropertyListener(IPropertyListener listener) { 430 } 432 } 433 | Popular Tags |