|
From: <pat...@us...> - 2010-07-01 20:11:48
|
Revision: 1078
http://cishell.svn.sourceforge.net/cishell/?rev=1078&view=rev
Author: pataphil
Date: 2010-07-01 20:11:42 +0000 (Thu, 01 Jul 2010)
Log Message:
-----------
* Added org.cishell.utilities.swt to org.cishell.utilities.
* Added optional SWT dependencies to the manifest.
* Discussed with Micah.
Modified Paths:
--------------
trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF
Added Paths:
-----------
trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/
trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/SWTUtilities.java
trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLClickedListener.java
trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLMouseCursorListener.java
Modified: trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF
===================================================================
--- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-06-24 18:18:27 UTC (rev 1077)
+++ trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-07-01 20:11:42 UTC (rev 1078)
@@ -35,4 +35,7 @@
org.cishell.utilities.mutateParameter,
org.cishell.utilities.mutateParameter.defaultvalue,
org.cishell.utilities.mutateParameter.dropdown,
- org.cishell.utilities.osgi.logging
+ org.cishell.utilities.osgi.logging,
+ org.cishell.utilities.swt
+Require-Bundle: org.eclipse.ui;resolution:=optional,
+ org.eclipse.core.runtime;resolution:=optional
Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/SWTUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/SWTUtilities.java (rev 0)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/SWTUtilities.java 2010-07-01 20:11:42 UTC (rev 1078)
@@ -0,0 +1,98 @@
+package org.cishell.utilities.swt;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.widgets.Display;
+
+public class SWTUtilities {
+ /*
+ * 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 void appendStringWithURL(
+ StyledText textField,
+ URLClickedListener urlListener,
+ URLMouseCursorListener urlCursorListener,
+ String message,
+ 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"));
+ }
+
+
+ syncedStyledPrint(textField, message.substring(0, index), normalColor, SWT.NORMAL);
+ urlListener.addURL(textField.getText().length(), url);
+ urlCursorListener.addURL(textField.getText().length(), url);
+ syncedStyledPrint(textField, url, urlColor, SWT.BOLD);
+ appendStringWithURL(
+ textField,
+ urlListener,
+ urlCursorListener,
+ message.substring(index + url.length()),
+ normalColor,urlColor);
+ } else {
+ syncedStyledPrint(textField, message, normalColor, SWT.NORMAL);
+ }
+ }
+
+ /*
+ * Helper to actually format the string with a style range and
+ * append it to the StyledText control.
+ */
+
+ public static void syncedStyledPrint(
+ final StyledText textField, final String message, final Color color, final int style) {
+ Display.getDefault().syncExec(new Runnable() {
+ public void run(){
+ styledPrint(textField, message, color, style);
+ }
+ });
+ }
+
+ public static void styledPrint(StyledText textField, String message, Color color, int style) {
+ if (!textField.isDisposed()) {
+ textField.append(message);
+
+ StyleRange styleRange = new StyleRange();
+ styleRange.start = textField.getText().length() - message.length();
+ styleRange.length = message.length();
+ styleRange.foreground = color;
+ styleRange.fontStyle = style;
+ textField.setStyleRange(styleRange);
+
+ // This makes it autoscroll.
+ textField.setTopIndex(textField.getLineCount());
+ }
+ }
+}
\ No newline at end of file
Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLClickedListener.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLClickedListener.java (rev 0)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLClickedListener.java 2010-07-01 20:11:42 UTC (rev 1078)
@@ -0,0 +1,70 @@
+package org.cishell.utilities.swt;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.program.Program;
+
+/*
+ * Listens for clicks on urls and launches a browser appropriately.
+ */
+public class URLClickedListener extends MouseAdapter {
+ private Map<Integer, String> offsetsToURLs = new HashMap<Integer, String>();
+ private Map<String, String> urlsToDisplayURLs = new HashMap<String, String>();
+ private StyledText textField;
+
+ public URLClickedListener(StyledText textField) {
+ super();
+ this.textField = textField;
+ }
+
+ public void addURL(int offset, String url) {
+ addURL(offset, url, url);
+ }
+
+ public void addURL(int offset, String url, String displayURL) {
+ this.offsetsToURLs.put(offset, url);
+ this.urlsToDisplayURLs.put(url, displayURL);
+ }
+
+ public void mouseDown(MouseEvent event) {
+ if (event.button != 1) {
+ return;
+ }
+
+ int clickedPosition = determineClickedPosition(event);
+
+ if (clickedPosition < 0) {
+ return;
+ }
+
+ for (Integer offset : this.offsetsToURLs.keySet().toArray(new Integer[0])) {
+ String url = this.offsetsToURLs.get(offset);
+ String displayURL = this.urlsToDisplayURLs.get(url);
+
+ if ((displayURL != null) &&
+ (clickedPosition >= offset.intValue()) &&
+ (clickedPosition <= (offset.intValue() + displayURL.length()))) {
+ try {
+ Program.launch(url);
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ private int determineClickedPosition(MouseEvent event) {
+ int clickedPosition = -1;
+
+ try {
+ clickedPosition = this.textField.getOffsetAtLocation(new Point(event.x, event.y));
+ } catch (IllegalArgumentException ex) {
+ }
+
+ return clickedPosition;
+ }
+}
\ No newline at end of file
Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLMouseCursorListener.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLMouseCursorListener.java (rev 0)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/swt/URLMouseCursorListener.java 2010-07-01 20:11:42 UTC (rev 1078)
@@ -0,0 +1,79 @@
+package org.cishell.utilities.swt;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseMoveListener;
+import org.eclipse.swt.graphics.Cursor;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.widgets.Composite;
+
+/*
+ * Monitors the mouse and changes the cursor when it is over a URL.
+ */
+public class URLMouseCursorListener implements MouseMoveListener {
+ private Map<Integer, String> offsetsToURLs = new HashMap<Integer, String>();
+ private Map<String, String> urlsToDisplayURLs = new HashMap<String, String>();
+ private Composite parent;
+ private StyledText textField;
+
+ public URLMouseCursorListener(Composite parent, StyledText textField) {
+ this.parent = parent;
+ this.textField = textField;
+ }
+
+ public void addURL(int offset, String url) {
+ addURL(offset, url, url);
+ }
+
+ public void addURL(int offset, String url, String displayURL) {
+ this.offsetsToURLs.put(new Integer(offset), url);
+ this.urlsToDisplayURLs.put(url, displayURL);
+ }
+
+ public void mouseMove(MouseEvent event) {
+ int urlOffsetOfMousePosition = determineURLOffsetOfMousePosition(event);
+ Integer[] urlOffsets = this.offsetsToURLs.keySet().toArray(new Integer[0]);
+ boolean mouseIsOverURL = determineIfMouseIsHoveringOverURL(urlOffsetOfMousePosition, urlOffsets);
+ Cursor cursor = new Cursor(parent.getDisplay(), determineMouseCursor(mouseIsOverURL));
+ textField.setCursor(cursor);
+ }
+
+ private int determineURLOffsetOfMousePosition(MouseEvent event) {
+ try {
+ return textField.getOffsetAtLocation(new Point(event.x, event.y));
+ } catch (IllegalArgumentException e) {
+ Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_ARROW);
+ textField.setCursor(cursor);
+ }
+
+ return -1;
+ }
+
+ private boolean determineIfMouseIsHoveringOverURL(
+ int urlOffsetOfMousePosition, Integer[] urlOffsets) {
+ for (Integer urlOffset : urlOffsets) {
+ String url = this.offsetsToURLs.get(urlOffset);
+
+ if ((urlOffset != null) &&
+ (url != null) &&
+ (urlOffsetOfMousePosition >= urlOffset.intValue()) &&
+ (urlOffsetOfMousePosition <= (urlOffset.intValue() + url.length()))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private int determineMouseCursor(boolean mouseIsOverURL) {
+ if (mouseIsOverURL) {
+ return SWT.CURSOR_HAND;
+ } else {
+ return SWT.CURSOR_ARROW;
+ }
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|