KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > PageCompareEditorInput


1 /*******************************************************************************
2  * Copyright (c) 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.ui;
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.runtime.IProgressMonitor;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.team.internal.ui.Utils;
25 import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.part.IPage;
28 import org.eclipse.ui.progress.IProgressService;
29
30 /**
31  * Abstract class for hosting a page based structure input view for the purposes
32  * of feeding compare viewers.
33  * <p>
34  * This class is not intended to be subclassed by clients outside of the Team framework.
35  *
36  * @since 3.3
37  */

38 public abstract class PageCompareEditorInput extends CompareEditorInput implements IContentChangeListener {
39
40     private CompareViewerPane pagePane;
41     private ICompareInput hookedInput;
42
43     /**
44      * Create a page compare editor input.
45      * @param configuration the compare configuration
46      */

47     protected PageCompareEditorInput(CompareConfiguration configuration) {
48         super(configuration);
49     }
50     
51     /* (non-Javadoc)
52      * @see org.eclipse.compare.CompareEditorInput#createStructureInputPane(org.eclipse.swt.widgets.Composite)
53      */

54     protected CompareViewerPane createStructureInputPane(Composite parent) {
55         pagePane = new CompareViewerPane(parent, SWT.BORDER | SWT.FLAT) {
56             public void selectionChanged(SelectionChangedEvent ev) {
57                 ISelection selection = ev.getSelection();
58                 StructuredSelection newSelection = convertSelection(selection, false);
59                 SelectionChangedEvent newEv = new SelectionChangedEvent(pagePane, newSelection);
60                 super.selectionChanged(newEv);
61             }
62             private StructuredSelection convertSelection(ISelection selection, boolean prepare) {
63                 ICompareInput ci = asCompareInput(selection);
64                 StructuredSelection newSelection;
65                 if (ci != null) {
66                     if (prepare)
67                         prepareCompareInput(ci);
68                     newSelection = new StructuredSelection(ci);
69                 } else {
70                     newSelection = StructuredSelection.EMPTY;
71                 }
72                 return newSelection;
73             }
74             public ISelection getSelection() {
75                 return convertSelection(getSelectionProvider().getSelection(), false);
76             }
77             public Object JavaDoc getInput() {
78                 return PageCompareEditorInput.this.getCompareResult();
79             }
80             public void open(OpenEvent event) {
81                 ISelection selection = event.getSelection();
82                 StructuredSelection newSelection = convertSelection(selection, true);
83                 super.open(new OpenEvent((Viewer)event.getSource(), newSelection));
84             }
85             public void doubleClick(DoubleClickEvent event) {
86                 ISelection selection = event.getSelection();
87                 StructuredSelection newSelection = convertSelection(selection, true);
88                 super.doubleClick(new DoubleClickEvent((Viewer)event.getSource(), newSelection));
89             }
90             
91             public void setInput(Object JavaDoc input) {
92                 super.setInput(input);
93                 Composite c = getParent();
94                 if (c instanceof Splitter)
95                     ((Splitter)c).setVisible(this, true);
96                 layout(true);
97             }
98         };
99         ToolBarManager toolBarManager = CompareViewerPane.getToolBarManager(pagePane);
100         IPage page = createPage(pagePane, toolBarManager);
101         pagePane.setContent(page.getControl());
102         if (parent instanceof Splitter)
103             ((Splitter)parent).setVisible(pagePane, false);
104         hookupListeners();
105         return pagePane;
106     }
107     
108     /**
109      * Create the page for this part and return the top level control
110      * for the page.
111      * @param parent the parent composite
112      * @param toolBarManager the toolbar manager for the page
113      * @return the top-level control for the page
114      */

115     protected abstract IPage createPage(CompareViewerPane parent, IToolBarManager toolBarManager);
116     
117     /**
118      * Return the selection provider for the page. This method is
119      * called after the page is created in order to register a
120      * selection listener on the page.
121      * @return the selection provider for the page
122      */

123     protected abstract ISelectionProvider getSelectionProvider();
124     
125     /**
126      * Set the title of the page's page to the given text. The title
127      * will appear in the header of the pane containing the page.
128      * @param title the page's title
129      */

130     protected void setPageDescription(String JavaDoc title) {
131         pagePane.setText(title);
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.compare.CompareEditorInput#handleDispose()
136      */

137     protected void handleDispose() {
138         super.handleDispose();
139         cleanupListeners();
140         unhookContentChangeListener();
141     }
142     
143     private void hookupListeners() {
144         ISelectionProvider selectionProvider = getSelectionProvider();
145         if (selectionProvider != null)
146             selectionProvider.addSelectionChangedListener(pagePane);
147         if (selectionProvider instanceof StructuredViewer) {
148             StructuredViewer sv = (StructuredViewer) selectionProvider;
149             sv.addOpenListener(pagePane);
150             sv.addDoubleClickListener(pagePane);
151         }
152     }
153     
154     private void cleanupListeners() {
155         ISelectionProvider selectionProvider = getSelectionProvider();
156         if (selectionProvider != null)
157             selectionProvider.removeSelectionChangedListener(pagePane);
158         if (selectionProvider instanceof StructuredViewer) {
159             StructuredViewer sv = (StructuredViewer) selectionProvider;
160             sv.removeOpenListener(pagePane);
161             sv.removeDoubleClickListener(pagePane);
162         }
163     }
164     
165     private void hookContentChangeListener(ICompareInput node) {
166         if (hookedInput == node)
167             return;
168         unhookContentChangeListener();
169         hookedInput = node;
170         ITypedElement left = node.getLeft();
171         if(left instanceof IContentChangeNotifier) {
172             ((IContentChangeNotifier)left).addContentChangeListener(this);
173         }
174         ITypedElement right = node.getRight();
175         if(right instanceof IContentChangeNotifier) {
176             ((IContentChangeNotifier)right).addContentChangeListener(this);
177         }
178     }
179     
180     private void unhookContentChangeListener() {
181         if (hookedInput != null) {
182             ITypedElement left = hookedInput.getLeft();
183             if(left instanceof IContentChangeNotifier) {
184                 ((IContentChangeNotifier)left).addContentChangeListener(this);
185             }
186             ITypedElement right = hookedInput.getRight();
187             if(right instanceof IContentChangeNotifier) {
188                 ((IContentChangeNotifier)right).addContentChangeListener(this);
189             }
190         }
191     }
192
193     /**
194      * Return a compare input that represents the selection.
195      * This input is used to feed the structure and content
196      * viewers. By default, a compare input is returned if the selection is
197      * of size 1 and the selected element implements <code>ICompareInput</code>.
198      * Subclasses may override.
199      * @param selection the selection
200      * @return a compare input representing the selection
201      */

202     protected ICompareInput asCompareInput(ISelection selection) {
203         if (selection != null && selection instanceof IStructuredSelection) {
204             IStructuredSelection ss= (IStructuredSelection) selection;
205             if (ss.size() == 1) {
206                 Object JavaDoc o = ss.getFirstElement();
207                 if(o instanceof ICompareInput) {
208                     return (ICompareInput)o;
209                 }
210             }
211         }
212         return null;
213     }
214     
215     /**
216      * Convenience method that calls {@link #prepareInput(ICompareInput, CompareConfiguration, IProgressMonitor)}
217      * with a progress monitor.
218      * @param input the compare input to be prepared
219      */

220     protected final void prepareCompareInput(final ICompareInput input) {
221         if (input == null)
222             return;
223         // Don't allow the use of shared documents with PageSaveableParts
224
Object JavaDoc left = input.getLeft();
225         if (left instanceof LocalResourceTypedElement) {
226             LocalResourceTypedElement lrte = (LocalResourceTypedElement) left;
227             lrte.enableSharedDocument(false);
228         }
229         IProgressService manager = PlatformUI.getWorkbench().getProgressService();
230         try {
231             // TODO: we need a better progress story here (i.e. support for cancellation) bug 127075
232
manager.busyCursorWhile(new IRunnableWithProgress() {
233                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
234                     prepareInput(input, getCompareConfiguration(), monitor);
235                     hookContentChangeListener(input);
236                 }
237             });
238         } catch (InvocationTargetException JavaDoc e) {
239             Utils.handle(e);
240         } catch (InterruptedException JavaDoc e) {
241             // Ignore
242
}
243     }
244     
245     /* (non-Javadoc)
246      * @see org.eclipse.compare.IContentChangeListener#contentChanged(org.eclipse.compare.IContentChangeNotifier)
247      */

248     public void contentChanged(IContentChangeNotifier source) {
249         setDirty(true);
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.compare.CompareEditorInput#canRunInBackground()
254      */

255     public boolean canRunAsJob() {
256         return true;
257     }
258     
259     /**
260      * Prepare the compare input for display in a content viewer. This method is
261      * called from {@link #prepareCompareInput(ICompareInput)} and may be called
262      * from a non-UI thread. This method should not be called by others.
263      * @param input the input
264      * @param configuration the compare configuration
265      * @param monitor a progress monitor
266      * @throws InvocationTargetException
267      */

268     protected abstract void prepareInput(ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException JavaDoc;
269
270
271 }
272
Popular Tags