Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6fcf4b4857d2d36a0da0e6ed660e0defd9809a6 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*****************************************************************************
 * Copyright (c) 2011, 2022 Atos Origin Integration - CEA LIST.
 *
 * All rights reserved. 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:
 *  Tristan Faure (Atos Origin Integration) tristan.faure@atosorigin.com - Initial API and implementation
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr
 *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Add copy Action
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Bug 578434
 *****************************************************************************/
package org.eclipse.papyrus.infra.onefile.internal.ui.providers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor;
import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
import org.eclipse.ltk.ui.refactoring.resource.RenameResourceWizard;
import org.eclipse.papyrus.infra.onefile.internal.ui.Activator;
import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
import org.eclipse.papyrus.infra.onefile.utils.OneFileUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.DeleteResourceAction;
import org.eclipse.ui.actions.MoveResourceAction;
import org.eclipse.ui.actions.RefreshAction;
import org.eclipse.ui.actions.RenameResourceAction;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionConstants;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonMenuConstants;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Actions available through an {@link IPapyrusFile}
 *
 * @author tfaure
 *
 */
@SuppressWarnings({ "restriction", "rawtypes" })
public class PapyrusModelActionProvider extends CommonActionProvider {

	private boolean fInViewPart = false;

	private ICommonViewerWorkbenchSite workbenchSite;

	private Action openAction;

	private Action deleteAction;

	private Action copyAction;

	private Action renameAction;

	private Action refreshAction;

	private Action moveAction;

	public PapyrusModelActionProvider() {

	}

	@Override
	protected boolean filterAction(IAction action) {
		return super.filterAction(action);
	}

	@Override
	public void fillActionBars(IActionBars actionBars) {
		if (fInViewPart) {
			actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, openAction);
			actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
			actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), renameAction);
			actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), moveAction);
			actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
			actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
		}
		super.fillActionBars(actionBars);
	}

	@Override
	public void fillContextMenu(IMenuManager menu) {
		super.fillContextMenu(menu);
		appendToGroup(menu, openAction, ICommonMenuConstants.GROUP_OPEN);
		appendToGroup(menu, deleteAction, ICommonMenuConstants.GROUP_EDIT);
		appendToGroup(menu, moveAction, ICommonMenuConstants.GROUP_EDIT);
		appendToGroup(menu, copyAction, ICommonMenuConstants.GROUP_EDIT);
		appendToGroup(menu, renameAction, ICommonMenuConstants.GROUP_EDIT);
		appendToGroup(menu, refreshAction, ICommonMenuConstants.GROUP_EDIT);
	}

	private void appendToGroup(IMenuManager menu, IAction action, String id) {
		if (action != null && action.isEnabled()) {
			menu.appendToGroup(id, action);
		}
	}

	@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();
		final ActionHelper helper = new ActionHelper();
		openAction = new Action() {

			@Override
			public void run() {
				if (getIFile() != null) {
					try {
						IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
						page.openEditor(new FileEditorInput(getIFile()), "org.eclipse.papyrus.infra.core.papyrusEditor", true, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT); //$NON-NLS-1$
					} catch (WorkbenchException e) {
						Activator.log.error(e);
					}
				}
			}

			public IFile getIFile() {
				return helper.getIFile(getContext());
			}

			@Override
			public boolean isEnabled() {
				return getIFile() != null;
			}

			@Override
			public String getText() {
				return IDEWorkbenchMessages.OpenFileAction_text;
			}
		};

		deleteAction = new DeleteResourceAction(provider) {

			@Override
			public boolean isEnabled() {
				return getSelectedResources() != null && getSelectedResources().size() > 0 && OneFileUtils.containsDi(getSelectedResources());
			}

			@Override
			public IStructuredSelection getStructuredSelection() {
				return helper.getStructuredSelection(getContext());
			}

			@Override
			protected List<IResource> getSelectedResources() {
				return helper.getSelectedResources(getContext());
			}
		};

		moveAction = new MoveResourceAction(provider) {

			@Override
			public IStructuredSelection getStructuredSelection() {
				return helper.getStructuredSelection(getContext());
			}

			@Override
			protected List<IResource> getSelectedResources() {
				return helper.getSelectedResources(getContext());
			}
		};

		copyAction = new CopyToClipboardAction(provider) {

			@Override
			public IStructuredSelection getStructuredSelection() {
				return helper.getStructuredSelection(getContext());
			}

			@Override
			protected List<IResource> getSelectedResources() {
				return helper.getSelectedResources(getContext());
			}

		};

		renameAction = new OneFileRenameAction(provider);

		refreshAction = new RefreshAction(provider) {

			@Override
			public void run() {
				super.run();
			}

		};

		makeAction(openAction, ICommonActionConstants.OPEN, ISharedImages.IMG_TOOL_COPY, ISharedImages.IMG_TOOL_COPY_DISABLED);
		makeAction(deleteAction, IWorkbenchCommandConstants.EDIT_DELETE, ISharedImages.IMG_TOOL_DELETE, ISharedImages.IMG_TOOL_DELETE_DISABLED);
		makeAction(moveAction, ActionFactory.MOVE.getId(), null, null);
		makeAction(copyAction, IWorkbenchCommandConstants.EDIT_CUT, ISharedImages.IMG_TOOL_CUT, ISharedImages.IMG_TOOL_CUT_DISABLED);
		makeAction(copyAction, IWorkbenchCommandConstants.EDIT_COPY, ISharedImages.IMG_TOOL_COPY, ISharedImages.IMG_TOOL_COPY_DISABLED);
		makeAction(refreshAction, ActionFactory.REFRESH.getCommandId(), null, null);
	}

	/**
	 * A Rename action applying to an IPapyrusFile
	 * The action will rename only the *.di file
	 * (Refactoring from infra.ui.resources will rename related files and fix URIs)
	 *
	 * The user won't be asked for a file extension (*.di will be kept)
	 *
	 * @author Camille Letavernier
	 */
	public class OneFileRenameAction extends RenameResourceAction {

		final ActionHelper helper = new ActionHelper();

		private IWorkbenchPartSite partSiteProvider;

		public OneFileRenameAction(IWorkbenchPartSite provider) {
			super(provider);
			this.partSiteProvider = provider;
		}

		private Shell getShell() {
			return partSiteProvider.getShell();
		}

		@Override
		public void run() {

			ISelection selection = partSiteProvider.getSelectionProvider().getSelection();

			if (selection instanceof IStructuredSelection) {
				IStructuredSelection structuredSelection = (IStructuredSelection) selection;
				if (structuredSelection.size() != 1) {
					return;
				}

				Object selectedElement = structuredSelection.getFirstElement();
				if (selectedElement instanceof IPapyrusFile) {
					IPapyrusFile modelToRename = (IPapyrusFile) selectedElement;

					RenamePapyrusModelWizard refactoringWizard = new RenamePapyrusModelWizard(modelToRename);
					RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(refactoringWizard);
					try {
						op.run(getShell(), RefactoringUIMessages.RenameResourceHandler_title);
					} catch (InterruptedException e) {
						// do nothing
					}
				}
			}
		}
	}

	private static class RenamePapyrusModelWizard extends RenameResourceWizard {

		private IPapyrusFile papyrusFile;

		public RenamePapyrusModelWizard(IPapyrusFile papyrusFile) {
			super(papyrusFile.getMainFile());
			this.papyrusFile = papyrusFile;
		}

		@Override
		protected void addUserInputPages() {
			RenameResourceProcessor processor = getRefactoring().getAdapter(RenameResourceProcessor.class);
			addPage(new RenamePapyrusModelPage(processor));
		}

		private class RenamePapyrusModelPage extends UserInputWizardPage {

			private final RenameResourceProcessor fRefactoringProcessor;

			private Text fNameField;

			public RenamePapyrusModelPage(RenameResourceProcessor processor) {
				super("RenamePapyrusModelRefactoringInputPage"); //$NON-NLS-1$
				fRefactoringProcessor = processor;
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
			 */
			@Override
			public void createControl(Composite parent) {
				Composite composite = new Composite(parent, SWT.NONE);
				composite.setLayout(new GridLayout(2, false));
				composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				composite.setFont(parent.getFont());

				Label label = new Label(composite, SWT.NONE);
				label.setText(RefactoringUIMessages.RenameResourceWizard_name_field_label);
				label.setLayoutData(new GridData());

				fNameField = new Text(composite, SWT.BORDER);
				fNameField.setText(papyrusFile.getMainFile().getFullPath().removeFileExtension().lastSegment());
				fNameField.setFont(composite.getFont());
				fNameField.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
				fNameField.addModifyListener(new ModifyListener() {

					@Override
					public void modifyText(ModifyEvent e) {
						validatePage();
					}
				});

				fNameField.selectAll();
				setPageComplete(false);
				setControl(composite);
			}

			@Override
			public void setVisible(boolean visible) {
				if (visible) {
					fNameField.setFocus();
				}
				super.setVisible(visible);
			}

			protected final void validatePage() {
				String text = fNameField.getText() + ".di"; //$NON-NLS-1$
				RefactoringStatus status = fRefactoringProcessor.validateNewElementName(text);
				setPageComplete(status);
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#performFinish()
			 */
			@Override
			protected boolean performFinish() {
				initializeRefactoring();
				storeSettings();
				return super.performFinish();
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see org.eclipse.ltk.ui.refactoring.UserInputWizardPage#getNextPage()
			 */
			@Override
			public IWizardPage getNextPage() {
				initializeRefactoring();
				storeSettings();
				return super.getNextPage();
			}

			private void storeSettings() {
			}

			private void initializeRefactoring() {
				fRefactoringProcessor.setNewResourceName(fNameField.getText() + ".di"); //$NON-NLS-1$
			}
		}
	}

	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));
			}
		}
	}

	public static class ActionHelper {

		public IStructuredSelection getStructuredSelection(ActionContext context) {
			return new StructuredSelection(getSelectedResources(context));
		}

		public List getOneSelectedResources(ActionContext context) {
			List selectedResources = getSelectedResources(context);
			if (selectedResources.size() > 0) {
				for (Iterator<?> i = selectedResources.iterator(); i.hasNext();) {
					Object o = i.next();
					if (o instanceof IFile) {
						IFile file = (IFile) o;
						if (!OneFileUtils.isDi(file)) {
							i.remove();
						}
					}
				}
				return selectedResources;
			}
			return Collections.EMPTY_LIST;
		}

		public IStructuredSelection getOneStructuredSelection(ActionContext context) {
			List selectedResources = getOneSelectedResources(context);
			if (selectedResources.size() > 0) {
				return new StructuredSelection(selectedResources);
			}
			return null;
		}

		protected List<IResource> getSelectedResources(ActionContext context) {
			ISelection selec = context.getSelection();
			List<IResource> resources = new ArrayList<>();
			if (selec instanceof IStructuredSelection) {
				IStructuredSelection struc = (IStructuredSelection) selec;
				for (Iterator<?> i = struc.iterator(); i.hasNext();) {
					Object o = i.next();
					if (o instanceof IPapyrusFile) {
						IPapyrusFile papy = (IPapyrusFile) o;
						resources.addAll(Arrays.asList(papy.getAssociatedResources()));
					}
				}
			}
			return resources;
		}

		public IFile getIFile(ActionContext context) {
			ISelection selec = context.getSelection();
			if (selec instanceof IStructuredSelection) {
				IStructuredSelection struc = (IStructuredSelection) selec;
				Object firstElement = struc.getFirstElement();
				if (firstElement instanceof IFile) {
					IFile file = (IFile) firstElement;
					return file;
				} else if (firstElement instanceof IPapyrusFile) {
					return ((IPapyrusFile) firstElement).getMainFile();
				}
			}
			return null;
		}
	}

}

Back to the top