1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 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 33 public void execute(IAction action) throws InvocationTargetException , InterruptedException { 34 final ICVSResource resource= getSingleSelectedCVSResource(); 35 if (resource == null) 36 return; 37 execute(resource); 38 } 39 40 49 public void execute(final ICVSResource cvsResource) throws InvocationTargetException , InterruptedException { 50 final String 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 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 [] {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 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 103 private ICVSResource getSingleSelectedCVSResource() { 104 final ICVSResource[] cvsResources = getSelectedCVSResources(); 106 if (cvsResources.length == 1) { 107 return cvsResources[0]; 108 } 109 110 final Object [] 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 final Object [] fileRevisions = getAdaptedSelection(CVSFileRevision.class); 120 if (fileRevisions.length == 1) { 121 final ICVSRemoteFile cvsRemoteFile =((CVSFileRevision) fileRevisions[0]).getCVSRemoteFile(); 122 return cvsRemoteFile; 123 } 124 125 126 final IResource[] resources = getSelectedResources(); 128 if (resources.length == 1) { 129 return getCVSResourceFor(resources[0]); 130 } 131 return null; 132 } 133 134 135 143 private String getRevision(ICVSResource cvsResource) throws InvocationTargetException { 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 [] { cvsResource.getName() })); 149 } catch (CVSException e) { 150 throw new InvocationTargetException (e); 151 } 152 return info.getRevision(); 153 } 154 155 public String getId() { 156 return ICVSUIConstants.CMD_ANNOTATE; 157 } 158 } 159 | Popular Tags |