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

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +5 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)",
Lines 30-35 Link Here
30
Bundle-RequiredExecutionEnvironment: J2SE-1.5
32
Bundle-RequiredExecutionEnvironment: J2SE-1.5
31
Bundle-ActivationPolicy: lazy
33
Bundle-ActivationPolicy: lazy
32
Bundle-Localization: plugin
34
Bundle-Localization: plugin
33
Export-Package: org.eclipse.mylyn.internal.wikitext.tasks.ui.editor;x-internal:=true,
35
Export-Package: org.eclipse.mylyn.internal.wikitext.tasks.ui;x-internal:=true,
36
 org.eclipse.mylyn.internal.wikitext.tasks.ui.editor;x-internal:=true,
34
 org.eclipse.mylyn.internal.wikitext.tasks.ui.util;x-internal:=true,
37
 org.eclipse.mylyn.internal.wikitext.tasks.ui.util;x-internal:=true,
35
 org.eclipse.mylyn.wikitext.tasks.ui.editor
38
 org.eclipse.mylyn.wikitext.tasks.ui.editor
39
Bundle-Activator: org.eclipse.mylyn.internal.wikitext.tasks.ui.WikiTextTasksUiPlugin
(-)plugin.xml (+17 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.internal.wikitext.tasks.ui.WikiTextContextUiBridge"
16
            contentType="org.eclipse.mylyn.wikitext"
17
            name="WikiText Context UI Bridge">
18
      </uiBridge>
19
   </extension>
20
   
21
   <extension
5
         point="org.eclipse.mylyn.tasks.ui.taskEditorExtensions">
22
         point="org.eclipse.mylyn.tasks.ui.taskEditorExtensions">
6
      <taskEditorExtension
23
      <taskEditorExtension
7
            class="org.eclipse.mylyn.internal.wikitext.tasks.ui.editor.TextileMarkupTaskEditorExtension"
24
            class="org.eclipse.mylyn.internal.wikitext.tasks.ui.editor.TextileMarkupTaskEditorExtension"
(-)src/org/eclipse/mylyn/internal/wikitext/tasks/ui/WikiTextContextStructureBridge.java (+293 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
	public static final String CONTENT_TYPE = WikiText.CONTENT_TYPE;
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?? we probably need some kind of core model
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
				outline.setResourcePath(file.getFullPath().toString());
268
				return outline;
269
			} catch (Exception e) {
270
				// ignore
271
				return null;
272
			}
273
		}
274
		return null;
275
	}
276
277
	private String getContents(IFile file) throws Exception {
278
		String charset = file.getCharset();
279
		StringWriter writer = new StringWriter();
280
		Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()), charset);
281
		int i;
282
		while ((i = reader.read()) != -1) {
283
			writer.write(i);
284
		}
285
		return writer.toString();
286
	}
287
288
	IFile getFile(OutlineItem item) {
289
		IPath fullPath = getFullPath(item);
290
291
		return fullPath == null ? null : ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
292
	}
293
}
(-)src/org/eclipse/mylyn/internal/wikitext/tasks/ui/WikiTextTasksUiPlugin.java (+83 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 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 org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.ILog;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.ui.plugin.AbstractUIPlugin;
19
import org.osgi.framework.BundleContext;
20
21
public class WikiTextTasksUiPlugin extends AbstractUIPlugin {
22
23
	private static WikiTextTasksUiPlugin plugin;
24
25
	public WikiTextTasksUiPlugin() {
26
		plugin = this;
27
	}
28
29
	@Override
30
	public void start(BundleContext context) throws Exception {
31
		super.start(context);
32
	}
33
34
	@Override
35
	public void stop(BundleContext context) throws Exception {
36
		plugin = null;
37
		super.stop(context);
38
	}
39
40
	public static WikiTextTasksUiPlugin getDefault() {
41
		return plugin;
42
	}
43
44
	public void log(Throwable ce) {
45
		if (ce instanceof CoreException) {
46
			getLog().log(((CoreException) ce).getStatus());
47
		} else {
48
			log(IStatus.ERROR, ce.getMessage(), ce);
49
		}
50
	}
51
52
	public void log(int severity, String message, Throwable exception) {
53
		if (message == null) {
54
			message = ""; //$NON-NLS-1$
55
		}
56
		ILog log = getLog();
57
		IStatus status = null;
58
		if (exception instanceof CoreException) {
59
			status = ((CoreException) exception).getStatus();
60
		}
61
		if (status == null) {
62
			status = new Status(severity, getPluginId(), severity, message, exception);
63
		}
64
		log.log(status);
65
	}
66
67
	public String getPluginId() {
68
		return getBundle().getSymbolicName();
69
	}
70
71
	public IStatus createStatus(int statusCode, Throwable exception) {
72
		return createStatus(null, statusCode, exception);
73
	}
74
75
	public IStatus createStatus(String message, int statusCode, Throwable exception) {
76
		if (message == null && exception != null) {
77
			message = exception.getClass().getName() + ": " + exception.getMessage(); //$NON-NLS-1$
78
		}
79
		Status status = new Status(statusCode, getPluginId(), statusCode, message, exception);
80
		return status;
81
	}
82
83
}
(-)src/org/eclipse/mylyn/internal/wikitext/tasks/ui/WikiTextContextUiBridge.java (+152 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 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.lang.reflect.Method;
15
import java.util.ArrayList;
16
import java.util.List;
17
18
import org.eclipse.core.resources.IFile;
19
import org.eclipse.jface.text.TextSelection;
20
import org.eclipse.jface.viewers.StructuredSelection;
21
import org.eclipse.jface.viewers.TreeViewer;
22
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
23
import org.eclipse.mylyn.context.core.ContextCore;
24
import org.eclipse.mylyn.context.core.IInteractionElement;
25
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge;
26
import org.eclipse.mylyn.wikitext.core.parser.outline.OutlineItem;
27
import org.eclipse.ui.IEditorInput;
28
import org.eclipse.ui.IEditorPart;
29
import org.eclipse.ui.IEditorReference;
30
import org.eclipse.ui.IFileEditorInput;
31
import org.eclipse.ui.IWorkbenchPage;
32
import org.eclipse.ui.PartInitException;
33
import org.eclipse.ui.PlatformUI;
34
import org.eclipse.ui.part.FileEditorInput;
35
import org.eclipse.ui.part.IShowInTarget;
36
import org.eclipse.ui.part.ShowInContext;
37
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
38
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
39
40
/**
41
 * a UI context bridge for WikiText.
42
 * 
43
 * @author David Green
44
 */
45
public class WikiTextContextUiBridge extends AbstractContextUiBridge {
46
47
	public WikiTextContextUiBridge() {
48
	}
49
50
	@Override
51
	public boolean acceptsEditor(IEditorPart editorPart) {
52
		return editorPart.getAdapter(OutlineItem.class) != null;
53
	}
54
55
	@Override
56
	public List<TreeViewer> getContentOutlineViewers(IEditorPart editorPart) {
57
		List<TreeViewer> viewers = new ArrayList<TreeViewer>();
58
		if (acceptsEditor(editorPart)) {
59
			try {
60
				IContentOutlinePage outlinePage = (IContentOutlinePage) editorPart.getAdapter(IContentOutlinePage.class);
61
				if (outlinePage != null) {
62
					Method method = ContentOutlinePage.class.getDeclaredMethod("getTreeViewer", new Class[] {}); //$NON-NLS-1$
63
					method.setAccessible(true);
64
					viewers.add((TreeViewer) method.invoke(outlinePage, new Object[] {}));
65
				}
66
			} catch (Exception e) {
67
				// ignore
68
			}
69
		}
70
		return viewers;
71
	}
72
73
	@Override
74
	public String getContentType() {
75
		return WikiTextContextStructureBridge.CONTENT_TYPE;
76
	}
77
78
	@Override
79
	public Object getObjectForTextSelection(TextSelection selection, IEditorPart editor) {
80
		OutlineItem outline = (OutlineItem) editor.getAdapter(OutlineItem.class);
81
		if (outline != null && selection != null) {
82
			return outline.findNearestMatchingOffset(selection.getOffset());
83
		}
84
		return outline;
85
	}
86
87
	@Override
88
	public IInteractionElement getElement(IEditorInput input) {
89
		Object adapter = input.getAdapter(OutlineItem.class);
90
		if (adapter != null) {
91
			String handle = ContextCore.getStructureBridge(adapter).getHandleIdentifier(adapter);
92
			return ContextCore.getContextManager().getElement(handle);
93
		}
94
		return null;
95
	}
96
97
	@Override
98
	public void open(IInteractionElement element) {
99
		AbstractContextStructureBridge structureBridge = ContextCore.getStructureBridge(WikiTextContextStructureBridge.CONTENT_TYPE);
100
		if (structureBridge instanceof WikiTextContextStructureBridge) {
101
			Object object = structureBridge.getObjectForHandle(element.getHandleIdentifier());
102
			OutlineItem item = null;
103
			if (object instanceof OutlineItem) {
104
				item = (OutlineItem) object;
105
				object = ((WikiTextContextStructureBridge) structureBridge).getFile(item);
106
			}
107
			if (object instanceof IFile) {
108
				FileEditorInput editorInput = new FileEditorInput((IFile) object);
109
				try {
110
					IEditorPart editor = PlatformUI.getWorkbench()
111
							.getActiveWorkbenchWindow()
112
							.getActivePage()
113
							.openEditor(editorInput, "org.eclipse.mylyn.wikitext.ui.editor.markupEditor"); //$NON-NLS-1$
114
					if (item != null && editor instanceof IShowInTarget) {
115
						((IShowInTarget) editor).show(new ShowInContext(editorInput, new StructuredSelection(item)));
116
					}
117
				} catch (PartInitException e) {
118
					WikiTextTasksUiPlugin.getDefault().log(e);
119
				}
120
			}
121
		}
122
123
	}
124
125
	@Override
126
	public void close(IInteractionElement element) {
127
		try {
128
			IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
129
			if (page != null) {
130
				List<IEditorReference> editors = new ArrayList<IEditorReference>(4);
131
				for (IEditorReference reference : page.getEditorReferences()) {
132
					try {
133
						if (reference.getEditorInput() instanceof IFileEditorInput) {
134
							IFileEditorInput input = (IFileEditorInput) reference.getEditorInput();
135
							if (input.getFile().getFullPath().toString().equals(element.getHandleIdentifier())) {
136
								editors.add(reference);
137
							}
138
						}
139
					} catch (PartInitException e) {
140
						// ignore
141
					}
142
				}
143
				if (!editors.isEmpty()) {
144
					page.closeEditors(editors.toArray(new IEditorReference[editors.size()]), true);
145
				}
146
			}
147
		} catch (Throwable t) {
148
			WikiTextTasksUiPlugin.getDefault().log(t);
149
		}
150
	}
151
152
}

Return to bug 260475