KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > MergeActionHandler


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.mapping;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.commands.*;
16 import org.eclipse.jface.operation.IRunnableContext;
17 import org.eclipse.jface.viewers.*;
18 import org.eclipse.team.internal.ui.Utils;
19 import org.eclipse.team.internal.ui.mapping.ResourceMarkAsMergedHandler;
20 import org.eclipse.team.internal.ui.mapping.ResourceMergeHandler;
21 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
22
23 /**
24  * An abstract superclass that enables models to create handlers
25  * for the basic merge operations (merge, overwrite and mark-as-merged).
26  * This class makes use of a {@link SynchronizationOperation} to determine its
27  * enablement state and execute the handler. Enablement is determined
28  * using {@link SynchronizationOperation#shouldRun()} and the handler will
29  * invoke {@link SynchronizationOperation#run()} when executed.
30  *
31  * @since 3.2
32  * @see SynchronizationActionProvider
33  */

34 public abstract class MergeActionHandler extends AbstractHandler {
35
36     private final ISynchronizePageConfiguration configuration;
37     private boolean enabled = false;
38     private IStructuredSelection selection;
39     private ISelectionChangedListener listener = new ISelectionChangedListener() {
40         public void selectionChanged(SelectionChangedEvent event) {
41             updatedEnablement(event);
42         }
43     };
44     
45     /**
46      * Return an instance of the default handler for the given merge action id.
47      * Note that this handler must be disposed by the caller when it is no longer
48      * needed.
49      * @param mergeActionId the merge action id
50      * @param configuration the synchronization page configuration
51      * @return the default handler for the given merge action or <code>null</code>
52      */

53     public static IHandler getDefaultHandler(String JavaDoc mergeActionId, ISynchronizePageConfiguration configuration) {
54         if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
55             ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, false /* no overwrite */);
56             resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
57             return resourceMergeHandler;
58         } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
59             ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, true /* overwrite */);
60             resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
61             return resourceMergeHandler;
62         } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
63             ResourceMarkAsMergedHandler resourceMarkAsMergedHandler = new ResourceMarkAsMergedHandler(configuration);
64             resourceMarkAsMergedHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
65             return resourceMarkAsMergedHandler;
66         }
67         return null;
68     }
69
70     /**
71      * Create the handler.
72      * @param configuration the configuration for the synchronize page displaying the model.
73      */

74     public MergeActionHandler(ISynchronizePageConfiguration configuration) {
75         this.configuration = configuration;
76         ISelectionProvider selectionProvider = getConfiguration().getSite().getSelectionProvider();
77         selectionProvider.addSelectionChangedListener(listener);
78         updateEnablement((IStructuredSelection)selectionProvider.getSelection());
79     }
80     
81     /**
82      * Deregister this handler from selection change events.
83      */

84     public void dispose() {
85         getConfiguration().getSite().getSelectionProvider().removeSelectionChangedListener(listener);
86     }
87
88     /* private */ void updatedEnablement(SelectionChangedEvent event) {
89         updateEnablement((IStructuredSelection)event.getSelection());
90     }
91
92     /**
93      * Update the enablement of this handler for the new selection.
94      * By default, this method uses the <code>shouldRun</code>
95      * method of the handler's operation to determine the enablement
96      * of this handler. Subclasses may override but should
97      * either still invoke this method or call {@link #setEnabled(boolean)}
98      * to set the enablement.
99      * @param selection the selection
100      */

101     protected void updateEnablement(IStructuredSelection selection) {
102         this.selection = selection;
103         boolean isEnabled = getOperation().shouldRun();
104         setEnabled(isEnabled);
105     }
106
107     /**
108      * Return the configuration of the synchronize page that is surfacing
109      * the merge action to which this handler is registered.
110      * @return the synchronize page configuration
111      */

112     protected final ISynchronizePageConfiguration getConfiguration() {
113         return configuration;
114     }
115
116     /**
117      * Return the current selection.
118      * @return the current selection.
119      */

120     protected final IStructuredSelection getStructuredSelection() {
121         return selection;
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
126      */

127     public boolean isEnabled() {
128         return enabled;
129     }
130     
131     /**
132      * Set the enablement of this handler.
133      * @param isEnabled whether the handler is enabled
134      */

135     protected void setEnabled(boolean isEnabled) {
136         if (enabled != isEnabled) {
137             enabled = isEnabled;
138             fireHandlerChanged(new HandlerEvent(this, true, false));
139         }
140     }
141     
142     /* (non-Javadoc)
143      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
144      */

145     public Object JavaDoc execute(final ExecutionEvent event) throws ExecutionException {
146         try {
147             SynchronizationOperation operation = getOperation();
148             IRunnableContext context = getConfiguration().getRunnableContext();
149             if (context != null) {
150                 context.run(true, true, operation);
151             } else {
152                 operation.run();
153             }
154         } catch (InvocationTargetException JavaDoc e) {
155             Utils.handle(e);
156         } catch (InterruptedException JavaDoc e) {
157             // Ignore
158
}
159         return null;
160     }
161
162     /**
163      * Return the synchronization operation that performs
164      * the merge operation.
165      * @return a synchronization operation
166      */

167     protected abstract SynchronizationOperation getOperation();
168     
169     /**
170      * Return the saveable that is the target of this handler.
171      * By default, the saveable of this handlers operation is returned.
172      * @return the saveable that is the target of this operation
173      */

174     public SaveableComparison getSaveable() {
175         return getOperation().getSaveable();
176     }
177 }
178
Popular Tags