1 11 package org.eclipse.team.internal.ccvs.ui.subscriber; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import com.ibm.icu.text.DateFormat; 15 16 import org.eclipse.compare.structuremergeviewer.IDiffElement; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.team.core.TeamException; 23 import org.eclipse.team.core.synchronize.*; 24 import org.eclipse.team.core.synchronize.FastSyncInfoFilter.*; 25 import org.eclipse.team.core.variants.IResourceVariant; 26 import org.eclipse.team.internal.ccvs.core.*; 27 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; 28 import org.eclipse.team.internal.ccvs.ui.operations.RemoteCompareOperation.CompareTreeBuilder; 29 import org.eclipse.team.internal.core.subscribers.ChangeSet; 30 import org.eclipse.team.internal.core.subscribers.CheckedInChangeSet; 31 import org.eclipse.team.internal.ui.synchronize.ChangeSetDiffNode; 32 import org.eclipse.team.ui.synchronize.*; 33 34 class OpenChangeSetAction extends SynchronizeModelAction { 35 36 protected OpenChangeSetAction(ISynchronizePageConfiguration configuration) { 37 super(CVSUIMessages.OpenCommitSetAction_20, configuration); 38 } 39 40 43 protected FastSyncInfoFilter getSyncInfoFilter() { 44 return new AndSyncInfoFilter(new FastSyncInfoFilter[] { 45 new FastSyncInfoFilter() { 46 public boolean select(SyncInfo info) { 47 return info.getLocal().getType() == IResource.FILE; 48 } 49 }, 50 new OrSyncInfoFilter(new FastSyncInfoFilter[] { 51 new SyncInfoDirectionFilter(new int[] { SyncInfo.INCOMING, SyncInfo.CONFLICTING }), 52 new FastSyncInfoFilter() { 53 public boolean select(SyncInfo info) { 54 return !info.getComparator().isThreeWay(); 55 } 56 } 57 }) 58 }); 59 } 60 61 private ChangeSet getChangeSet(IStructuredSelection selection) { 62 if (selection.size() == 1) { 64 Object o = selection.getFirstElement(); 65 if (o instanceof IAdaptable) { 66 ChangeSet set = (ChangeSet)((IAdaptable)o).getAdapter(ChangeSet.class); 67 if (set != null) 68 return set; 69 } 70 } 71 return getChangeSet(selection.toArray()); 73 } 74 75 private ChangeSet getChangeSet(Object [] elements) { 76 ChangeSet foundSet = null; 77 for (int i = 0; i < elements.length; i++) { 78 Object object = elements[i]; 79 ChangeSet set = getChangeSet((ISynchronizeModelElement)object); 80 if (set == null) return null; 81 if (foundSet == null) { 82 foundSet = set; 83 } else if (foundSet != set) { 84 return null; 85 } 86 } 87 return foundSet; 88 } 89 90 private ChangeSet getChangeSet(ISynchronizeModelElement element) { 91 if (element == null) return null; 92 if (element instanceof IAdaptable) { 93 ChangeSet set = (ChangeSet)((IAdaptable)element).getAdapter(ChangeSet.class); 94 if (set != null) 95 return set; 96 } 97 return getChangeSet((ISynchronizeModelElement)element.getParent()); 98 } 99 100 103 protected boolean updateSelection(IStructuredSelection selection) { 104 boolean enabled = super.updateSelection(selection); 105 if (enabled) { 106 ChangeSet set = getChangeSet(selection); 109 return set != null; 110 } 111 return false; 112 } 113 114 117 protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) { 118 return new SynchronizeModelOperation(configuration, elements) { 119 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 120 SyncInfoSet set = getSyncInfoSet(); 121 SyncInfo[] infos = set.getSyncInfos(); 122 if (infos.length > 0) { 123 ICVSRepositoryLocation location = getLocation(infos[0]); 124 if (location == null) { 125 handle(new CVSException(CVSUIMessages.OpenCommitSetAction_21)); 126 return; 127 } 128 CompareTreeBuilder builder = new CompareTreeBuilder(location, null, null); 129 if (buildTrees(builder, infos)) { 130 try { 131 builder.cacheContents(monitor); 132 builder.openCompareEditor(getConfiguration().getSite().getPart().getSite().getPage(), getCompareTitle(), getCompareToolTip()); 133 } catch (CVSException e) { 134 handle(e); 135 return; 136 } 137 } 138 } 139 } 140 141 private String getCompareToolTip() { 142 IDiffElement[] elements = getSelectedDiffElements(); 143 for (int i = 0; i < elements.length; i++) { 144 IDiffElement element = elements[i]; 145 while (element != null) { 146 if (element instanceof ChangeSetDiffNode) { 147 return ((ChangeSetDiffNode)element).getName(); 148 } 149 element = element.getParent(); 150 } 151 } 152 return null; 153 } 154 155 private String getCompareTitle() { 156 IDiffElement[] elements = getSelectedDiffElements(); 157 ChangeSet set = getChangeSet(elements); 158 if (set instanceof CheckedInChangeSet) { 159 CheckedInChangeSet cics = (CheckedInChangeSet)set; 160 String date = DateFormat.getDateTimeInstance().format(cics.getDate()); 161 return NLS.bind(CVSUIMessages.OpenChangeSetAction_0, new String [] {cics.getAuthor(), date}); 162 } 163 return CVSUIMessages.OpenChangeSetAction_1; 164 } 165 166 private ICVSRepositoryLocation getLocation(SyncInfo info) { 167 IResourceVariant remote = info.getRemote(); 168 if (remote == null) { 169 remote = info.getBase(); 170 } 171 if (remote != null) { 172 return ((ICVSRemoteResource)remote).getRepository(); 173 } 174 return null; 175 } 176 177 180 private boolean buildTrees(CompareTreeBuilder builder, SyncInfo[] infos) { 181 for (int i = 0; i < infos.length; i++) { 182 SyncInfo info = infos[i]; 183 IResourceVariant remote = info.getRemote(); 184 if (remote == null) { 185 IResourceVariant predecessor = info.getBase(); 186 if (predecessor instanceof ICVSRemoteFile) { 187 builder.addToTrees((ICVSRemoteFile)predecessor, null); 188 } 189 } else if (remote instanceof ICVSRemoteFile) { 190 try { 191 ICVSRemoteFile predecessor = getImmediatePredecessor(remote); 192 builder.addToTrees(predecessor, (ICVSRemoteFile)remote); 193 } catch (TeamException e) { 194 handle(e); 195 return false; 196 } 197 } 198 } 199 return true; 200 } 201 }; 202 } 203 204 private ICVSRemoteFile getImmediatePredecessor(IResourceVariant remote) throws TeamException { 205 CVSChangeSetCollector changeSetCollector = getChangeSetCollector(); 206 if (changeSetCollector != null) { 207 return changeSetCollector.getImmediatePredecessor((ICVSRemoteFile)remote); 208 } 209 return null; 210 } 211 212 private CVSChangeSetCollector getChangeSetCollector() { 213 return (CVSChangeSetCollector)getConfiguration().getProperty(CVSChangeSetCollector.CVS_CHECKED_IN_COLLECTOR); 214 } 215 216 } 217 | Popular Tags |