Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ec0bb2e1f27c457799684d9928b9daaa0ebbb57 (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
/*****************************************************************************
 * 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.export.image;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.export.IOutputStreamProvider;
import org.eclipse.nebula.widgets.nattable.export.image.ImageExporter;
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.export.streamprovider.PapyrusFileOutputStreamProvider;
import org.eclipse.papyrus.infra.nattable.style.configattribute.PapyrusExportConfigAttributes;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
 * An {@link ImageExporter} to export a NatTable to image in Papyrus.
 *
 * <p>
 * Warning: Using this class is risky as it could cause severe damage to
 * Papyrus NatTables that show huge data sets (for example: a table with more than 20k
 * rows).
 * </p>
 */
public class PapyrusImageExporter extends ImageExporter {

	/** Flag indicates the usage of Papyrus ExportTableDialog to retrieve file path, file name and file extension. */
	private Boolean usePapyrusExportTableDialog = Boolean.TRUE;

	/** The current image file name. */
	private String currentImageName = null;

	/** The file output stream provider. */
	private IOutputStreamProvider outputStreamProvider = null;

	/**
	 * Default constructor.
	 */
	public PapyrusImageExporter() {
		// Use a custom file output stream provider, which permits to change the default image name
		this(new PapyrusFileOutputStreamProvider(ImageFormat.DEFAULT_IMAGE_NAME, ImageFormat.IMAGE_FILTER_NAME_LIST, ImageFormat.IMAGE_FILTER_EXTENSION_LIST));
	}

	/**
	 * Constructor with an output stream provider.
	 *
	 * @param outputStreamProvider
	 *            The output stream provider
	 */
	public PapyrusImageExporter(final IOutputStreamProvider outputStreamProvider) {
		super(outputStreamProvider);
		this.outputStreamProvider = outputStreamProvider;
	}

	/**
	 * {@inheritDoc}
	 *
	 * Overridden to use the file name and file extension defined in the {@link PapyrusExportConfigAttributes} for exporting image
	 * in case the default export table dialog is not used.
	 */
	@Override
	public void exportTable(final Shell shell, final ProgressBar progressBar, final OutputStream outputStream, final ILayer layer, final IConfigRegistry configRegistry) throws IOException {
		if (null == shell || null == layer || null == configRegistry) {
			throw new IllegalArgumentException("Shell, layer or configure registry must not be null"); //$NON-NLS-1$
		}

		this.usePapyrusExportTableDialog = configRegistry.getConfigAttribute(
				PapyrusExportConfigAttributes.EXPORT_IMAGE_USE_PAPYRUS_EXPORT_TABLE_DIALOG,
				DisplayMode.NORMAL);

		// If the ExportTableDialog is used, export table using the default behavior.
		// Otherwise, get the image name and image file type from the config registry to export
		if (this.usePapyrusExportTableDialog) {
			super.exportTable(shell, progressBar, outputStream, layer, configRegistry);
		} else {
			ImageFormat imageFormat = configRegistry.getConfigAttribute(
					PapyrusExportConfigAttributes.EXPORT_IMAGE_FORMAT,
					DisplayMode.NORMAL);

			this.currentImageName = configRegistry.getConfigAttribute(
					PapyrusExportConfigAttributes.EXPORT_IMAGE_FILENAME,
					DisplayMode.NORMAL);

			String imageFormatExtension = ImageFormat.getImageFilterExtension(imageFormat);

			if (null != this.currentImageName && null != imageFormatExtension) {
				final int imageFormatIndex = getImageFormatIndex(imageFormatExtension);

				final int width = layer.getWidth();
				final int height = layer.getHeight();

				final Image image = new Image(shell.getDisplay(), width, height);
				GC gc = new GC(image);

				final Rectangle layerBounds = new Rectangle(0, 0, width, height);
				layer.getLayerPainter().paintLayer(layer, gc, 0, 0, layerBounds, configRegistry);

				final ImageLoader imageLoader = new ImageLoader();
				imageLoader.data = new ImageData[] { image.getImageData() };

				try {
					imageLoader.save(this.currentImageName, imageFormatIndex);
				} catch (IllegalArgumentException e) {
					Activator.log.error(e);
				} catch (SWTException e) {
					Activator.log.error(e);
				} finally {
					gc.dispose();
					image.dispose();
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * Overidden to return the current image file as a result incase the default NatTable file dialog is not used.
	 */
	@Override
	public Object getResult() {
		if (this.usePapyrusExportTableDialog) {
			return super.getResult();
		} else {
			return (null != this.currentImageName) ? new File(this.currentImageName) : null;
		}
	}

	/**
	 * @return The output stream provider used in this exporter.
	 */
	public IOutputStreamProvider getOutputStreamProvider() {
		return this.outputStreamProvider;
	}
}

Back to the top