1 11 package org.eclipse.team.internal.ccvs.ui.mappings; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.resources.mapping.ResourceMapping; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.team.core.TeamException; 22 import org.eclipse.team.core.mapping.ISynchronizationScope; 23 import org.eclipse.team.core.mapping.ISynchronizationScopeManager; 24 import org.eclipse.team.core.mapping.provider.MergeContext; 25 import org.eclipse.team.core.subscribers.SubscriberScopeManager; 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.CVSUIPlugin; 29 import org.eclipse.team.internal.ccvs.ui.actions.ShowAnnotationAction; 30 import org.eclipse.team.internal.ccvs.ui.actions.ShowResourceInHistoryAction; 31 import org.eclipse.team.internal.ccvs.ui.subscriber.CVSActionDelegateWrapper; 32 import org.eclipse.team.internal.ui.Utils; 33 import org.eclipse.team.ui.TeamUI; 34 import org.eclipse.team.ui.synchronize.*; 35 import org.eclipse.ui.IMemento; 36 import org.eclipse.ui.PartInitException; 37 38 public class ModelMergeParticipant extends CVSModelSynchronizeParticipant { 39 40 public static final String VIEWER_ID = "org.eclipse.team.cvs.ui.mergeSynchronization"; 42 public static final String CONTEXT_MENU_CONTRIBUTION_GROUP_1 = "otherActions1"; 44 public static final String ID = "org.eclipse.team.cvs.ui.modelMergeParticipant"; 46 private final static String CTX_SUBSCRIBER = "mergeSubscriber"; private final static String CTX_ROOT = "root"; private final static String CTX_ROOT_PATH = "root_resource"; private final static String CTX_START_TAG = "start_tag"; private final static String CTX_START_TAG_TYPE = "start_tag_type"; private final static String CTX_END_TAG = "end_tag"; private final static String CTX_END_TAG_TYPE = "end_tag_type"; 54 public class MergeActionGroup extends ModelSynchronizeParticipantActionGroup { 55 public void initialize(ISynchronizePageConfiguration configuration) { 56 super.initialize(configuration); 57 if (!configuration.getSite().isModal()) { 58 appendToGroup( 59 ISynchronizePageConfiguration.P_CONTEXT_MENU, 60 CONTEXT_MENU_CONTRIBUTION_GROUP_1, 61 new CVSActionDelegateWrapper(new ShowAnnotationAction(), configuration)); 62 appendToGroup( 63 ISynchronizePageConfiguration.P_CONTEXT_MENU, 64 CONTEXT_MENU_CONTRIBUTION_GROUP_1, 65 new CVSActionDelegateWrapper(new ShowResourceInHistoryAction(), configuration)); 66 } 67 } 68 } 69 70 private CVSMergeSubscriber subscriber; 71 72 public ModelMergeParticipant() { 73 } 74 75 public ModelMergeParticipant(MergeSubscriberContext context) { 76 super(context); 77 subscriber = getSubscriber(); 78 initialize(); 79 } 80 81 protected ModelSynchronizeParticipantActionGroup createMergeActionGroup() { 82 return new MergeActionGroup(); 83 } 84 85 88 protected void initializeConfiguration(ISynchronizePageConfiguration configuration) { 89 configuration.setProperty(ISynchronizePageConfiguration.P_VIEWER_ID, VIEWER_ID); 90 super.initializeConfiguration(configuration); 91 configuration.setSupportedModes(ISynchronizePageConfiguration.INCOMING_MODE | ISynchronizePageConfiguration.CONFLICTING_MODE); 92 configuration.setMode(ISynchronizePageConfiguration.INCOMING_MODE); 93 } 94 95 private void initialize() { 96 try { 97 ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(ID); 98 setInitializationData(descriptor); 99 CVSMergeSubscriber s = (CVSMergeSubscriber)getSubscriber(); 100 setSecondaryId(s.getId().getLocalName()); 101 } catch (CoreException e) { 102 CVSUIPlugin.log(e); 103 } 104 } 105 106 private CVSMergeSubscriber getSubscriber() { 107 return (CVSMergeSubscriber)((MergeSubscriberContext)getContext()).getSubscriber(); 108 } 109 110 public void init(String secondaryId, IMemento memento) throws PartInitException { 111 if(memento != null) { 112 ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(ID); 113 String qualifier = descriptor.getId(); 114 String localname = secondaryId; 115 if(qualifier == null || localname == null) { 116 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_8); 117 } 118 try { 119 subscriber = read(new QualifiedName(qualifier, localname), memento.getChild(CTX_SUBSCRIBER)); 120 } catch (CVSException e) { 121 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_9, e); 122 } 123 } 124 try { 125 super.init(secondaryId, memento); 126 } catch (PartInitException e) { 127 subscriber.cancel(); 128 throw e; 129 } 130 } 131 132 public void saveState(IMemento memento) { 133 super.saveState(memento); 134 write(subscriber, memento.createChild(CTX_SUBSCRIBER)); 135 } 136 137 private CVSMergeSubscriber read(QualifiedName id, IMemento memento) throws CVSException { 138 CVSTag start = new CVSTag(memento.getString(CTX_START_TAG), memento.getInteger(CTX_START_TAG_TYPE).intValue()); 139 CVSTag end = new CVSTag(memento.getString(CTX_END_TAG), memento.getInteger(CTX_END_TAG_TYPE).intValue()); 140 141 IMemento[] rootNodes = memento.getChildren(CTX_ROOT); 142 if(rootNodes == null || rootNodes.length == 0) { 143 throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_10, new String [] { id.toString() })); 144 } 145 146 List resources = new ArrayList (); 147 for (int i = 0; i < rootNodes.length; i++) { 148 IMemento rootNode = rootNodes[i]; 149 IPath path = new Path(rootNode.getString(CTX_ROOT_PATH)); 150 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path, true ); 151 if(resource != null) { 152 resources.add(resource); 153 } else { 154 CVSProviderPlugin.log(IStatus.INFO, NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_11, new String [] { path.toString() }), null); 156 } 157 } 158 if(resources.isEmpty()) { 159 throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_12, new String [] { id.toString() })); 160 } 161 IResource[] roots = (IResource[]) resources.toArray(new IResource[resources.size()]); 162 return new CVSMergeSubscriber(id, roots, start, end); 163 } 164 165 private void write(CVSMergeSubscriber s, IMemento memento) { 166 CVSTag start = s.getStartTag(); 168 CVSTag end = s.getEndTag(); 169 memento.putString(CTX_START_TAG, start.getName()); 170 memento.putInteger(CTX_START_TAG_TYPE, start.getType()); 171 memento.putString(CTX_END_TAG, end.getName()); 172 memento.putInteger(CTX_END_TAG_TYPE, end.getType()); 173 174 IResource[] roots = s.roots(); 176 for (int i = 0; i < roots.length; i++) { 177 IResource resource = roots[i]; 178 IMemento rootNode = memento.createChild(CTX_ROOT); 179 rootNode.putString(CTX_ROOT_PATH, resource.getFullPath().toString()); 180 } 181 } 182 183 protected String getShortTaskName() { 184 return CVSUIMessages.Participant_merging; 185 } 186 187 public void dispose() { 188 if(TeamUI.getSynchronizeManager().get(getId(), getSecondaryId()) != null) { 189 ((MergeSubscriberContext)getContext()).setCancelSubscriber(false); 192 } 193 super.dispose(); 194 } 195 196 protected ISynchronizationScopeManager createScopeManager(ResourceMapping[] mappings) { 197 return new SubscriberScopeManager(subscriber.getName(), 198 mappings, subscriber, true); 199 } 200 201 protected MergeContext restoreContext(ISynchronizationScopeManager manager) throws CoreException { 202 return MergeSubscriberContext.createContext(manager, subscriber); 203 } 204 205 208 public String getName() { 209 return NLS.bind(CVSUIMessages.CompareParticipant_0, new String [] { ((CVSMergeSubscriber)getSubscriber()).getName(), Utils.getScopeDescription(getContext().getScope()) }); 210 } 211 212 215 public static ModelMergeParticipant getMatchingParticipant(ResourceMapping[] mappings, CVSTag startTag, CVSTag endTag) { 216 ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants(); 217 for (int i = 0; i < refs.length; i++) { 218 ISynchronizeParticipantReference reference = refs[i]; 219 if (reference.getId().equals(ID)) { 220 ModelMergeParticipant p; 221 try { 222 p = (ModelMergeParticipant) reference.getParticipant(); 223 } catch (TeamException e) { 224 continue; 225 } 226 ISynchronizationScope scope = p.getContext().getScope().asInputScope(); 227 ResourceMapping[] roots = scope.getMappings(); 228 if (roots.length == mappings.length) { 229 boolean match = true; 230 for (int j = 0; j < mappings.length; j++) { 231 232 ResourceMapping mapping = mappings[j]; 233 if (scope.getTraversals(mapping) == null) { 234 match = false; 236 break; 237 } 238 } 239 if (match && p.getSubscriber().getStartTag().equals(startTag) && p.getSubscriber().getEndTag().equals(endTag)) { 240 return p; 241 } 242 } 243 } 244 } 245 return null; 246 } 247 } 248 | Popular Tags |