Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9022839279bf28131ecf56d25f56e254f33816f9 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize.actions;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
import org.eclipse.team.ui.synchronize.ISynchronizePageSite;
import org.eclipse.ui.*;
import org.eclipse.ui.actions.*;
import org.eclipse.ui.navigator.INavigatorContentService;

/**
 * This action group is modeled after the class of the same name in
 * the org.eclipse.ui.workbench plugin. We couldn't reuse that class
 * because of a hard dependency on the navigator.
 */
public class RefactorActionGroup extends ActionGroup {

	private CopyToClipboardAction copyAction;
	private MoveResourceAction moveAction;
	private RenameResourceAction renameAction;
	private ISynchronizePageSite site;
	private DeleteResourceAction deleteAction;
	private final INavigatorContentService navigatorContentService;

	public RefactorActionGroup(ISynchronizePageSite site) {
		this(site, null);
	}

	public RefactorActionGroup(ISynchronizePageSite site, INavigatorContentService navigatorContentService) {
		this.site = site;
		this.navigatorContentService = navigatorContentService;
		makeActions();
	}

	public void fillContextMenu(IMenuManager parentMenu, String groupId) {
		parentMenu.appendToGroup(groupId, copyAction);
		// the paste action has been already created in the Sync view
		IWorkbenchPart part = site.getPart();
		if (part instanceof SynchronizeView) {
			SynchronizeView sv = (SynchronizeView) part;
			IAction pasteAction = sv.getPastePatchAction();
			parentMenu.appendToGroup(groupId, pasteAction);
		}
		parentMenu.appendToGroup(groupId, deleteAction);
		parentMenu.appendToGroup(groupId, moveAction);
		parentMenu.appendToGroup(groupId, renameAction);
	}

 	@Override
	public void fillActionBars(IActionBars actionBars) {
    	actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
    	actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
    	actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), renameAction);
    	actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), moveAction);
    }

	@Override
	public void updateActionBars() {
		copyAction.selectionChanged(getObjectSelection());
		deleteAction.selectionChanged(getObjectSelection());
		moveAction.selectionChanged(getObjectSelection());
		renameAction.selectionChanged(getObjectSelection());
	}

	protected void makeActions() {
		final Shell shell = site.getShell();
		final ISharedImages images = PlatformUI.getWorkbench()
				.getSharedImages();

		copyAction = new CopyToClipboardAction(shell, navigatorContentService);
		copyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
		copyAction.setImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
		copyAction.setDisabledImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));

		deleteAction = new DeleteResourceAction(shell) {
			@Override
			protected List getSelectedResources() {
				return getSelection().toList();// Arrays.asList(Utils.getResources(getSelection().toArray()));
			}

			@Override
			protected boolean updateSelection(IStructuredSelection selection) {
				// TODO Auto-generated method stub
				return super.updateSelection(selection)
						&& allResourcesAreOfType(selection, IResource.PROJECT
								| IResource.FOLDER | IResource.FILE);
			}
		};
		deleteAction
				.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE);
		deleteAction.setImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
		deleteAction.setDisabledImageDescriptor(images
				.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));

		moveAction = new MoveResourceAction(shell) {
			@Override
			protected boolean updateSelection(IStructuredSelection selection) {
				// TODO Auto-generated method stub
				return super.updateSelection(selection)
						&& allResourcesAreOfType(selection, IResource.PROJECT
								| IResource.FOLDER | IResource.FILE);
			}
		};
		moveAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_MOVE);

		renameAction = new RenameResourceAction(shell) {
			@Override
			protected boolean updateSelection(IStructuredSelection selection) {
				// TODO Auto-generated method stub
				return super.updateSelection(selection)
						&& allResourcesAreOfType(selection, IResource.PROJECT
								| IResource.FOLDER | IResource.FILE);
			}
		};
		renameAction
				.setActionDefinitionId(IWorkbenchCommandConstants.FILE_RENAME);
	}

    private IStructuredSelection getSelection() {
        final ISelection selection= getContext().getSelection();

        if (!(selection instanceof IStructuredSelection))
            return new StructuredSelection();

    	return new StructuredSelection(Utils.getResources(((IStructuredSelection)selection).toArray()));
	}

    private IStructuredSelection getObjectSelection() {
        final ISelection selection= getContext().getSelection();

        if (!(selection instanceof IStructuredSelection))
            return new StructuredSelection();

    	return (IStructuredSelection)selection;
	}

	private boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) {
		Iterator resources = selection.iterator();
		while (resources.hasNext()) {
			Object next = resources.next();
			IResource resource = null;
			if (next instanceof IResource) {
				resource = (IResource)next;
			} else if (next instanceof IAdaptable) {
				IAdaptable adaptable = (IAdaptable)next;
				resource = adaptable.getAdapter(IResource.class);
			}
			if(resource == null) {
				IResource[] r = Utils.getResources(new Object[] {next});
				if(r.length == 1) {
					resource = r[0];
				}
			}
			if (resource == null || (resource.getType() & resourceMask) == 0) {
				return false;
			}
		}
		return true;
	}

	@Override
	public void dispose() {
		super.dispose();
		copyAction.dispose();
	}
}

Back to the top