Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d84a8e4a7b13d1f9ed9fe12e403f66fedb2e8ddf (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
/*****************************************************************************
 * Copyright (c) 2014 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.wizard;

import java.io.File;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.manager.ImportAxisInNattableManager;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.messages.Messages;
import org.eclipse.papyrus.infra.nattable.paste.PasteSeparator;
import org.eclipse.papyrus.infra.nattable.paste.TextDelimiter;
import org.eclipse.papyrus.infra.nattable.utils.CSVPasteHelper;
import org.eclipse.papyrus.infra.nattable.wizard.pages.ImportCSVConfigurationPage;
import org.eclipse.papyrus.infra.nattable.wizard.pages.ImportFilePage;
import org.eclipse.papyrus.infra.nattable.wizard.pages.ImportInvertedTableErrorPage;
import org.eclipse.papyrus.infra.nattable.wizard.pages.ImportTableErrorPage;
import org.eclipse.papyrus.infra.widgets.util.FileUtil;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;

/**
 * 
 * This wizard allows to import a table in the Papyrus nattable editor
 * 
 */
public class ImportTableWizard extends Wizard implements IImportWizard {

	/**
	 * the import page
	 */
	private ImportFilePage importPage;

	/**
	 * the page used to configure the CSV import
	 */
	private ImportCSVConfigurationPage csvConfigurationPage;

	/**
	 * the nattable model manager to use for the import
	 */
	private INattableModelManager manager;


	/**
	 * 
	 * Constructor.
	 * 
	 */
	public ImportTableWizard() {
		setWindowTitle(Messages.ImportTableWizard_ImportTableFromFileInPapyrusModel);
		String pluginId = Activator.PLUGIN_ID;
		String path = "icons/importTablePapyrusWizban.png"; //$NON-NLS-1$
		ImageDescriptor desc = org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImageDescriptor(pluginId, path);
		setDefaultPageImageDescriptor(desc);
	}

	/**
	 * 
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 * 
	 * @param workbench
	 * @param selection
	 */
	@Override
	public void init(final IWorkbench workbench, final IStructuredSelection selection) {
		this.manager = getNattableModelManager(workbench, selection);
	}

	/**
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 * 
	 */
	@Override
	public void addPages() {
		final String pageTitle = Messages.ImportTableWizard_ImportTable;
		final ImageDescriptor desc = null;
		if(manager != null) {
			if(manager.getTable().isInvertAxis()) {
				addPage(new ImportInvertedTableErrorPage(Messages.ImportTableWizard_ImportTableError, pageTitle, desc));
			} else {
				this.importPage = new ImportFilePage(pageTitle, pageTitle, desc, this.manager);
				addPage(this.importPage);
				this.csvConfigurationPage = new ImportCSVConfigurationPage(pageTitle, Messages.ImportTableWizard_ConfigureImport, desc, PasteSeparator.SEMICOLON, TextDelimiter.DOUBLE_QUOTE);
				addPage(this.csvConfigurationPage);
			}
		} else {
			addPage(new ImportTableErrorPage(Messages.ImportTableWizard_ImportTableError, pageTitle, desc));
		}
	}


	/**
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 * 
	 * @return
	 */
	@Override
	public boolean performFinish() {
		//the import file
		final File file = FileUtil.getFile(this.importPage.getFilePath());
		final CSVPasteHelper pasteHelper = new CSVPasteHelper(this.csvConfigurationPage.getSeparator(), this.csvConfigurationPage.getTextDelimiter(), ","); //$NON-NLS-1$
		final ImportAxisInNattableManager pasteManager = new ImportAxisInNattableManager(this.manager, pasteHelper, file, true);
		return pasteManager.doPaste();
	}

	/**
	 * 
	 * @param workbench
	 *        the current workbench
	 * @param selection
	 *        the current selection
	 * @return
	 *         the nattable manager to use to do the import
	 */
	private INattableModelManager getNattableModelManager(final IWorkbench workbench, final IStructuredSelection selection) {
		final IEditorPart editorPart = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(editorPart != null) {
			return (INattableModelManager)editorPart.getAdapter(INattableModelManager.class);
		}
		return null;
	}


}

Back to the top