Tuesday, July 26, 2011

Adding a WPF RichTextBox hyperlink at specific character positions?


Adding hyperlinks to a WPF RichTextBox can be achieved at design time in XAML or programmatically at run time.
Hyperlinks are created programmatically via the hyperlink constructor by parsing either the start and end TextPointers or InLine objects. These constructors are not particularly developer friendly unless the developer is programmatically populating a RichTextBox’s FlowDocument from Runs (see here).
In a recent project I need to add a hyperlink for text between predefined character index. For example, I needed a hyperlink for the text located between character positions 100 and 110.  The following extension method enables this.
RichTextBox extension:
namespace ESRI.PrototypeLab.Sample {
    public static class Extensions {
        public static Hyperlink ToHyperlink(this RichTextBox rtb, int start, int end) {
            TextPointer p1 = rtb.ToTextPointer(start);
            TextPointer p2 = rtb.ToTextPointer(end);
            if (p1 == null || p2 == null) { return null; }
            return new Hyperlink(p1, p2);
        }
        public static TextPointer ToTextPointer(this RichTextBox rtb, int index) {
            int count = 0;
            TextPointer position = rtb.Document.ContentStart;
            while (position != null) {
                if (position.GetPointerContext(LogicalDirection.Forward) ==
                    TextPointerContext.Text) {
                    string textRun = position.GetTextInRun(LogicalDirection.Forward);
                    int length = textRun.Length;
                    if (count + length > index) {
                        return position.GetPositionAtOffset(index - count);
                    }
                    count += length;
                }
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
            return null;
        }
    }
}
How to use:
Hyperlink hyperlink = this.RichTextBox.ToHyperlink(100, 110);
hyperlink.Click += (s, e) => {
    MessageBox.Show("Hyperlink Clicked");};

Wednesday, July 20, 2011

The App Interoperability Conundrum


I would argue that mobile computing began the early 20th century with the popularization of wristwatches. The wristwatch, like its ancestral pocket watch, allowed people to know, at any moment, what time it was. Mobile computers advanced significantly in 2007 when Apple introduced the iPhone. This device was not only a mobile communication device but it allowed people know, at any moment, where they are.
But I don’t want to discuss the historical importance of the iPhone, rather the paradigm shift in application design.
For the past twenty years or so, the personal computer (or PC) has dominated homes and offices throughout the world. The vast majority of PCs used some generation of Microsoft’s Windows operating system. Regardless of the criticism that Microsoft stifled innovation, Windows users have enjoyed one unquestioned privilege, interoperability.
Quite unconsciously, a PC user can perform information exchange between applications using operations like copy and paste or drag and drop. Not only can a user exchange information, like text or imagery, between applications like Internet Explorer, Excel and Word but also with third party applications. This interoperability is available because of two factors:
  1. All Windows applications understand the same data and file types, and
  2. Windows provides a built-in framework that software developers can use to enable the inter-application exchange of information.
It is without question that mobile devices will continue to proliferate in the consumer market with improvements in mobile infrastructure, device affordability and cultural acceptance. However I see an uneasy trend in mobile application, that is, mobile applications are becoming increasingly focused but less interoperability.
To illustrate this point, consider the following hypothetical example. I am in downtown Los Angeles, I’m hungry and need nourishment from a reputable establishment. On my iPhone I can use:
  1. Google Maps to find a nearby restaurant,
  2. MapQuest to get voice guided routing directions,
  3. Built-in camera to record my memorable meal,
  4. FourSquare to “check-in” and recommend this restaurant to my friends, and
  5. Yelp to post a review (and snapshot) of my dining experience.
In this workflow, the only interoperable elements were the street address I copied from (1) to (2) and the photo created in (3) and shared with (5).
Obviously this workflow could be optimized by using one app to perform two or more tasks like using Google Map to a find a restaurant and driving directions. But these five apps, in my hypothetical opinion, were the best in each task.
My criticism is not targeted at the number of app used during my lunch break but the lack of interoperability between them. For example:
  1. From Google Map, why is it not possible to send the restaurant address directly to my favorite routing apps?
  2. From the camera app, why is it not possible to share a photo directly with Yelp.
  3. From MapQuest, can I send the “route” to my friends so that they know where I am driving from?
  4. To rate (or “check-in” at) the restaurant with FourSquare and Yelp why did I have to locate the restaurant in each app? Why not just click the “get current establishment” button in each app?
These comments may seem like iPhone-bashing, but they are not. I am just bringing your attention the unintentional stovepiping caused by increasingly focused mobile apps.
To paraphrase Apple’s advertising slogan, there may be an “app for that” but what is lacking is the “app to app” interoperability. Again this is not a criticism of Apple or other mobile device vendors, just a mere observation of the consumer-driven “app paradigm”.
The "app paradigm” does have its advantages. Smaller focused apps with a handful of capabilities can be deployed quickly, updated frequently, serve niches and most importantly, be profitable. Many software developers have made a health living by serving the “long tail” with $0.99 apps.
But I predict that there may be an equilibrium shift in near future in which, due to the lack of app cohesion, that devices will eventually be ruled by a handful of super-apps. The foremost, without doubt, is Google. Google may (or should?) consolidate their shopping, social networking, mapping, translation, book and search services into a single super-app.

Thursday, June 30, 2011

Modeling the Real World with Silverlight 5 3D

Late last year Esri’s Applications Prototype Lab published a Silverlight-based web application that could render a small area of the Earth’s surface in three dimensions. This app used Einar Ingebrigtsen’s graphics library called Balder and referenced imagery and elevation services from arcgis.com.

This year at MIX11, Microsoft released the beta of the Silverlight 5. Probably the most important improvement is the added support for GPU accelerated 3d graphics. This means Silverlight apps with 3d content will be rendered by a computer’s graphics card rather than dominating the CPU. It is now conceptually possible to develop Google Earth-like applications using Silverlight 5 3d!

Creating a virtual globe is a rather daunting project. However, inspired by Andy Beaulieu’s physics sample and associated blog post I decided to explore the possibility of modeling real world data in a browser.

Nicknamed “project alpha”, the prototype shown in the video above used the following pieces:

  1. Esri Silverlight SDK 2.2 (link)
    Mapping
  2. Microsoft Silverlight 5 beta (link)
    RIA
  3. Balder 0.8.8.9 (link)
    3D graphics engine
  4. JigLibX 0.3.1 (link)
    3D physics engine

Displaying a 3d world using streaming imagery and elevation data is not new. What is interesting about this prototype is the inclusion of a physics engine. Objects such as cars, people, tanks, planes can interact with each other and the Earth’s surface. Additionally, this prototypes shows that any location, and at any scale, can be modeled completely within a browser.

Whilst dropping hundred of tennis balls on Mount Ruapehu might seem a little fanciful, there are many practical uses of combining a 3d physics engine with mapping data. For example, an engineer may want to test drive a proposed road to examine it drivability and view. In defense, this technology can be for modeling and simulation of the battlefield.

Unless there is significant interest, the app and source code will be published once Silverlight 5 goes final.

Monday, May 23, 2011

Landsat Touch for Silverlight

clip_image002

The Lab is proud to release Landsat Touch, a Silverlight based web application for browsing the newly published Landsat imagery.

Try the application here:
http://maps.esri.com/sldemos/landsat/default.html
(click the “need help” button in the lower left hand corner for usage tips)

Download the source code from here:
http://www.arcgis.com/home/item.html?id=04de702976434f4d8c054005ce4603a2

Earlier this year Esri announced the publication of more than 8TB of Landsat imagery as image services on ArcGIS Online. These services can be consumed by any ArcGIS client, the ChangeMatters viewer or by any custom app using the REST API.

Landsat Touch is a custom web application developed using the ArcGIS API for Silverlight. The objective of this project was to create an easy to use application that could browse, compare and contrast Landsat content and provide support for touch devices (if available). The application is fully functional using a mouse on Windows or Mac computer, but the application is optimized for Windows 7 touch devices.

Multi-touch behavior is provided using the Multi-Touch Manipulation library from CodePlex which is partially derived from Microsoft’s Manipulation and Inertial sample.

The ability for this application to compare two layers of content is conceptually similar to ArcMap’s swipe layer tool. An important distinction with Landsat Touch is that users can explicitly define the “swiped” area by manipulating the size, orientation and location of windows. However the most exciting features demonstrated by this application is its ability to be multi-touch and multi-user. On large touch devices, say 40”+ diagonally, it is conceivable that two or more users can simultaneously add and manipulate Landsat windows.

Wednesday, April 27, 2011

Geochat for Silverlight

image

Geochat for Silverlight is a browser-to-browser geo-collaboration sample. Graphics drawn in browser’s map can be immediately pushed to all other browsers on the internet. The web application uses Microsoft’s implementation of Comet, a model using long lived HTTP requests to simulate server to client communication without the overhead of polling.

Geochat is implemented in two parts.

  1. Server-side
    A Windows Communication Foundation (WCF) duplex service is created to maintain a connection to Silverlight clients.  The geochat implementation acts as broker between Silverlight clients by forwarding messages to individual (or all) clients.
    http://msdn.microsoft.com/en-us/library/cc645027(VS.95).aspx
  2. Client-side
    The Silverlight client is using PollingDuplexHttpBinding to maintain a connection with the server using AJAX polling.  The polling occurs every five seconds.
    http://msdn.microsoft.com/en-us/library/cc645028(VS.95).aspx

Click here to experiment with a live sample. Complete source code for this app is available here.