diff options
author | slewis | 2007-11-08 00:21:20 +0000 |
---|---|---|
committer | slewis | 2007-11-08 00:21:20 +0000 |
commit | 1eeb0dc2676045ffa4b86929a63bc87c92fba3e7 (patch) | |
tree | d4f9c2e7f914cc2c3cd7931e0e8ce7e4d1f5da7e /examples/bundles/org.eclipse.ecf.example.collab/src | |
parent | 283b4941275bf13972747bf993aa91a091b653d0 (diff) | |
download | org.eclipse.ecf-1eeb0dc2676045ffa4b86929a63bc87c92fba3e7.tar.gz org.eclipse.ecf-1eeb0dc2676045ffa4b86929a63bc87c92fba3e7.tar.xz org.eclipse.ecf-1eeb0dc2676045ffa4b86929a63bc87c92fba3e7.zip |
Added hyperlink detector for Collaboration Example.
Diffstat (limited to 'examples/bundles/org.eclipse.ecf.example.collab/src')
3 files changed, 242 insertions, 7 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlink.java b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlink.java new file mode 100644 index 000000000..3a35c167f --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlink.java @@ -0,0 +1,91 @@ +/**************************************************************************** + * Copyright (c) 2007 Composent, Inc. 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 + * + * Contributors: + * Composent, Inc. - initial API and implementation + *****************************************************************************/ + +package org.eclipse.ecf.example.collab.share; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.Path; +import org.eclipse.ecf.example.collab.share.EclipseCollabHyperlinkDetector.Selection; +import org.eclipse.ecf.internal.example.collab.ClientPlugin; +import org.eclipse.ecf.internal.example.collab.ui.EditorHelper; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.hyperlink.IHyperlink; +import org.eclipse.osgi.util.NLS; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; + +/** + * + */ +public class EclipseCollabHyperlink implements IHyperlink { + + private final IRegion region; + + private final String fileName; + + private final Selection selection; + + /** + * @param detectedRegion + * @param fileName + * @param selection + */ + public EclipseCollabHyperlink(IRegion detectedRegion, String fileName, Selection selection) { + this.region = detectedRegion; + this.fileName = fileName; + this.selection = selection; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkRegion() + */ + public IRegion getHyperlinkRegion() { + return region; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkText() + */ + public String getHyperlinkText() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.text.hyperlink.IHyperlink#getTypeLabel() + */ + public String getTypeLabel() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.text.hyperlink.IHyperlink#open() + */ + public void open() { + final IWorkbench wb = PlatformUI.getWorkbench(); + final IWorkbenchWindow ww = wb.getActiveWorkbenchWindow(); + final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fileName)); + if (file != null) { + final EditorHelper eh = new EditorHelper(ww); + try { + eh.openAndSelectForFile(file, (selection == null) ? 0 : selection.getStart(), (selection == null) ? 0 : (selection.getEnd() - selection.getStart())); + } catch (final Exception e) { + ClientPlugin.log("Exception in openEditorAndSelectForFile", e); + } + } else { + MessageDialog.openInformation(ww.getShell(), "Open Editor Failed", NLS.bind("Cannot open editor for {0}. It was not found in your workspace.", fileName)); + } + + } + +} diff --git a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlinkDetector.java b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlinkDetector.java new file mode 100644 index 000000000..2ac8499e3 --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabHyperlinkDetector.java @@ -0,0 +1,150 @@ +/**************************************************************************** + * Copyright (c) 2007 Composent, Inc. 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 + * + * Contributors: + * Composent, Inc. - initial API and implementation + *****************************************************************************/ +package org.eclipse.ecf.example.collab.share; + +import org.eclipse.ecf.example.collab.share.EclipseCollabSharedObject.SharedMarker; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; +import org.eclipse.jface.text.hyperlink.IHyperlink; + +public class EclipseCollabHyperlinkDetector extends AbstractHyperlinkDetector { + + public static final String SHARE_FILE_HYPERLINK_END = "/>"; + public static final String SHARE_FILE_HYPERLINK_START = "<open file=\""; + public static final String SHARE_FILE_HYPERLINK_SELECTION = " selection="; + + /* (non-Javadoc) + * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean) + */ + public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { + + if (region == null || textViewer == null) + return null; + + final IDocument document = textViewer.getDocument(); + if (document == null) + return null; + + final int offset = region.getOffset(); + + IRegion lineInfo; + String line; + try { + lineInfo = document.getLineInformationOfOffset(offset); + line = document.get(lineInfo.getOffset(), lineInfo.getLength()); + } catch (final BadLocationException ex) { + return null; + } + + final IRegion detectedRegion = detectSubRegion(lineInfo, line); + if (detectedRegion == null) + return null; + + final int detectedStart = detectedRegion.getOffset() - lineInfo.getOffset(); + final String substring = line.substring(detectedStart, detectedStart + detectedRegion.getLength()); + final String fileName = detectFileName(substring); + if (fileName == null) + return null; + + final Selection selection = detectSelection(substring); + + return new IHyperlink[] {new EclipseCollabHyperlink(detectedRegion, fileName, selection)}; + + } + + /** + * @param substring + * @return + */ + private Selection detectSelection(String linkString) { + final int beginIndex = linkString.indexOf(SHARE_FILE_HYPERLINK_SELECTION); + if (beginIndex == -1) + return null; + final int endIndex = linkString.indexOf(SHARE_FILE_HYPERLINK_END); + if (endIndex == -1) + return null; + // should have syntax start-end + final String selection = linkString.substring(beginIndex + SHARE_FILE_HYPERLINK_SELECTION.length(), endIndex); + final int dashIndex = selection.indexOf("-"); + if (dashIndex == -1) + return null; + try { + final int start = Integer.parseInt(selection.substring(0, dashIndex)); + final int end = Integer.parseInt(selection.substring(dashIndex + 1)); + return new Selection(start, end); + } catch (final NumberFormatException e) { + return null; + } + } + + class Selection { + int start; + int end; + + public Selection(int start, int end) { + this.start = start; + this.end = end; + } + + public int getStart() { + return start; + } + + public int getEnd() { + return end; + } + } + + /** + * @param substring + * @return + */ + private String detectFileName(String substring) { + final int startIndex = substring.indexOf("\""); + if (startIndex == -1) + return null; + final int endIndex = substring.indexOf("\"", startIndex + 1); + if (endIndex == -1) + return null; + return substring.substring(startIndex + 1, endIndex); + } + + protected IRegion detectSubRegion(IRegion lineInfo, String fromLine) { + final int startIndex = fromLine.indexOf(SHARE_FILE_HYPERLINK_START); + if (startIndex == -1) + return null; + // got one...look for terminator after + final int endIndex = fromLine.indexOf(SHARE_FILE_HYPERLINK_END, startIndex); + if (endIndex == -1) + return null; + return new Region(lineInfo.getOffset() + startIndex, (endIndex - startIndex) + SHARE_FILE_HYPERLINK_END.length()); + } + + public static String createDisplayStringForEditorOpen(String resourceName, SharedMarker marker) { + final StringBuffer se = new StringBuffer(EclipseCollabHyperlinkDetector.SHARE_FILE_HYPERLINK_START); + se.append(resourceName).append("\""); + if (marker != null) { + final int start = marker.getOffset().intValue(); + final int length = marker.getLength().intValue(); + if (length > 0) { + se.append(EclipseCollabHyperlinkDetector.SHARE_FILE_HYPERLINK_SELECTION); + se.append(start).append("-").append(start + length); + } + } + se.append(EclipseCollabHyperlinkDetector.SHARE_FILE_HYPERLINK_END); + return se.toString(); + } + +} diff --git a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabSharedObject.java b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabSharedObject.java index 5ec65403c..b32f65494 100644 --- a/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabSharedObject.java +++ b/examples/bundles/org.eclipse.ecf.example.collab/src/org/eclipse/ecf/example/collab/share/EclipseCollabSharedObject.java @@ -835,13 +835,7 @@ public class EclipseCollabSharedObject extends GenericSharedObject implements Li } protected String createDisplayStringForEditorOpen(String resourceName, SharedMarker marker) { - final StringBuffer se = new StringBuffer((marker == null) ? "<open " : "<share "); - se.append(resourceName).append(" "); - if (marker != null) { - se.append(marker.getOffset()).append("-").append(marker.getOffset().intValue() + marker.getLength().intValue()); - } - se.append(">"); - return se.toString(); + return EclipseCollabHyperlinkDetector.createDisplayStringForEditorOpen(resourceName, marker); } protected IFile getLocalFileForRemote(String file) { |