KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > MergeWizard


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.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.team.internal.ccvs.core.CVSMergeSubscriber;
25 import org.eclipse.team.internal.ccvs.core.CVSTag;
26 import org.eclipse.team.internal.ccvs.core.client.Command;
27 import org.eclipse.team.internal.ccvs.core.client.Update;
28 import org.eclipse.team.internal.ccvs.ui.*;
29 import org.eclipse.team.internal.ccvs.ui.actions.WorkspaceTraversalAction;
30 import org.eclipse.team.internal.ccvs.ui.mappings.ModelMergeOperation;
31 import org.eclipse.team.internal.ccvs.ui.mappings.ModelMergeParticipant;
32 import org.eclipse.team.internal.ccvs.ui.operations.UpdateOperation;
33 import org.eclipse.team.internal.ccvs.ui.subscriber.MergeSynchronizeParticipant;
34 import org.eclipse.team.internal.ccvs.ui.tags.TagSource;
35 import org.eclipse.team.ui.TeamUI;
36 import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;
37 import org.eclipse.ui.IWorkbenchPart;
38
39 public class MergeWizard extends Wizard {
40     MergeWizardPage page;
41     IResource[] resources;
42     private final IWorkbenchPart part;
43     private final ResourceMapping[] mappings;
44     
45     public MergeWizard(IWorkbenchPart part, IResource[] resources, ResourceMapping[] mappings) {
46         this.part = part;
47         this.resources = resources;
48         this.mappings = mappings;
49     }
50
51     public void addPages() {
52         setNeedsProgressMonitor(true);
53         TagSource tagSource = TagSource.create(resources);
54         setWindowTitle(CVSUIMessages.MergeWizard_title);
55         ImageDescriptor mergeImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_MERGE);
56         page = new MergeWizardPage("mergePage", CVSUIMessages.MergeWizard_0, mergeImage, CVSUIMessages.MergeWizard_1, tagSource); //$NON-NLS-1$
57
addPage(page);
58     }
59
60     public boolean performFinish() {
61         
62         CVSTag startTag = page.getStartTag();
63         CVSTag endTag = page.getEndTag();
64         
65         if (startTag == null || !page.isPreview()) {
66             // Perform the update (merge) in the background
67
UpdateOperation op = new UpdateOperation(getPart(), mappings, getLocalOptions(startTag, endTag), null);
68             try {
69                 op.run();
70             } catch (InvocationTargetException JavaDoc e) {
71                 CVSUIPlugin.openError(getShell(), null, null, e);
72             } catch (InterruptedException JavaDoc e) {
73                 // Ignore
74
}
75         } else {
76             if (isShowModelSync()) {
77                 ModelMergeParticipant participant = ModelMergeParticipant.getMatchingParticipant(mappings, startTag, endTag);
78                 if(participant == null) {
79                     CVSMergeSubscriber s = new CVSMergeSubscriber(getProjects(resources), startTag, endTag);
80                     try {
81                         new ModelMergeOperation(getPart(), mappings, s, page.isOnlyPreviewConflicts()).run();
82                     } catch (InvocationTargetException JavaDoc e) {
83                         CVSUIPlugin.log(IStatus.ERROR, "Internal error", e.getTargetException()); //$NON-NLS-1$
84
} catch (InterruptedException JavaDoc e) {
85                         // Ignore
86
}
87                 } else {
88                     participant.refresh(null, mappings);
89                 }
90             } else {
91                 // First check if there is an existing matching participant, if so then re-use it
92
try {
93                     resources = getAllResources(startTag, endTag);
94                 } catch (InvocationTargetException JavaDoc e) {
95                     // Log and continue with the original resources
96
CVSUIPlugin.log(IStatus.ERROR, "An error occurred while detemrining if extra resources should be included in the merge", e.getTargetException()); //$NON-NLS-1$
97
}
98                 MergeSynchronizeParticipant participant = MergeSynchronizeParticipant.getMatchingParticipant(resources, startTag, endTag);
99                 if(participant == null) {
100                     CVSMergeSubscriber s = new CVSMergeSubscriber(resources, startTag, endTag);
101                     participant = new MergeSynchronizeParticipant(s);
102                     TeamUI.getSynchronizeManager().addSynchronizeParticipants(new ISynchronizeParticipant[] {participant});
103                 }
104                 participant.refresh(resources, null, null, null);
105
106             }
107         }
108         return true;
109     }
110
111     private IResource[] getAllResources(CVSTag startTag, CVSTag endTag) throws InvocationTargetException JavaDoc {
112         // Only do the extra work if the model is a logical model (i.e. not IResource)
113
if (!WorkspaceTraversalAction.isLogicalModel(mappings))
114             return resources;
115         CVSMergeSubscriber s = new CVSMergeSubscriber(WorkspaceTraversalAction.getProjects(resources), startTag, endTag);
116         IResource[] allResources = WorkspaceTraversalAction.getResourcesToCompare(mappings, s);
117         s.cancel();
118         return allResources;
119     }
120     
121     public static boolean isShowModelSync() {
122         return CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_ENABLE_MODEL_SYNC);
123     }
124     
125     private IResource[] getProjects(IResource[] resources) {
126         Set JavaDoc projects = new HashSet JavaDoc();
127         for (int i = 0; i < resources.length; i++) {
128             IResource resource = resources[i];
129             projects.add(resource.getProject());
130         }
131         return (IResource[]) projects.toArray(new IResource[projects.size()]);
132     }
133
134     private Command.LocalOption[] getLocalOptions(CVSTag startTag, CVSTag endTag) {
135         List JavaDoc options = new ArrayList JavaDoc();
136         if (startTag != null) {
137             options.add(Command.makeArgumentOption(Update.JOIN, startTag.getName()));
138         }
139         options.add(Command.makeArgumentOption(Update.JOIN, endTag.getName()));
140         return (Command.LocalOption[]) options.toArray(new Command.LocalOption[options.size()]);
141     }
142
143     private IWorkbenchPart getPart() {
144         return part;
145     }
146 }
147
Popular Tags