1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.jface.text.*; 21 import org.eclipse.jface.viewers.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.widgets.*; 26 import org.eclipse.team.internal.ccvs.core.*; 27 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 28 import org.eclipse.team.ui.TeamUI; 29 import org.eclipse.team.ui.history.IHistoryPage; 30 import org.eclipse.team.ui.history.IHistoryView; 31 import org.eclipse.ui.*; 32 import org.eclipse.ui.editors.text.EditorsUI; 33 import org.eclipse.ui.internal.registry.EditorDescriptor; 34 import org.eclipse.ui.part.ViewPart; 35 import org.eclipse.ui.texteditor.IDocumentProvider; 36 import org.eclipse.ui.texteditor.ITextEditor; 37 38 43 public class AnnotateView extends ViewPart implements ISelectionChangedListener { 44 45 ITextEditor editor; 46 IHistoryView historyView; 47 IWorkbenchPage page; 48 49 ListViewer viewer; 50 IDocument document; 51 Collection cvsAnnotateBlocks; 52 ICVSResource cvsResource; 53 InputStream contents; 54 55 IStructuredSelection previousListSelection; 56 ITextSelection previousTextSelection; 57 boolean lastSelectionWasText = false; 58 59 60 public static final String VIEW_ID = "org.eclipse.team.ccvs.ui.AnnotateView"; private Composite top; 62 63 private IPartListener partListener = new IPartListener() { 64 public void partActivated(IWorkbenchPart part) { 65 } 66 public void partBroughtToTop(IWorkbenchPart part) { 67 } 68 public void partClosed(IWorkbenchPart part) { 69 if (editor != null && part == editor) { 70 disconnect(); 71 } 72 } 73 public void partDeactivated(IWorkbenchPart part) { 74 } 75 public void partOpened(IWorkbenchPart part) { 76 } 77 }; 78 79 public AnnotateView() { 80 super(); 81 } 82 83 public void createPartControl(Composite parent) { 84 85 this.top = parent; 86 87 Label label = new Label(top, SWT.WRAP); 89 label.setText(CVSUIMessages.CVSAnnotateView_viewInstructions); 90 top.layout(); 91 PlatformUI.getWorkbench().getHelpSystem().setHelp(label, IHelpContextIds.ANNOTATE_VIEW); 92 } 93 94 101 public void showAnnotations(ICVSResource cvsResource, Collection cvsAnnotateBlocks, InputStream contents) throws PartInitException, CVSException { 102 showAnnotations(cvsResource, cvsAnnotateBlocks, contents, true); 103 } 104 105 113 public void showAnnotations(ICVSResource cvsResource, Collection cvsAnnotateBlocks, InputStream contents, boolean useHistoryView) throws PartInitException, CVSException { 114 115 disconnect(); 117 118 Control[] oldChildren = top.getChildren(); 120 if (oldChildren != null) { 121 for (int i = 0; i < oldChildren.length; i++) { 122 oldChildren[i].dispose(); 123 } 124 } 125 126 viewer = new ListViewer(top, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL); 127 viewer.setContentProvider(new ArrayContentProvider()); 128 viewer.setLabelProvider(new LabelProvider()); 129 viewer.addSelectionChangedListener(this); 130 viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH)); 131 132 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), IHelpContextIds.ANNOTATE_VIEW); 133 134 top.layout(); 135 136 this.cvsResource = cvsResource; 137 this.contents = contents; 138 this.cvsAnnotateBlocks = cvsAnnotateBlocks; 139 page = CVSUIPlugin.getActivePage(); 140 viewer.setInput(cvsAnnotateBlocks); 141 editor = (ITextEditor) openEditor(); 142 IDocumentProvider provider = editor.getDocumentProvider(); 143 document = provider.getDocument(editor.getEditorInput()); 144 145 setPartName(NLS.bind(CVSUIMessages.CVSAnnotateView_showFileAnnotation, (new Object [] {cvsResource.getName()}))); 146 IResource localResource = cvsResource.getIResource(); 147 if (localResource != null) { 148 setTitleToolTip(localResource.getFullPath().toString()); 149 } else { 150 setTitleToolTip(cvsResource.getName()); 151 } 152 153 if (!useHistoryView) { 154 return; 155 } 156 157 historyView = TeamUI.showHistoryFor(page, CVSWorkspaceRoot.getRemoteResourceFor(cvsResource), null); 159 } 160 161 protected void disconnect() { 162 if(editor != null) { 163 if (editor.getSelectionProvider() instanceof IPostSelectionProvider) { 164 ((IPostSelectionProvider) editor.getSelectionProvider()).removePostSelectionChangedListener(this); 165 } 166 editor.getSite().getPage().removePartListener(partListener); 167 editor = null; 168 document = null; 169 } 170 } 171 172 177 public static AnnotateView openInActivePerspective() throws PartInitException { 178 return (AnnotateView) CVSUIPlugin.getActivePage().showView(VIEW_ID); 179 } 180 181 185 public void selectionChanged(SelectionChangedEvent event) { 186 187 if (event.getSelection() instanceof IStructuredSelection) { 188 listSelectionChanged((IStructuredSelection) event.getSelection()); 189 } else if (event.getSelection() instanceof ITextSelection) { 190 textSelectionChanged((ITextSelection) event.getSelection()); 191 } 192 } 193 194 197 public void dispose() { 198 disconnect(); 199 } 200 201 205 private void textSelectionChanged(ITextSelection selection) { 206 207 lastSelectionWasText = true; 210 211 CVSAnnotateBlock match = null; 213 for (Iterator iterator = cvsAnnotateBlocks.iterator(); iterator.hasNext();) { 214 CVSAnnotateBlock block = (CVSAnnotateBlock) iterator.next(); 215 if (block.contains(selection.getStartLine())) { 216 match = block; 217 break; 218 } 219 } 220 221 if (match == null) { 223 return; 224 } 225 226 StructuredSelection listSelection = new StructuredSelection(match); 227 viewer.setSelection(listSelection, true); 228 } 229 230 234 private void listSelectionChanged(IStructuredSelection selection) { 235 236 if (editor == null || editor.getSelectionProvider() == null) { 238 try { 239 contents.reset(); 240 showAnnotations(cvsResource, cvsAnnotateBlocks, contents, false); 241 } catch (CVSException e) { 242 return; 243 } catch (PartInitException e) { 244 return; 245 } catch (IOException e) { 246 return; 247 } 248 } 249 250 ISelectionProvider selectionProvider = editor.getSelectionProvider(); 251 if (selectionProvider == null) { 252 return; 254 } 255 256 ITextSelection textSelection = (ITextSelection) selectionProvider.getSelection(); 257 if (textSelection == null) return; 258 259 if (selection.size() != 1 || !(selection.getFirstElement() instanceof CVSAnnotateBlock)) 260 return; 261 262 CVSAnnotateBlock listSelection = (CVSAnnotateBlock) selection.getFirstElement(); 263 if (listSelection == null) return; 264 265 271 272 if (textSelection.getStartLine() == listSelection.getStartLine() && textSelection.getEndLine() == listSelection.getEndLine() && selection.equals(previousListSelection)) { 273 return; 274 } 275 276 if (!lastSelectionWasText) { 278 try { 279 int start = document.getLineOffset(listSelection.getStartLine()); 280 int end = document.getLineOffset(listSelection.getEndLine() + 1); 281 editor.selectAndReveal(start, end - start); 282 if (editor != null && !page.isPartVisible(editor)) { 283 page.activate(editor); 284 } 285 286 } catch (BadLocationException e) { 287 } 289 } 290 291 292 if(historyView != null) { 294 IHistoryPage historyPage = historyView.getHistoryPage(); 295 if (historyPage instanceof CVSHistoryPage){ 296 ((CVSHistoryPage) historyPage).selectRevision(listSelection.getRevision()); 297 lastSelectionWasText = false; 298 } 299 } 300 } 301 302 306 private IEditorPart openEditor() throws CVSException, PartInitException { 307 ICVSRemoteFile file = (ICVSRemoteFile) CVSWorkspaceRoot.getRemoteResourceFor(cvsResource); 308 309 String id = getEditorId(file); 314 ITextEditor editor = getEditor(id, file); 315 316 if (editor.getSelectionProvider() instanceof IPostSelectionProvider) { 318 ((IPostSelectionProvider) editor.getSelectionProvider()).addPostSelectionChangedListener(this); 319 } 320 editor.getSite().getPage().addPartListener(partListener); 321 return editor; 322 } 323 324 private ITextEditor getEditor(String id, ICVSRemoteFile file) throws PartInitException { 325 if (editor != null && editor instanceof IReusableEditor && page.isPartVisible(editor) && editor.getSite().getId().equals(id)) { 327 ((IReusableEditor) editor).setInput(new RemoteAnnotationEditorInput(file, contents)); 329 return editor; 330 } else { 331 if (editor != null) { 333 page.closeEditor(editor, false); 334 editor = null; 335 } 336 IEditorPart part = page.openEditor(new RemoteAnnotationEditorInput(file, contents), id); 337 if (part instanceof ITextEditor) { 338 return (ITextEditor)part; 339 } else { 340 page.closeEditor(part, false); 343 part = page.openEditor(new RemoteAnnotationEditorInput(file, contents), EditorsUI.DEFAULT_TEXT_EDITOR_ID); 344 if (part instanceof ITextEditor) { 345 return (ITextEditor)part; 346 } else { 347 throw new PartInitException(CVSUIMessages.AnnotateView_0); 349 } 350 } 351 } 352 } 353 354 private String getEditorId(ICVSRemoteFile file) { 355 String id; 356 IEditorRegistry registry = CVSUIPlugin.getPlugin().getWorkbench().getEditorRegistry(); 357 IEditorDescriptor descriptor = registry.getDefaultEditor(file.getName()); 358 if (descriptor == null || !descriptor.isInternal()) { 359 id = EditorsUI.DEFAULT_TEXT_EDITOR_ID; 360 } else { 361 try { 362 if (isTextEditor(descriptor)) { 363 id = descriptor.getId(); 364 } else { 365 id = EditorsUI.DEFAULT_TEXT_EDITOR_ID; 366 } 367 } catch (CoreException e) { 368 id = EditorsUI.DEFAULT_TEXT_EDITOR_ID; 369 } 370 } 371 return id; 372 } 373 374 private boolean isTextEditor(IEditorDescriptor descriptor) 375 throws CoreException { 376 if (descriptor instanceof EditorDescriptor) { 377 EditorDescriptor desc = (EditorDescriptor) descriptor; 378 return desc.createEditor() instanceof ITextEditor; 379 } 380 return false; 381 } 382 383 public void setFocus() { 386 return; 387 } 388 } 389 | Popular Tags |