Skip to main content
summaryrefslogtreecommitdiffstats
blob: e1205aec958f1f934151139e90ce54b8ae148e89 (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
/*******************************************************************************
* 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:
 *     Willian Mitsuda - initial API and implementation
 *******************************************************************************/

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

import java.io.File;

import org.eclipse.mylyn.internal.provisional.commons.ui.IImageCreator;
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

/**
 * Represents a attachment created from a {@link Image}; handles lazy persistence into a {@link File} and image data
 * change
 * 
 * @author Willian Mitsuda
 */
public class ImageAttachment extends LocalAttachment {

	private static final long serialVersionUID = 28264291629999181L;

	/**
	 * Provides the {@link Image} object that will be converted to desired file format, and then attached
	 */
	private final IImageCreator imageCreator;

	public ImageAttachment(IImageCreator imageCreator) {
		this.imageCreator = imageCreator;
	}

	@Override
	public void setContentType(String contentType) {
		// Does not apply; actually always save as JPEG
		// Will be implemented on bug#210179
	}

	@Override
	public String getContentType() {
		return "image/jpeg";
	}

	@Override
	public String getFilename() {
		return "screenshot.jpg";
	}

	private boolean dirty = true;

	public void markDirty() {
		dirty = true;
	}

	public void ensureImageFileWasCreated() {
		if (!dirty) {
			return;
		}

		dirty = false;
		createContents();
	}

	private void createContents() {
		Image image = imageCreator.createImage();
		try {
			String path = TasksUiPlugin.getDefault().getDefaultDataDirectory();
			ImageLoader loader = new ImageLoader();
			loader.data = new ImageData[] { image.getImageData() };
			String fileName = path + "/" + getFilename();
			loader.save(fileName, SWT.IMAGE_JPEG);
			setFile(new File(fileName));
			setFilePath(fileName);
		} finally {
			image.dispose();
		}
	}

	public void clearImageFile() {
		String path = TasksUiPlugin.getDefault().getDefaultDataDirectory();
		new File(path + "/" + getFilename()).delete();
	}

}

Back to the top