### Eclipse Workspace Patch 1.0 #P org.eclipse.mylyn.wikitext.tasks.ui Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.wikitext.tasks.ui/META-INF/MANIFEST.MF,v retrieving revision 1.2 diff -u -r1.2 MANIFEST.MF --- META-INF/MANIFEST.MF 4 Dec 2008 19:29:15 -0000 1.2 +++ META-INF/MANIFEST.MF 14 Jan 2009 02:09:41 -0000 @@ -17,6 +17,8 @@ org.eclipse.core.resources, org.eclipse.mylyn.tasks.ui;bundle-version="[3.0.2,4.0.0)", org.eclipse.mylyn.tasks.core;bundle-version="[3.0.2,4.0.0)", + org.eclipse.mylyn.context.core;bundle-version="[3.0,4.0.0)", + org.eclipse.mylyn.context.ui;bundle-version="[3.0,4.0.0)", org.eclipse.mylyn.wikitext.confluence.core;bundle-version="[0.9.3,1.0.0)", org.eclipse.mylyn.wikitext.confluence.ui;bundle-version="[0.9.3,1.0.0)", org.eclipse.mylyn.wikitext.mediawiki.core;bundle-version="[0.9.3,1.0.0)", Index: plugin.xml =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.wikitext.tasks.ui/plugin.xml,v retrieving revision 1.1 diff -u -r1.1 plugin.xml --- plugin.xml 4 Dec 2008 19:14:52 -0000 1.1 +++ plugin.xml 14 Jan 2009 02:09:41 -0000 @@ -2,6 +2,22 @@ + + + + + + + + + getChildHandles(String handle) { + Object object = getObjectForHandle(handle); + if (object instanceof OutlineItem) { + OutlineItem item = (OutlineItem) object; + if (!item.getChildren().isEmpty()) { + List handles = new ArrayList(item.getChildren().size()); + for (OutlineItem child : item.getChildren()) { + handles.add(getHandleIdentifier(child)); + } + return handles; + } + } + return Collections.emptyList(); + } + + @Override + public String getContentType() { + return CONTENT_TYPE; + } + + @Override + public String getContentType(String elementHandle) { + if (elementHandle.indexOf(HANDLE_FILE_SEPARATOR) == -1) { + return parentContentType; + } + return CONTENT_TYPE; + } + + @Override + public String getHandleForOffsetInObject(Object object, int offset) { + IResource resource = null; + try { + if (object instanceof IResource) { + resource = (IResource) object; + } else if (object instanceof IMarker) { + resource = ((IMarker) object).getResource(); + } else { + try { + // works with ConcreteMarker without creating a compile-time dependency on internals + IMarker marker = (IMarker) object.getClass().getMethod("getMarker").invoke(object); //$NON-NLS-1$ + resource = marker.getResource(); + } catch (Exception e) { + // ignore + } + } + } catch (Exception e) { + return null; + } + if (resource instanceof IFile) { + IFile file = (IFile) resource; + if (acceptsObject(file)) { + OutlineItem outline = getOutline(file); + if (outline != null) { + OutlineItem item = outline.findNearestMatchingOffset(offset); + if (item != null) { + return getHandleIdentifier(item); + } + } + } + } + return null; + } + + @Override + public String getLabel(Object object) { + if (object instanceof OutlineItem) { + OutlineItem item = (OutlineItem) object; + if (item.getParent() == null) { + return getFile(item).getName(); + } else { + return item.getLabel(); + } + } + return ""; //$NON-NLS-1$ + } + + @Override + public Object getObjectForHandle(String handle) { + if (handle == null) { + return null; + } + int idxOfSeparator = handle.indexOf(HANDLE_FILE_SEPARATOR); + String filename = handle; + if (idxOfSeparator != -1) { + filename = handle.substring(0, idxOfSeparator); + } + IFile file; + try { + file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filename)); + } catch (Exception e) { + // be error-tolerant since we don't know much about the handle at this point + return null; + } + if (file != null) { + if (idxOfSeparator != -1) { + String headingId = handle.substring(idxOfSeparator + 1); + OutlineItem outline = getOutline(file); + if (outline != null) { + OutlineItem item = outline.findItemById(headingId); + return item; + } + } else { + return file; + } + } + return null; + } + + @Override + public String getParentHandle(String handle) { + Object object = getObjectForHandle(handle); + if (object instanceof OutlineItem) { + OutlineItem item = (OutlineItem) object; + if (item.getParent() != null) { + return getHandleIdentifier(item.getParent()); + } + } + return null; + } + + @Override + public boolean isDocument(String handle) { + Object object = getObjectForHandle(handle); + if (object instanceof OutlineItem) { + OutlineItem item = (OutlineItem) object; + return item.getParent() == null; + } + return false; + } + + @Override + public String getHandleIdentifier(Object object) { + if (object instanceof OutlineItem) { + OutlineItem item = (OutlineItem) object; + return item.getResourcePath() + HANDLE_FILE_SEPARATOR + item.getId(); + } else if (object instanceof IFile && acceptsObject(object)) { + return ((IFile) object).getFullPath().toString(); + } + return null; + } + + private IPath getFullPath(OutlineItem item) { + String resourcePath = item.getResourcePath(); + return resourcePath == null ? null : new Path(resourcePath); + } + + private OutlineItem getOutline(IFile file) { + // FIXME: is editor integration the way to go?? + IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + if (editorPart != null) { + OutlineItem outline = (OutlineItem) editorPart.getAdapter(OutlineItem.class); + if (outline != null) { + return outline; + } + } + MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName()); + if (markupLanguage != null) { + OutlineParser parser = new OutlineParser(markupLanguage); + try { + String contents = getContents(file); + OutlineItem outline = parser.parse(contents); + return outline; + } catch (Exception e) { + // ignore + return null; + } + } + return null; + } + + private String getContents(IFile file) throws Exception { + String charset = file.getCharset(); + StringWriter writer = new StringWriter(); + Reader reader = new InputStreamReader(new BufferedInputStream(file.getContents()), charset); + int i; + while ((i = reader.read()) != -1) { + writer.write(i); + } + return writer.toString(); + } + + private IFile getFile(OutlineItem item) { + IPath fullPath = getFullPath(item); + + return fullPath == null ? null : ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath); + } +} #P org.eclipse.mylyn.wikitext.core Index: src/org/eclipse/mylyn/wikitext/core/WikiText.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/WikiText.java,v retrieving revision 1.2 diff -u -r1.2 WikiText.java --- src/org/eclipse/mylyn/wikitext/core/WikiText.java 9 Jan 2009 05:18:33 -0000 1.2 +++ src/org/eclipse/mylyn/wikitext/core/WikiText.java 14 Jan 2009 02:09:41 -0000 @@ -13,6 +13,7 @@ import java.util.Set; +import org.eclipse.core.runtime.content.IContentType; import org.eclipse.mylyn.internal.wikitext.core.WikiTextPlugin; import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage; import org.eclipse.mylyn.wikitext.core.util.ServiceLocator; @@ -31,6 +32,10 @@ * @since 1.0 */ public class WikiText { + /** + * the {@link IContentType#getId() content type id} of wikitext files. + */ + public static final String CONTENT_TYPE = "org.eclipse.mylyn.wikitext"; //$NON-NLS-1$ private WikiText() { // prevent instantiation and subclassing } Index: src/org/eclipse/mylyn/wikitext/core/parser/outline/OutlineItem.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.mylyn/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/outline/OutlineItem.java,v retrieving revision 1.10 diff -u -r1.10 OutlineItem.java --- src/org/eclipse/mylyn/wikitext/core/parser/outline/OutlineItem.java 13 Jan 2009 04:02:32 -0000 1.10 +++ src/org/eclipse/mylyn/wikitext/core/parser/outline/OutlineItem.java 14 Jan 2009 02:09:41 -0000 @@ -46,6 +46,8 @@ private Map itemsById; + private String resourcePath; + public OutlineItem(OutlineItem parent, int level, String id, int offset, int length, String label) { super(); this.parent = parent; @@ -249,6 +251,32 @@ } /** + * the resource path to the resource of this outline item + * + * @return the resource path, or null if it's unknown. + */ + public String getResourcePath() { + if (getParent() != null) { + return getParent().getResourcePath(); + } + return resourcePath; + } + + /** + * the resource path to the resource of this outline item + * + * @param resourcePath + * the resource path, or null if it's unknown. + */ + public void setResourcePath(String resourcePath) { + if (getParent() != null) { + getParent().setResourcePath(resourcePath); + } else { + this.resourcePath = resourcePath; + } + } + + /** * move children from the given outline item to this */ public void moveChildren(OutlineItem otherParent) {