Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44e56bfb3f8dbbd9a1d781fc512b47a4e15c860a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * Copyright (c) 2006, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.ui.mapping;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.*;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.viewers.*;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.mapping.ResourceMarkAsMergedHandler;
import org.eclipse.team.internal.ui.mapping.ResourceMergeHandler;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;

/**
 * An abstract superclass that enables models to create handlers
 * for the basic merge operations (merge, overwrite and mark-as-merged).
 * This class makes use of a {@link SynchronizationOperation} to determine its
 * enablement state and execute the handler. Enablement is determined
 * using {@link SynchronizationOperation#shouldRun()} and the handler will
 * invoke {@link SynchronizationOperation#run()} when executed.
 *
 * @since 3.2
 * @see SynchronizationActionProvider
 */
public abstract class MergeActionHandler extends AbstractHandler {

	private final ISynchronizePageConfiguration configuration;
	private boolean enabled = false;
	private IStructuredSelection selection;
	private ISelectionChangedListener listener = event -> updatedEnablement(event);

	/**
	 * Return an instance of the default handler for the given merge action id.
	 * Note that this handler must be disposed by the caller when it is no longer
	 * needed.
	 * @param mergeActionId the merge action id
	 * @param configuration the synchronization page configuration
	 * @return the default handler for the given merge action or <code>null</code>
	 */
	public static IHandler getDefaultHandler(String mergeActionId, ISynchronizePageConfiguration configuration) {
		if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
			ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, false /* no overwrite */);
			resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
			return resourceMergeHandler;
		} else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
			ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, true /* overwrite */);
			resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
			return resourceMergeHandler;
		} else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
			ResourceMarkAsMergedHandler resourceMarkAsMergedHandler = new ResourceMarkAsMergedHandler(configuration);
			resourceMarkAsMergedHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection());
			return resourceMarkAsMergedHandler;
		}
		return null;
	}

	/**
	 * Create the handler.
	 * @param configuration the configuration for the synchronize page displaying the model.
	 */
	public MergeActionHandler(ISynchronizePageConfiguration configuration) {
		this.configuration = configuration;
		ISelectionProvider selectionProvider = getConfiguration().getSite().getSelectionProvider();
		selectionProvider.addSelectionChangedListener(listener);
		updateEnablement((IStructuredSelection)selectionProvider.getSelection());
	}

	/**
	 * Deregister this handler from selection change events.
	 */
	@Override
	public void dispose() {
		getConfiguration().getSite().getSelectionProvider().removeSelectionChangedListener(listener);
	}

	/* private */ void updatedEnablement(SelectionChangedEvent event) {
		updateEnablement(event.getStructuredSelection());
	}

	/**
	 * Update the enablement of this handler for the new selection.
	 * By default, this method uses the <code>shouldRun</code>
	 * method of the handler's operation to determine the enablement
	 * of this handler. Subclasses may override but should
	 * either still invoke this method or call {@link #setEnabled(boolean)}
	 * to set the enablement.
	 * @param selection the selection
	 */
	protected void updateEnablement(IStructuredSelection selection) {
		this.selection = selection;
		boolean isEnabled = getOperation().shouldRun();
		setEnabled(isEnabled);
	}

	/**
	 * Return the configuration of the synchronize page that is surfacing
	 * the merge action to which this handler is registered.
	 * @return the synchronize page configuration
	 */
	protected final ISynchronizePageConfiguration getConfiguration() {
		return configuration;
	}

	/**
	 * Return the current selection.
	 * @return the current selection.
	 */
	protected final IStructuredSelection getStructuredSelection() {
		return selection;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * Set the enablement of this handler.
	 * @param isEnabled whether the handler is enabled
	 */
	protected void setEnabled(boolean isEnabled) {
		if (enabled != isEnabled) {
			enabled = isEnabled;
			fireHandlerChanged(new HandlerEvent(this, true, false));
		}
	}

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		try {
			SynchronizationOperation operation = getOperation();
			IRunnableContext context = getConfiguration().getRunnableContext();
			if (context != null) {
				context.run(true, true, operation);
			} else {
				operation.run();
			}
		} catch (InvocationTargetException e) {
			Utils.handle(e);
		} catch (InterruptedException e) {
			// Ignore
		}
		return null;
	}

	/**
	 * Return the synchronization operation that performs
	 * the merge operation.
	 * @return a synchronization operation
	 */
	protected abstract SynchronizationOperation getOperation();

	/**
	 * Return the saveable that is the target of this handler.
	 * By default, the saveable of this handlers operation is returned.
	 * @return the saveable that is the target of this operation
	 */
	public SaveableComparison getSaveable() {
		return getOperation().getSaveable();
	}
}

Back to the top