Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94251f9198de5cc03440571a93a3fe0d27cb5809 (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
/*****************************************************************************
 * 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.pages;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
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.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The page to configure the separator used in the CSV file
 *
 * @author Vincent Lorenzo
 *
 */
public class ImportCSVConfigurationPage extends WizardPage {

	/**
	 * the paste separator
	 */
	private PasteSeparator separator;

	/**
	 * the text delimiter
	 */
	private TextDelimiter textDelimiter;

	/**
	 * the specific seaprator
	 */
	private char[] textChar = null;

	/**
	 *
	 * Constructor.
	 *
	 * @param pageName
	 *            the name of the import page
	 * @param title
	 *            the title of the page
	 * @param titleImage
	 *            the image for this page
	 * @param tableManager
	 *            the table manager where the import will be done
	 */
	public ImportCSVConfigurationPage(final String pageName, final String title, final ImageDescriptor titleImage, final PasteSeparator defaultSeparator, final TextDelimiter defaultDelimiter) {
		super(pageName, title, titleImage);
		setDescription(Messages.ImportFilePage_SelectTheFileToImport);
		this.separator = defaultSeparator;
		this.textDelimiter = defaultDelimiter;
	}

	/**
	 *
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 */
	@Override
	public void createControl(final Composite parent1) {
		final Composite pageComposite = new Composite(parent1, SWT.NONE);
		setControl(pageComposite);
		pageComposite.setLayout(new GridLayout(2, true));
		final Group separatorGroup = new Group(pageComposite, SWT.NONE);
		separatorGroup.setText(Messages.ImportCSVConfigurationPage_Separators);
		separatorGroup.setLayout(new GridLayout(2, false));

		for (final PasteSeparator current : PasteSeparator.values()) {
			final Button button = new Button(separatorGroup, SWT.RADIO);
			button.setText(current.getName());
			button.addSelectionListener(new SelectionListener() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					if (button.isEnabled()) {
						separator = current;
					}
					setPageComplete(validate());
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					// nothing to do
				}
			});
			button.setSelection(current == separator);
			final GridData data = new GridData();
			if (current == PasteSeparator.OTHER) {
				final Text text = new Text(separatorGroup, SWT.BORDER);
				text.addKeyListener(new org.eclipse.swt.events.KeyListener() {

					@Override
					public void keyReleased(KeyEvent e) {
						textChar = text.getTextChars();
						setPageComplete(validate());
					}

					@Override
					public void keyPressed(KeyEvent e) {
						// nothing to do
					}
				});
				button.addSelectionListener(new SelectionListener() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						text.setEnabled(button.getSelection());
						setPageComplete(validate());
					}

					@Override
					public void widgetDefaultSelected(SelectionEvent e) {
					}
				});
				text.setEnabled(this.separator == PasteSeparator.OTHER);
			} else {
				data.horizontalSpan = 2;
			}
			button.setLayoutData(data);
		}

		final Composite textDelimiterComp = new Composite(pageComposite, SWT.NONE);
		textDelimiterComp.setLayout(new GridLayout(2, false));
		textDelimiterComp.setLayoutData(new GridData());
		final Label label2 = new Label(textDelimiterComp, SWT.NONE);
		label2.setText(Messages.ImportCSVConfigurationPage_SelectTheTextDelimiter);
		final Combo combo = new Combo(textDelimiterComp, SWT.DROP_DOWN | SWT.READ_ONLY);

		for (int i = 0; i < TextDelimiter.values().length; i++) {
			final TextDelimiter current = TextDelimiter.values()[i];
			combo.add(String.valueOf(current.getDelimiter()), i);
			if (this.textDelimiter == current) {
				combo.select(i);
			}
		}
		combo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				textDelimiter = TextDelimiter.values()[combo.getSelectionIndex()];
				setPageComplete(validate());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// nothing to do
			}
		});

		setPageComplete(validate());
	}

	/**
	 *
	 * @return
	 */
	private boolean validate() {
		if (this.separator == PasteSeparator.OTHER) {
			if (this.textChar == null || this.textChar.length == 0) {
				setErrorMessage(Messages.ImportCSVConfigurationPage_TheCellSeparatorIsNotDefined);
				return false;
			} else if (this.textChar.length > 1) {
				setErrorMessage(Messages.ImportCSVConfigurationPage_TheCellSeparatorMustBeExcatlyOneChar);
				return false;
			}
		}
		setErrorMessage(null);
		return true;
	}

	/**
	 *
	 * @return
	 *         the selected separator
	 */
	public char getSeparator() {
		if (this.separator == PasteSeparator.OTHER) {
			return textChar[0];
		}
		return this.separator.getSeparator();
	}

	/**
	 *
	 * @return
	 *         the selected text delimiter
	 */
	public char getTextDelimiter() {
		return this.textDelimiter.getDelimiter();
	}



}

Back to the top