KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > history > HistoryPageCompareEditorInput


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.history;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.CompareConfiguration;
16 import org.eclipse.compare.CompareViewerPane;
17 import org.eclipse.compare.structuremergeviewer.ICompareInput;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.action.IToolBarManager;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.team.internal.ui.TeamUIMessages;
26 import org.eclipse.team.internal.ui.Utils;
27 import org.eclipse.team.internal.ui.history.DialogHistoryPageSite;
28 import org.eclipse.team.ui.PageCompareEditorInput;
29 import org.eclipse.ui.part.IPage;
30 import org.eclipse.ui.part.Page;
31
32 /**
33  * Displays a history page combined with the compare/merge infrastructure. This only works properly if the
34  * history page adapts to an {@link IHistoryCompareAdapter}.
35  *
36  * @since 3.3
37  */

38 public class HistoryPageCompareEditorInput extends PageCompareEditorInput {
39
40     private IHistoryPage historyPage;
41     private DialogHistoryPageSite site;
42     private final Object JavaDoc object;
43     private final IHistoryPageSource pageSource;
44     private final IPropertyChangeListener changeListener = new IPropertyChangeListener() {
45         public void propertyChange(PropertyChangeEvent event) {
46             handlePropertyChange(event);
47         }
48     };
49     private boolean isReplace;
50     
51     /**
52      * Create a history page compare editor input for the given page and object.
53      * @param configuration the compare configuration
54      * @param pageSource the page source
55      * @param object the object whose history is to be displayed
56      */

57     public HistoryPageCompareEditorInput(CompareConfiguration configuration, IHistoryPageSource pageSource, Object JavaDoc object) {
58         super(configuration);
59         this.pageSource = pageSource;
60         this.object = object;
61     }
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.compare.CompareEditorInput#prepareInput(org.eclipse.core.runtime.IProgressMonitor)
65      */

66     protected Object JavaDoc prepareInput(IProgressMonitor monitor)
67             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
68         return object;
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.compare.CompareEditorInput#handleDispose()
73      */

74     protected void handleDispose() {
75         super.handleDispose();
76         if (historyPage != null) {
77             historyPage.removePropertyChangeListener(changeListener);
78             historyPage.dispose();
79         }
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.team.ui.PageCompareEditorInput#createPage(org.eclipse.compare.CompareViewerPane, org.eclipse.jface.action.IToolBarManager)
84      */

85     protected IPage createPage(CompareViewerPane parent, IToolBarManager toolBarManager) {
86         site = new DialogHistoryPageSite(parent.getShell());
87         historyPage = (IHistoryPage)pageSource.createPage(object);
88         historyPage.setSite(site);
89         site.setToolBarManager(toolBarManager);
90         ((Page) historyPage).createControl(parent);
91         historyPage.setInput(object);
92         String JavaDoc description = historyPage.getDescription();
93         if (description == null)
94             description = ""; //$NON-NLS-1$
95
setPageDescription(description);
96         if (getTitle() == null)
97             setTitle(historyPage.getName());
98         historyPage.addPropertyChangeListener(changeListener);
99         return (IPage)historyPage;
100     }
101     
102     /* (non-Javadoc)
103      * @see org.eclipse.team.ui.PageCompareEditorInput#asCompareInput(org.eclipse.jface.viewers.ISelection)
104      */

105     protected ICompareInput asCompareInput(ISelection selection) {
106         ICompareInput compareInput = super.asCompareInput(selection);
107         if (compareInput != null)
108             return compareInput;
109         IHistoryCompareAdapter compareAdapter = (IHistoryCompareAdapter) Utils.getAdapter(historyPage, IHistoryCompareAdapter.class);
110         if (compareAdapter != null){
111             if (selection instanceof IStructuredSelection) {
112                 IStructuredSelection ss= (IStructuredSelection) selection;
113                 if (ss.size() == 1) {
114                     Object JavaDoc o = ss.getFirstElement();
115                     return compareAdapter.getCompareInput(o);
116                 }
117             }
118         }
119         return null;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.team.ui.PageCompareEditorInput#getSelectionProvider()
124      */

125     protected ISelectionProvider getSelectionProvider() {
126         return site.getSelectionProvider();
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.team.ui.PageCompareEditorInput#prepareInput(org.eclipse.compare.structuremergeviewer.ICompareInput, org.eclipse.compare.CompareConfiguration, org.eclipse.core.runtime.IProgressMonitor)
131      */

132     protected void prepareInput(ICompareInput input,
133             CompareConfiguration configuration, IProgressMonitor monitor)
134             throws InvocationTargetException JavaDoc {
135         IHistoryCompareAdapter compareAdapter = (IHistoryCompareAdapter) Utils.getAdapter(historyPage, IHistoryCompareAdapter.class);
136         if (compareAdapter != null){
137             compareAdapter.prepareInput(input, configuration, monitor);
138         }
139     }
140
141     /**
142      * Return the history page for this input or <code>null</code> if the
143      * page hasn't been created yet.
144      * @return the history page for this input
145      */

146     public final IHistoryPage getHistoryPage() {
147         return historyPage;
148     }
149
150     /**
151      * Handle a property change event from the history page.
152      * @param event the change event
153      */

154     protected void handlePropertyChange(PropertyChangeEvent event) {
155         if (event.getSource() == historyPage) {
156             if (event.getProperty().equals(IHistoryPage.P_NAME)) {
157                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
158                     public void run() {
159                         setTitle(historyPage.getName());
160                     }
161                 });
162             } else if (event.getProperty().equals(IHistoryPage.P_DESCRIPTION)) {
163                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
164                     public void run() {
165                         setPageDescription(historyPage.getDescription());
166                     }
167                 });
168             }
169         }
170     }
171     
172     /* (non-Javadoc)
173      * @see org.eclipse.compare.CompareEditorInput#isEditionSelectionDialog()
174      */

175     public boolean isEditionSelectionDialog() {
176         return isReplaceDialog();
177     }
178
179     /**
180      * Return whether this compare editor input is being used in a replace
181      * dialog.
182      * @return whether this compare editor input is being used in a replace
183      * dialog
184      */

185     protected boolean isReplaceDialog() {
186         return isReplace;
187     }
188
189     /**
190      * Set whether this compare editor input is being used in a replace
191      * dialog.
192      * @param isReplace whether this compare editor input is being used in a replace
193      * dialog
194      */

195     public void setReplace(boolean isReplace) {
196         this.isReplace = isReplace;
197     }
198     
199     /* (non-Javadoc)
200      * @see org.eclipse.compare.CompareEditorInput#getOKButtonLabel()
201      */

202     public String JavaDoc getOKButtonLabel() {
203         if (isReplaceDialog())
204             return TeamUIMessages.HistoryPageCompareEditorInput_0;
205         return super.getOKButtonLabel();
206     }
207     
208     /* (non-Javadoc)
209      * @see org.eclipse.compare.CompareEditorInput#okPressed()
210      */

211     public boolean okPressed() {
212         if (!isReplaceDialog())
213             return super.okPressed();
214         try {
215             Object JavaDoc o = getSelectedEdition();
216             performReplace(((ICompareInput)o).getRight());
217         } catch (CoreException e) {
218             Utils.handle(e);
219             return false;
220         }
221         return true;
222     }
223     
224     /**
225      * A replace has been requested. This method will be
226      * invoked if {@link #isReplaceDialog()} is <code>true</code>
227      * and the user has clicked the "Replace" button.
228      * By default, this method does nothing.
229      * Subclasses may override.
230      * @param selectedObject the selected object
231      * @throws CoreException if an error occurs performing the replace
232      */

233     protected void performReplace(Object JavaDoc selectedObject) throws CoreException {
234         // By default, do nothing
235
}
236     
237 }
238
Popular Tags