Skip to main content
summaryrefslogtreecommitdiffstats
blob: 544a9b21fe1fc1aeb0d096b41d9178f1be0a5b56 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar committers 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
 *******************************************************************************/

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

import java.io.InputStream;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IAttachmentHandler;
import org.eclipse.mylyn.tasks.core.RepositoryAttachment;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;

/**
 * @author Jeff Pound
 */
public class RepositoryAttachmentEditorInput extends PlatformObject implements IStorageEditorInput {

	private RepositoryAttachment attachment;

	private RepositoryAttachmentStorage storage;

	private TaskRepository repository;

	public RepositoryAttachmentEditorInput(TaskRepository repository, RepositoryAttachment att) {
		this.attachment = att;
		this.storage = new RepositoryAttachmentStorage();
		this.repository = repository;
	}

	public IStorage getStorage() throws CoreException {
		return storage;
	}

	public boolean exists() {
		return true;
	}

	public ImageDescriptor getImageDescriptor() {
		// ignore
		return null;
	}

	public String getName() {
		return storage.getName();
	}

	public IPersistableElement getPersistable() {
		return null;
	}

	public String getToolTipText() {
		return "Repository Attachment: " + attachment.getId() + " [" + attachment.getUrl() + "]";
	}

	class RepositoryAttachmentStorage extends PlatformObject implements IStorage {

		private static final String ATTR_FILENAME = "filename";

		private static final String ATTACHMENT_DEFAULT_NAME = "attachment";

		private static final String CTYPE_ZIP = "zip";

		private static final String CTYPE_OCTET_STREAM = "octet-stream";

		private static final String CTYPE_TEXT = "text";

		private static final String CTYPE_HTML = "html";

		public InputStream getContents() throws CoreException {
			AbstractRepositoryConnector connector = TasksUiPlugin.getRepositoryManager().getRepositoryConnector(
					repository.getKind());
			IAttachmentHandler handler = connector.getAttachmentHandler();
			return handler.getAttachmentAsStream(repository, attachment, new NullProgressMonitor());
		}

		public IPath getFullPath() {
			// ignore
			return null;
		}

		public String getName() {
			String name = attachment.getAttributeValue(ATTR_FILENAME);

			// if no filename is set, make one up with the proper extension so
			// we can support opening in that filetype's default editor
			if (name == null || "".equals(name)) {
				String ctype = attachment.getContentType();
				if (ctype.endsWith(CTYPE_HTML)) {
					name = ATTACHMENT_DEFAULT_NAME + ".html";
				} else if (ctype.startsWith(CTYPE_TEXT)) {
					name = ATTACHMENT_DEFAULT_NAME + ".txt";
				} else if (ctype.endsWith(CTYPE_OCTET_STREAM)) {
					name = ATTACHMENT_DEFAULT_NAME;
				} else if (ctype.endsWith(CTYPE_ZIP)) {
					name = ATTACHMENT_DEFAULT_NAME + "." + CTYPE_ZIP;
				} else {
					name = ATTACHMENT_DEFAULT_NAME + "." + ctype.substring(ctype.indexOf("/") + 1);
				}
			}
			// treat .patch files as text files
			if (name.endsWith(".patch")) {
				name += ".txt";
			}

			return name;
		}

		public boolean isReadOnly() {
			return true;
		}

	}
}

Back to the top