KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.synchronize;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.events.DisposeEvent;
23 import org.eclipse.swt.events.DisposeListener;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.team.core.diff.*;
26 import org.eclipse.team.core.mapping.ISynchronizationContext;
27 import org.eclipse.team.internal.ui.TeamUIMessages;
28 import org.eclipse.team.ui.mapping.*;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.BaseSelectionListenerAction;
31
32 /**
33  * Model provider actions for use with a {@link ModelSynchronizeParticipant}.
34  *
35  * @since 3.2
36  */

37 public abstract class ModelParticipantAction extends BaseSelectionListenerAction {
38
39     private final ISynchronizePageConfiguration configuration;
40
41     /**
42      * Create the model participant action.
43      * @param text the label of the action or <code>null</code>
44      * @param configuration the configuration for the page that is surfacing the action
45      */

46     public ModelParticipantAction(String JavaDoc text, ISynchronizePageConfiguration configuration) {
47         super(text);
48         this.configuration = configuration;
49         initialize(configuration);
50     }
51
52     private void initialize(ISynchronizePageConfiguration configuration) {
53         configuration.getSite().getSelectionProvider().addSelectionChangedListener(this);
54         configuration.getPage().getViewer().getControl().addDisposeListener(new DisposeListener() {
55             public void widgetDisposed(DisposeEvent e) {
56                 getConfiguration().getSite().getSelectionProvider().removeSelectionChangedListener(ModelParticipantAction.this);
57             }
58         });
59     }
60
61     /**
62      * Return the page configuration.
63      * @return the page configuration
64      */

65     protected ISynchronizePageConfiguration getConfiguration() {
66         return configuration;
67     }
68     
69     /**
70      * Set the selection of this action to the given selection
71      *
72      * @param selection the selection
73      */

74     public void selectionChanged(ISelection selection) {
75         if (selection instanceof IStructuredSelection) {
76             super.selectionChanged((IStructuredSelection)selection);
77         } else {
78             super.selectionChanged(StructuredSelection.EMPTY);
79         }
80         
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
85      */

86     protected boolean updateSelection(IStructuredSelection selection) {
87         super.updateSelection(selection);
88         return isEnabledForSelection(selection);
89     }
90     
91     /**
92      * Return whether the action is enabled for the given selection
93      * @param selection the selection
94      * @return whether the action is enabled for the given selection
95      */

96     protected abstract boolean isEnabledForSelection(IStructuredSelection selection);
97
98     /**
99      * Return the synchronization context associated with this action.
100      * @return the synchronization context associated with this action
101      */

102     protected ISynchronizationContext getSynchronizationContext() {
103         return (ISynchronizationContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT);
104     }
105
106     /**
107      * Return whether the given node is visible in the page based
108      * on the mode in the configuration.
109      * @param node a diff node
110      * @return whether the given node is visible in the page
111      */

112     protected boolean isVisible(IDiff node) {
113         ISynchronizePageConfiguration configuration = getConfiguration();
114         if (configuration.getComparisonType() == ISynchronizePageConfiguration.THREE_WAY
115                 && node instanceof IThreeWayDiff) {
116             IThreeWayDiff twd = (IThreeWayDiff) node;
117             int mode = configuration.getMode();
118             switch (mode) {
119             case ISynchronizePageConfiguration.INCOMING_MODE:
120                 if (twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) {
121                     return true;
122                 }
123                 break;
124             case ISynchronizePageConfiguration.OUTGOING_MODE:
125                 if (twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.OUTGOING) {
126                     return true;
127                 }
128                 break;
129             case ISynchronizePageConfiguration.CONFLICTING_MODE:
130                 if (twd.getDirection() == IThreeWayDiff.CONFLICTING) {
131                     return true;
132                 }
133                 break;
134             case ISynchronizePageConfiguration.BOTH_MODE:
135                 return true;
136             }
137         } else if (configuration.getComparisonType() == ISynchronizePageConfiguration.TWO_WAY
138                 && node instanceof ITwoWayDiff) {
139             return true;
140         }
141         return false;
142     }
143     
144     /**
145      * Check to see if the target saveable differs from the currently
146      * active saveable. If it does, prompt to save changes in the
147      * active saveable if it is dirty.
148      * @throws InterruptedException
149      * @throws InvocationTargetException
150      */

151     protected void handleTargetSaveableChange() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
152         final SaveableComparison targetSaveable = getTargetSaveable();
153         final SaveableComparison activeSaveable = getActiveSaveable();
154         if (activeSaveable != null && activeSaveable.isDirty()) {
155             PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
156                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
157                         InterruptedException JavaDoc {
158                     try {
159                         handleTargetSaveableChange(configuration.getSite().getShell(), targetSaveable, activeSaveable, true, monitor);
160                     } catch (CoreException e) {
161                         throw new InvocationTargetException JavaDoc(e);
162                     }
163                 }
164             });
165         }
166         setActiveSaveable(targetSaveable);
167     }
168
169     /**
170      * Convenience method that prompts if the currently active saveable is dirty
171      * and either saves or reverts the saveable depending on the users input.
172      * @param shell a parent shell
173      * @param targetSaveable the new saveable
174      * @param activeSaveable the current saveable
175      * @param allowCancel whether canceling the action is an option
176      * @param monitor a progress monitor
177      * @throws CoreException
178      * @throws InterruptedException
179      */

180     public static void handleTargetSaveableChange(Shell shell, SaveableComparison targetSaveable, SaveableComparison activeSaveable, boolean allowCancel, IProgressMonitor monitor) throws CoreException, InterruptedException JavaDoc {
181         if (activeSaveable != null && targetSaveable != activeSaveable) {
182             if (activeSaveable.isDirty()) {
183                 if (promptToSaveChanges(shell, activeSaveable, allowCancel)) {
184                     activeSaveable.doSave(monitor);
185                 } else {
186                     activeSaveable.doRevert(monitor);
187                 }
188             }
189         }
190     }
191
192     /**
193      * Convenience method that prompts to save changes in the given dirty model.
194      * @param shell a shell
195      * @param saveable a dirty saveable model
196      * @param allowCancel whether canceling the action is an option
197      * @return whether the user choose to save (<code>true</code>) or revert (<code>false</code>() the model
198      * @throws InterruptedException thrown if the user choose to cancel
199      */

200     public static boolean promptToSaveChanges(final Shell shell, final SaveableComparison saveable, final boolean allowCancel) throws InterruptedException JavaDoc {
201         final int[] result = new int[] { 0 };
202         Runnable JavaDoc runnable = new Runnable JavaDoc() {
203             public void run() {
204                 String JavaDoc[] options;
205                 if (allowCancel) {
206                     options = new String JavaDoc[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL};
207                 } else {
208                     options = new String JavaDoc[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL};
209                 }
210                 MessageDialog dialog = new MessageDialog(
211                         shell,
212                         TeamUIMessages.ModelParticipantAction_0, null,
213                         NLS.bind(TeamUIMessages.ModelParticipantAction_1, saveable.getName()),
214                         MessageDialog.QUESTION,
215                         options,
216                         result[0]);
217                 result[0] = dialog.open();
218             }
219         };
220         shell.getDisplay().syncExec(runnable);
221         if (result[0] == 2)
222             throw new InterruptedException JavaDoc();
223         return result[0] == 0;
224     }
225
226     /**
227      * Return the currently active saveable. By default,
228      * the active saveable is obtained from the synchronization
229      * page configuration.
230      * @return the currently active saveable (or <code>null</code> if
231      * no buffer is active).
232      */

233     protected SaveableComparison getActiveSaveable() {
234         return ((ModelSynchronizeParticipant)configuration.getParticipant()).getActiveSaveable();
235     }
236
237     /**
238      * Set the active saveable. By default to active saveable is stored with the
239      * synchronize page configuration.
240      * @param saveable the saveable that is now active (or <code>null</code> if
241      * no saveable is active).
242      */

243     protected void setActiveSaveable(SaveableComparison saveable) {
244         ((ModelSynchronizeParticipant)configuration.getParticipant()).setActiveSaveable(saveable);
245     }
246     
247     /**
248      * Return the saveable that is the target of this operation.
249      * By default, <code>null</code> is returned.
250      * @return the saveable that is the target of this operation
251      */

252     protected SaveableComparison getTargetSaveable() {
253         return null;
254     }
255     
256     /**
257      * Method called when the action is about to be shown in a context menu.
258      * This method recalculates the enablement for the current
259      * selection and uses that to set the enablement.
260      */

261     public void updateEnablement() {
262         setEnabled(isEnabledForSelection(getStructuredSelection()));
263     }
264     
265 }
266
Popular Tags