KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > AbstractSynchronizePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.internal.ui.synchronize;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.jface.action.*;
15 import org.eclipse.jface.dialogs.IDialogSettings;
16 import org.eclipse.jface.viewers.*;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.team.internal.ui.Policy;
23 import org.eclipse.team.internal.ui.Utils;
24 import org.eclipse.team.internal.ui.synchronize.actions.DirectionFilterActionGroup;
25 import org.eclipse.team.ui.synchronize.*;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.IPageLayout;
28 import org.eclipse.ui.part.*;
29
30 /**
31  * Abstract synchronize page that can filter changes by mode (incoming, outgoing,
32  * both or conflicting). It also uses forms to indicate when a model is empty and
33  * provide a link to a non-empty mode.
34  */

35 public abstract class AbstractSynchronizePage extends Page implements ISynchronizePage, IAdaptable {
36     
37     private ISynchronizePageConfiguration configuration;
38     private ISynchronizePageSite site;
39     
40     // Parent composite of this view. It is remembered so that we can dispose of its children when
41
// the viewer type is switched.
42
private Composite composite;
43     private ChangesSection changesSection;
44     private Viewer changesViewer;
45     
46     private AbstractViewerAdvisor viewerAdvisor;
47     
48     /*
49      * Contribute actions for changing modes to the page.
50      */

51     class ModeFilterActions extends SynchronizePageActionGroup {
52         private DirectionFilterActionGroup modes;
53         public void initialize(ISynchronizePageConfiguration configuration) {
54             super.initialize(configuration);
55             if (isThreeWay()) {
56                 modes = new DirectionFilterActionGroup(configuration);
57             }
58         }
59         public void fillActionBars(IActionBars actionBars) {
60             super.fillActionBars(actionBars);
61             if (modes == null) return;
62             IToolBarManager manager = actionBars.getToolBarManager();
63             IContributionItem group = findGroup(manager, ISynchronizePageConfiguration.MODE_GROUP);
64             if (manager != null && group != null) {
65                 modes.fillToolBar(group.getId(), manager);
66             }
67             IMenuManager viewMenu = actionBars.getMenuManager();
68             group = findGroup(manager, ISynchronizePageConfiguration.MODE_GROUP);
69             if (viewMenu != null && group != null) {
70                 IContributionItem layoutGroup = findGroup(manager, ISynchronizePageConfiguration.LAYOUT_GROUP);
71                 if (layoutGroup != null) {
72                     // Put the modes in the layout group to save space
73
group = layoutGroup;
74                 }
75                 MenuManager modesItem = new MenuManager(Utils.getString("action.modes.label", Policy.getActionBundle())); //$NON-NLS-1$
76
viewMenu.appendToGroup(group.getId(), modesItem);
77                 modes.fillMenu(modesItem);
78             }
79         }
80         private boolean isThreeWay() {
81             return ISynchronizePageConfiguration.THREE_WAY.equals(configuration.getComparisonType());
82         }
83     }
84     
85     /**
86      * Create a new instance of the page
87      * @param configuration a synchronize page configuration
88      */

89     protected AbstractSynchronizePage(ISynchronizePageConfiguration configuration) {
90         this.configuration = configuration;
91         configuration.setPage(this);
92         configuration.addActionContribution(new ModeFilterActions());
93     }
94     
95     /* (non-Javadoc)
96      * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
97      */

98     public void createControl(Composite parent) {
99         composite = new Composite(parent, SWT.NONE);
100         //sc.setContent(composite);
101
GridLayout gridLayout= new GridLayout();
102         gridLayout.makeColumnsEqualWidth= false;
103         gridLayout.marginWidth= 0;
104         gridLayout.marginHeight = 0;
105         gridLayout.verticalSpacing = 0;
106         composite.setLayout(gridLayout);
107         GridData data = new GridData(GridData.FILL_BOTH);
108         data.grabExcessVerticalSpace = true;
109         composite.setLayoutData(data);
110         
111         // Create the changes section which, in turn, creates the changes viewer and its configuration
112
this.changesSection = createChangesSection(composite);
113         this.changesViewer = createChangesViewer(changesSection.getContainer());
114         changesSection.setViewer(changesViewer);
115     }
116
117     /**
118      * Create the changes section that will contain the changes viewer.
119      * @return the changes section that will contain the changes viewer
120      */

121     protected abstract ChangesSection createChangesSection(Composite parent);
122     
123     /**
124      * Return the viewer that will display the changes associated
125      * with the page.
126      *
127      * @param parent the parent of the viewer
128      * @return the changes viewer control
129      */

130     protected Viewer createChangesViewer(Composite parent) {
131         viewerAdvisor = createViewerAdvisor(parent);
132         return viewerAdvisor.getViewer();
133     }
134
135     protected abstract AbstractViewerAdvisor createViewerAdvisor(Composite parent);
136     
137     public AbstractViewerAdvisor getViewerAdvisor() {
138         return viewerAdvisor;
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.ui.part.IPage#setActionBars(org.eclipse.ui.IActionBars)
143      */

144     public void setActionBars(IActionBars actionBars) {
145         // Delegate menu creation to the advisor
146
viewerAdvisor.setActionBars(actionBars);
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.ui.part.IPage#getControl()
151      */

152     public Control getControl() {
153         return composite;
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.ui.part.IPage#setFocus()
158      */

159     public void setFocus() {
160         changesSection.setFocus();
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.team.ui.synchronize.ISynchronizePage#init(org.eclipse.team.ui.synchronize.ISynchronizePageSite)
165      */

166     public void init(ISynchronizePageSite site) {
167         this.site = site;
168         IDialogSettings settings = getSettings();
169         if (settings != null) {
170             try {
171                 int mode = settings.getInt(ISynchronizePageConfiguration.P_MODE);
172                 if (mode != 0) {
173                     configuration.setMode(mode);
174                 }
175             } catch (NumberFormatException JavaDoc e) {
176                 // The mode settings does not exist.
177
// Leave the mode as is (assuming the
178
// participant initialized it to an
179
// appropriate value
180
}
181         }
182     }
183     
184     /* (non-Javadoc)
185      * @see org.eclipse.ui.part.Page#dispose()
186      */

187     public void dispose() {
188         changesSection.dispose();
189         composite.dispose();
190         super.dispose();
191     }
192     
193     /* (non-Javadoc)
194      * @see org.eclipse.team.ui.synchronize.ISynchronizePage#getViewer()
195      */

196     public Viewer getViewer() {
197         return changesViewer;
198     }
199     
200     /* (non-Javadoc)
201      * @see org.eclipse.team.ui.synchronize.ISynchronizePage#aboutToChangeProperty(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration, java.lang.String, java.lang.Object)
202      */

203     public boolean aboutToChangeProperty(
204             ISynchronizePageConfiguration configuration, String JavaDoc key,
205             Object JavaDoc newValue) {
206         if (key.equals(ISynchronizePageConfiguration.P_MODE)) {
207             return (internalSetMode(configuration.getMode(), ((Integer JavaDoc)newValue).intValue()));
208         }
209         return true;
210     }
211
212     private boolean internalSetMode(int oldMode, int mode) {
213         if(oldMode == mode) return false;
214         updateMode(mode);
215         IDialogSettings settings = getSettings();
216         if (settings != null) {
217             settings.put(ISynchronizePageConfiguration.P_MODE, mode);
218         }
219         return true;
220     }
221
222     /*
223      * This method enables "Show In" support for this view
224      *
225      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
226      */

227     public Object JavaDoc getAdapter(Class JavaDoc key) {
228         if (key.equals(ISelectionProvider.class))
229             return changesViewer;
230         if (key == IShowInSource.class) {
231             return new IShowInSource() {
232                 public ShowInContext getShowInContext() {
233                     StructuredViewer v = (StructuredViewer)changesViewer;
234                     if (v == null) return null;
235                     ISelection s = v.getSelection();
236                     if (s instanceof IStructuredSelection) {
237                         Object JavaDoc[] resources = Utils.getResources(((IStructuredSelection)s).toArray());
238                         return new ShowInContext(null, new StructuredSelection(resources));
239                     }
240                     return null;
241                 }
242             };
243         }
244         if (key == IShowInTargetList.class) {
245             return new IShowInTargetList() {
246                 public String JavaDoc[] getShowInTargetIds() {
247                     return new String JavaDoc[] { IPageLayout.ID_RES_NAV };
248                 }
249
250             };
251         }
252         return null;
253     }
254     
255     /**
256      * Return the page site that was assigned to this page.
257      * @return the page site that was assigned to this page
258      */

259     public ISynchronizePageSite getSynchronizePageSite() {
260         return site;
261     }
262     
263     /**
264      * Return the synchronize page configuration that was used to create
265      * this page.
266      * @return Returns the configuration.
267      */

268     public ISynchronizePageConfiguration getConfiguration() {
269         return configuration;
270     }
271
272     /**
273      * Return the settings for the page from the configuration
274      * os <code>null</code> if settings can not be persisted
275      * for the page
276      * @return the persisted page settings
277      */

278     protected IDialogSettings getSettings() {
279         return configuration.getSite().getPageSettings();
280     }
281
282     /**
283      * Callback from the changes section that indicates that the
284      * user has chosen to reset the view contents after an error
285      * has occurred
286      */

287     public abstract void reset();
288     
289     /**
290      * Change the mode to the given mode. This method is invoked
291      * when the mode in the configuration is changed by a client.
292      * @param mode the mode to be used
293      */

294     protected abstract void updateMode(int mode);
295
296     public ChangesSection getChangesSection() {
297         return changesSection;
298     }
299 }
300
Popular Tags