Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 222833 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/tasks/ui/wizards/TaskAttachmentWizard.java (+320 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *******************************************************************************/
8
9
package org.eclipse.mylyn.internal.tasks.ui.wizards;
10
11
import java.io.BufferedInputStream;
12
import java.io.ByteArrayInputStream;
13
import java.io.File;
14
import java.io.FileInputStream;
15
import java.io.FileNotFoundException;
16
import java.io.InputStream;
17
import java.lang.reflect.InvocationTargetException;
18
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
import org.eclipse.jface.operation.IRunnableWithProgress;
24
import org.eclipse.jface.window.Window;
25
import org.eclipse.jface.wizard.Wizard;
26
import org.eclipse.mylyn.internal.tasks.core.AbstractTaskAttachmentSource;
27
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
28
import org.eclipse.mylyn.internal.tasks.ui.SubmitTaskAttachmentJob;
29
import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages;
30
import org.eclipse.mylyn.monitor.core.StatusHandler;
31
import org.eclipse.mylyn.tasks.core.AbstractAttachmentHandler;
32
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
33
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
34
import org.eclipse.mylyn.tasks.core.TaskRepository;
35
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
36
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
37
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.dnd.Clipboard;
39
import org.eclipse.swt.dnd.TextTransfer;
40
import org.eclipse.swt.graphics.Image;
41
import org.eclipse.swt.graphics.ImageData;
42
import org.eclipse.swt.graphics.ImageLoader;
43
import org.eclipse.ui.PlatformUI;
44
45
/**
46
 * A wizard to add a new attachment to a task report.
47
 * 
48
 * @since 3.0
49
 * @author Jeff Pound
50
 * @author Steffen Pingel
51
 */
52
public class TaskAttachmentWizard extends Wizard {
53
54
	static class ClipboardSource extends AbstractTaskAttachmentSource {
55
56
		@Override
57
		public InputStream createInputStream(IProgressMonitor monitor) throws CoreException {
58
			String content = getContent();
59
			if (content != null) {
60
				return new ByteArrayInputStream(content.getBytes());
61
			}
62
			throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Invalid content type."));
63
		}
64
65
		protected String getContent() {
66
			Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay());
67
			Object o = clipboard.getContents(TextTransfer.getInstance());
68
			clipboard.dispose();
69
			if (o instanceof String) {
70
				return (String) o;
71
			}
72
			return null;
73
		}
74
75
		@Override
76
		public String getContentType() {
77
			return "text/plain";
78
		}
79
80
		@Override
81
		public long getLength() {
82
			String content = getContent();
83
			if (content != null) {
84
				return content.length();
85
			}
86
			return -1;
87
		}
88
89
		@Override
90
		public String getName() {
91
			return "clipboard.txt";
92
		}
93
94
		@Override
95
		public boolean isLocal() {
96
			return true;
97
		}
98
99
	};
100
101
	static class FileSource extends AbstractTaskAttachmentSource {
102
103
		private final File file;
104
105
		public FileSource(File file) {
106
			this.file = file;
107
		}
108
109
		@Override
110
		public InputStream createInputStream(IProgressMonitor monitor) throws CoreException {
111
			try {
112
				return new FileInputStream(file);
113
			} catch (FileNotFoundException e) {
114
				throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, e.getMessage(), e));
115
			}
116
		}
117
118
		@Override
119
		public String getContentType() {
120
			return null;
121
		}
122
123
		@Override
124
		public long getLength() {
125
			return file.length();
126
		}
127
128
		@Override
129
		public String getName() {
130
			return file.getName();
131
		}
132
133
		@Override
134
		public boolean isLocal() {
135
			return true;
136
		}
137
138
	}
139
140
	static class ImageSource extends AbstractTaskAttachmentSource {
141
142
		private final IImageCreator imageCreator;
143
144
		public ImageSource(IImageCreator imageCreator) {
145
			this.imageCreator = imageCreator;
146
		}
147
148
		@Override
149
		public InputStream createInputStream(IProgressMonitor monitor) throws CoreException {
150
			try {
151
				return new BufferedInputStream(new FileInputStream(new File(getImageFileName())));
152
			} catch (FileNotFoundException e) {
153
				throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, e.getMessage(), e));
154
			}
155
		}
156
157
		private String getImageFileName() {
158
			String path = TasksUiPlugin.getDefault().getDefaultDataDirectory();
159
			return path + "/" + getName();
160
		}
161
162
		@Override
163
		public String getContentType() {
164
			return "image/jpeg";
165
		}
166
167
		@Override
168
		public long getLength() {
169
			File file = new File(getImageFileName());
170
			return file.exists() ? file.length() : -1;
171
		}
172
173
		@Override
174
		public String getName() {
175
			return "screenshot.jpg";
176
		}
177
178
		@Override
179
		public boolean isLocal() {
180
			return true;
181
		}
182
183
		@Override
184
		public void prepareAttachmentData() {
185
			Image image = imageCreator.createImage();
186
			try {
187
				ImageLoader loader = new ImageLoader();
188
				loader.data = new ImageData[] { image.getImageData() };
189
				loader.save(getImageFileName(), SWT.IMAGE_JPEG);
190
			} finally {
191
				image.dispose();
192
			}
193
		}
194
195
		@Override
196
		public void dispose() {
197
			new File(getImageFileName()).delete();
198
		}
199
200
	}
201
202
	public enum Mode {
203
		DEFAULT, SCREENSHOT
204
	}
205
206
	static class StringSource extends ClipboardSource {
207
208
		private final String content;
209
210
		public StringSource(String content) {
211
			this.content = content;
212
		}
213
214
		@Override
215
		protected String getContent() {
216
			return content;
217
		}
218
219
	}
220
221
	private static final String DIALOG_SETTINGS_KEY = "AttachmentWizard";
222
223
	private Mode mode = Mode.DEFAULT;
224
225
	private final TaskAttachment taskAttachment;
226
227
	private final TaskRepository taskRepository;
228
229
	public TaskAttachmentWizard(TaskRepository taskRepository, TaskAttachment taskAttachment) {
230
		this.taskRepository = taskRepository;
231
		this.taskAttachment = taskAttachment;
232
233
		if (mode == Mode.SCREENSHOT) {
234
			setWindowTitle("Attach Screenshot");
235
			setDefaultPageImageDescriptor(TasksUiImages.BANNER_SCREENSHOT);
236
		} else {
237
			setWindowTitle("Add Attachment");
238
			setDefaultPageImageDescriptor(TasksUiImages.BANNER_REPOSITORY);
239
		}
240
241
		setNeedsProgressMonitor(true);
242
		setDialogSettings(TasksUiPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS_KEY));
243
	}
244
245
	@Override
246
	public void addPages() {
247
		if (taskAttachment.getSource() == null) {
248
			if (mode == Mode.SCREENSHOT) {
249
				ScreenshotAttachmentPage shotPage = new ScreenshotAttachmentPage();
250
				taskAttachment.setSource(new ImageSource(shotPage));
251
				addPage(shotPage);
252
			} else {
253
				InputAttachmentSourcePage inputPage = new InputAttachmentSourcePage(null);
254
				inputPage.setTaskAttachment(taskAttachment);
255
				addPage(inputPage);
256
			}
257
		}
258
259
		addPage(createEditPage());
260
	}
261
262
	protected TaskAttachmentPage createEditPage() {
263
		return new TaskAttachmentPage(taskAttachment);
264
	}
265
266
	public TaskAttachment getAttachment() {
267
		return taskAttachment;
268
	}
269
270
	private void handleSubmitError(final CoreException exception) {
271
		if (exception.getStatus().getCode() == RepositoryStatus.ERROR_REPOSITORY_LOGIN) {
272
			if (TasksUiUtil.openEditRepositoryWizard(taskRepository) == Window.OK) {
273
				// performFinish();
274
			}
275
		} else {
276
			StatusHandler.displayStatus("Attachment failed", exception.getStatus());
277
		}
278
	}
279
280
	@Override
281
	public boolean performFinish() {
282
//		attachPage.populateAttachment();
283
//		final String path = inputPage.getAbsoluteAttachmentPath();
284
		// upload the attachment
285
		final AbstractRepositoryConnector connector = TasksUiPlugin.getRepositoryManager().getRepositoryConnector(
286
				taskRepository.getConnectorKind());
287
		final AbstractAttachmentHandler attachmentHandler = connector.getAttachmentHandler();
288
		if (attachmentHandler == null) {
289
			return false;
290
		}
291
292
//		final boolean attachContext = attachPage.getAttachContext();
293
294
		final SubmitTaskAttachmentJob job = new SubmitTaskAttachmentJob(connector, taskRepository, taskAttachment);
295
		try {
296
			getContainer().run(true, true, new IRunnableWithProgress() {
297
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
298
					job.run(monitor);
299
				}
300
			});
301
		} catch (InvocationTargetException e) {
302
			if (e.getCause() instanceof CoreException) {
303
				handleSubmitError((CoreException) e.getCause());
304
			} else {
305
				StatusHandler.fail(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Attachment failure", e));
306
			}
307
			return false;
308
		} catch (InterruptedException e) {
309
			// cancelled
310
			return false;
311
		}
312
313
		return true;
314
	}
315
316
	public void setMode(Mode mode) {
317
		this.mode = mode;
318
	}
319
320
}
(-)src/org/eclipse/mylyn/internal/tasks/core/AbstractTaskAttachmentSource.java (+72 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *******************************************************************************/
8
9
package org.eclipse.mylyn.internal.tasks.core;
10
11
import java.io.InputStream;
12
13
import org.eclipse.core.runtime.CoreException;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
16
public abstract class AbstractTaskAttachmentSource {
17
18
	/**
19
	 * Indicates that the attachment source model was modified, and attachment data needs to be recreated
20
	 */
21
	private boolean dirty = true;
22
23
	public abstract InputStream createInputStream(IProgressMonitor monitor) throws CoreException;
24
25
	public abstract boolean isLocal();
26
27
	public abstract long getLength();
28
29
	public abstract String getName();
30
31
	public abstract String getContentType();
32
33
	/**
34
	 * Marks the attachment source as dirty, so attachment data will be lazily recreated next time it is needed.
35
	 */
36
	public void markDirty() {
37
		dirty = true;
38
	}
39
40
	public boolean isDirty() {
41
		return dirty;
42
	}
43
44
	/**
45
	 * Ensures the attachment data is ready for use by clients of this object.
46
	 */
47
	public void prepareAttachmentData() {
48
		if (!dirty) {
49
			return;
50
		}
51
52
		dirty = false;
53
		createAttachmentData();
54
	}
55
56
	/**
57
	 * Creates the attachment data.
58
	 * <p>
59
	 * Subclasses may override.
60
	 */
61
	protected void createAttachmentData() {
62
	}
63
64
	/**
65
	 * Disposes resources created by this attachment source model.
66
	 * <p>
67
	 * Subclasses may override.
68
	 */
69
	public void dispose() {
70
	}
71
72
}

Return to bug 222833