Skip to main content
summaryrefslogtreecommitdiffstats
blob: df9e9f8f51baada6ab6325b5e7a95bcc8ac54ddf (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
/*******************************************************************************
* Copyright (c) 2004, 2008 Tasktop Technologies 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:
 *     Jeff Pound - initial API and implementation
 *     Tasktop Technologies - improvement
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.wizards;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Text;

/**
 * Shows a preview of an attachment.
 * 
 * @author Jeff Pound
 * @author Steffen Pingel
 */
// TODO 3.1 rename to PreviewAttachmentPage
public class PreviewAttachmentPage2 extends WizardPage {

	private static final String DESCRIPTION = "Review the attachment before submitting";

	protected static final int MAX_TEXT_SIZE = 50000;

	private static final String PAGE_NAME = "PreviewAttachmentPage";

	private static final String TITLE = "Attachment Preview";

	private final Set<String> imageTypes;

	private final TaskAttachmentModel model;

	private Button runInBackgroundButton;

	private ScrolledComposite scrolledComposite;

	private final Set<String> textTypes;

	private Composite contentComposite;

	public PreviewAttachmentPage2(TaskAttachmentModel model) {
		super(PAGE_NAME);
		this.model = model;
		setTitle(TITLE);
		setDescription(DESCRIPTION);

		textTypes = new HashSet<String>();
		textTypes.add("text/plain");
		textTypes.add("text/html");
		textTypes.add("text/html");
		textTypes.add("application/xml");

		imageTypes = new HashSet<String>();
		imageTypes.add("image/jpeg");
		imageTypes.add("image/gif");
		imageTypes.add("image/png");
	}

	private void adjustScrollbars(Rectangle imgSize) {
		Rectangle clientArea = scrolledComposite.getClientArea();

		ScrollBar hBar = scrolledComposite.getHorizontalBar();
		hBar.setMinimum(0);
		hBar.setMaximum(imgSize.width - 1);
		hBar.setPageIncrement(clientArea.width);
		hBar.setIncrement(10);

		ScrollBar vBar = scrolledComposite.getVerticalBar();
		vBar.setMinimum(0);
		vBar.setMaximum(imgSize.height - 1);
		vBar.setPageIncrement(clientArea.height);
		vBar.setIncrement(10);
	}

	public void createControl(Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		setControl(composite);

		contentComposite = new Composite(composite, SWT.NONE);
		contentComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		contentComposite.setLayout(new GridLayout());

		runInBackgroundButton = new Button(composite, SWT.CHECK);
		runInBackgroundButton.setText("Run in background");
	}

	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			Control[] children = contentComposite.getChildren();
			for (Control control : children) {
				control.dispose();
			}
			if (isTextAttachment() || isImageAttachment()) {
				Object content = getContent(contentComposite);
				if (content instanceof String) {
					createTextPreview(contentComposite, (String) content);
				} else if (content instanceof Image) {
					createImagePreview(contentComposite, (Image) content);
				}
			} else {
				createGenericPreview(contentComposite);
			}
			contentComposite.layout(true, true);
		}
		super.setVisible(visible);
	}

	private void createErrorPreview(Composite composite, String message) {
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(GridData.FILL_BOTH));
		label.setText(message);
	}

	private void createGenericPreview(Composite composite) {
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(GridData.FILL_BOTH));
		// TODO 3.1 put filename on model
		String name = model.getSource().getName();
		TaskAttachmentMapper taskAttachment = TaskAttachmentMapper.createFrom(model.getAttribute());
		if (taskAttachment.getFileName() != null) {
			name = taskAttachment.getFileName();
		}
		label.setText("Attaching File '" + name + "'\nA preview the type '" + model.getContentType()
				+ "' is currently not available");
	}

	private void createImagePreview(Composite composite, final Image bufferedImage) {
		scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		scrolledComposite.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		Composite canvasComposite = new Composite(scrolledComposite, SWT.NONE);
		canvasComposite.setLayout(GridLayoutFactory.fillDefaults().create());
		Canvas canvas = new Canvas(canvasComposite, SWT.NO_BACKGROUND);
		final Rectangle imgSize = bufferedImage.getBounds();
		canvas.setLayoutData(GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).hint(
				imgSize.width, imgSize.height).create());
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				event.gc.drawImage(bufferedImage, 0, 0);
			}
		});
		canvas.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				bufferedImage.dispose();
			}
		});
		canvas.setSize(imgSize.width, imgSize.height);
		scrolledComposite.setMinSize(imgSize.width, imgSize.height);
		scrolledComposite.setContent(canvasComposite);
		scrolledComposite.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent event) {
				adjustScrollbars(imgSize);
			}

		});
		adjustScrollbars(imgSize);
	}

	private void createTextPreview(Composite composite, String contents) {
		Text text = new Text(composite, SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = composite.getBounds().y;
		gd.widthHint = composite.getBounds().x;
		text.setLayoutData(gd);
		text.setText(contents);
	}

	private Object getContent(final Composite composite) {
		final Object result[] = new Object[1];
		try {
			getContainer().run(true, false, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
						monitor.beginTask("Preparing preview", IProgressMonitor.UNKNOWN);
						final InputStream in = model.getSource().createInputStream(monitor);
						try {
							if (isTextAttachment()) {
								StringBuilder content = new StringBuilder();
								BufferedReader reader = new BufferedReader(new InputStreamReader(in));
								String line;
								while ((line = reader.readLine()) != null && content.length() < MAX_TEXT_SIZE
										&& !monitor.isCanceled()) {
									content.append(line);
									content.append("\n");
								}
								result[0] = content.toString();
							} else if (isImageAttachment()) {
								Display.getDefault().syncExec(new Runnable() {
									public void run() {
										// Uses double buffering to paint the image; there was a weird behavior
										// with transparent images and flicker with large images
										Image originalImage = new Image(getShell().getDisplay(), in);
										final Image bufferedImage = new Image(getShell().getDisplay(),
												originalImage.getBounds());
										GC gc = new GC(bufferedImage);
										gc.setBackground(composite.getBackground());
										gc.fillRectangle(originalImage.getBounds());
										gc.drawImage(originalImage, 0, 0);
										gc.dispose();
										originalImage.dispose();
										result[0] = bufferedImage;
									}
								});
							}
						} catch (IOException e) {
							throw new InvocationTargetException(e);
						} finally {
							try {
								in.close();
							} catch (IOException e) {
								StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
										"Failed to close file", e));
							}
						}
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					} finally {
						monitor.done();
					}
				}
			});
		} catch (InvocationTargetException e) {
			StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Error generating preview", e));
			createErrorPreview(composite, "Could not create preview");
			return null;
		} catch (InterruptedException e) {
			return null;
		}
		return result[0];
	}

	private boolean isImageAttachment() {
		return imageTypes.contains(model.getContentType());
	}

	private boolean isTextAttachment() {
		return textTypes.contains(model.getContentType());
	}

	public boolean runInBackground() {
		return runInBackgroundButton.getSelection();
	}

}

Back to the top