Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de0dd4a05cf42add3bd90eee46e344778cb7da5f (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Thanh Liem PHAN (ALL4TEC) <thanhliem.phan@all4tec.net> - Bug 417095
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.dialog;

import java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.nattable.enums.TreeTableAction;
import org.eclipse.papyrus.infra.nattable.export.image.ImageFormat;
import org.eclipse.papyrus.infra.nattable.messages.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

/**
 * Dialog lets user to export table to image.
 * @since 3.0
 */
public class ExportTableDialog extends Dialog {

	/** The dialog composite. */
	protected Composite dialogComposite = null;

	/** The filter index. */
	protected int filterIndex = -1;

	/** The output directory path. */
	protected Text outputPathTxt = null;

	/** The browse file system button. */
	protected Button browseFileSystemBtn = null;

	/** The browse workspace button. */
	protected Button browseWorkSpaceBtn = null;

	/** The image name. */
	protected Text imageNameTxt = null;

	/** The output format combobox. */
	protected Combo outputFormatCb = null;

	/** The tree action combobox. */
	protected Combo treeActionCb = null;

	/** The output directory resource. */
	protected IResource outputDirectory = null;

	/** The exporter string. */
	protected String exportFormat = null;

	/** The selected tree action string. */
	protected String selectedTreeAction = null;

	/** The default table name. */
	protected String defaultTableName = null;

	/** The default filter extension string array. */
	protected String[] defaultFilterExtensions = null;

	/** Flag to indicate that the table is a tree one. */
	protected boolean isTreeTable = false;

	/** The exported file name. */
	protected String exportedFileName = null;

	/**
	 * Instantiate a dialog to export a table.
	 *
	 * @param parentShell
	 *            The parent shell
	 * @param defaultOutputDir
	 *            The default output directory resource
	 * @param defaultTableName
	 *            The default table name
	 * @param defaultFilterExtensions
	 *            The default filter extensions
	 * @param isTreeTable
	 *            Flag to indicate that the table is a tree one
	 */
	public ExportTableDialog(final Shell parentShell, final IResource defaultOutputDir, final String defaultTableName, final String[] defaultFilterExtensions, final boolean isTreeTable) {
		super(parentShell);
		setBlockOnOpen(true);
		setShellStyle(getShellStyle() | SWT.RESIZE);

		this.outputDirectory = defaultOutputDir;
		this.defaultTableName = defaultTableName;
		this.defaultFilterExtensions = defaultFilterExtensions;
		this.isTreeTable = isTreeTable;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void configureShell(final Shell newShell) {
		super.configureShell(newShell);

		// Set title to dialog
		newShell.setText(Messages.ExportTableDialog_ExportTableDialogTitle);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(final Composite parent) {
		dialogComposite = (Composite) super.createDialogArea(parent);

		createDialogComposite();
		createDialogContents();
		return dialogComposite;
	}

	/**
	 * Create the dialog composite.
	 */
	protected void createDialogComposite() {
		GridLayout dialogLayout = new GridLayout();
		GridData dialogLayoutData = new GridData(GridData.FILL_BOTH);
		dialogComposite.setLayout(dialogLayout);
		dialogComposite.setLayoutData(dialogLayoutData);
	}

	/**
	 * Create the dialog contents.
	 */
	protected void createDialogContents() {
		Composite composite = new Composite(dialogComposite, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 4;
		composite.setLayout(layout);
		composite.setFont(dialogComposite.getFont());
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Create the 1st line for selecting output directory
		createOutputPathTextbox(composite);
		createBrowseWorkSpaceBtn(composite);
		createBrowseFileSystemBtn(composite);

		// Create the 2nd line for filling image file name
		createImageNameTextbox(composite);

		// Create the 3rd line for selecting image format
		createOutputFormatCombobox(composite);

		// If the table is the tree one, create a combobox to select expand|collapse|none action before exporting
		if (this.isTreeTable) {
			createTreeActionCombobox(composite);
		}

		// Initialise the exported file name
		computeExportedFileName();
	}

	/**
	 * Create the output path text box.
	 *
	 * @param composite
	 *            The parent composite
	 */
	protected void createOutputPathTextbox(final Composite composite) {
		Label selectOutputDirLbl = new Label(composite, SWT.NONE);
		selectOutputDirLbl.setText(Messages.ExportTableDialog_SelectOutputDirLabel);

		this.outputPathTxt = new Text(composite, SWT.BORDER);
		this.outputPathTxt.setEnabled(false);
		this.outputPathTxt.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		if (null != this.outputDirectory) {
			outputPathTxt.setText(this.outputDirectory.getLocation().toString());
		}
	}

	/**
	 * Create the browse file system button.
	 *
	 * @param composite
	 *            the parent composite
	 */
	protected void createBrowseFileSystemBtn(final Composite composite) {

		this.browseFileSystemBtn = new Button(composite, SWT.NONE);
		this.browseFileSystemBtn.setText(Messages.ExportTableDialog_BrowseFileSystem);
		this.browseFileSystemBtn.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				DirectoryDialog dirDialog = new DirectoryDialog(getShell(), SWT.NONE);

				String absolutePath = dirDialog.open();
				if (null != absolutePath && !absolutePath.isEmpty()) {

					// Update the output path
					outputPathTxt.setText(absolutePath);

					// Update the exported file name when output dir is changed
					computeExportedFileName();
				}
			}
		});
	}

	/**
	 * Create the browse workspace button.
	 *
	 * @param composite
	 *            the parent composite
	 */
	protected void createBrowseWorkSpaceBtn(final Composite composite) {

		this.browseWorkSpaceBtn = new Button(composite, SWT.NONE);
		this.browseWorkSpaceBtn.setText(Messages.ExportTableDialog_BrowseWorkSpace);
		this.browseWorkSpaceBtn.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				ContainerSelectionDialog csDialog = new ContainerSelectionDialog(Display.getCurrent().getActiveShell(), ResourcesPlugin.getWorkspace().getRoot(), true, Messages.ExportTableDialog_ContainerSelectionDialogTitle);

				if (Window.OK == csDialog.open()) {
					Object[] results = csDialog.getResult();
					if (1 == results.length && results[0] instanceof IPath) {
						URI uri = URI.createPlatformResourceURI(((IPath) results[0]).toString(), true);
						outputDirectory = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(uri.toPlatformString(true)));
						outputPathTxt.setText(outputDirectory.getLocation().toString());

						// Update the exported file name when output dir is changed
						computeExportedFileName();
					}
				}
			}
		});
	}

	/**
	 * Create the image name text box.
	 *
	 * @param composite
	 *            The parent composite
	 */
	protected void createImageNameTextbox(final Composite composite) {
		Label imageNameLbl = new Label(composite, SWT.NONE);
		imageNameLbl.setText(Messages.ExportTableDialog_ImageNameLabel);

		this.imageNameTxt = new Text(composite, SWT.BORDER);
		GridData imageNameTxtGridData = new GridData(GridData.FILL_HORIZONTAL);
		imageNameTxtGridData.horizontalSpan = 3;
		this.imageNameTxt.setLayoutData(imageNameTxtGridData);
		this.imageNameTxt.setText(this.defaultTableName);
		this.imageNameTxt.addModifyListener(new ModifyListener() {

			@Override
			public final void modifyText(final ModifyEvent event) {
				// Update the exported file name when image name text is changed
				computeExportedFileName();
			}
		});
	}

	/**
	 * Create the output format combobox.
	 *
	 * @param composite
	 *            The parent composite
	 */
	protected void createOutputFormatCombobox(final Composite composite) {
		Label outputFormatLbl = new Label(composite, SWT.NONE);
		outputFormatLbl.setText(Messages.ExportTableDialog_SelectOutputFormatLabel);

		this.outputFormatCb = new Combo(composite, SWT.NONE);
		GridData outputFormatCbGridData = new GridData(GridData.FILL_HORIZONTAL);
		outputFormatCbGridData.horizontalSpan = 3;
		this.outputFormatCb.setLayoutData(outputFormatCbGridData);

		// Load output format combobox
		for (String imageFileFormat : defaultFilterExtensions) {
			this.outputFormatCb.add(imageFileFormat);
		}
		this.outputFormatCb.setText(ImageFormat.getDefaultImageExtension());
		this.exportFormat = this.outputFormatCb.getText();
		this.filterIndex = 0;

		this.outputFormatCb.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				exportFormat = outputFormatCb.getText();
				filterIndex = outputFormatCb.getSelectionIndex();

				// Update the exported file name when image format is changed
				computeExportedFileName();
			}
		});
	}

	/**
	 * Create the tree action combo box.
	 *
	 * @param composite
	 *            The parent composite
	 */
	protected void createTreeActionCombobox(final Composite composite) {
		Label selectTreeActionLbl = new Label(composite, SWT.NONE);
		selectTreeActionLbl.setText(Messages.ExportTableDialog_SelectTreeActionLabel);

		this.treeActionCb = new Combo(composite, SWT.NONE);
		GridData treeActionCbGridData = new GridData(GridData.FILL_HORIZONTAL);
		treeActionCbGridData.horizontalSpan = 3;
		this.treeActionCb.setLayoutData(treeActionCbGridData);

		// Load all values for the tree action combobox
		for (TreeTableAction action : TreeTableAction.VALUES) {
			this.treeActionCb.add(action.toString());
		}

		// Set the default action
		this.treeActionCb.setText(TreeTableAction.getDefaultTableAction().toString());
		this.selectedTreeAction = this.treeActionCb.getText();

		// Add the listener for the combobox
		this.treeActionCb.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				// Update the selected tree action from the tree action combobox
				selectedTreeAction = treeActionCb.getText();
			}
		});
	}

	/**
	 * @return The selected tree action
	 */
	public String getSelectedTreeAction() {
		return this.selectedTreeAction;
	}

	/**
	 * @return The exported file name
	 */
	public String getExportedFileName() {
		return this.exportedFileName;
	}

	/**
	 * Compute the exported filename when controls are updated.
	 */
	protected void computeExportedFileName() {
		this.exportedFileName = this.outputPathTxt.getText() + File.separator + this.imageNameTxt.getText() + "." + this.exportFormat; //$NON-NLS-1$
	}

	/**
	 * @return The filter index
	 */
	public int getFilterIndex() {
		return filterIndex;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Overridden to show an override confirm message dialog when the exported file already exists in the destination folder.
	 */
	@Override
	protected void okPressed() {
		File file = new File(this.exportedFileName);
		if (file.exists()) {
			final MessageDialog dialog = new MessageDialog(
					getShell(),
					Messages.ExportTableDialog_OverrideConfirmMessasgeDialogText,
					null,
					NLS.bind(Messages.ExportTableDialog_OverrideConfirmMessasgeDialogMessage, this.imageNameTxt.getText() + "." + this.exportFormat), //$NON-NLS-1$
					MessageDialog.WARNING,
					new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL },
					0);

			if (dialog.open() != Window.OK) {
				return;
			}
		}

		super.okPressed();
	}
}

Back to the top