Author: adietish
Date: 2011-12-08 08:46:11 -0500 (Thu, 08 Dec 2011)
New Revision: 37111
Removed:
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/utils/UrlUtils.java
Modified:
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/CreationLogDialog.java
Log:
[JBIDE-10395] corrected url detection when you click on a link
Modified:
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/CreationLogDialog.java
===================================================================
---
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/CreationLogDialog.java 2011-12-08
13:15:14 UTC (rev 37110)
+++
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/CreationLogDialog.java 2011-12-08
13:46:11 UTC (rev 37111)
@@ -33,20 +33,21 @@
import org.jboss.tools.openshift.express.internal.ui.OpenShiftImages;
import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
import org.jboss.tools.openshift.express.internal.ui.common.StringUtils;
-import org.jboss.tools.openshift.express.internal.utils.UrlUtils;
/**
* @author André Dietisheim
*/
public class CreationLogDialog extends TitleAreaDialog {
- private static final Pattern HTTP_LINK_REGEX = Pattern.compile("http[^
|\n]+");
-
+ private static final Pattern HTTP_LINK_REGEX = Pattern.compile("(http[^
|\n]+)");
+
private LogEntry[] logEntries;
+ private List<LinkSubstring> linkSubstrings;
public CreationLogDialog(Shell parentShell, LogEntry... logEntries) {
super(parentShell);
this.logEntries = logEntries;
+ this.linkSubstrings = new ArrayList<LinkSubstring>();
}
@Override
@@ -84,8 +85,7 @@
public void handleEvent(Event event) {
try {
String url = getUrl(logText, event);
- if (url != null
- && url.length() > 0) {
+ if (url != null && url.length() > 0) {
BrowserUtil.checkedCreateExternalBrowser(
url, OpenShiftUIActivator.PLUGIN_ID,
OpenShiftUIActivator.getDefault().getLog());
}
@@ -97,14 +97,12 @@
private String getUrl(StyledText logText, Event event) {
int offset = logText.getOffsetAtLocation(new Point(event.x, event.y));
StyleRange style = logText.getStyleRangeAtOffset(offset);
- if (style == null
+ if (style == null
|| !style.underline) {
return null;
}
- return UrlUtils.getUrl(offset, logText.getText());
+ return CreationLogDialog.this.getUrl(offset);
}
-
-
};
}
@@ -132,18 +130,26 @@
appendLog(logEntry, builder, styles);
}
- private void appendLog(LogEntry logEntry, StringBuilder builder, List<StyleRange>
styles) {
+ private void appendLog(LogEntry logEntry, StringBuilder builder,
+ List<StyleRange> styles) {
String log = logEntry.getLog();
- createLinkStyles(log, builder.length(), styles);
+ createLinks(log, builder.length(), styles);
builder.append(log);
builder.append(StringUtils.getLineSeparator());
}
- private void createLinkStyles(String log, int baseIndex, List<StyleRange> styles)
{
+ private void createLinks(String log, int baseIndex, List<StyleRange> styles) {
Matcher matcher = HTTP_LINK_REGEX.matcher(log);
- while (matcher.find()) {
- StyleRange linkStyle = createLinkStyleRange(matcher.start() + baseIndex, matcher.end()
+ baseIndex);
+ while (matcher.find()
+ && matcher.groupCount() == 1) {
+ int linkStart = matcher.start() + baseIndex;
+ int linkStop = matcher.end() + baseIndex;
+ StyleRange linkStyle = createLinkStyleRange(linkStart, linkStop);
styles.add(linkStyle);
+ String url = matcher.group(1);
+ LinkSubstring linkEntry =
+ new LinkSubstring(linkStart, linkStop, url);
+ linkSubstrings.add(linkEntry);
}
}
@@ -204,4 +210,54 @@
}
}
+ /**
+ * Gets the url at the given index within the log. Returns
<code>null</code>
+ * if none was found. Looks through the links that were found when parsing
+ * the log.
+ *
+ * @param offset
+ * @return the link
+ *
+ * @see #createLinks
+ */
+ private String getUrl(int offset) {
+ String url = null;
+ for (LinkSubstring linkSubstring : linkSubstrings) {
+ if (offset < linkSubstring.getStopOffset()
+ && offset > linkSubstring.getStartOffset()) {
+ url = linkSubstring.getUrl();
+ break;
+ }
+ }
+ return url;
+ }
+
+ /**
+ * A link within the log text.
+ */
+ private static class LinkSubstring {
+
+ private int startOffset;
+ private int stopOffset;
+ private String url;
+
+ public LinkSubstring(int startOffset, int stopOffset, String url) {
+ this.startOffset = startOffset;
+ this.stopOffset = stopOffset;
+ this.url = url;
+ }
+
+ private int getStartOffset() {
+ return startOffset;
+ }
+
+ private int getStopOffset() {
+ return stopOffset;
+ }
+
+ private String getUrl() {
+ return url;
+ }
+ }
+
}
Deleted:
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/utils/UrlUtils.java
===================================================================
---
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/utils/UrlUtils.java 2011-12-08
13:15:14 UTC (rev 37110)
+++
branches/jbosstools-3.3.0.M5/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/utils/UrlUtils.java 2011-12-08
13:46:11 UTC (rev 37111)
@@ -1,80 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2011 Red Hat, Inc.
- * Distributed under license by Red Hat, Inc. All rights reserved.
- * This program is 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:
- * Red Hat, Inc. - initial API and implementation
- ******************************************************************************/
-package org.jboss.tools.openshift.express.internal.utils;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * @author André Dietisheim
- */
-public class UrlUtils {
-
- private static final int HTTP_SCHEME_MAX_LENGTH = 8;
- private static final char HTTP_SCHEME_START_CHAR = 'h';
- private static final Pattern HTTP_SCHEME_REGEX =
Pattern.compile("http[s]{0,1}://");
-
- /**
- * Returns an url that was found in the given text. It starts looking
- * backwards from the given offset within the given string. Returns
- * <code>null</code> if none was found.
- *
- * @param offset
- * starting point to look back in the given text.
- * @param text
- * the text to search for an url
- * @return the url that was found in the text
- */
- public static String getUrl(int offset, String text) {
- int start = getUrlStart(offset, text);
- if (start == -1) {
- return null;
- }
- int stop = getUrlStop(offset, text);
- if (stop == -1) {
- return null;
- }
- return text.substring(start, stop);
-
- }
-
- /**
- * Steps back in the given text until the beginning of the text or an
- * occurrence of http(s):// is found.
- *
- * @param offset the offset to start with stepping backwards
- * @param text the text to search
- * @return the index at which http(s):// was found
- */
- private static int getUrlStart(int offset, String text) {
- for (int i = offset; i > 0; --i) {
- if (text.charAt(i) == HTTP_SCHEME_START_CHAR
- && (i + HTTP_SCHEME_MAX_LENGTH < text.length())) {
- Matcher matcher = HTTP_SCHEME_REGEX.matcher(text.substring(i, i +
HTTP_SCHEME_MAX_LENGTH));
- if (matcher.find()) {
- return i;
- }
- }
- }
- return -1;
- }
-
- private static int getUrlStop(int offset, String text) {
- for (int i = offset; i < text.length(); ++i) {
- if (' ' == text.charAt(i)
- || '\n' == text.charAt(i)) {
- return i;
- }
- }
- return -1;
- }
-
-}