Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b50926f1c1885d0b7062409a1d3ede83b6402f23 (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
/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.handler;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.Cell;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.NattablecellPackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableproblem.Problem;
import org.eclipse.papyrus.infra.nattable.utils.TableSelectionWrapper;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;

/**
 *
 * @author VL222926
 *
 */
public class ResolvedProblemHandler extends AbstractTableHandler {



	/**
	 *
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 *
	 * @param event
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final TransactionalEditingDomain domain = getTableEditingDomain();
		
		Problem problemToDestroy = getProblemToDestroy(event);
		DestroyElementRequest request = new DestroyElementRequest(domain, problemToDestroy, false);
		final Cell cell = (Cell) problemToDestroy.eContainer();
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(cell);
		CompositeCommand composite = new CompositeCommand("Destroy Problem Command"); //$NON-NLS-1$
		composite.add(provider.getEditCommand(request));

		// TODO : improve me and move me into an edit helper when we will have customization for the cell
		composite.add(new AbstractTransactionalCommand(domain, "Clean Table Model : remove empty Cell", null) { //$NON-NLS-1$

					@Override
					protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
						boolean isEmpty = true;
						if (cell.getProblems().size() == 0) {
							Collection<EStructuralFeature> featureToIgnore = new ArrayList<EStructuralFeature>();
							featureToIgnore.add(EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
							featureToIgnore.add(NattablecellPackage.eINSTANCE.getCell_ColumnWrapper());
							featureToIgnore.add(NattablecellPackage.eINSTANCE.getCell_RowWrapper());
							Collection<EStructuralFeature> allFeatures = new ArrayList<EStructuralFeature>(cell.eClass().getEAllStructuralFeatures());
							allFeatures.removeAll(featureToIgnore);
							for (EStructuralFeature eStructuralFeature : allFeatures) {
								if (eStructuralFeature.isMany()) {
									if (!((Collection<?>) cell.eGet(eStructuralFeature)).isEmpty()) {
										isEmpty = false;
									}
								} else {
									if (cell.eGet(eStructuralFeature) != eStructuralFeature.getDefaultValue()) {
										isEmpty = false;
									}
								}

							}
						}
						if (isEmpty) {
							DestroyElementRequest request = new DestroyElementRequest(domain, cell, false);
							// final Cell cell = (Cell)problemToDestroy.eContainer();
							IElementEditService provider = ElementEditServiceUtils.getCommandProvider(cell.eContainer());
							provider.getEditCommand(request).execute(null, null);
						}
						return null;
					}
				});
		Command cmd = new GMFtoEMFCommandWrapper(composite);
		domain.getCommandStack().execute(cmd);
		return null;
	}

	/**
	 *
	 * @see org.eclipse.papyrus.infra.nattable.handler.AbstractTableHandler#setEnabled(java.lang.Object)
	 *
	 * @param evaluationContext
	 */
	@Override
	public void setEnabled(Object evaluationContext) {
		super.setEnabled(evaluationContext);
		if (isEnabled()) {
			setBaseEnabled(getProblemToDestroy(evaluationContext) != null);
		}
	}

	/**
	 * 
	 * @param evaluationContextOrExecutionEvent
	 * an evaluation context (coming from setEnable(Object evaluationContext) or an ExecutionEvent (coming from execute(ExecutionEvent e)
	 * @return
	 */
	private Problem getProblemToDestroy(Object evaluationContextOrExecutionEvent) {
		TableSelectionWrapper wrapper = getTableSelectionWrapper();
		if (isEnabled() && wrapper != null) {
			Collection<PositionCoordinate> selectionCells = wrapper.getSelectedCells();
			if (selectionCells.size() == 1) {
				final PositionCoordinate positionCoordinate = selectionCells.iterator().next();
				final INattableModelManager nattableManager = getCurrentNattableModelManager();
				final Object rowElement;
				final Object columnElement;
				if (!nattableManager.getTable().isInvertAxis()) {
					rowElement = nattableManager.getRowElement(positionCoordinate.getRowPosition());
					columnElement = nattableManager.getColumnElement(positionCoordinate.getColumnPosition());
				} else {
					columnElement = nattableManager.getRowElement(positionCoordinate.getRowPosition());
					rowElement = nattableManager.getColumnElement(positionCoordinate.getColumnPosition());
				}

				final Cell cell = nattableManager.getCell(columnElement, rowElement);
				if (cell != null) {
					final Collection<Problem> problems = cell.getProblems();
					if (problems.size() == 1) {
						return problems.iterator().next();
					}
				}
			}
		}
		return null;
	}

}

Back to the top