KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.compare.structuremergeviewer.IDiffElement;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.swt.events.DisposeEvent;
20 import org.eclipse.swt.events.DisposeListener;
21 import org.eclipse.team.core.synchronize.*;
22 import org.eclipse.team.internal.ui.Utils;
23 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
24 import org.eclipse.ui.actions.BaseSelectionListenerAction;
25 import org.eclipse.ui.ide.IDE;
26
27 /**
28  * This action provides utilities for performing operations on selections that
29  * contain {@link org.eclipse.team.ui.synchronize.ISynchronizeModelElement}
30  * instances. Subclasses can use this support to filter the selection in order
31  * to determine action enablement and generate the input for a
32  * {@link SynchronizeModelOperation}.
33  *
34  * @see SyncInfo
35  * @see SyncInfoSet
36  * @see SynchronizeModelOperation
37  * @since 3.0
38  */

39 public abstract class SynchronizeModelAction extends BaseSelectionListenerAction {
40     
41     private ISynchronizePageConfiguration configuration;
42
43     /**
44      * Create an action with the given text and configuration. By default,
45      * the action registers for selection change with the selection provider
46      * from the configuration's site.
47      *
48      * @param text the action's text
49      * @param configuration the actions synchronize page configuration
50      */

51     protected SynchronizeModelAction(String JavaDoc text, ISynchronizePageConfiguration configuration) {
52         this(text, configuration, configuration.getSite().getSelectionProvider());
53     }
54     
55     /**
56      * Create an action with the given text and configuration. By default,
57      * the action registers for selection change with the given selection provider.
58      *
59      * @param text the action's text
60      * @param configuration the actions synchronize page configuration
61      * @param selectionProvider a selection provider
62      */

63     protected SynchronizeModelAction(String JavaDoc text, ISynchronizePageConfiguration configuration, ISelectionProvider selectionProvider) {
64         super(text);
65         this.configuration = configuration;
66         initialize(configuration, selectionProvider);
67     }
68     
69     /**
70      * Method invoked from the constructor.
71      * The default implementation registers the action as a selection change
72      * listener. Subclasses may override.
73      *
74      * @param configuration the synchronize page configuration
75      * @param selectionProvider a selection provider
76      */

77     protected void initialize(final ISynchronizePageConfiguration configuration, final ISelectionProvider selectionProvider) {
78         selectionProvider.addSelectionChangedListener(this);
79         configuration.getPage().getViewer().getControl().addDisposeListener(new DisposeListener() {
80             public void widgetDisposed(DisposeEvent e) {
81                 selectionProvider.removeSelectionChangedListener(SynchronizeModelAction.this);
82             }
83         });
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.jface.action.Action#run()
88      */

89     public void run() {
90         if(needsToSaveDirtyEditors()) {
91             if(!saveAllEditors(confirmSaveOfDirtyEditor())) {
92                 return;
93             }
94         }
95         try {
96             runOperation();
97         } catch (InvocationTargetException JavaDoc e) {
98             handle(e);
99         } catch (InterruptedException JavaDoc e) {
100             handle(e);
101         }
102     }
103
104     /**
105      * Create and run the operation for this action. By default, the operation is created
106      * by calling <code>getSubscriberOperation</code> and then run. Subclasses may
107      * override.
108      *
109      * @throws InvocationTargetException
110      * @throws InterruptedException
111      * @since 3.1
112      */

113     protected void runOperation() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
114         getSubscriberOperation(configuration, getFilteredDiffElements()).run();
115     }
116
117     /**
118      * Return whether dirty editor should be saved before this action is run.
119      * Default is <code>true</code>.
120      *
121      * @return whether dirty editor should be saved before this action is run
122      */

123     protected boolean needsToSaveDirtyEditors() {
124         return true;
125     }
126
127     /**
128      * Returns whether the user should be prompted to save dirty editors. The
129      * default is <code>true</code>.
130      *
131      * @return whether the user should be prompted to save dirty editors
132      */

133     protected boolean confirmSaveOfDirtyEditor() {
134         return true;
135     }
136     
137     /**
138      * Return the subscriber operation associated with this action. This
139      * operation will be run when the action is run. Subclass may implement this
140      * method and provide an operation subclass or may override the
141      * <code>run(IAction)</code> method directly if they choose not to
142      * implement a <code>SynchronizeModelOperation</code>.
143      *
144      * @param configuration the synchronize page configuration for the page to
145      * which this action is associated
146      * @param elements the selected diff element for which this action is
147      * enabled.
148      * @return the subscriber operation to be run by this action.
149      */

150     protected abstract SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements);
151     
152     /**
153      * Generic error handling code that uses an error dialog to show the error
154      * to the user. Subclasses can use this method and/or override it.
155      *
156      * @param e the exception that occurred.
157      */

158     protected void handle(Exception JavaDoc e) {
159         Utils.handle(e);
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
164      */

165     protected boolean updateSelection(IStructuredSelection selection) {
166         super.updateSelection(selection);
167         return isEnabledForSelection(selection);
168     }
169     
170     private boolean isEnabledForSelection(IStructuredSelection selection) {
171         return Utils.hasMatchingDescendant(selection, getSyncInfoFilter());
172     }
173
174     /**
175      * This method returns all instances of IDiffElement that are in the current
176      * selection.
177      *
178      * @return the selected elements
179      */

180     protected final IDiffElement[] getSelectedDiffElements() {
181         return Utils.getDiffNodes(getStructuredSelection().toArray());
182     }
183
184     /**
185      * Filter uses to filter the user selection to contain only those elements
186      * for which this action is enabled. Default filter includes all out-of-sync
187      * elements in the current selection. Subclasses may override.
188      *
189      * @return a sync info filter which selects all out-of-sync resources.
190      */

191     protected FastSyncInfoFilter getSyncInfoFilter() {
192         return new FastSyncInfoFilter();
193     }
194
195     /**
196      * Return the selected diff element for which this action is enabled.
197      * @return the list of selected diff elements for which this action is
198      * enabled.
199      */

200     protected final IDiffElement[] getFilteredDiffElements() {
201         IDiffElement[] elements = getSelectedDiffElements();
202         List JavaDoc filtered = new ArrayList JavaDoc();
203         for (int i = 0; i < elements.length; i++) {
204             IDiffElement e = elements[i];
205             if (e instanceof SyncInfoModelElement) {
206                 SyncInfo info = ((SyncInfoModelElement) e).getSyncInfo();
207                 if (info != null && getSyncInfoFilter().select(info)) {
208                     filtered.add(e);
209                 }
210             }
211         }
212         return (IDiffElement[]) filtered.toArray(new IDiffElement[filtered.size()]);
213     }
214
215     /**
216      * Set the selection of this action to the given selection
217      *
218      * @param selection the selection
219      */

220     public void selectionChanged(ISelection selection) {
221         if (selection instanceof IStructuredSelection) {
222             super.selectionChanged((IStructuredSelection)selection);
223         } else {
224             super.selectionChanged(StructuredSelection.EMPTY);
225         }
226         
227     }
228     
229     /**
230      * Returns the configuration showing this action.
231      *
232      * @return the configuration showing this action.
233      */

234     public ISynchronizePageConfiguration getConfiguration() {
235         return configuration;
236     }
237     
238     /**
239      * Save all dirty editors in the workbench that are open on files that may
240      * be affected by this operation. Opens a dialog to prompt the user if
241      * <code>confirm</code> is true. Return true if successful. Return false
242      * if the user has canceled the command. Must be called from the UI thread.
243      *
244      * @param confirm prompt the user if true
245      * @return boolean false if the operation was canceled.
246      */

247     public final boolean saveAllEditors(boolean confirm) {
248         return IDE.saveAllEditors(Utils.getResources(getFilteredDiffElements()), confirm);
249     }
250 }
251
Popular Tags