Add example pop down button to change screen

Change-Id: I38cf9617498af41153285046aa0e3ea157c378f6
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/HelloForm.gwt.xml b/src/main/java/com/googlesource/gerrit/plugins/cookbook/HelloForm.gwt.xml
index 880520c..d056c5d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/HelloForm.gwt.xml
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/HelloForm.gwt.xml
@@ -23,6 +23,7 @@
   <inherits name="com.google.gwt.http.HTTP"/>
   <inherits name="com.google.gwt.json.JSON"/>
   <inherits name='com.google.gwtexpui.clippy.Clippy'/>
+  <inherits name='com.google.gwtexpui.globalkey.GlobalKey'/>
   <!-- Using GWT built-in themes adds a number of static          -->
   <!-- resources to the plugin. No theme inherits lines were      -->
   <!-- added in order to make this plugin as simple as possible   -->
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/BuildsDropDownPanel.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/BuildsDropDownPanel.java
new file mode 100644
index 0000000..86b7034
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/BuildsDropDownPanel.java
@@ -0,0 +1,74 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.cookbook.client;
+
+import com.google.gerrit.plugin.client.extension.Panel;
+import com.google.gwt.user.client.ui.FlowPanel;
+import com.google.gwt.user.client.ui.Grid;
+import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.Image;
+import com.google.gwt.user.client.ui.InlineHyperlink;
+import com.google.gwt.user.client.ui.InlineLabel;
+
+/**
+ * Extension for change screen that displays a status in the header bar.
+ */
+public class BuildsDropDownPanel extends FlowPanel {
+  static class Factory implements Panel.EntryPoint {
+    @Override
+    public void onLoad(Panel panel) {
+      panel.setWidget(new BuildsDropDownPanel());
+    }
+  }
+
+  BuildsDropDownPanel() {
+    Grid g = new Grid(3, 4);
+    g.addStyleName("infoBlock");
+    CellFormatter fmt = g.getCellFormatter();
+
+    g.setText(0, 0, "State");
+    fmt.addStyleName(0, 0, "header");
+    g.setText(0, 1, "PS");
+    fmt.addStyleName(0, 1, "header");
+    g.setText(0, 2, "Date");
+    fmt.addStyleName(0, 2, "header");
+    g.setText(0, 3, "Log");
+    fmt.addStyleName(0, 3, "header");
+
+    HorizontalPanel p = new HorizontalPanel();
+    p.add(new Image(CookBookPlugin.RESOURCES.greenCheck()));
+    p.add(new InlineLabel("OK"));
+    g.setWidget(1, 0, p);
+    g.setWidget(1, 1, new InlineLabel("2"));
+    g.setWidget(1, 2, new InlineLabel("2015-07-09 11:06:13"));
+    g.setWidget(1, 3, new InlineHyperlink("Build Log", "TODO"));
+
+    p = new HorizontalPanel();
+    p.add(new Image(CookBookPlugin.RESOURCES.redNot()));
+    p.add(new InlineLabel("FAILED"));
+    g.setWidget(2, 0, p);
+    g.setWidget(2, 1, new InlineLabel("1"));
+    g.setWidget(2, 2, new InlineLabel("2015-07-09 09:17:28"));
+    g.setWidget(2, 3, new InlineHyperlink("Build Log", "TODO"));
+
+    fmt.addStyleName(0, 0, "topmost");
+    fmt.addStyleName(0, 1, "topmost");
+    fmt.addStyleName(0, 2, "topmost");
+    fmt.addStyleName(0, 3, "topmost");
+
+    add(new PopDownButton("Builds", g));
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/CookBookPlugin.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/CookBookPlugin.java
index 4a83b64..5a15e56 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/CookBookPlugin.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/CookBookPlugin.java
@@ -35,5 +35,8 @@
         new CookBookChangeScreenExtension.Factory());
     Plugin.get().panel(GerritUiExtensionPoint.CHANGE_SCREEN_HEADER,
         new ChangeScreenStatusExtension.Factory());
+    Plugin.get().panel(
+        GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_POP_DOWNS,
+        new BuildsDropDownPanel.Factory());
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/PopDownButton.java b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/PopDownButton.java
new file mode 100644
index 0000000..3cbe5d6
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/cookbook/client/PopDownButton.java
@@ -0,0 +1,101 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.cookbook.client;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Style;
+import com.google.gwt.dom.client.Style.FontWeight;
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.logical.shared.CloseEvent;
+import com.google.gwt.event.logical.shared.CloseHandler;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.PopupPanel;
+import com.google.gwt.user.client.ui.Widget;
+import com.google.gwtexpui.globalkey.client.GlobalKey;
+import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
+
+/**
+ * Pop down button for header line in change screen.
+ *
+ * This class implements a button that on click opens a pop down panel with the
+ * provided widget, similar to the "Patch Sets", "Download" or "Included In" pop
+ * down panels on the change screen.
+ *
+ * This class can *only* be used within a panel that extends the header line of
+ * the change screen, but will not work standalone.
+ */
+public class PopDownButton extends Button {
+  private final Widget widget;
+  private PopupPanel popup;
+
+  public PopDownButton(String text, Widget widget) {
+    // Create Button with inner div. This is required to get proper styling
+    // in the context of the change screen.
+    super((new SafeHtmlBuilder()).openDiv().append(text).closeDiv());
+    getElement().removeClassName("gwt-Button");
+    addClickHandler(new ClickHandler() {
+      @Override
+      public void onClick(ClickEvent event) {
+        show();
+      }
+    });
+    this.widget = widget;
+  }
+
+  private void show() {
+    if (popup != null) {
+      getElement().getStyle().clearFontWeight();
+      popup.hide();
+      return;
+    }
+
+    final Widget relativeTo = getParent();
+    final PopupPanel p = new PopupPanel(true) {
+      @Override
+      public void setPopupPosition(int left, int top) {
+        top -= Document.get().getBodyOffsetTop();
+
+        int w = Window.getScrollLeft() + Window.getClientWidth();
+        int r = relativeTo.getAbsoluteLeft() + relativeTo.getOffsetWidth();
+        int right = w - r;
+        Style style = getElement().getStyle();
+        style.clearProperty("left");
+        style.setPropertyPx("right", right);
+        style.setPropertyPx("top", top);
+      }
+    };
+    Style popupStyle = p.getElement().getStyle();
+    popupStyle.setBorderWidth(0, Unit.PX);
+    popupStyle.setBackgroundColor("#EEEEEE");
+    p.addAutoHidePartner(getElement());
+    p.addCloseHandler(new CloseHandler<PopupPanel>() {
+      @Override
+      public void onClose(CloseEvent<PopupPanel> event) {
+        if (popup == p) {
+          getElement().getStyle().clearFontWeight();
+          popup = null;
+        }
+      }
+    });
+    p.add(widget);
+    p.showRelativeTo(relativeTo);
+    GlobalKey.dialog(p);
+    getElement().getStyle().setFontWeight(FontWeight.BOLD);
+    popup = p;
+  }
+}