KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > actions > ShowAnnotationAction


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.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.internal.ccvs.core.*;
22 import org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry;
23 import org.eclipse.team.internal.ccvs.core.filehistory.CVSFileRevision;
24 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
25 import org.eclipse.team.internal.ccvs.ui.*;
26 import org.eclipse.team.internal.ccvs.ui.operations.ShowAnnotationOperation;
27
28 public class ShowAnnotationAction extends WorkspaceAction {
29
30     /**
31      * Action to open a CVS Annotate View
32      */

33     public void execute(IAction action) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
34         final ICVSResource resource= getSingleSelectedCVSResource();
35         if (resource == null)
36             return;
37         execute(resource);
38     }
39     
40     /**
41      * Fetch the revision number of a CVS resource and perform a ShowAnnotationOperation
42      * in the background.
43      *
44      * @param cvsResource The CVS resource (must not be null)
45      *
46      * @throws InvocationTargetException
47      * @throws InterruptedException
48      */

49     public void execute(final ICVSResource cvsResource) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
50         final String JavaDoc revision= getRevision(cvsResource);
51         if (revision == null)
52             return;
53         boolean binary = isBinary(cvsResource);
54         if (binary) {
55             final IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore();
56             final String JavaDoc option = store.getString(ICVSUIConstants.PREF_ANNOTATE_PROMPTFORBINARY);
57             if (option.equals(MessageDialogWithToggle.PROMPT)) {
58                 final MessageDialogWithToggle dialog = (MessageDialogWithToggle.openYesNoQuestion(getShell(), CVSUIMessages.ShowAnnotationAction_2, NLS.bind(CVSUIMessages.ShowAnnotationAction_3, new String JavaDoc[] {cvsResource.getName()}), CVSUIMessages.ShowAnnotationOperation_4, false, store, ICVSUIConstants.PREF_ANNOTATE_PROMPTFORBINARY));
59                 final int result = dialog.getReturnCode();
60                 switch (result) {
61                     case IDialogConstants.NO_ID :
62                         return;
63                 }
64             } else if (option.equals(MessageDialogWithToggle.NEVER))
65                 return;
66         }
67         
68         new ShowAnnotationOperation(getTargetPart(), cvsResource, revision, binary).run();
69     }
70
71     private boolean isBinary(ICVSResource cvsResource) {
72         if (cvsResource.isFolder()) return false;
73         
74         try {
75             byte[] syncBytes = ((ICVSFile)cvsResource).getSyncBytes();
76                 if (syncBytes == null)
77                     return false;
78             return ResourceSyncInfo.isBinary(syncBytes);
79         } catch (CVSException e) {
80             return false;
81         }
82     }
83
84     /**
85      * Only enabled for single resource selection
86      */

87     public boolean isEnabled() {
88         ICVSResource resource = getSingleSelectedCVSResource();
89         try {
90             return (resource != null && ! resource.isFolder() && resource.isManaged());
91         } catch (CVSException e) {
92             return isEnabledForException(e);
93         }
94     }
95
96     /**
97      * This action is called from one of a Resource Navigator a CVS Resource
98      * Navigator or a History Log Viewer. Return the selected resource as an
99      * ICVSResource
100      *
101      * @return ICVSResource
102      */

103     private ICVSResource getSingleSelectedCVSResource() {
104         // Selected from a CVS Resource Navigator
105
final ICVSResource[] cvsResources = getSelectedCVSResources();
106         if (cvsResources.length == 1) {
107             return cvsResources[0];
108         }
109
110         // Selected from a History Viewer
111
final Object JavaDoc[] logEntries = getAdaptedSelection(LogEntry.class);
112         if (logEntries.length == 1) {
113             final LogEntry aLogEntry = (LogEntry) logEntries[0];
114             final ICVSRemoteFile cvsRemoteFile = aLogEntry.getRemoteFile();
115             return cvsRemoteFile;
116         }
117         
118         //Selected from the CVS History Page
119
final Object JavaDoc[] fileRevisions = getAdaptedSelection(CVSFileRevision.class);
120         if (fileRevisions.length == 1) {
121             final ICVSRemoteFile cvsRemoteFile =((CVSFileRevision) fileRevisions[0]).getCVSRemoteFile();
122             return cvsRemoteFile;
123         }
124         
125
126         // Selected from a Resource Navigator
127
final IResource[] resources = getSelectedResources();
128         if (resources.length == 1) {
129             return getCVSResourceFor(resources[0]);
130         }
131         return null;
132     }
133
134     
135     /**
136      * Get the revision for the CVS resource. Throws an InvocationTargetException
137      * if the revision could not be determined.
138      *
139      * @param cvsResource The CVS resource
140      * @return The revision of the resource.
141      * @throws InvocationTargetException
142      */

143     private String JavaDoc getRevision(ICVSResource cvsResource) throws InvocationTargetException JavaDoc {
144         final ResourceSyncInfo info;
145         try {
146             info= cvsResource.getSyncInfo();
147             if (info == null)
148                 throw new CVSException(NLS.bind(CVSUIMessages.ShowAnnotationAction_noSyncInfo, new String JavaDoc[] { cvsResource.getName() }));
149         } catch (CVSException e) {
150             throw new InvocationTargetException JavaDoc(e);
151         }
152         return info.getRevision();
153     }
154     
155     public String JavaDoc getId() {
156         return ICVSUIConstants.CMD_ANNOTATE;
157     }
158 }
159
Popular Tags