KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ModelCompareEditorInput


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.ui.mapping;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.*;
16 import org.eclipse.compare.structuremergeviewer.ICompareInput;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.action.*;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.team.core.ICache;
26 import org.eclipse.team.core.ICacheListener;
27 import org.eclipse.team.internal.ui.*;
28 import org.eclipse.team.internal.ui.synchronize.*;
29 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput;
30 import org.eclipse.team.ui.mapping.SaveableComparison;
31 import org.eclipse.team.ui.synchronize.*;
32 import org.eclipse.ui.*;
33
34 /**
35  * A saveable based compare editor input for compare inputs from a {@link ModelSynchronizeParticipant}.
36  */

37 public class ModelCompareEditorInput extends SaveableCompareEditorInput implements IPropertyChangeListener {
38
39     private static final String JavaDoc IGNORE_WHITSPACE_PAGE_PROPERTY = "org.eclipse.compare." + CompareConfiguration.IGNORE_WHITESPACE; //$NON-NLS-1$
40

41     private final ModelSynchronizeParticipant participant;
42     private final ICompareInput input;
43     private final ICacheListener contextListener;
44     private final ISynchronizePageConfiguration synchronizeConfiguration;
45
46     public ModelCompareEditorInput(ModelSynchronizeParticipant participant, ICompareInput input, IWorkbenchPage page, ISynchronizePageConfiguration synchronizeConfiguration) {
47         super(createCompareConfiguration(synchronizeConfiguration), page);
48         this.synchronizeConfiguration = synchronizeConfiguration;
49         Assert.isNotNull(participant);
50         Assert.isNotNull(input);
51         this.participant = participant;
52         this.input = input;
53         contextListener = new ICacheListener() {
54             public void cacheDisposed(ICache cache) {
55                 closeEditor(true);
56             }
57         };
58         getCompareConfiguration().addPropertyChangeListener(this);
59         setTitle(NLS.bind(TeamUIMessages.SyncInfoCompareInput_title, new String JavaDoc[] { input.getName() }));
60     }
61
62     private static CompareConfiguration createCompareConfiguration(ISynchronizePageConfiguration pageConfiguration) {
63         CompareConfiguration compareConfiguration = new CompareConfiguration();
64         Object JavaDoc o = pageConfiguration.getProperty(IGNORE_WHITSPACE_PAGE_PROPERTY);
65         if (o != null && (o.equals(Boolean.TRUE) || o.equals(Boolean.FALSE))) {
66             compareConfiguration.setProperty(CompareConfiguration.IGNORE_WHITESPACE, o);
67         }
68         return compareConfiguration;
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.compare.CompareEditorInput#contentsCreated()
73      */

74     protected void contentsCreated() {
75         super.contentsCreated();
76         participant.getContext().getCache().addCacheListener(contextListener);
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.compare.CompareEditorInput#handleDispose()
81      */

82     protected void handleDispose() {
83         super.handleDispose();
84         participant.getContext().getCache().removeCacheListener(contextListener);
85         getCompareConfiguration().removePropertyChangeListener(this);
86         ICompareNavigator navigator = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR);
87         if (navigator != null && navigator == super.getNavigator()) {
88             synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, new CompareNavigator() {
89                 protected INavigatable[] getNavigatables() {
90                     return new INavigatable[0];
91                 }
92             });
93         }
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.team.ui.synchronize.LocalResourceCompareEditorInput#createSaveable()
98      */

99     protected Saveable createSaveable() {
100         if (input instanceof ISynchronizationCompareInput) {
101             ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input;
102             SaveableComparison compareModel = mci.getSaveable();
103             if (compareModel != null)
104                 return compareModel;
105         }
106         return super.createSaveable();
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.compare.CompareEditorInput#prepareInput(org.eclipse.core.runtime.IProgressMonitor)
111      */

112     protected ICompareInput prepareCompareInput(IProgressMonitor monitor)
113             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
114         monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100);
115         monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3);
116         try {
117             ISynchronizationCompareInput adapter = asModelCompareInput(input);
118             if (adapter != null) {
119                 adapter.prepareInput(getCompareConfiguration(), Policy.subMonitorFor(monitor, 100));
120             }
121         } catch (CoreException e) {
122             throw new InvocationTargetException JavaDoc(e);
123         } finally {
124             monitor.done();
125         }
126         return input;
127     }
128
129     private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) {
130         return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class);
131     }
132
133     /**
134      * Return whether the compare input of this editor input matches the
135      * given object.
136      * @param object the object
137      * @param participant the participant associated with the given object
138      * @return whether the compare input of this editor input matches the
139      * given object
140      */

141     public boolean matches(Object JavaDoc object, ISynchronizeParticipant participant) {
142         if (participant == this.participant && input instanceof ISynchronizationCompareInput) {
143             ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input;
144             return mci.isCompareInputFor(object);
145         }
146         return false;
147     }
148     
149     /*
150      * (non-Javadoc)
151      * @see org.eclipse.ui.IEditorInput#getToolTipText()
152      */

153     public String JavaDoc getToolTipText() {
154         String JavaDoc fullPath;
155         ISynchronizationCompareInput adapter = asModelCompareInput(input);
156         if (adapter != null) {
157             fullPath = adapter.getFullPath();
158         } else {
159             fullPath = getName();
160         }
161         return NLS.bind(TeamUIMessages.SyncInfoCompareInput_tooltip, new String JavaDoc[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName()), fullPath });
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.team.ui.synchronize.LocalResourceCompareEditorInput#fireInputChange()
166      */

167     protected void fireInputChange() {
168         if (input instanceof ResourceDiffCompareInput) {
169             ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input;
170             rdci.fireChange();
171         }
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.compare.CompareEditorInput#registerContextMenu(org.eclipse.jface.action.MenuManager)
176      */

177     public void registerContextMenu(MenuManager menu, ISelectionProvider provider) {
178         super.registerContextMenu(menu, provider);
179         Saveable saveable = getSaveable();
180         if (saveable instanceof LocalResourceSaveableComparison) {
181             menu.addMenuListener(new IMenuListener() {
182                 public void menuAboutToShow(IMenuManager manager) {
183                     handleMenuAboutToShow(manager);
184                 }
185             });
186         }
187     }
188
189     protected void handleMenuAboutToShow(IMenuManager manager) {
190         StructuredSelection selection = new StructuredSelection(((ResourceDiffCompareInput)getCompareInput()).getResource());
191         final ResourceMarkAsMergedHandler markAsMergedHandler = new ResourceMarkAsMergedHandler(getSynchronizeConfiguration());
192         markAsMergedHandler.updateEnablement(selection);
193         Action markAsMergedAction = new Action(TeamUIMessages.ModelCompareEditorInput_0) {
194             public void run() {
195                 try {
196                     markAsMergedHandler.execute(new ExecutionEvent());
197                 } catch (ExecutionException e) {
198                     TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
199                 }
200             }
201             
202         };
203         Utils.initAction(markAsMergedAction, "action.markAsMerged."); //$NON-NLS-1$
204
markAsMergedAction.setEnabled(markAsMergedAction.isEnabled());
205         
206         final ResourceMergeHandler mergeHandler = new ResourceMergeHandler(getSynchronizeConfiguration(), false);
207         mergeHandler.updateEnablement(selection);
208         Action mergeAction = new Action(TeamUIMessages.ModelCompareEditorInput_1) {
209             public void run() {
210                 try {
211                     mergeHandler.execute(new ExecutionEvent());
212                 } catch (ExecutionException e) {
213                     TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
214                 }
215             }
216             
217         };
218         Utils.initAction(mergeAction, "action.merge."); //$NON-NLS-1$
219
mergeAction.setEnabled(markAsMergedAction.isEnabled());
220         
221         final ResourceMergeHandler overwriteHandler = new ResourceMergeHandler(getSynchronizeConfiguration(), true);
222         overwriteHandler.updateEnablement(selection);
223         Action overwriteAction = new Action(TeamUIMessages.ModelCompareEditorInput_2) {
224             public void run() {
225                 try {
226                     overwriteHandler.execute(new ExecutionEvent());
227                 } catch (ExecutionException e) {
228                     TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e);
229                 }
230             }
231             
232         };
233         Utils.initAction(overwriteAction, "action.overwrite."); //$NON-NLS-1$
234
overwriteAction.setEnabled(markAsMergedAction.isEnabled());
235         
236         manager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator("merge")); //$NON-NLS-1$
237
manager.insertAfter("merge", new Separator("overwrite")); //$NON-NLS-1$ //$NON-NLS-2$
238
manager.insertAfter("merge", markAsMergedAction); //$NON-NLS-1$
239
manager.insertAfter("merge", mergeAction); //$NON-NLS-1$
240
manager.insertAfter("overwrite", overwriteAction); //$NON-NLS-1$
241
}
242
243     protected ISynchronizePageConfiguration getSynchronizeConfiguration() {
244         return synchronizeConfiguration;
245     }
246
247     public void propertyChange(PropertyChangeEvent event) {
248         if (event.getProperty().equals(CompareConfiguration.IGNORE_WHITESPACE)) {
249             synchronizeConfiguration.setProperty(IGNORE_WHITSPACE_PAGE_PROPERTY, event.getNewValue());
250         }
251     }
252     
253     /* (non-Javadoc)
254      * @see org.eclipse.compare.CompareEditorInput#belongsTo(java.lang.Object)
255      */

256     public boolean belongsTo(Object JavaDoc family) {
257         return super.belongsTo(family) || family == participant;
258     }
259     
260     public synchronized ICompareNavigator getNavigator() {
261         if (isSelectedInSynchronizeView()) {
262             ICompareNavigator nav = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_NAVIGATOR);
263             // Set the input navigator property so that the advisor can get at it if needed.
264
synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, super.getNavigator());
265             return nav;
266         }
267         return super.getNavigator();
268     }
269
270     private boolean isSelectedInSynchronizeView() {
271         ISelection s = synchronizeConfiguration.getSite().getSelectionProvider().getSelection();
272         if (s instanceof IStructuredSelection) {
273             IStructuredSelection ss = (IStructuredSelection) s;
274             Object JavaDoc element = ss.getFirstElement();
275             return matches(element, participant);
276         }
277         return false;
278     }
279     
280     /* (non-Javadoc)
281      * @see java.lang.Object#equals(java.lang.Object)
282      */

283     public boolean equals(Object JavaDoc obj) {
284         if (obj == this)
285             return true;
286         if (obj instanceof ModelCompareEditorInput) {
287             ModelCompareEditorInput other = (ModelCompareEditorInput) obj;
288             return other.input.equals(input) && other.participant.equals(participant);
289         }
290         return false;
291     }
292     
293     /* (non-Javadoc)
294      * @see java.lang.Object#hashCode()
295      */

296     public int hashCode() {
297         return input.hashCode();
298     }
299 }
300
Popular Tags