Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1960608988d8c80f44a29e4542272b70d4baef53 (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
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 *
 * 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:
 *  Benoit Maggi  benoit.maggi@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.onefile.providers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.papyrus.infra.onefile.action.PapyrusModelPasteAction;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;

/**
 * Action provider for paste Papyrus models
 */
public class PapyrusEditActionProvider extends CommonActionProvider {

	private boolean fInViewPart = false;

	private ICommonViewerWorkbenchSite workbenchSite;

	private Action pasteAction;

	public PapyrusEditActionProvider() {
	}

	@Override
	public void fillActionBars(IActionBars actionBars) {
		super.fillActionBars(actionBars);
		if (fInViewPart) {
			actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction);
		}
	}

	@Override
	public void fillContextMenu(IMenuManager menu) {
		super.fillContextMenu(menu);
		if (pasteAction != null && pasteAction.isEnabled()) {
			menu.insertAfter(PapyrusModelPasteAction.ID, pasteAction); // same as the internal org.eclipse.ui.internal.navigator.resources.actions.PasteAction.ID
			menu.remove(PapyrusModelPasteAction.ID);
		}
	}

	@Override
	public void init(ICommonActionExtensionSite site) {
		if (site.getViewSite() instanceof ICommonViewerWorkbenchSite) {
			workbenchSite = (ICommonViewerWorkbenchSite) site.getViewSite();
		}
		if (workbenchSite != null) {
			if (workbenchSite.getPart() != null && workbenchSite.getPart() instanceof IViewPart) {
				fInViewPart = true;
			}
			makeActions();
		}
	}

	private void makeActions() {
		final IWorkbenchPartSite provider = workbenchSite.getSite();

		Shell shell = provider.getShell();
		Clipboard clipboard = new Clipboard(shell.getDisplay());
		pasteAction = new PapyrusModelPasteAction(shell, clipboard) {

			// the helper is filtering Ipapyrus files

			@Override
			protected List getSelectedResources() {
				ActionContext context = getContext();
				List selectedResources = getSelectedResources(context);
				return selectedResources;
			}

			protected List getSelectedResources(ActionContext context) {
				ISelection selec = context.getSelection();
				List<IResource> resources = new ArrayList<IResource>();

				if (selec instanceof TreeSelection) {
					TreeSelection tree = (TreeSelection) selec;
					Object firstElement = tree.getFirstElement();
					if (firstElement instanceof IResource) {
						resources.add((IResource) firstElement);
					}
				}
				return resources;
			}
		};

		makeAction(pasteAction, IWorkbenchCommandConstants.EDIT_PASTE, ISharedImages.IMG_TOOL_PASTE, ISharedImages.IMG_TOOL_PASTE_DISABLED);
	}

	/**
	 * Create an action
	 *
	 * @param action
	 * @param id
	 * @param imgTool
	 * @param imgToolDisabled
	 */
	protected void makeAction(Action action, String id, String imgTool, String imgToolDisabled) {
		if (action != null) {
			ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
			if (id != null) {
				action.setId(id);
				action.setActionDefinitionId(id);
			}
			if (imgTool != null) {
				action.setImageDescriptor(images.getImageDescriptor(imgTool));
			}
			if (imgToolDisabled != null) {
				action.setDisabledImageDescriptor(images.getImageDescriptor(imgToolDisabled));
			}
		}
	}

}

Back to the top