KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ToolBarManager;
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.graphics.Image;
24 import org.eclipse.swt.widgets.*;
25 import org.eclipse.team.internal.ui.*;
26 import org.eclipse.team.internal.ui.synchronize.*;
27 import org.eclipse.team.ui.PageCompareEditorInput;
28 import org.eclipse.team.ui.PageSaveablePart;
29 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput;
30 import org.eclipse.team.ui.mapping.SaveableComparison;
31 import org.eclipse.ui.PartInitException;
32 import org.eclipse.ui.part.IPageBookViewPage;
33
34 /**
35  * Displays a synchronize participant page combined with the compare/merge infrastructure. This only works if the
36  * synchronize page viewer provides selections that are of the following types: ITypedElement and ICompareInput
37  * or if the participant is a {@link ModelSynchronizeParticipant}.
38  *
39  * @since 3.0
40  * @deprecated Clients should use a subclass of {@link PageCompareEditorInput}
41  * and {@link CompareUI#openCompareDialog(org.eclipse.compare.CompareEditorInput)}
42  */

43 public class ParticipantPageSaveablePart extends PageSaveablePart implements IContentChangeListener {
44
45     private ISynchronizeParticipant participant;
46     private ISynchronizePageConfiguration pageConfiguration;
47     private Image titleImage;
48
49     private IPageBookViewPage page;
50     private DialogSynchronizePageSite site;
51     
52     private IPropertyChangeListener listener;
53     private Viewer viewer;
54
55     /**
56      * Creates a part for the provided participant. The page configuration is used when creating the participant page and the resulting
57      * compare/merge panes will be configured with the provided compare configuration.
58      * <p>
59      * For example, clients can decide if the user can edit the compare panes by calling {@link CompareConfiguration#setLeftEditable(boolean)}
60      * or {@link CompareConfiguration#setRightEditable(boolean)}.
61      * </p>
62      * @param shell the parent shell for this part
63      * @param cc 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 ParticipantPageSaveablePart(Shell shell, CompareConfiguration cc, ISynchronizePageConfiguration pageConfiguration, ISynchronizeParticipant participant) {
68         super(shell,cc);
69         this.participant = participant;
70         this.pageConfiguration = pageConfiguration;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.team.ui.SaveablePartAdapter#dispose()
75      */

76     public void dispose() {
77         if(titleImage != null) {
78             titleImage.dispose();
79         }
80         if (page != null)
81             page.dispose();
82         if (site != null)
83             site.dispose();
84         pageConfiguration.removePropertyChangeListener(listener);
85         super.dispose();
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.ui.IWorkbenchPart#getTitleImage()
90      */

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

101     public String JavaDoc getTitle() {
102         return Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName());
103     }
104
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.ISaveablePart#isDirty()
107      */

108     public boolean isDirty() {
109         if (participant instanceof ModelSynchronizeParticipant) {
110             ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
111             SaveableComparison currentBuffer = msp.getActiveSaveable();
112             if (currentBuffer != null) {
113                 return currentBuffer.isDirty();
114             }
115         }
116         return super.isDirty();
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.compare.IContentChangeListener#contentChanged(org.eclipse.compare.IContentChangeNotifier)
121      */

122     public void contentChanged(IContentChangeNotifier source) {
123         try {
124             if (source instanceof DiffNode) {
125                 commit(new NullProgressMonitor(), (DiffNode) source);
126             } else if (source instanceof LocalResourceTypedElement) {
127                  ((LocalResourceTypedElement) source).commit(new NullProgressMonitor());
128             }
129         } catch (CoreException e) {
130             Utils.handle(e);
131         }
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
136      */

137     public void doSave(IProgressMonitor pm) {
138         // TODO needs to work for models
139
super.doSave(pm);
140         Object JavaDoc input = viewer.getInput();
141         if (input instanceof ISynchronizeModelElement) {
142             ISynchronizeModelElement root = (ISynchronizeModelElement)input;
143             if (root != null && root instanceof DiffNode) {
144                 try {
145                     commit(pm, (DiffNode)root);
146                 } catch (CoreException e) {
147                     Utils.handle(e);
148                 } finally {
149                     setDirty(false);
150                 }
151             }
152         }
153     }
154     
155     /* (non-Javadoc)
156      * @see org.eclipse.team.ui.PageSaveablePart#createPage(org.eclipse.swt.widgets.Composite, org.eclipse.jface.action.ToolBarManager)
157      */

158     protected Control createPage(Composite parent, ToolBarManager toolBarManager) {
159         listener = new IPropertyChangeListener() {
160             public void propertyChange(PropertyChangeEvent event) {
161                 if (event.getProperty().equals(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION)) {
162                     updateDescription();
163                 }
164             }
165         };
166         pageConfiguration.addPropertyChangeListener(listener);
167         updateDescription();
168
169         page = participant.createPage(pageConfiguration);
170         site = new DialogSynchronizePageSite(getShell(), true);
171         ((SynchronizePageConfiguration)pageConfiguration).setSite(site);
172         site.createActionBars(toolBarManager);
173         try {
174             ((ISynchronizePage)page).init(pageConfiguration.getSite());
175         } catch (PartInitException e1) {
176         }
177
178         page.createControl(parent);
179         
180         initializeDiffViewer(((ISynchronizePage)page).getViewer());
181         
182         page.setActionBars(site.getActionBars());
183         toolBarManager.update(true);
184         viewer = ((ISynchronizePage)page).getViewer();
185         
186         setNavigator(pageConfiguration);
187         return page.getControl();
188     }
189
190     /* (non-Javadoc)
191      * @see org.eclipse.team.ui.PageSaveablePart#getPageSelectionProvider()
192      */

193     protected final ISelectionProvider getSelectionProvider() {
194         return ((ISynchronizePage)page).getViewer();
195     }
196     
197     private void updateDescription() {
198         String JavaDoc description = (String JavaDoc)pageConfiguration.getProperty(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION);
199         if (description != null) {
200             setPageDescription(description);
201         }
202     }
203     
204     /**
205      * Initialize the diff viewer created for this compare input. If a subclass
206      * overrides the <code>createDiffViewer(Composite)</code> method, it should
207      * invoke this method on the created viewer in order to get the proper
208      * labeling in the compare input's contents viewers.
209      * @param viewer the diff viewer created by the compare input
210      */

211     private void initializeDiffViewer(Viewer viewer) {
212         if (viewer instanceof StructuredViewer) {
213             ((StructuredViewer) viewer).addOpenListener(new IOpenListener() {
214                 public void open(OpenEvent event) {
215                     ISelection s = event.getSelection();
216                     final SyncInfoModelElement node = getElement(s);
217                     if (node == null) {
218                         ICompareInput input = getCompareInput(s);
219                         if (input != null) {
220                             prepareCompareInput(input);
221                         }
222                     } else {
223                         prepareCompareInput(node);
224                     }
225                 }
226             });
227         }
228     }
229     
230     /**
231      * {@inheritDoc}
232      * @since 3.2
233      */

234     protected void prepareInput(final ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
235         monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100);
236         monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3);
237         try {
238             // First, see if the active buffer is changing
239
checkForBufferChange(pageConfiguration.getSite().getShell(), input, false /* cancel not allowed */, monitor);
240             if (input instanceof SyncInfoModelElement) {
241                 final SyncInfoModelElement node = (SyncInfoModelElement) input;
242                 IResource resource = node.getResource();
243                 if (resource != null && resource.getType() == IResource.FILE) {
244                     participant.prepareCompareInput(node, configuration, monitor);
245                 }
246             } else {
247                 ISynchronizationCompareInput adapter = asModelCompareInput(input);
248                 if (adapter != null) {
249                     adapter.prepareInput(configuration, Policy.subMonitorFor(monitor, 90));
250                 }
251             }
252         } catch (CoreException e) {
253             throw new InvocationTargetException JavaDoc(e);
254         } finally {
255             monitor.done();
256         }
257     }
258
259     private void checkForBufferChange(Shell shell, final ICompareInput input, boolean cancelAllowed, IProgressMonitor monitor) throws CoreException {
260         ISynchronizeParticipant participant = pageConfiguration.getParticipant();
261         if (participant instanceof ModelSynchronizeParticipant) {
262             ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
263             if (input instanceof ISynchronizationCompareInput) {
264                 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input;
265                 msp.checkForBufferChange(shell, mci, cancelAllowed, monitor);
266             }
267         }
268     }
269
270     private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) {
271         return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class);
272     }
273     
274     private SyncInfoModelElement getElement(ISelection selection) {
275         ICompareInput input = getCompareInput(selection);
276         if(input instanceof SyncInfoModelElement) {
277             return (SyncInfoModelElement)input;
278         }
279         return null;
280     }
281     
282     private static void commit(IProgressMonitor pm, DiffNode node) throws CoreException {
283         ITypedElement left = node.getLeft();
284         if (left instanceof LocalResourceTypedElement)
285              ((LocalResourceTypedElement) left).commit(pm);
286
287         ITypedElement right = node.getRight();
288         if (right instanceof LocalResourceTypedElement)
289              ((LocalResourceTypedElement) right).commit(pm);
290         
291         IDiffElement[] children = node.getChildren();
292         for (int i = 0; i < children.length; i++) {
293             commit(pm, (DiffNode)children[i]);
294         }
295     }
296     
297     /**
298      * Return the synchronize page configuration for this part
299      *
300      * @return Returns the pageConfiguration.
301      */

302     public ISynchronizePageConfiguration getPageConfiguration() {
303         return pageConfiguration;
304     }
305     
306     /**
307      * Return the Synchronize participant for this part
308      *
309      * @return Returns the participant.
310      */

311     public ISynchronizeParticipant getParticipant() {
312         return participant;
313     }
314
315     /**
316      * {@inheritDoc}
317      * @since 3.2
318      */

319     protected ICompareInput getCompareInput(ISelection selection) {
320         ICompareInput compareInput = super.getCompareInput(selection);
321         if (compareInput != null)
322             return compareInput;
323         
324         if (selection != null && selection instanceof IStructuredSelection) {
325             IStructuredSelection ss= (IStructuredSelection) selection;
326             if (ss.size() == 1) {
327                 Object JavaDoc o = ss.getFirstElement();
328                 if (participant instanceof ModelSynchronizeParticipant) {
329                     ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
330                     return msp.asCompareInput(o);
331                 }
332             }
333         }
334         return null;
335     }
336     
337     
338
339 }
340
Popular Tags