Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af5d5796fc14c8b93e26d60824dd289f5efbdeb1 (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST and others.
 *
 * 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.manager;

import java.io.IOException;
import java.io.Reader;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.dialog.PasteImportStatusDialog;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.messages.Messages;
import org.eclipse.papyrus.infra.nattable.provider.PasteInSelectionNattableCommandProvider;
import org.eclipse.papyrus.infra.nattable.provider.PasteInSelectionTreeNattableCommandProvider;
import org.eclipse.papyrus.infra.nattable.provider.PasteNattableCommandProvider;
import org.eclipse.papyrus.infra.nattable.utils.CSVPasteHelper;
import org.eclipse.papyrus.infra.nattable.utils.IPapyrusNattableStatus;
import org.eclipse.papyrus.infra.nattable.utils.PapyrusNattableStatus;
import org.eclipse.papyrus.infra.nattable.utils.PasteEnablementStatus;
import org.eclipse.papyrus.infra.nattable.utils.PasteModeEnumeration;
import org.eclipse.papyrus.infra.nattable.utils.TableHelper;
import org.eclipse.papyrus.infra.nattable.utils.TableSelectionWrapper;
import org.eclipse.swt.widgets.Display;

/**
 * This class allows to insert axis from string.
 */
public abstract class AbstractInsertImportInNattableManager extends AbstractPasteImportInsertInNattableManager {

	/**
	 * Constructor.
	 *
	 * @param tableManager
	 *            The nattable model manager.
	 * @param pasteHelper
	 *            The paste helper.
	 * @param useProgressMonitorDialog
	 *            Boolean to determinate if a progress monitor dialog must be used.
	 * @param openDialog
	 *            Boolean to determinate if the dialog must be opened during the process.
	 * @param preferredUserAction
	 *            The preferred user action for the insert row action.
	 */
	public AbstractInsertImportInNattableManager(final INattableModelManager tableManager, final CSVPasteHelper pasteHelper, final boolean useProgressMonitorDialog, final boolean openDialog, final int preferredUserAction) {
		this(tableManager, pasteHelper, useProgressMonitorDialog, openDialog, preferredUserAction, null);
	}

	/**
	 * Constructor.
	 *
	 * @param tableManager
	 *            The nattable model manager.
	 * @param pasteHelper
	 *            The paste helper.
	 * @param useProgressMonitorDialog
	 *            Boolean to determinate if a progress monitor dialog must be used.
	 * @param openDialog
	 *            Boolean to determinate if the dialog must be opened during the process.
	 * @param preferredUserAction
	 *            The preferred user action for the insert row action.
	 * @param tableSelectionWrapper
	 *            The current selection in the table.
	 */
	public AbstractInsertImportInNattableManager(final INattableModelManager tableManager, final CSVPasteHelper pasteHelper, final boolean useProgressMonitorDialog, final boolean openDialog, final int preferredUserAction,
			final TableSelectionWrapper tableSelectionWrapper) {
		super(tableManager, pasteHelper, useProgressMonitorDialog, openDialog, preferredUserAction, tableSelectionWrapper);
	}

	/**
	 * This method manages the paste and the possible error dialog before doing the paste.
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.papyrus.infra.nattable.manager.AbstractPasteImportInsertInNattableManager#doAction()
	 */
	@Override
	public IStatus doAction() {
		IStatus resultStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.InsertInNattableManager_InsertNotYetManaged);

		PasteEnablementStatus pasteStatus = null;
		// Check if the paste configuration is needed for the paste action
		pasteStatus = findPasteModeFromTableConfiguration(this.tableManager);
		if (PasteModeEnumeration.PASTE_NO_CONFIGURATION != pasteStatus.getPasteMode() && PasteModeEnumeration.CANT_PASTE != pasteStatus.getPasteMode()) {
			pasteStatus = findPasteConfigurationAxisIdentifier(this.tableManager);
		}
		if (pasteStatus.getPasteMode() == PasteModeEnumeration.PASTE_EOBJECT_ROW_OR_COLUMN) {
			final boolean value = MessageDialog.openQuestion(Display.getDefault().getActiveShell(), PasteImportStatusDialog.DIALOG_TITLE, Messages.AbstractPasteImportInsertInNattableManager_WhatAreYouPasting);
			if (value) {
				pasteStatus.getColumnStatus().add(new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.AbstractPasteImportInsertInNattableManager_TheUserChosesToPasteRows));
			} else {
				pasteStatus.getRowStatus().add(new Status(IStatus.CANCEL, Activator.PLUGIN_ID, Messages.AbstractPasteImportInsertInNattableManager_TheUserChosesToPasteColumns));
			}
		}

		final PasteModeEnumeration pasteMode = pasteStatus.getPasteMode();
		switch (pasteMode) {
		case PASTE_NO_CONFIGURATION:
			resultStatus = new PapyrusNattableStatus(IPapyrusNattableStatus.PASTE_CONFIGURATiON_INFO, Activator.PLUGIN_ID, Messages.AbstractPasteImportInsertInNattableManager_NoPasteConfiguration2);
			break;
		case CANT_PASTE:
			resultStatus = createCantInsertStatus(pasteStatus);
			break;
		case PASTE_EOBJECT_ROW:
			resultStatus = insertRow(this.tableManager, pasteStatus, pasteHelper);
			break;
		case PASTE_EOBJECT_COLUMN:
		case PASTE_EOBJECT_ROW_OR_COLUMN:
			// nothing to do
			break;
		default:
			break;
		}

		return resultStatus;
	}

	/**
	 * Manage the status when can't paste.
	 * 
	 * @param pasteStatus
	 *            The paste status.
	 * @return The status when this can't be pasted
	 */
	protected IStatus createCantInsertStatus(final PasteEnablementStatus pasteStatus) {
		IStatus resultStatus = new PapyrusNattableStatus(IPapyrusNattableStatus.PASTE_CONFIGURATiON_INFO, Activator.PLUGIN_ID, Messages.AbstractPasteImportInsertInNattableManager_NoPasteConfiguration2);

		final MultiStatus rowStatus = pasteStatus.getRowStatus();
		final MultiStatus columnStatus = pasteStatus.getColumnStatus();
		if (rowStatus == null && columnStatus != null) {
			resultStatus = new PapyrusNattableStatus(IPapyrusNattableStatus.PASTE_CONFIGURATiON_ERROR, Activator.PLUGIN_ID, getFirstNonOKStatus(columnStatus).getMessage());
		} else if (columnStatus == null && rowStatus != null) {
			resultStatus = new PapyrusNattableStatus(IPapyrusNattableStatus.PASTE_CONFIGURATiON_ERROR, Activator.PLUGIN_ID, getFirstNonOKStatus(rowStatus).getMessage());
		} else if (columnStatus != null && rowStatus != null) {
			final StringBuffer buffer = new StringBuffer(Messages.AbstractPasteImportInsertInNattableManager_NoPasteConfiguration);
			buffer.append(Messages.InsertInNattableManager_InsertRowsError);
			buffer.append(getFirstNonOKStatus(rowStatus).getMessage());
			buffer.append(Messages.InsertInNattableManager_InsertColumnsError);
			buffer.append(getFirstNonOKStatus(columnStatus).getMessage());
			resultStatus = new PapyrusNattableStatus(IPapyrusNattableStatus.PASTE_CONFIGURATiON_ERROR, Activator.PLUGIN_ID, buffer.toString());
		}

		return resultStatus;
	}

	/**
	 * Insert the row.
	 *
	 * @param manager
	 *            the table manager
	 * @param pasteStatus
	 *            the paste status
	 * @param pasteHelper
	 *            the paste helper
	 * @return
	 *         <code>true</code> if the paste can be done
	 */
	private IStatus insertRow(final INattableModelManager manager, final PasteEnablementStatus pasteStatus, final CSVPasteHelper pasteHelper) {
		IStatus resultStatus = Status.OK_STATUS;

		if (TableHelper.isTreeTable(manager)) {
			if (null != tableSelectionWrapper) {
				resultStatus = insertTreeRows(manager, pasteStatus, pasteHelper, tableSelectionWrapper);
			} else {
				resultStatus = insertTreeRows(manager, pasteStatus, pasteHelper);
			}
		} else {
			if (null != tableSelectionWrapper) {
				resultStatus = insertNormalRows(manager, pasteStatus, pasteHelper, tableSelectionWrapper);
			} else {
				resultStatus = insertNormalRows(manager, pasteStatus, pasteHelper);
			}
		}

		return resultStatus;
	}

	/**
	 * Insert the rows for a tree table with a current selection.
	 * 
	 * @param manager
	 *            The nattable manager.
	 * @param pasteStatus
	 *            The paste status.
	 * @param pasteHelper
	 *            The paste helper.
	 * @param tableSelectionWrapper
	 *            The current selection.
	 * @return The status of the paste.
	 */
	private IStatus insertTreeRows(final INattableModelManager manager, final PasteEnablementStatus pasteStatus, final CSVPasteHelper pasteHelper, final TableSelectionWrapper tableSelectionWrapper) {
		IStatus status = Status.OK_STATUS;
		if (status.isOK()) {
			status = insertTree(manager, pasteStatus, useProgressMonitorDialog, createReader(), tableSelectionWrapper, getDataSize());
		}
		return status;
	}

	/**
	 * Insert the rows for a tree table without selection.
	 * 
	 * @param manager
	 *            The nattable manager.
	 * @param pasteStatus
	 *            The paste status.
	 * @param pasteHelper
	 *            The paste helper.
	 * @param tableSelectionWrapper
	 *            The current selection.
	 * @return The status of the paste.
	 */
	private IStatus insertTreeRows(final INattableModelManager manager, final PasteEnablementStatus pasteStatus, final CSVPasteHelper pasteHelper) {
		IStatus status = checkTreeTableConfiguration(manager);
		if (status.isOK()) {
			status = insertTree(manager, pasteStatus, useProgressMonitorDialog, createReader(), null, getDataSize());
		}
		return status;
	}

	/**
	 * This allow to insert rows in a flat nattable.
	 * 
	 * @param manager
	 *            The nattable model manager.
	 * @param pasteStatus
	 *            The paste status.
	 * @param pasteHelper
	 *            The paste helper.
	 * @return The status corresponding to the paste.
	 */
	private IStatus insertNormalRows(final INattableModelManager manager, final PasteEnablementStatus pasteStatus, final CSVPasteHelper pasteHelper) {
		final Reader reader = createReader();
		IStatus status = verifyColumnCountOnFirstLine(manager, reader);
		try {
			reader.close();
		} catch (final IOException e) {
			Activator.log.error(e);
		}
		if (status.isOK()) {
			status = insert(manager, pasteStatus, useProgressMonitorDialog, createReader(), null, getDataSize());
		}
		return status;
	}

	/**
	 * This allow to insert rows in a flat nattable with selection
	 * 
	 * @param manager
	 *            The nattable model manager.
	 * @param pasteStatus
	 *            The paste status.
	 * @param pasteHelper
	 *            The paste helper.
	 * @param tableSelectionWrapper
	 *            The current selection in table.
	 * @return The status corresponding to the paste.
	 */
	private IStatus insertNormalRows(final INattableModelManager manager, final PasteEnablementStatus pasteStatus, final CSVPasteHelper pasteHelper, final TableSelectionWrapper tableSelectionWrapper) {
		final Reader reader = createReader();
		IStatus status = verifyColumnCountOnFirstLine(manager, reader);
		try {
			reader.close();
		} catch (final IOException e) {
			Activator.log.error(e);
		}
		if (status.isOK()) {
			status = insert(manager, pasteStatus, useProgressMonitorDialog, createReader(), tableSelectionWrapper, getDataSize());
		}
		return status;
	}

	/**
	 * Create the command provider for the insert in the tree table and execute the this command.
	 * 
	 * @param manager
	 *            the table manager
	 * @param status
	 *            the status
	 * @param useProgressMonitorDialog
	 *            if <code>true</code> a progress monitor will be used
	 * @param reader
	 *            the reader used to import data in the table.
	 * @param tableSelectionWrapper
	 *            The table selection wrapper for the current table.
	 * @param totalSize
	 *            the full size of the elements to import
	 * @return The result status
	 */
	private IStatus insertTree(final INattableModelManager manager, final PasteEnablementStatus status, final boolean useProgressMonitorDialog, final Reader reader, final TableSelectionWrapper tableSelectionWrapper, final long totalSize) {
		IStatus resultStatus = Status.OK_STATUS;

		PasteNattableCommandProvider commandProvider = null;
		switch (status.getPasteMode()) {
		case PASTE_EOBJECT_ROW:
			commandProvider = new PasteInSelectionTreeNattableCommandProvider(manager, false, true, reader, this.pasteHelper, this.tableSelectionWrapper, preferredUserAction, totalSize);
			break;
		default:
			break;
		}
		if (null != commandProvider) {
			resultStatus = commandProvider.executePasteFromStringCommand(useProgressMonitorDialog, openDialog);
		}
		return resultStatus;
	}

	/**
	 * Create the command provider for the insert in the flat table and execute the this command.
	 *
	 * @param manager
	 *            the table manager
	 * @param status
	 *            the status
	 * @param useProgressMonitorDialog
	 *            if <code>true</code> a progress monitor will be used
	 * @param reader
	 *            the reader used to import data in the table.
	 * @param tableSelectionWrapper
	 *            The table selection wrapper for the current table.
	 * @param totalSize
	 *            the full size of the elements to import
	 * @return The result status
	 */
	private IStatus insert(final INattableModelManager manager, final PasteEnablementStatus status, final boolean useProgressMonitorDialog, final Reader reader, final TableSelectionWrapper tableSelectionWrapper, final long totalSize) {
		IStatus resultStatus = Status.OK_STATUS;

		PasteNattableCommandProvider commandProvider = null;
		switch (status.getPasteMode()) {
		case PASTE_EOBJECT_ROW:
			commandProvider = new PasteInSelectionNattableCommandProvider(manager, false, true, reader, pasteHelper, tableSelectionWrapper, preferredUserAction, totalSize);
			break;
		default:
			break;
		}
		if (commandProvider != null) {
			resultStatus = commandProvider.executePasteFromStringCommand(useProgressMonitorDialog, openDialog);
		}
		return resultStatus;
	}
}

Back to the top