KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > model > RefactoringSynchronizationActionProvider


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.ltk.ui.refactoring.model;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.team.core.mapping.ISynchronizationContext;
19 import org.eclipse.team.core.mapping.ISynchronizationScope;
20 import org.eclipse.team.ui.mapping.MergeActionHandler;
21 import org.eclipse.team.ui.mapping.SynchronizationActionProvider;
22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
23
24 import org.eclipse.core.runtime.Assert;
25
26 import org.eclipse.core.commands.AbstractHandler;
27 import org.eclipse.core.commands.ExecutionEvent;
28 import org.eclipse.core.commands.ExecutionException;
29 import org.eclipse.core.commands.IHandler;
30
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.mapping.ResourceMapping;
33
34 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
36 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
37 import org.eclipse.ltk.core.refactoring.model.AbstractRefactoringDescriptorResourceMapping;
38 import org.eclipse.ltk.core.refactoring.model.AbstractRefactoringHistoryResourceMapping;
39
40 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringDescriptorProxyAdapter;
41 import org.eclipse.ltk.internal.ui.refactoring.actions.AcceptRefactoringsAction;
42 import org.eclipse.ltk.internal.ui.refactoring.actions.RejectRefactoringsAction;
43
44 import org.eclipse.jface.action.IMenuManager;
45 import org.eclipse.jface.viewers.ISelection;
46 import org.eclipse.jface.viewers.IStructuredSelection;
47
48 /**
49  * Refactoring-aware synchronization action provider which contributes an action
50  * to accept pending refactorings during team synchronization.
51  * <p>
52  * This action provider contributes an action for refactoring history objects.
53  * Additionally, existing command handlers for the <code>Merge</code>,
54  * <code>Mark As Merged</code> and <code>Overwrite</code> actions are
55  * wrapped and automatically disabled for refactoring history objects.
56  * </p>
57  * <p>
58  * Note: this class is intended to be extended by clients who need refactoring
59  * support in a team synchronization viewer. It needs to be be registered with
60  * the <code>org.eclipse.ui.navigator.navigatorContent</code> or
61  * <code>org.eclipse.ui.navigator.viewer</code> extension points in order to
62  * participate in the team synchronization viewers.
63  * </p>
64  *
65  * @see org.eclipse.team.ui.mapping.SynchronizationActionProvider
66  *
67  * @since 3.2
68  */

69 public class RefactoringSynchronizationActionProvider extends SynchronizationActionProvider {
70
71     /** Delegate for refactoring action handlers */
72     private final class RefactoringHandlerDelegate extends AbstractHandler {
73
74         /** The delegate handler */
75         private final IHandler fDelegateHandler;
76
77         /**
78          * Creates a new synchronization handler delegate.
79          *
80          * @param handler
81          * the delegate handler
82          */

83         public RefactoringHandlerDelegate(final IHandler handler) {
84             Assert.isNotNull(handler);
85             fDelegateHandler= handler;
86         }
87
88         /**
89          * {@inheritDoc}
90          */

91         public void dispose() {
92             fDelegateHandler.dispose();
93             super.dispose();
94         }
95
96         /**
97          * {@inheritDoc}
98          */

99         public Object JavaDoc execute(final ExecutionEvent event) throws ExecutionException {
100             return fDelegateHandler.execute(event);
101         }
102
103         /**
104          * {@inheritDoc}
105          */

106         public boolean isEnabled() {
107             return !hasRefactorings(getSynchronizationContext(), getSynchronizePageConfiguration()) && fDelegateHandler.isEnabled();
108         }
109     }
110
111     /**
112      * Gets the refactoring represented by the specified proxy.
113      *
114      * @param scope
115      * the synchronization scope
116      * @param proxy
117      * the refactoring descriptor proxy
118      * @param set
119      * the set of refactoring descriptor proxies
120      */

121     private static void getRefactoring(final ISynchronizationScope scope, final RefactoringDescriptorProxy proxy, final Set JavaDoc set) {
122         final ResourceMapping mapping= (ResourceMapping) proxy.getAdapter(ResourceMapping.class);
123         if (mapping instanceof AbstractRefactoringDescriptorResourceMapping) {
124             final AbstractRefactoringDescriptorResourceMapping extended= (AbstractRefactoringDescriptorResourceMapping) mapping;
125             final IResource resource= extended.getResource();
126             if (resource != null && scope.contains(resource))
127                 set.add(proxy);
128         }
129     }
130
131     /**
132      * Returns the currently selected refactorings.
133      *
134      * @param context
135      * the synchronization context
136      * @param configuration
137      * the synchronize page configuration
138      * @return the selected refactorings, or the empty array
139      */

140     private static RefactoringDescriptorProxy[] getRefactorings(final ISynchronizationContext context, final ISynchronizePageConfiguration configuration) {
141         Assert.isNotNull(context);
142         Assert.isNotNull(configuration);
143         final Set JavaDoc set= new HashSet JavaDoc();
144         final ISelection selection= configuration.getSite().getSelectionProvider().getSelection();
145         if (selection instanceof IStructuredSelection) {
146             final IStructuredSelection structured= (IStructuredSelection) selection;
147             if (!structured.isEmpty()) {
148                 final Object JavaDoc[] elements= structured.toArray();
149                 final ISynchronizationScope scope= context.getScope();
150                 for (int index= 0; index < elements.length; index++) {
151                     if (elements[index] instanceof RefactoringHistory) {
152                         getRefactorings(scope, (RefactoringHistory) elements[index], set);
153                     } else if (elements[index] instanceof RefactoringDescriptorProxy) {
154                         getRefactoring(scope, (RefactoringDescriptorProxy) elements[index], set);
155                     } else if (elements[index] instanceof RefactoringDescriptor) {
156                         getRefactoring(scope, new RefactoringDescriptorProxyAdapter(((RefactoringDescriptor) elements[index])), set);
157                     }
158                 }
159             }
160         }
161         return (RefactoringDescriptorProxy[]) set.toArray(new RefactoringDescriptorProxy[set.size()]);
162     }
163
164     /**
165      * Gets the refactorings represented by the specified history.
166      *
167      * @param scope
168      * the synchronization scope
169      * @param history
170      * the refactoring history
171      * @param set
172      * the set of refactoring descriptor proxies
173      */

174     private static void getRefactorings(final ISynchronizationScope scope, final RefactoringHistory history, final Set JavaDoc set) {
175         final ResourceMapping mapping= (ResourceMapping) history.getAdapter(ResourceMapping.class);
176         if (mapping instanceof AbstractRefactoringHistoryResourceMapping) {
177             final AbstractRefactoringHistoryResourceMapping extended= (AbstractRefactoringHistoryResourceMapping) mapping;
178             final IResource resource= extended.getResource();
179             if (resource != null && scope.contains(resource))
180                 set.addAll(Arrays.asList(history.getDescriptors()));
181         }
182     }
183
184     /**
185      * Is the specified refactoring in the scope?
186      *
187      * @param scope
188      * the synchronization scope
189      * @param proxy
190      * the refactoring descriptor proxy
191      * @return <code>true</code> if the refactoring is in the scope,
192      * <code>false</code> otherwise
193      */

194     private static boolean hasRefactoring(final ISynchronizationScope scope, final RefactoringDescriptorProxy proxy) {
195         final ResourceMapping mapping= (ResourceMapping) proxy.getAdapter(ResourceMapping.class);
196         if (mapping instanceof AbstractRefactoringDescriptorResourceMapping) {
197             final AbstractRefactoringDescriptorResourceMapping extended= (AbstractRefactoringDescriptorResourceMapping) mapping;
198             final IResource resource= extended.getResource();
199             if (resource != null)
200                 return scope.contains(resource);
201         }
202         return false;
203     }
204
205     /**
206      * Returns whether any refactorings from the given synchronization context
207      * are selected.
208      *
209      * @param context
210      * the synchronization context
211      * @param configuration
212      * the synchronize page configuration
213      * @return <code>true</code> if any refactorings are selected,
214      * <code>false</code> otherwise
215      */

216     private static boolean hasRefactorings(final ISynchronizationContext context, final ISynchronizePageConfiguration configuration) {
217         Assert.isNotNull(context);
218         Assert.isNotNull(configuration);
219         final ISelection selection= configuration.getSite().getSelectionProvider().getSelection();
220         if (selection instanceof IStructuredSelection) {
221             final IStructuredSelection structured= (IStructuredSelection) selection;
222             if (!structured.isEmpty()) {
223                 final Object JavaDoc[] elements= structured.toArray();
224                 final ISynchronizationScope scope= context.getScope();
225                 for (int index= 0; index < elements.length; index++) {
226                     if (elements[index] instanceof RefactoringHistory) {
227                         return hasRefactorings(scope, (RefactoringHistory) elements[index]);
228                     } else if (elements[index] instanceof RefactoringDescriptorProxy) {
229                         return hasRefactoring(scope, (RefactoringDescriptorProxy) elements[index]);
230                     } else if (elements[index] instanceof RefactoringDescriptor) {
231                         return hasRefactoring(scope, new RefactoringDescriptorProxyAdapter((RefactoringDescriptor) elements[index]));
232                     }
233                 }
234             }
235         }
236         return false;
237     }
238
239     /**
240      * Does the specified refactoring history contain any refactorings in the
241      * scope?
242      *
243      * @param scope
244      * the synchronization scope
245      * @param history
246      * the refactoring history
247      * @return <code>true</code> if any refactorings are in the scope,
248      * <code>false</code> otherwise
249      */

250     private static boolean hasRefactorings(final ISynchronizationScope scope, final RefactoringHistory history) {
251         final ResourceMapping mapping= (ResourceMapping) history.getAdapter(ResourceMapping.class);
252         if (mapping instanceof AbstractRefactoringHistoryResourceMapping) {
253             final AbstractRefactoringHistoryResourceMapping extended= (AbstractRefactoringHistoryResourceMapping) mapping;
254             final IResource resource= extended.getResource();
255             if (resource != null)
256                 return scope.contains(resource);
257         }
258         return false;
259     }
260
261     /**
262      * {@inheritDoc}
263      */

264     public void fillContextMenu(final IMenuManager menu) {
265         super.fillContextMenu(menu);
266         if (isRefactoringElementSelected()) {
267             final ISynchronizationContext context= getSynchronizationContext();
268             final RefactoringDescriptorProxy[] proxies= getRefactorings(context, getSynchronizePageConfiguration());
269             final AcceptRefactoringsAction accept= new AcceptRefactoringsAction(context, getExtensionSite().getViewSite().getShell());
270             accept.setRefactoringDescriptors(proxies);
271             menu.add(accept);
272             final RejectRefactoringsAction reject= new RejectRefactoringsAction(context);
273             reject.setRefactoringDescriptors(proxies);
274             menu.add(reject);
275         }
276     }
277
278     /**
279      * {@inheritDoc}
280      */

281     protected void initialize() {
282         super.initialize();
283         final ISynchronizePageConfiguration configuration= getSynchronizePageConfiguration();
284         registerHandler(MERGE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(MERGE_ACTION_ID, configuration)));
285         registerHandler(OVERWRITE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(OVERWRITE_ACTION_ID, configuration)));
286         registerHandler(MARK_AS_MERGE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(MARK_AS_MERGE_ACTION_ID, configuration)));
287     }
288
289     /**
290      * {@inheritDoc}
291      */

292     protected void initializeOpenActions() {
293         if (!hasRefactorings(getSynchronizationContext(), getSynchronizePageConfiguration()))
294             super.initializeOpenActions();
295     }
296
297     private boolean isRefactoringElementSelected() {
298         final ISelection selection= getContext().getSelection();
299         if (selection instanceof IStructuredSelection) {
300             final IStructuredSelection extended= (IStructuredSelection) selection;
301             for (final Iterator JavaDoc iterator= extended.iterator(); iterator.hasNext();) {
302                 final Object JavaDoc element= iterator.next();
303                 if (element instanceof RefactoringDescriptorProxy || element instanceof RefactoringDescriptor || element instanceof RefactoringHistory) {
304                     return true;
305                 }
306             }
307         }
308         return false;
309     }
310 }
Popular Tags