KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > ParticipantPageCompareEditorInput


1 /*******************************************************************************
2  * Copyright (c) 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.ui.synchronize;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.*;
16 import org.eclipse.compare.structuremergeviewer.*;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.action.IToolBarManager;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.jface.viewers.*;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.team.internal.ui.*;
29 import org.eclipse.team.internal.ui.synchronize.*;
30 import org.eclipse.team.ui.PageCompareEditorInput;
31 import org.eclipse.team.ui.TeamUI;
32 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput;
33 import org.eclipse.team.ui.mapping.SaveableComparison;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.part.IPage;
36 import org.eclipse.ui.part.IPageBookViewPage;
37
38 /**
39  * Displays a synchronize participant page combined with the compare/merge infrastructure. This only works if the
40  * synchronize page viewer provides selections that are of the following types: ITypedElement and ICompareInput
41  * or if the participant is a {@link ModelSynchronizeParticipant}.
42  *
43  * @since 3.3
44  */

45 public class ParticipantPageCompareEditorInput extends PageCompareEditorInput {
46
47     private ISynchronizeParticipant participant;
48     private ISynchronizePageConfiguration pageConfiguration;
49     
50     private Image titleImage;
51     private IPageBookViewPage page;
52     private DialogSynchronizePageSite site;
53     private IPropertyChangeListener listener;
54     private Button rememberParticipantButton;
55     
56     /**
57      * Creates a part for the provided participant. The page configuration is used when creating the participant page and the resulting
58      * compare/merge panes will be configured with the provided compare configuration.
59      * <p>
60      * For example, clients can decide if the user can edit the compare panes by calling {@link CompareConfiguration#setLeftEditable(boolean)}
61      * or {@link CompareConfiguration#setRightEditable(boolean)}.
62      * </p>
63      * @param configuration the compare configuration that will be used to create the compare panes
64      * @param pageConfiguration the configuration that will be provided to the participant prior to creating the page
65      * @param participant the participant whose page will be displayed in this part
66      */

67     public ParticipantPageCompareEditorInput(
68             CompareConfiguration configuration,
69             ISynchronizePageConfiguration pageConfiguration,
70             ISynchronizeParticipant participant) {
71         super(configuration);
72         this.pageConfiguration = pageConfiguration;
73         this.participant = participant;
74         pageConfiguration.setRunnableContext(this);
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.compare.CompareEditorInput#prepareInput(org.eclipse.core.runtime.IProgressMonitor)
79      */

80     protected Object JavaDoc prepareInput(IProgressMonitor monitor)
81             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
82         setTitle(Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName()));
83         return participant;
84     }
85     
86     /* (non-Javadoc)
87      * @see org.eclipse.compare.CompareEditorInput#getTitleImage()
88      */

89     public Image getTitleImage() {
90         if(titleImage == null) {
91             titleImage = participant.getImageDescriptor().createImage();
92         }
93         return titleImage;
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.team.ui.PageCompareEditorInput#handleDispose()
98      */

99     protected void handleDispose() {
100         if(titleImage != null) {
101             titleImage.dispose();
102         }
103         if (page != null)
104             page.dispose();
105         if (site != null)
106             site.dispose();
107         pageConfiguration.removePropertyChangeListener(listener);
108         super.handleDispose();
109     }
110     
111     /* (non-Javadoc)
112      * @see org.eclipse.team.ui.PageCompareEditorInput#createPage(org.eclipse.compare.CompareViewerPane, org.eclipse.jface.action.IToolBarManager)
113      */

114     protected IPage createPage(CompareViewerPane parent,
115             IToolBarManager toolBarManager) {
116         listener = new IPropertyChangeListener() {
117             public void propertyChange(PropertyChangeEvent event) {
118                 if (event.getProperty().equals(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION)) {
119                     updateDescription();
120                 }
121             }
122         };
123         pageConfiguration.addPropertyChangeListener(listener);
124         updateDescription();
125
126         page = participant.createPage(pageConfiguration);
127         site = new DialogSynchronizePageSite(parent.getShell(), true);
128         ((SynchronizePageConfiguration)pageConfiguration).setSite(site);
129         site.createActionBars(toolBarManager);
130         try {
131             ((ISynchronizePage)page).init(pageConfiguration.getSite());
132         } catch (PartInitException e1) {
133         }
134
135         page.createControl(parent);
136         
137         page.setActionBars(site.getActionBars());
138         toolBarManager.update(true);
139         return page;
140     }
141
142     /* private */ void updateDescription() {
143         String JavaDoc description = (String JavaDoc)pageConfiguration.getProperty(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION);
144         if (description != null) {
145             setPageDescription(description);
146         }
147     }
148
149     /* (non-Javadoc)
150      * @see org.eclipse.team.ui.PageCompareEditorInput#getSelectionProvider()
151      */

152     protected ISelectionProvider getSelectionProvider() {
153         return ((ISynchronizePage)page).getViewer();
154     }
155
156     protected ICompareInput asCompareInput(ISelection selection) {
157         ICompareInput compareInput = super.asCompareInput(selection);
158         if (compareInput != null)
159             return compareInput;
160         
161         if (selection != null && selection instanceof IStructuredSelection) {
162             IStructuredSelection ss= (IStructuredSelection) selection;
163             if (ss.size() == 1) {
164                 Object JavaDoc o = ss.getFirstElement();
165                 if (participant instanceof ModelSynchronizeParticipant) {
166                     ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
167                     return msp.asCompareInput(o);
168                 }
169             }
170         }
171         return null;
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.team.ui.PageCompareEditorInput#prepareInput(org.eclipse.compare.structuremergeviewer.ICompareInput, org.eclipse.compare.CompareConfiguration, org.eclipse.core.runtime.IProgressMonitor)
176      */

177     protected void prepareInput(ICompareInput input,
178             CompareConfiguration configuration, IProgressMonitor monitor)
179             throws InvocationTargetException JavaDoc {
180         monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100);
181         monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3);
182         try {
183             // First, see if the active buffer is changing
184
checkForBufferChange(pageConfiguration.getSite().getShell(), input, false /* cancel not allowed */, monitor);
185             if (input instanceof SyncInfoModelElement) {
186                 final SyncInfoModelElement node = (SyncInfoModelElement) input;
187                 IResource resource = node.getResource();
188                 if (resource != null && resource.getType() == IResource.FILE) {
189                     participant.prepareCompareInput(node, configuration, monitor);
190                 }
191             } else {
192                 ISynchronizationCompareInput adapter = asModelCompareInput(input);
193                 if (adapter != null) {
194                     adapter.prepareInput(configuration, Policy.subMonitorFor(monitor, 90));
195                 }
196             }
197         } catch (CoreException e) {
198             throw new InvocationTargetException JavaDoc(e);
199         } finally {
200             monitor.done();
201         }
202     }
203     
204     private void checkForBufferChange(Shell shell, final ICompareInput input, boolean cancelAllowed, IProgressMonitor monitor) throws CoreException {
205         ISynchronizeParticipant participant = pageConfiguration.getParticipant();
206         if (participant instanceof ModelSynchronizeParticipant) {
207             ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
208             if (input instanceof ISynchronizationCompareInput) {
209                 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input;
210                 msp.checkForBufferChange(shell, mci, cancelAllowed, monitor);
211             }
212         }
213     }
214
215     private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) {
216         return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class);
217     }
218     
219     /* (non-Javadoc)
220      * @see org.eclipse.compare.CompareEditorInput#isSaveNeeded()
221      */

222     public boolean isSaveNeeded() {
223         if (participant instanceof ModelSynchronizeParticipant) {
224             ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
225             SaveableComparison currentBuffer = msp.getActiveSaveable();
226             if (currentBuffer != null) {
227                 return currentBuffer.isDirty();
228             }
229         }
230         return super.isSaveNeeded();
231     }
232     
233     /* (non-Javadoc)
234      * @see org.eclipse.compare.CompareEditorInput#saveChanges(org.eclipse.core.runtime.IProgressMonitor)
235      */

236     public void saveChanges(IProgressMonitor monitor) throws CoreException {
237         super.saveChanges(monitor);
238         Object JavaDoc input = ((ISynchronizePage)page).getViewer().getInput();
239         if (input instanceof ISynchronizeModelElement) {
240             ISynchronizeModelElement root = (ISynchronizeModelElement)input;
241             if (root != null && root instanceof DiffNode) {
242                 try {
243                     commit(monitor, (DiffNode)root);
244                 } catch (CoreException e) {
245                     Utils.handle(e);
246                 } finally {
247                     setDirty(false);
248                 }
249             }
250         }
251     }
252     
253     private static void commit(IProgressMonitor pm, DiffNode node) throws CoreException {
254         ITypedElement left = node.getLeft();
255         if (left instanceof LocalResourceTypedElement)
256              ((LocalResourceTypedElement) left).commit(pm);
257
258         ITypedElement right = node.getRight();
259         if (right instanceof LocalResourceTypedElement)
260              ((LocalResourceTypedElement) right).commit(pm);
261         
262         IDiffElement[] children = node.getChildren();
263         for (int i = 0; i < children.length; i++) {
264             commit(pm, (DiffNode)children[i]);
265         }
266     }
267     
268     /* (non-Javadoc)
269      * @see org.eclipse.team.ui.PageCompareEditorInput#contentChanged(org.eclipse.compare.IContentChangeNotifier)
270      */

271     public void contentChanged(IContentChangeNotifier source) {
272         super.contentChanged(source);
273         try {
274             if (source instanceof DiffNode) {
275                 commit(new NullProgressMonitor(), (DiffNode) source);
276             } else if (source instanceof LocalResourceTypedElement) {
277                  ((LocalResourceTypedElement) source).commit(new NullProgressMonitor());
278             }
279         } catch (CoreException e) {
280             Utils.handle(e);
281         }
282     }
283     
284     /**
285      * Return the synchronize page configuration for this part
286      *
287      * @return Returns the pageConfiguration.
288      */

289     public ISynchronizePageConfiguration getPageConfiguration() {
290         return pageConfiguration;
291     }
292     
293     /**
294      * Return the Synchronize participant for this part
295      *
296      * @return Returns the participant.
297      */

298     public ISynchronizeParticipant getParticipant() {
299         return participant;
300     }
301     
302     /* (non-Javadoc)
303      * @see org.eclipse.compare.CompareEditorInput#createContents(org.eclipse.swt.widgets.Composite)
304      */

305     public Control createContents(Composite parent) {
306         if (shouldCreateRememberButton()) {
307             Composite composite = new Composite(parent, SWT.NONE);
308             composite.setLayout(new GridLayout());
309             Control control = super.createContents(composite);
310             control.setLayoutData(new GridData(GridData.FILL_BOTH));
311             rememberParticipantButton = new Button(composite, SWT.CHECK);
312             rememberParticipantButton.setText(TeamUIMessages.ParticipantCompareDialog_1);
313             rememberParticipantButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
314             return composite;
315         }
316         return super.createContents(parent);
317     }
318     
319     /**
320      * Return whether the ability to remember the participant in the synchronize
321      * view should be presented to the user. By default, <code>true</code> is
322      * returned. Subclasses may override.
323      * @return whether the ability to remember the participant in the synchronize
324      * view should be presented to the user
325      */

326     protected boolean isOfferToRememberParticipant() {
327         return true;
328     }
329
330     private boolean shouldCreateRememberButton() {
331         return isOfferToRememberParticipant() && participant != null && ! particantRegisteredWithSynchronizeManager(participant);
332     }
333     
334     private boolean isRememberParticipant() {
335         return getParticipant() != null && rememberParticipantButton != null && rememberParticipantButton.getSelection();
336     }
337     
338     private boolean particantRegisteredWithSynchronizeManager(ISynchronizeParticipant participant) {
339         return TeamUI.getSynchronizeManager().get(participant.getId(), participant.getSecondaryId()) != null;
340     }
341     
342     private void rememberParticipant() {
343         if(getParticipant() != null) {
344             ISynchronizeManager mgr = TeamUI.getSynchronizeManager();
345             ISynchronizeView view = mgr.showSynchronizeViewInActivePage();
346             mgr.addSynchronizeParticipants(new ISynchronizeParticipant[] {getParticipant()});
347             view.display(participant);
348         }
349     }
350     
351     /* (non-Javadoc)
352      * @see org.eclipse.compare.CompareEditorInput#okPressed()
353      */

354     public boolean okPressed() {
355         if (isEditable()) {
356             // If the CompareConfiguration is editable, then OK is Save and we want to leave the dialog open
357
super.okPressed();
358             return false;
359         }
360         // If the CompareConfiguration is not editable, then the OK button is the done button
361
if (isRememberParticipant())
362             rememberParticipant();
363         return super.okPressed();
364     }
365     
366     /* (non-Javadoc)
367      * @see org.eclipse.compare.CompareEditorInput#cancelPressed()
368      */

369     public void cancelPressed() {
370         // If the CompareConfiguration is editable, then the CANCEL button is the done button
371
if (isEditable() && isRememberParticipant())
372             rememberParticipant();
373         super.cancelPressed();
374     }
375     
376     /* (non-Javadoc)
377      * @see org.eclipse.compare.CompareEditorInput#getOKButtonLabel()
378      */

379     public String JavaDoc getOKButtonLabel() {
380         if (isEditable())
381             return TeamUIMessages.ParticipantPageCompareEditorInput_0;
382         return TeamUIMessages.ResourceMappingMergeOperation_2;
383     }
384     
385     private boolean isEditable() {
386         return getCompareConfiguration().isLeftEditable()
387             || getCompareConfiguration().isRightEditable();
388     }
389     
390     /* (non-Javadoc)
391      * @see org.eclipse.compare.CompareEditorInput#getCancelButtonLabel()
392      */

393     public String JavaDoc getCancelButtonLabel() {
394         return TeamUIMessages.ResourceMappingMergeOperation_2;
395     }
396
397 }
398
Popular Tags