Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a79ff198bf3d902a2ca16aa9385d96f891cb367 (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
/*****************************************************************************
 * Copyright (c) 2014, 2017 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
 *     Thanh Liem PHAN (ALL4TEC) thanhliem.phan@all4tec.net - Bug 515806
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.utils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.manager.cell.ICellManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.NattablePackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.Table;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.Cell;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.ICellAxisWrapper;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.IdAxisWrapper;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.NattablecellFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablecell.NattablecellPackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.NattableconfigurationPackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableproblem.NattableproblemFactory;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableproblem.NattableproblemPackage;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableproblem.Problem;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableproblem.StringResolutionProblem;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.StyledElement;
import org.eclipse.papyrus.infra.nattable.paste.IValueSetter;
import org.eclipse.papyrus.infra.nattable.paste.ReferenceValueSetter;
import org.eclipse.papyrus.infra.nattable.preferences.pages.CellPreferencePage;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.ui.converter.ConvertedValueContainer;
import org.eclipse.papyrus.infra.ui.converter.StringValueConverterStatus;


public class CellHelper {

	private CellHelper() {
		// to prevent instanciation
	}

	/**
	 * Create the string problems if required.
	 *
	 * @param tableManager
	 *            the table manager
	 * @param columnElement
	 *            the column element
	 * @param rowElement
	 *            the row element
	 * @param pastedText
	 *            the pasted text
	 * @param valueContainer
	 *            the value container
	 * @param sharedMap
	 *            the shared map
	 */
	public static final void createStringResolutionProblem(final INattableModelManager tableManager, final Object columnElement, final Object rowElement, final String pastedText, final ConvertedValueContainer<?> valueContainer, final Map<?, ?> sharedMap) {
		final IStatus status = valueContainer.getStatus();
		if (!status.isOK()) {
			if (status.matches(IStatus.ERROR)) {
				@SuppressWarnings("unchecked")
				final List<IValueSetter> references = (List<IValueSetter>) sharedMap.get(Constants.REFERENCES_TO_SET_KEY);
				if (status.matches(IStatus.ERROR)) {
					Cell cell = tableManager.getCell(columnElement, rowElement);

					if (cell == null) {
						// we create the cell
						cell = NattablecellFactory.eINSTANCE.createCell();

						// create the columnWrapper;
						final Object column = AxisUtils.getRepresentedElement(columnElement);
						ICellAxisWrapper columnWrapper = null;
						if (column instanceof String) {
							columnWrapper = NattablecellFactory.eINSTANCE.createIdAxisWrapper();
							((IdAxisWrapper) columnWrapper).setElement((String) column);
						} else if (column instanceof EObject) {
							columnWrapper = NattablecellFactory.eINSTANCE.createEObjectAxisWrapper();
							ReferenceValueSetter structure = new ReferenceValueSetter(columnWrapper, NattablecellPackage.eINSTANCE.getEObjectAxisWrapper_Element(), column);
							references.add(structure);
						}

						final Object row = AxisUtils.getRepresentedElement(rowElement);
						ICellAxisWrapper rowWrapper = null;
						if (row instanceof String) {
							rowWrapper = NattablecellFactory.eINSTANCE.createIdAxisWrapper();
							((IdAxisWrapper) rowWrapper).setElement((String) row);
						} else if (row instanceof EObject) {
							rowWrapper = NattablecellFactory.eINSTANCE.createEObjectAxisWrapper();
							ReferenceValueSetter structure = new ReferenceValueSetter(rowWrapper, NattablecellPackage.eINSTANCE.getEObjectAxisWrapper_Element(), row);
							references.add(structure);
						}

						if (rowWrapper == null || columnWrapper == null) {
							throw new UnsupportedOperationException("Case not managed"); //$NON-NLS-1$
						}
						cell.setColumnWrapper(columnWrapper);
						cell.setRowWrapper(rowWrapper);
						@SuppressWarnings("unchecked")
						final List<Cell> cells = (List<Cell>) sharedMap.get(Constants.CELLS_TO_ADD_KEY);
						cells.add(cell);


					}


					final StringResolutionProblem problem = NattableproblemFactory.eINSTANCE.createStringResolutionProblem();
					problem.setName("Set Value As Text Problem"); //$NON-NLS-1$
					problem.setDescription(status.getMessage());
					problem.setValueAsString(pastedText);

					if (status instanceof StringValueConverterStatus) {
						problem.getUnresolvedString().addAll(((StringValueConverterStatus) status).getUnresolvedString());
					}

					if (cell.eContainer() == null) {
						cell.eSet(NattablecellPackage.eINSTANCE.getCell_Problems(), Collections.singleton(problem));
					} else {
						final ReferenceValueSetter structure = new ReferenceValueSetter(cell, NattablecellPackage.eINSTANCE.getCell_Problems(), problem);
						references.add(structure);
					}
				}
			}
		}
	}

	/**
	 *
	 * @param domain
	 *            the editing domain
	 * @param tableManager
	 *            the table manager
	 * @param rowElement
	 *            the row element
	 * @param columnElement
	 *            the column element
	 * @param pastedText
	 *            the pasted text
	 * @param valueContainer
	 *            the converted value
	 * @return
	 * 		the command to create a String resolution Problem
	 */
	public static final Command getCreateStringResolutionProblemCommand(final TransactionalEditingDomain domain, final INattableModelManager tableManager, final Object columnElement, final Object rowElement, final String pastedText,
			final ConvertedValueContainer<?> valueContainer) {
		final IStatus status = valueContainer.getStatus();
		if (!status.isOK()) {
			if (status.matches(IStatus.ERROR)) {
				final CompositeCommand command = new CompositeCommand("Create Cell For Error Command"); //$NON-NLS-1$
				Cell cell = tableManager.getCell(columnElement, rowElement);

				if (cell == null) {
					// we create the cell
					final Table table = tableManager.getTable();
					cell = NattablecellFactory.eINSTANCE.createCell();

					SetRequest request = new SetRequest(domain, table, NattablePackage.eINSTANCE.getTable_Cells(), cell);
					IElementEditService provider = ElementEditServiceUtils.getCommandProvider(table);
					command.add(provider.getEditCommand(request));

					// create the columnWrapper;
					final Object column = AxisUtils.getRepresentedElement(columnElement);
					ICellAxisWrapper columnWrapper = null;
					if (column instanceof String) {
						columnWrapper = NattablecellFactory.eINSTANCE.createIdAxisWrapper();
						provider = ElementEditServiceUtils.getCommandProvider(columnWrapper);
						request = new SetRequest(domain, columnWrapper, NattablecellPackage.eINSTANCE.getIdAxisWrapper_Element(), column);
						command.add(provider.getEditCommand(request));
					} else if (column instanceof EObject) {
						columnWrapper = NattablecellFactory.eINSTANCE.createEObjectAxisWrapper();
						provider = ElementEditServiceUtils.getCommandProvider(columnWrapper);
						request = new SetRequest(domain, columnWrapper, NattablecellPackage.eINSTANCE.getEObjectAxisWrapper_Element(), column);
						command.add(provider.getEditCommand(request));
					}

					final Object row = AxisUtils.getRepresentedElement(rowElement);
					ICellAxisWrapper rowWrapper = null;
					if (row instanceof String) {
						rowWrapper = NattablecellFactory.eINSTANCE.createIdAxisWrapper();
						provider = ElementEditServiceUtils.getCommandProvider(rowWrapper);
						request = new SetRequest(domain, rowWrapper, NattablecellPackage.eINSTANCE.getIdAxisWrapper_Element(), row);
						command.add(provider.getEditCommand(request));
					} else if (row instanceof EObject) {
						rowWrapper = NattablecellFactory.eINSTANCE.createEObjectAxisWrapper();
						provider = ElementEditServiceUtils.getCommandProvider(rowWrapper);
						request = new SetRequest(domain, rowWrapper, NattablecellPackage.eINSTANCE.getEObjectAxisWrapper_Element(), row);
						command.add(provider.getEditCommand(request));
					}

					if (rowWrapper == null || columnWrapper == null) {
						throw new UnsupportedOperationException("Case not managed"); //$NON-NLS-1$
					}


					provider = ElementEditServiceUtils.getCommandProvider(cell);
					request = new SetRequest(domain, cell, NattablecellPackage.eINSTANCE.getCell_ColumnWrapper(), columnWrapper);
					command.add(provider.getEditCommand(request));
					request = new SetRequest(domain, cell, NattablecellPackage.eINSTANCE.getCell_RowWrapper(), rowWrapper);
					command.add(provider.getEditCommand(request));
				}
				final List<Problem> problems = cell.getProblems();
				StringResolutionProblem problem = null;
				for (final Problem current : problems) {
					if (current instanceof StringResolutionProblem) {
						problem = (StringResolutionProblem) current;
						break;
					}
				}

				// we create a new string problem
				if (problem == null) {
					problem = NattableproblemFactory.eINSTANCE.createStringResolutionProblem();
					problem.setName("Set Value As Text Problem"); //$NON-NLS-1$
					problem.setDescription(status.getMessage());
					problem.setValueAsString(pastedText);

					if (status instanceof StringValueConverterStatus) {
						problem.getUnresolvedString().addAll(((StringValueConverterStatus) status).getUnresolvedString());
						SetRequest setProblemRequest = new SetRequest(domain, cell, NattablecellPackage.eINSTANCE.getCell_Problems(), problem);
						IElementEditService provider = ElementEditServiceUtils.getCommandProvider(cell);
						command.add(provider.getEditCommand(setProblemRequest));
					}
				} else {// we update the string problem
					IElementEditService provider = ElementEditServiceUtils.getCommandProvider(problem);
					SetRequest request = new SetRequest(domain, problem, NattableproblemPackage.eINSTANCE.getStringResolutionProblem_ValueAsString(), pastedText);
					command.add(provider.getEditCommand(request));
					request = new SetRequest(domain, problem, NattableproblemPackage.eINSTANCE.getStringResolutionProblem_UnresolvedString(), ((StringValueConverterStatus) status).getUnresolvedString());
					command.add(provider.getEditCommand(request));
					request = new SetRequest(domain, problem, NattableconfigurationPackage.eINSTANCE.getTableNamedElement_Description(), status.getMessage());
					command.add(provider.getEditCommand(request));
				}


				return new GMFtoEMFCommandWrapper(command);
			}
		}
		return null;
	}

	/**
	 *
	 * @param domain
	 *            the editing domain
	 * @param columnElement
	 *            the column element
	 * @param rowElement
	 *            the row element
	 * @param newValue
	 *            the new value
	 * @param tableManager
	 *            the table manager
	 * @return
	 * 		the command to use to destroy the string problem referenced by a cell
	 */
	public static final Command getDestroyStringResolutionProblemCommand(final TransactionalEditingDomain domain, final Object columnElement, final Object rowElement, final Object newValue, final INattableModelManager tableManager) {
		Cell cell = tableManager.getCell(columnElement, rowElement);
		CompoundCommand removeProblemCommand = new CompoundCommand("Destroy string problem command"); //$NON-NLS-1$
		// we remove the problems of this cells
		if (cell != null && !cell.getProblems().isEmpty()) {
			for (Problem current : cell.getProblems()) {
				if (current instanceof StringResolutionProblem) {
					RemoveCommand rc = new RemoveCommand(domain, cell, NattablecellPackage.eINSTANCE.getCell_Problems(), current);
					removeProblemCommand.append(rc);
				}
			}
			// now the cell has no problem
			if (cell.getEAnnotations().isEmpty()) {
				if (cell instanceof StyledElement) {// currenlty it is impossible, but it could change in future version of Papyrus
					StyledElement styledElement = (StyledElement) cell;
					if (styledElement.getStyles().isEmpty()) {
						RemoveCommand rc = new RemoveCommand(domain, tableManager.getTable(), NattablePackage.eINSTANCE.getTable_Cells(), cell);
						removeProblemCommand.append(rc);
					}
				} else {
					RemoveCommand rc = new RemoveCommand(domain, tableManager.getTable(), NattablePackage.eINSTANCE.getTable_Cells(), cell);
					removeProblemCommand.append(rc);
				}
			}
		}
		if (!removeProblemCommand.isEmpty()) {
			return removeProblemCommand;
		}
		return null;
	}

	/**
	 * @return the text of cell to be displayed with unsupported column
	 * @since 4.0
	 */
	public static final String getUnsupportedCellContentsText() {
		return Activator.getDefault().getPreferenceStore().getString(CellPreferencePage.UNSUPPORTED_COLUMN_CELL_TEXT);
	}
}

Back to the top