CopyableLabel: Allow setting the preview text for the label

When Gerrit Code Review puts the entire SSH host key into the
copyable label it causes the entire layout to fall apart as
the text is simply too wide for the screen, and the Clippy
Flash movie runs off the edge.

Instead allow the application to provide its own abbreviation
string for the preview you click on.

Change-Id: I5aaaa7b4b41c2f461671955a7d82f09107e0e0a5
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java b/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java
index 60a988b..e2e91ed 100644
--- a/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java
+++ b/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java
@@ -71,6 +71,7 @@
 
   private final FlowPanel content;
   private String text;
+  private int visibleLen;
   private Label textLabel;
   private TextBox textBox;
   private Element swf;
@@ -96,6 +97,8 @@
     initWidget(content);
 
     text = str;
+    visibleLen = text.length();
+
     if (showLabel) {
       textLabel = new InlineLabel(getText());
       textLabel.setStyleName(ClippyResources.I.css().label());
@@ -110,6 +113,19 @@
     embedMovie();
   }
 
+  /**
+   * Change the text which is displayed in the clickable label.
+   *
+   * @param text the new preview text, should be shorter than the original text
+   *        which would be copied to the clipboard.
+   */
+  public void setPreviewText(final String text) {
+    if (textLabel != null) {
+      textLabel.setText(text);
+      visibleLen = text.length();
+    }
+  }
+
   private void embedMovie() {
     if (flashEnabled && UserAgent.hasFlash) {
       final String flashVars = "text=" + URL.encodeComponent(getText());
@@ -164,7 +180,7 @@
     if (textBox == null) {
       textBox = new TextBox();
       textBox.setText(getText());
-      textBox.setVisibleLength(getText().length());
+      textBox.setVisibleLength(visibleLen);
       textBox.addKeyPressHandler(new KeyPressHandler() {
         @Override
         public void onKeyPress(final KeyPressEvent event) {