From: <jrb...@us...> - 2011-04-01 20:57:36
|
Revision: 1241 http://cishell.svn.sourceforge.net/cishell/?rev=1241&view=rev Author: jrbibers Date: 2011-04-01 20:57:29 +0000 (Fri, 01 Apr 2011) Log Message: ----------- Welcome text URLs. Changed method of hyperlinking from welcome text. Previously we attempted to detect URLs in bare strings. This is a hard problem; consider cases like "(URL)", "(URL).", "(text URL)", and "(URL text)", especially when URL may contain infrequent-but-valid characters like parentheses. See http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html . Now we do not attempt URL detection. Strings in a piece of welcome text intended for formatting as a clickable link should be enclosed in "url" tags as in "Visit our website ([url]http://example.com[/url]) for more details". Reviewed by Micah. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.brand.cishell/plugin.properties trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java trunk/core/org.cishell.utility.swt/src/org/cishell/utility/swt/SWTUtilities.java Modified: trunk/clients/gui/org.cishell.reference.gui.brand.cishell/plugin.properties =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.brand.cishell/plugin.properties 2011-03-31 20:10:14 UTC (rev 1240) +++ trunk/clients/gui/org.cishell.reference.gui.brand.cishell/plugin.properties 2011-04-01 20:57:29 UTC (rev 1241) @@ -2,6 +2,7 @@ appName = CIShellApplication aboutImage = icons/about.gif windowImages = icons/alt16.gif,icons/alt32.gif,icons/alt64.gif +# Mark up URLs with url tags, like [url]http://example.com[/url]. blurb = Welcome to the Cyberinfrastructure Shell (CIShell) \ developed at the InfoVis Lab and the CI for Network Science Center \ at Indiana University.\n\n\ @@ -9,5 +10,5 @@ Bruce Herr, Weixia Huang, Shashikant Penumarthy, and Katy Borner. (in press). \ Designing Highly Flexible and Usable Cyberinfrastructures for Convergence. \ William S. Bainbridge (Ed.) Progress in Convergence. Annals of the New York Academy of Sciences.\n\ -http://cishell.org/papers/06-cishell.pdf +[url]http://cishell.org/papers/06-cishell.pdf[/url] Modified: trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2011-03-31 20:10:14 UTC (rev 1240) +++ trunk/clients/gui/org.cishell.reference.gui.log/src/org/cishell/reference/gui/log/LogView.java 2011-04-01 20:57:29 UTC (rev 1241) @@ -127,8 +127,8 @@ for (StyleRange highlightStyle : highlights) { int start = nonHighlightStyle.start; - if ((start >= highlightStyle.start) && - (start < (highlightStyle.start + highlightStyle.length))) { + if ((start >= highlightStyle.start) + && (start < (highlightStyle.start + highlightStyle.length))) { newStyle = (StyleRange) newStyle.clone(); newStyle.background = HIGHLIGHTED_BACKGROUND_COLOR; } @@ -208,7 +208,8 @@ @SuppressWarnings("unchecked") public void createPartControl(Composite parent) { this.parent = parent; - this.textField = new StyledText(parent, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.READ_ONLY); + this.textField = + new StyledText(parent, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.READ_ONLY); this.textField.setEditable(false); this.textField.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); this.textField.getCaret().setVisible(false); @@ -261,11 +262,10 @@ Enumeration backLogEntries = logReaderService.getLog(); while (backLogEntries.hasMoreElements()) { - LogEntry logEntry = (LogEntry)backLogEntries.nextElement(); + LogEntry logEntry = (LogEntry) backLogEntries.nextElement(); this.logged(logEntry); } - } - else { + } else { System.out.println("reader is null"); } @@ -328,7 +328,7 @@ } } - Collection<StyleRange> styles = SWTUtilities.appendStringWithURL( + Collection<StyleRange> styles = SWTUtilities.urlifyUrls( LogView.this.textField, LogView.this.urlListener, LogView.this.urlCursorListener, @@ -348,10 +348,10 @@ } private boolean goodMessage(String msg) { - if (msg == null || - msg.startsWith("ServiceEvent ") || - msg.startsWith("BundleEvent ") || - msg.startsWith("FrameworkEvent ")) { + if (msg == null + || msg.startsWith("ServiceEvent ") + || msg.startsWith("BundleEvent ") + || msg.startsWith("FrameworkEvent ")) { return false; } else { return true; Modified: trunk/core/org.cishell.utility.swt/src/org/cishell/utility/swt/SWTUtilities.java =================================================================== --- trunk/core/org.cishell.utility.swt/src/org/cishell/utility/swt/SWTUtilities.java 2011-03-31 20:10:14 UTC (rev 1240) +++ trunk/core/org.cishell.utility.swt/src/org/cishell/utility/swt/SWTUtilities.java 2011-04-01 20:57:29 UTC (rev 1241) @@ -2,6 +2,7 @@ import java.util.Collection; import java.util.HashSet; +import java.util.regex.Pattern; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyleRange; @@ -10,131 +11,90 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; -/** TODO: The URL (http://-prefixed) parsing utilities in this class need to be updated to handle - * https as well. - */ public class SWTUtilities { - public static final Color DEFAULT_BACKGROUND_COLOR = - new Color(Display.getDefault(), 255, 255, 255); - /* - * Append the given string to the console with the given color, this will do the job of - * checking for URLs within the string and registering the proper listeners on them as well. + public static final String URL_START_TAG = "[url]"; + public static final String URL_END_TAG = "[/url]"; + + /** Do not instantiate utilities. */ + protected SWTUtilities() { + throw new UnsupportedOperationException(); + } + + /** + * Style text within URL_START_TAG and URL_END_TAG like URLs and make them clickable links. */ - public static Collection<StyleRange> appendStringWithURL( + public static Collection<StyleRange> urlifyUrls( StyledText textField, URLClickedListener urlListener, URLMouseCursorListener urlCursorListener, String message, Color normalColor, Color urlColor) { - return appendStringWithURL( - textField, - urlListener, - urlCursorListener, - message, - DEFAULT_BACKGROUND_COLOR, - normalColor, - urlColor); - } - - public static Collection<StyleRange> appendStringWithURL( - StyledText textField, - URLClickedListener urlListener, - URLMouseCursorListener urlCursorListener, - String message, - Color backgroundColor, - Color normalColor, - Color urlColor) { - - //find a URL in the message - - int index = message.indexOf("http://"); - if (index == -1) { - index = message.indexOf("https://"); - } - if (index == -1) { - index = message.indexOf("www."); - } - - if (index > -1) { - String url = message.substring(index); - if (url.indexOf(") ") > -1) { - url = url.substring(0, url.indexOf(") ")); - } - else if (url.indexOf(" ") > -1) { - url = url.substring(0, url.indexOf(" ")); - if (url.trim().endsWith(".") ){ - url=url.substring(0, url.length()-1); - } - } - if (url.endsWith(".\n") || url.endsWith(".\t")){ - url=url.substring(0, url.length()-2); - } - if (url.indexOf("\n") > -1) { - url = url.substring(0, url.indexOf("\n")); - } - if (url.indexOf("\t") > -1) { - url = url.substring(0, url.indexOf("\n")); - } - - - StyleRange preURLStyle = syncedStyledPrint( - textField, message.substring(0, index), backgroundColor, normalColor, SWT.NORMAL); - urlListener.addURL(textField.getText().length(), url); - urlCursorListener.addURL(textField.getText().length(), url); - StyleRange urlStyle = - syncedStyledPrint(textField, url, backgroundColor, urlColor, SWT.BOLD); - Collection<StyleRange> postURLStyles = appendStringWithURL( - textField, - urlListener, - urlCursorListener, - message.substring(index + url.length()), - backgroundColor, - normalColor, - urlColor); - - Collection<StyleRange> finalStyles = new HashSet<StyleRange>(); - - if (preURLStyle != null) { - finalStyles.add(preURLStyle); - } - - if (urlStyle != null) { - finalStyles.add(urlStyle); - } - - finalStyles.addAll(postURLStyles); - - return finalStyles; - } else { - StyleRange style = syncedStyledPrint( - textField, message, backgroundColor, normalColor, SWT.NORMAL); - - if (style != null) { - Collection<StyleRange> finalStyles = new HashSet<StyleRange>(); - finalStyles.add(style); - - return finalStyles; - } else { - return new HashSet<StyleRange>(); - } - } + int startTagIndex = message.indexOf(URL_START_TAG); + int endTagIndex = message.indexOf(URL_END_TAG); + + boolean urlDetected = startTagIndex >= 0 && endTagIndex >= 0; + if (urlDetected) { + String urlWithTags = + message.substring(startTagIndex, endTagIndex + URL_END_TAG.length()); + String url = + urlWithTags.substring( + URL_START_TAG.length(), + urlWithTags.length() - URL_END_TAG.length()); + + String messageWithFirstUrlDetagged = + message.replaceFirst(Pattern.quote(urlWithTags), url); + + String messageBeforeUrl = messageWithFirstUrlDetagged.substring(0, startTagIndex); + StyleRange preURLStyle = syncedStyledPrint( + textField, messageBeforeUrl, normalColor, SWT.NORMAL); + urlListener.addURL(textField.getText().length(), url); + urlCursorListener.addURL(textField.getText().length(), url); + StyleRange urlStyle = + syncedStyledPrint(textField, url, urlColor, SWT.BOLD); + String messageBeyondFirstUrl = + messageWithFirstUrlDetagged.substring(startTagIndex + url.length()); + Collection<StyleRange> postURLStyles = urlifyUrls( + textField, + urlListener, + urlCursorListener, + messageBeyondFirstUrl, + normalColor, + urlColor); + + Collection<StyleRange> finalStyles = new HashSet<StyleRange>(); + + if (preURLStyle != null) { + finalStyles.add(preURLStyle); + } + + if (urlStyle != null) { + finalStyles.add(urlStyle); + } + + finalStyles.addAll(postURLStyles); + + return finalStyles; + } else { + StyleRange style = syncedStyledPrint(textField, message, normalColor, SWT.NORMAL); + + if (style != null) { + Collection<StyleRange> finalStyles = new HashSet<StyleRange>(); + finalStyles.add(style); + + return finalStyles; + } else { + return new HashSet<StyleRange>(); + } + } } - /* - * Helper to actually format the string with a style range and - * append it to the StyledText control. + /** + * Format the string with a style range and append it to the StyledText control. */ - public static StyleRange syncedStyledPrint( - StyledText textField, String message, Color color, int style) { - return syncedStyledPrint(textField, message, DEFAULT_BACKGROUND_COLOR, color, style); - } - - public static StyleRange syncedStyledPrint( final StyledText textField, final String message, - final Color backgroundColor, final Color color, final int style) { final StyleRange[] styleRange = new StyleRange[1]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |