KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > AnnotateView


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;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
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 /**
39  * A view showing the results of the CVS Annotate Command. A linked
40  * combination of a View of annotations, a source editor and the
41  * Resource History View
42  */

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 JavaDoc cvsAnnotateBlocks;
52     ICVSResource cvsResource;
53     InputStream JavaDoc contents;
54     
55     IStructuredSelection previousListSelection;
56     ITextSelection previousTextSelection;
57     boolean lastSelectionWasText = false;
58     
59     
60     public static final String JavaDoc VIEW_ID = "org.eclipse.team.ccvs.ui.AnnotateView"; //$NON-NLS-1$
61
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         // Create default contents
88
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     /**
95      * Show the annotation view.
96      * @param cvsResource
97      * @param cvsAnnotateBlocks
98      * @param contents
99      * @throws PartInitException, CVSException
100      */

101     public void showAnnotations(ICVSResource cvsResource, Collection JavaDoc cvsAnnotateBlocks, InputStream JavaDoc contents) throws PartInitException, CVSException {
102         showAnnotations(cvsResource, cvsAnnotateBlocks, contents, true);
103     }
104     
105     /**
106      * Show the annotation view.
107      * @param cvsResource
108      * @param cvsAnnotateBlocks
109      * @param contents
110      * @param useHistoryView
111      * @throws PartInitException, CVSException
112      */

113     public void showAnnotations(ICVSResource cvsResource, Collection JavaDoc cvsAnnotateBlocks, InputStream JavaDoc contents, boolean useHistoryView) throws PartInitException, CVSException {
114
115         // Disconnect from old annotation editor
116
disconnect();
117         
118         // Remove old viewer
119
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 JavaDoc[] {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         // Show the history in the history view
158
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     /**
173      * Makes the view visible in the active perspective. If there
174      * isn't a view registered <code>null</code> is returned.
175      * Otherwise the opened view part is returned.
176      */

177     public static AnnotateView openInActivePerspective() throws PartInitException {
178         return (AnnotateView) CVSUIPlugin.getActivePage().showView(VIEW_ID);
179     }
180
181     /**
182      * Selection changed in either the Annotate List View or the
183      * Source editor.
184      */

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     /* (non-Javadoc)
195      * @see org.eclipse.ui.IWorkbenchPart#dispose()
196      */

197     public void dispose() {
198         disconnect();
199     }
200     
201     /**
202      * A selection event in the Annotate Source Editor
203      * @param event
204      */

205     private void textSelectionChanged(ITextSelection selection) {
206         
207         // Track where the last selection event came from to avoid
208
// a selection event loop.
209
lastSelectionWasText = true;
210             
211         // Locate the annotate block containing the selected line number.
212
CVSAnnotateBlock match = null;
213         for (Iterator JavaDoc 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         // Select the annotate block in the List View.
222
if (match == null) {
223             return;
224         }
225         
226         StructuredSelection listSelection = new StructuredSelection(match);
227         viewer.setSelection(listSelection, true);
228     }
229
230     /**
231      * A selection event in the Annotate List View
232      * @param selection
233      */

234     private void listSelectionChanged(IStructuredSelection selection) {
235
236         // If the editor was closed, reopen it.
237
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 JavaDoc e) {
246                 return;
247             }
248         }
249         
250         ISelectionProvider selectionProvider = editor.getSelectionProvider();
251         if (selectionProvider == null) {
252             // Failed to open the editor but what else can we do.
253
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         /**
266          * Ignore event if the current text selection is already equal to the corresponding
267          * list selection. Nothing to do. This prevents infinite event looping.
268          *
269          * Extra check to handle single line deltas
270          */

271         
272         if (textSelection.getStartLine() == listSelection.getStartLine() && textSelection.getEndLine() == listSelection.getEndLine() && selection.equals(previousListSelection)) {
273             return;
274         }
275         
276         // If the last selection was a text selection then bale to prevent a selection loop.
277
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                 // Ignore - nothing we can do.
288
}
289         }
290         
291         
292         // Select the revision in the history view.
293
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     /**
303      * Try and open the correct registered editor type for the file.
304      * @throws CVSException, PartInitException
305      */

306     private IEditorPart openEditor() throws CVSException, PartInitException {
307          ICVSRemoteFile file = (ICVSRemoteFile) CVSWorkspaceRoot.getRemoteResourceFor(cvsResource);
308
309         // Determine if the registered editor is an ITextEditor.
310
// There is currently no support from UI to determine this information. This
311
// problem has been logged in: https://bugs.eclipse.org/bugs/show_bug.cgi?id=47362
312
// For now, use internal classes.
313
String JavaDoc id = getEditorId(file);
314         ITextEditor editor = getEditor(id, file);
315         
316         // Hook Editor post selection listener.
317
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 JavaDoc id, ICVSRemoteFile file) throws PartInitException {
325         // Either reuse an existing editor or open a new editor of the correct type.
326
if (editor != null && editor instanceof IReusableEditor && page.isPartVisible(editor) && editor.getSite().getId().equals(id)) {
327             // We can reuse the editor
328
((IReusableEditor) editor).setInput(new RemoteAnnotationEditorInput(file, contents));
329             return editor;
330         } else {
331             // We can not reuse the editor so close the existing one and open a new one.
332
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                 // We asked for a text editor but didn't get one
341
// so open a vanilla text editor
342
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                     // There is something really wrong so just bail
348
throw new PartInitException(CVSUIMessages.AnnotateView_0);
349                 }
350             }
351         }
352     }
353
354     private String JavaDoc getEditorId(ICVSRemoteFile file) {
355         String JavaDoc 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     // This method implemented to be an ISelectionChangeListener but we
384
// don't really care when the List or Editor get focus.
385
public void setFocus() {
386         return;
387     }
388 }
389
Popular Tags