View | Details | Raw Unified | Return to bug 260475 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (+2 lines)
Lines 17-22 Link Here
17
 org.eclipse.core.resources,
17
 org.eclipse.core.resources,
18
 org.eclipse.mylyn.tasks.ui;bundle-version="[3.0.2,4.0.0)",
18
 org.eclipse.mylyn.tasks.ui;bundle-version="[3.0.2,4.0.0)",
19
 org.eclipse.mylyn.tasks.core;bundle-version="[3.0.2,4.0.0)",
19
 org.eclipse.mylyn.tasks.core;bundle-version="[3.0.2,4.0.0)",
20
 org.eclipse.mylyn.context.core;bundle-version="[3.0,4.0.0)",
21
 org.eclipse.mylyn.context.ui;bundle-version="[3.0,4.0.0)",
20
 org.eclipse.mylyn.wikitext.confluence.core;bundle-version="[0.9.3,1.0.0)",
22
 org.eclipse.mylyn.wikitext.confluence.core;bundle-version="[0.9.3,1.0.0)",
21
 org.eclipse.mylyn.wikitext.confluence.ui;bundle-version="[0.9.3,1.0.0)",
23
 org.eclipse.mylyn.wikitext.confluence.ui;bundle-version="[0.9.3,1.0.0)",
22
 org.eclipse.mylyn.wikitext.mediawiki.core;bundle-version="[0.9.3,1.0.0)",
24
 org.eclipse.mylyn.wikitext.mediawiki.core;bundle-version="[0.9.3,1.0.0)",
(-)plugin.xml (+16 lines)
Lines 2-7 Link Here
2
<?eclipse version="3.2"?>
2
<?eclipse version="3.2"?>
3
<plugin>
3
<plugin>
4
   <extension
4
   <extension
5
         point="org.eclipse.mylyn.context.core.bridges">
6
      <structureBridge
7
            class="org.eclipse.mylyn.internal.wikitext.tasks.ui.WikiTextContextStructureBridge"
8
            name="WikiText Structure Bridge"
9
            parentContentType="resource">
10
      </structureBridge>
11
   </extension>
12
   <extension
13
         point="org.eclipse.mylyn.context.ui.bridges">
14
      <uiBridge
15
            class="org.eclipse.mylyn.wikitext.tasks.ui.AbstractContextUiBridge1"
16
            contentType="org.eclipse.mylyn.wikitext">
17
      </uiBridge>
18
   </extension>
19
   
20
   <extension
5
         point="org.eclipse.mylyn.tasks.ui.taskEditorExtensions">
21
         point="org.eclipse.mylyn.tasks.ui.taskEditorExtensions">
6
      <taskEditorExtension
22
      <taskEditorExtension
7
            class="org.eclipse.mylyn.internal.wikitext.tasks.ui.editor.TextileMarkupTaskEditorExtension"
23
            class="org.eclipse.mylyn.internal.wikitext.tasks.ui.editor.TextileMarkupTaskEditorExtension"
(-)src/org/eclipse/mylyn/internal/wikitext/tasks/ui/WikiTextContextStructureBridge.java (+292 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 David Green 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
 * Contributors:
9
 *     David Green - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.wikitext.tasks.ui;
13
14
import java.io.BufferedInputStream;
15
import java.io.InputStreamReader;
16
import java.io.Reader;
17
import java.io.StringWriter;
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.List;
21
22
import org.eclipse.core.resources.IFile;
23
import org.eclipse.core.resources.IMarker;
24
import org.eclipse.core.resources.IResource;
25
import org.eclipse.core.resources.ResourcesPlugin;
26
import org.eclipse.core.runtime.CoreException;
27
import org.eclipse.core.runtime.IPath;
28
import org.eclipse.core.runtime.Path;
29
import org.eclipse.core.runtime.content.IContentDescription;
30
import org.eclipse.core.runtime.content.IContentType;
31
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
32
import org.eclipse.mylyn.wikitext.core.WikiText;
33
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
34
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineItem;
35
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineParser;
36
import org.eclipse.ui.IEditorPart;
37
import org.eclipse.ui.PlatformUI;
38
39
/**
40
 * A Mylyn {@link AbstractContextStructureBridge context structure bridge} for wikitext files.
41
 * 
42
 * @author David Green
43
 */
44
public class WikiTextContextStructureBridge extends AbstractContextStructureBridge {
45
46
	private static final char HANDLE_FILE_SEPARATOR = ';';
47
48
	private static final String CONTENT_TYPE = "wikitext"; //$NON-NLS-1$
49
50
	public WikiTextContextStructureBridge() {
51
	}
52
53
	@Override
54
	public boolean acceptsObject(Object object) {
55
		if (object instanceof OutlineItem) {
56
			return true;
57
		} else if (object instanceof IFile) {
58
			IFile file = (IFile) object;
59
60
			try {
61
				IContentDescription description = file.getContentDescription();
62
				if (description != null) {
63
					IContentType contentType = description.getContentType();
64
					if (contentType != null) {
65
						if (isWikiText(contentType)) {
66
							return true;
67
						}
68
					}
69
				}
70
			} catch (CoreException e) {
71
				// ignore
72
			}
73
74
			String languageName = WikiText.getMarkupLanguageNameForFilename(file.getName());
75
			return languageName != null;
76
		}
77
		return false;
78
	}
79
80
	private boolean isWikiText(IContentType contentType) {
81
		if (WikiText.CONTENT_TYPE.equals(contentType.getId())) {
82
			return true;
83
		}
84
		IContentType baseType = contentType.getBaseType();
85
		if (baseType != null) {
86
			return isWikiText(baseType);
87
		}
88
		return false;
89
	}
90
91
	@Override
92
	public boolean canBeLandmark(String handle) {
93
		if (handle == null) {
94
			return false;
95
		} else {
96
			return handle.indexOf(HANDLE_FILE_SEPARATOR) != -1;
97
		}
98
	}
99
100
	@Override
101
	public boolean canFilter(Object element) {
102
		return true;
103
	}
104
105
	@Override
106
	public List<String> getChildHandles(String handle) {
107
		Object object = getObjectForHandle(handle);
108
		if (object instanceof OutlineItem) {
109
			OutlineItem item = (OutlineItem) object;
110
			if (!item.getChildren().isEmpty()) {
111
				List<String> handles = new ArrayList<String>(item.getChildren().size());
112
				for (OutlineItem child : item.getChildren()) {
113
					handles.add(getHandleIdentifier(child));
114
				}
115
				return handles;
116
			}
117
		}
118
		return Collections.emptyList();
119
	}
120
121
	@Override
122
	public String getContentType() {
123
		return CONTENT_TYPE;
124
	}
125
126
	@Override
127
	public String getContentType(String elementHandle) {
128
		if (elementHandle.indexOf(HANDLE_FILE_SEPARATOR) == -1) {
129
			return parentContentType;
130
		}
131
		return CONTENT_TYPE;
132
	}
133
134
	@Override
135
	public String getHandleForOffsetInObject(Object object, int offset) {
136
		IResource resource = null;
137
		try {
138
			if (object instanceof IResource) {
139
				resource = (IResource) object;
140
			} else if (object instanceof IMarker) {
141
				resource = ((IMarker) object).getResource();
142
			} else {
143
				try {
144
					// works with ConcreteMarker without creating a compile-time dependency on internals
145
					IMarker marker = (IMarker) object.getClass().getMethod("getMarker").invoke(object); //$NON-NLS-1$
146
					resource = marker.getResource();
147
				} catch (Exception e) {
148
					// ignore
149
				}
150
			}
151
		} catch (Exception e) {
152
			return null;
153
		}
154
		if (resource instanceof IFile) {
155
			IFile file = (IFile) resource;
156
			if (acceptsObject(file)) {
157
				OutlineItem outline = getOutline(file);
158
				if (outline != null) {
159
					OutlineItem item = outline.findNearestMatchingOffset(offset);
160
					if (item != null) {
161
						return getHandleIdentifier(item);
162
					}
163
				}
164
			}
165
		}
166
		return null;
167
	}
168
169
	@Override
170
	public String getLabel(Object object) {
171
		if (object instanceof OutlineItem) {
172
			OutlineItem item = (OutlineItem) object;
173
			if (item.getParent() == null) {
174
				return getFile(item).getName();
175
			} else {
176
				return item.getLabel();
177
			}
178
		}
179
		return ""; //$NON-NLS-1$
180
	}
181
182
	@Override
183
	public Object getObjectForHandle(String handle) {
184
		if (handle == null) {
185
			return null;
186
		}
187
		int idxOfSeparator = handle.indexOf(HANDLE_FILE_SEPARATOR);
188
		String filename = handle;
189
		if (idxOfSeparator != -1) {
190
			filename = handle.substring(0, idxOfSeparator);
191
		}
192
		IFile file;
193
		try {
194
			file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filename));
195
		} catch (Exception e) {
196
			// be error-tolerant since we don't know much about the handle at this point
197
			return null;
198
		}
199
		if (file != null) {
200
			if (idxOfSeparator != -1) {
201
				String headingId = handle.substring(idxOfSeparator + 1);
202
				OutlineItem outline = getOutline(file);
203
				if (outline != null) {
204
					OutlineItem item = outline.findItemById(headingId);
205
					return item;
206
				}
207
			} else {
208
				return file;
209
			}
210
		}
211
		return null;
212
	}
213
214
	@Override
215
	public String getParentHandle(String handle) {
216
		Object object = getObjectForHandle(handle);
217
		if (object instanceof OutlineItem) {
218
			OutlineItem item = (OutlineItem) object;
219
			if (item.getParent() != null) {
220
				return getHandleIdentifier(item.getParent());
221
			}
222
		}
223
		return null;
224
	}
225
226
	@Override
227
	public boolean isDocument(String handle) {
228
		Object object = getObjectForHandle(handle);
229
		if (object instanceof OutlineItem) {
230
			OutlineItem item = (OutlineItem) object;
231
			return item.getParent() == null;
232
		}
233
		return false;
234
	}
235
236
	@Override
237
	public String getHandleIdentifier(Object object) {
238
		if (object instanceof OutlineItem) {
239
			OutlineItem item = (OutlineItem) object;
240
			return item.getResourcePath() + HANDLE_FILE_SEPARATOR + item.getId();
241
		} else if (object instanceof IFile && acceptsObject(object)) {
242
			return ((IFile) object).getFullPath().toString();
243
		}
244
		return null;
245
	}
246
247
	private IPath getFullPath(OutlineItem item) {
248
		String resourcePath = item.getResourcePath();
249
		return resourcePath == null ? null : new Path(resourcePath);
250
	}
251
252
	private OutlineItem getOutline(IFile file) {
253
		// FIXME: is editor integration the way to go??
254
		IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
255
		if (editorPart != null) {
256
			OutlineItem outline = (OutlineItem) editorPart.getAdapter(OutlineItem.class);
257
			if (outline != null) {
258
				return outline;
259
			}
260
		}
261
		MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName());
262
		if (markupLanguage != null) {
263
			OutlineParser parser = new OutlineParser(markupLanguage);
264
			try {
265
				String contents = getContents(file);
266
				OutlineItem outline = parser.parse(contents);
267
				return outline;
268
			} catch (Exception e) {
269
				// ignore
270
				return null;
271
			}
272
		}
273
		return null;
274
	}
275
276
	private String getContents(IFile file) throws Exception {
277
		String charset = file.getCharset();
278
		StringWriter writer = new StringWriter();
279
		Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()), charset);
280
		int i;
281
		while ((i = reader.read()) != -1) {
282
			writer.write(i);
283
		}
284
		return writer.toString();
285
	}
286
287
	private IFile getFile(OutlineItem item) {
288
		IPath fullPath = getFullPath(item);
289
290
		return fullPath == null ? null : ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
291
	}
292
}
(-)src/org/eclipse/mylyn/wikitext/core/WikiText.java (+5 lines)
Lines 13-18 Link Here
13
13
14
import java.util.Set;
14
import java.util.Set;
15
15
16
import org.eclipse.core.runtime.content.IContentType;
16
import org.eclipse.mylyn.internal.wikitext.core.WikiTextPlugin;
17
import org.eclipse.mylyn.internal.wikitext.core.WikiTextPlugin;
17
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
18
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
18
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
19
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
Lines 31-36 Link Here
31
 * @since 1.0
32
 * @since 1.0
32
 */
33
 */
33
public class WikiText {
34
public class WikiText {
35
	/**
36
	 * the {@link IContentType#getId() content type id} of wikitext files.
37
	 */
38
	public static final String CONTENT_TYPE = "org.eclipse.mylyn.wikitext"; //$NON-NLS-1$
34
39
35
	private WikiText() { // prevent instantiation and subclassing
40
	private WikiText() { // prevent instantiation and subclassing
36
	}
41
	}
(-)src/org/eclipse/mylyn/wikitext/core/parser/outline/OutlineItem.java (+28 lines)
Lines 46-51 Link Here
46
46
47
	private Map<String, OutlineItem> itemsById;
47
	private Map<String, OutlineItem> itemsById;
48
48
49
	private String resourcePath;
50
49
	public OutlineItem(OutlineItem parent, int level, String id, int offset, int length, String label) {
51
	public OutlineItem(OutlineItem parent, int level, String id, int offset, int length, String label) {
50
		super();
52
		super();
51
		this.parent = parent;
53
		this.parent = parent;
Lines 249-254 Link Here
249
	}
251
	}
250
252
251
	/**
253
	/**
254
	 * the resource path to the resource of this outline item
255
	 * 
256
	 * @return the resource path, or null if it's unknown.
257
	 */
258
	public String getResourcePath() {
259
		if (getParent() != null) {
260
			return getParent().getResourcePath();
261
		}
262
		return resourcePath;
263
	}
264
265
	/**
266
	 * the resource path to the resource of this outline item
267
	 * 
268
	 * @param resourcePath
269
	 *            the resource path, or null if it's unknown.
270
	 */
271
	public void setResourcePath(String resourcePath) {
272
		if (getParent() != null) {
273
			getParent().setResourcePath(resourcePath);
274
		} else {
275
			this.resourcePath = resourcePath;
276
		}
277
	}
278
279
	/**
252
	 * move children from the given outline item to this
280
	 * move children from the given outline item to this
253
	 */
281
	 */
254
	public void moveChildren(OutlineItem otherParent) {
282
	public void moveChildren(OutlineItem otherParent) {

Return to bug 260475