Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f08cf62b2417d14d48231ac4aa6889d4db47fea (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.actions;

import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.ui.editors.PapyrusCDOEditorInput;
import org.eclipse.papyrus.cdo.internal.ui.l10n.Messages;
import org.eclipse.papyrus.cdo.internal.ui.views.DIModel;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;

import com.google.common.collect.Lists;

/**
 * This is the DeleteModelAction type. Enjoy.
 */
public class DeleteModelAction extends AsyncEditAction<DIModel> {

	private final IWorkbenchPart part;

	public DeleteModelAction(IWorkbenchPart part) {
		super(DIModel.class, Messages.DeleteModelAction_0, ImageDescriptor.createFromImage(part.getSite().getWorkbenchWindow().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE)));

		this.part = part;

		setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE);
	}

	@Override
	protected CDOView getView(DIModel selection) {
		return selection.getResource().cdoView();
	}

	@Override
	protected boolean gatherInput(DIModel selection) {
		boolean result = false;

		if (MessageDialog.openQuestion(part.getSite().getShell(), Messages.DeleteModelAction_1, NLS.bind(Messages.DeleteModelAction_2, selection.getName()))) {

			IWorkbenchPage page = part.getSite().getPage();
			URI uri = selection.getResource().getURI();
			PapyrusCDOEditorInput input = new PapyrusCDOEditorInput(uri);
			IEditorPart openEditor = page.findEditor(input);
			if (openEditor != null) {
				page.closeEditor(openEditor, false);
			}

			result = true;
		}

		return result;
	}

	@Override
	protected void doRun(DIModel selection, CDOTransaction transaction, IProgressMonitor monitor) throws CoreException {

		List<IStatus> failures = Lists.newArrayListWithExpectedSize(1);
		for (Object next : selection.getChildren()) {
			if (next instanceof CDOResource) {
				// get the resource local to this transaction

				CDOID oid = ((CDOResource) next).cdoID();
				CDOResource toDelete = (CDOResource) transaction.getObject(oid);
				if (toDelete != null) {
					try {
						toDelete.delete(null);
					} catch (Exception e) {
						failures.add(error("Failed to delete resource " + toDelete.getPath(), e)); //$NON-NLS-1$
					}
				}
			}
		}

		if (!failures.isEmpty()) {
			throw new CoreException(wrap(failures, "Errors occurred in deleting model.")); //$NON-NLS-1$
		}
	}

	@Override
	protected boolean updateSelection(IStructuredSelection selection) {
		boolean result = super.updateSelection(selection);

		if (result) {
			// must also be able to modify the containing folder/resource
			CDOObject toDelete = getSelectedCDOObject();
			EObject container = toDelete.eContainer();
			if (container != null) {
				CDOObject cdoContainer = CDOUtils.getCDOObject(container);
				result = (cdoContainer == null) || cdoContainer.cdoPermission().isWritable();
			}
		}

		return result;
	}
}

Back to the top