Skip to main content
summaryrefslogtreecommitdiffstats
blob: 00a40e2d9926aaf6e2e0c33f0a4848b1db69cc91 (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
/*******************************************************************************
 *  Copyright (c) 2007, 2008 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.equinox.internal.p2.ui.actions;

import org.eclipse.equinox.internal.p2.ui.ProvUI;
import org.eclipse.equinox.p2.operations.ProvisioningSession;
import org.eclipse.equinox.p2.ui.Policy;
import org.eclipse.equinox.p2.ui.ProvisioningUI;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.SelectionProviderAction;

public abstract class ProvisioningAction extends SelectionProviderAction {
	ProvisioningUI ui;

	protected ProvisioningAction(ProvisioningUI ui, String text, ISelectionProvider selectionProvider) {
		super(selectionProvider, text);
		this.ui = ui;
	}

	/*
	 * perform initialization that should be done after creation.
	 */
	protected void init() {
		// prime the selection validation
		ISelection selection = getSelection();
		if (selection instanceof IStructuredSelection) {
			selectionChanged((IStructuredSelection) selection);
		} else {
			selectionChanged(selection);
		}
	}

	protected Shell getShell() {
		return ProvUI.getDefaultParentShell();
	}

	/*
	 * Overridden to use the selection from the selection provider, not the one
	 * from the triggering event.  Some selection providers reinterpret the raw selections
	 * (non-Javadoc)
	 * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public final void selectionChanged(IStructuredSelection selection) {
		ISelection providerSelection = getSelectionProvider().getSelection();
		if (providerSelection instanceof IStructuredSelection) {
			checkEnablement(((IStructuredSelection) providerSelection).toArray());
		} else {
			// shouldn't really happen, but a provider could decide to de-structure the selection
			selectionChanged(providerSelection);
		}
	}

	protected void checkEnablement(Object[] selections) {
		// Default is to nothing
	}

	/**
	 * Recheck the enablement.  Called by clients when some condition outside of
	 * the action that may effect its enablement should be changed.
	 */
	public final void checkEnablement() {
		ISelection selection = getSelection();
		if (selection instanceof IStructuredSelection) {
			checkEnablement(((IStructuredSelection) selection).toArray());
		} else {
			selectionChanged(selection);
		}
	}

	protected ProvisioningSession getSession() {
		return ui.getSession();
	}

	protected Policy getPolicy() {
		return ui.getPolicy();
	}

	protected ProvisioningUI getProvisioningUI() {
		return ui;
	}
}

Back to the top