Revision: 8124
Author: [email protected]
Date: Wed May 12 21:49:14 2010
Log: Add a more user friendly popup for logging messages using the new logging framework

Review at http://gwt-code-reviews.appspot.com/508801

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8124

Added:
 /branches/2.1/user/src/com/google/gwt/logging/client/LoggingPopup.java
Deleted:
 /branches/2.1/user/src/com/google/gwt/logging/client/BasicLoggingPopup.java
Modified:
 /branches/2.1/user/src/com/google/gwt/logging/Logging.gwt.xml
 /branches/2.1/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java
 /branches/2.1/user/src/com/google/gwt/logging/client/LogConfiguration.java

=======================================
--- /dev/null
+++ /branches/2.1/user/src/com/google/gwt/logging/client/LoggingPopup.java Wed May 12 21:49:14 2010
@@ -0,0 +1,201 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * 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.google.gwt.logging.client;
+
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.dom.client.HasAllMouseHandlers;
+import com.google.gwt.event.dom.client.MouseDownEvent;
+import com.google.gwt.event.dom.client.MouseDownHandler;
+import com.google.gwt.event.dom.client.MouseMoveEvent;
+import com.google.gwt.event.dom.client.MouseMoveHandler;
+import com.google.gwt.event.dom.client.MouseUpEvent;
+import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.HasHorizontalAlignment;
+import com.google.gwt.user.client.ui.HasVerticalAlignment;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.PopupPanel;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.ScrollPanel;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * A simple popup to show log messages, which can be resized, minimized, and
+ * dragged to a different location.
+ */
+public class LoggingPopup extends PopupPanel {
+
+  /**
+   * Handles the logic to track click-drag movements with the mouse
+   */
+  private abstract class MouseDragHandler implements MouseMoveHandler,
+  MouseUpHandler, MouseDownHandler {
+    protected boolean dragging = false;
+    protected Widget dragHandle;
+    protected int dragStartX;
+    protected int dragStartY;
+
+    public MouseDragHandler(Widget dragHandle) {
+      this.dragHandle = dragHandle;
+      HasAllMouseHandlers hamh = (HasAllMouseHandlers) dragHandle;
+      hamh.addMouseDownHandler(this);
+      hamh.addMouseUpHandler(this);
+      hamh.addMouseMoveHandler(this);
+    }
+
+    public abstract void handleDrag(int absX, int absY);
+
+    public void onMouseDown(MouseDownEvent event) {
+      dragging = true;
+      DOM.setCapture(dragHandle.getElement());
+      dragStartX = event.getClientX();
+      dragStartY = event.getClientY();
+      DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
+    }
+
+    public void onMouseMove(MouseMoveEvent event) {
+      if (dragging) {
+        handleDrag(event.getClientX() - dragStartX,
+            event.getClientY() - dragStartY);
+        dragStartX = event.getClientX();
+        dragStartY = event.getClientY();
+      }
+    }
+
+    public void onMouseUp(MouseUpEvent event) {
+      dragging = false;
+      DOM.releaseCapture(dragHandle.getElement());
+    }
+  }
+
+  private static class ScrollPanelWithMinSize extends ScrollPanel {
+    private int minScrollPanelHeight = 100;
+    private int minScrollPanelWidth = 100;
+    private int scrollPanelHeight;
+    private int scrollPanelWidth;
+
+    public void incrementPixelSize(int width, int height) {
+      setPixelSize(scrollPanelWidth + width, scrollPanelHeight + height);
+    }
+
+    @Override
+    public void setPixelSize(int width, int height) {
+      super.setPixelSize(
+          scrollPanelWidth = Math.max(width, minScrollPanelWidth),
+          scrollPanelHeight = Math.max(height, minScrollPanelHeight));
+    }
+  }
+
+  private class WindowMoveHandler extends MouseDragHandler {
+    public WindowMoveHandler(Widget dragHandle) {
+      super(dragHandle);
+    }
+
+    @Override
+    public void handleDrag(int absX, int absY) {
+      Widget moveTarget = LoggingPopup.this;
+      RootPanel.get().setWidgetPosition(moveTarget,
+          moveTarget.getAbsoluteLeft() + absX,
+          moveTarget.getAbsoluteTop() + absY);
+    }
+  }
+
+  private class WindowResizeHandler extends MouseDragHandler {
+    public WindowResizeHandler(Widget dragHandle) {
+      super(dragHandle);
+    }
+
+    @Override
+    public void handleDrag(int absX, int absY) {
+      scrollPanel.incrementPixelSize(absX, absY);
+    }
+  }
+
+  private final HTML resizeIcon;
+  private final ScrollPanelWithMinSize scrollPanel;
+  private VerticalPanel logArea;
+
+  public LoggingPopup() {
+ // Since we don't want to pull UiBinder, or style sheets into the core GWT
+    // library, styling for this window is done pretty manually.
+    super(false, false);
+    VerticalPanel mainPanel = new VerticalPanel();
+    mainPanel.setBorderWidth(1);
+    mainPanel.getElement().getStyle().setBackgroundColor("white");
+
+ final HTML titleBar = new HTML("<div align=center><b>Logging</b></div>");
+    mainPanel.add(titleBar);
+    new WindowMoveHandler(titleBar);
+
+    scrollPanel = new ScrollPanelWithMinSize();
+    mainPanel.add(scrollPanel);
+    logArea = new VerticalPanel();
+    scrollPanel.setWidget(logArea);
+    scrollPanel.setPixelSize(
+        Window.getClientWidth() / 4, Window.getClientHeight() / 4);
+
+    HorizontalPanel bottomBar = new HorizontalPanel();
+    mainPanel.add(bottomBar);
+    bottomBar.setWidth("100%");
+    bottomBar.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
+
+    final Button maxmin = new Button();
+    // For some reason, if we pass in this text in the constructor, webkit
+    // browsers drop the "<", but if we set the text explicitly, it works
+    maxmin.setText("><");
+    bottomBar.add(maxmin);
+    maxmin.addClickHandler(new ClickHandler() {
+      public void onClick(ClickEvent event) {
+        if (maxmin.getText().equals("><")) {
+          maxmin.setText("<>");
+          scrollPanel.setVisible(false);
+          resizeIcon.setVisible(false);
+        } else {
+          scrollPanel.setVisible(true);
+          resizeIcon.setVisible(true);
+          maxmin.setText("><");
+        }
+      }
+    });
+
+    resizeIcon =
+      new HTML("<div style='font-size:200%; line-height:75%'>//</div>");
+    resizeIcon.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
+    bottomBar.add(resizeIcon);
+    new WindowResizeHandler(resizeIcon);
+
+    super.setWidget(mainPanel);
+    show();
+  }
+
+  @Override
+  public void add(Widget w) {
+    logArea.add(w);
+  }
+
+  @Override
+  public void setWidget(Widget w) {
+    logArea.clear();
+    logArea.add(w);
+  }
+
+}
=======================================
--- /branches/2.1/user/src/com/google/gwt/logging/client/BasicLoggingPopup.java Thu May 6 12:01:07 2010
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright 2010 Google Inc.
- *
- * 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.google.gwt.logging.client;
-
-import com.google.gwt.user.client.ui.DialogBox;
-import com.google.gwt.user.client.ui.ScrollPanel;
-import com.google.gwt.user.client.ui.VerticalPanel;
-import com.google.gwt.user.client.ui.Button;
-import com.google.gwt.user.client.ui.Widget;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.event.dom.client.ClickHandler;
-
-/**
- * A simple popup to show log messages
- */
-public class BasicLoggingPopup extends DialogBox {
-  private VerticalPanel v;
-
-  // TODO(unnurg): Make this into a popup that is less intrusive to the
-  // running of the application.
-  public BasicLoggingPopup() {
-    super(false, false);
-    ScrollPanel s = new ScrollPanel();
-    v = new VerticalPanel();
-    s.setWidget(v);
-    s.setAlwaysShowScrollBars(true);
-    s.setHeight("100px");
-    super.setWidget(s);
-    setText("Logging");
-    Button ok = new Button("OK");
-    ok.addClickHandler(new ClickHandler() {
-      public void onClick(ClickEvent event) {
-         hide();
-      }
-    });
-    v.add(ok);
-    show();
-  }
-
-  @Override
-  public void add(Widget w) {
-    v.add(w);
-  }
-
-  @Override
-  public void setWidget(Widget w) {
-    v.clear();
-    v.add(w);
-  }
-
-
-}
=======================================
--- /branches/2.1/user/src/com/google/gwt/logging/Logging.gwt.xml Thu May 6 12:01:07 2010 +++ /branches/2.1/user/src/com/google/gwt/logging/Logging.gwt.xml Wed May 12 21:49:14 2010
@@ -81,7 +81,7 @@
   </replace-with>
<define-property name="gwt.logging.popupHandler" values="ENABLED, DISABLED" />
   <replace-with class="com.google.gwt.logging.client.NullLoggingPopup">
- <when-type-is class="com.google.gwt.logging.client.BasicLoggingPopup" />
+    <when-type-is class="com.google.gwt.logging.client.LoggingPopup" />
     <when-property-is name="gwt.logging.popupHandler" value="DISABLED" />
   </replace-with>

=======================================
--- /branches/2.1/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java Thu May 6 12:01:07 2010 +++ /branches/2.1/user/src/com/google/gwt/logging/client/HtmlLogFormatter.java Wed May 12 21:49:14 2010
@@ -18,6 +18,7 @@

 import java.util.Date;
 import java.util.logging.Formatter;
+import java.util.logging.Level;
 import java.util.logging.LogRecord;

 /**
@@ -39,11 +40,14 @@
   protected String getHtmlPrefix(LogRecord event) {
     Date date = new Date(event.getMillis());
     StringBuilder prefix = new StringBuilder();
+    prefix.append("<span style='color:");
+    prefix.append(getColor(event.getLevel().intValue()));
+    prefix.append("'>");
     prefix.append("<code>");
     prefix.append(date.toString());
     prefix.append(" ");
     prefix.append(event.getLoggerName());
-    prefix.append("<br/>");
+    prefix.append(" ");
     prefix.append(event.getLevel().getName());
     prefix.append(": ");
     return prefix.toString();
@@ -51,9 +55,37 @@

   protected String getHtmlSuffix(LogRecord event) {
     // TODO(unnurg): output throwables correctly
-    return "</code>";
+    return "</code></span>";
   }

+  private String getColor(int logLevel) {
+    if (logLevel == Level.OFF.intValue()) {
+      return "#000"; // black
+    }
+    if (logLevel >= Level.SEVERE.intValue()) {
+      return "#F00"; // bright red
+    }
+    if (logLevel >= Level.WARNING.intValue()) {
+      return "#E56717"; // dark orange
+    }
+    if (logLevel >= Level.INFO.intValue()) {
+      return "#20b000"; // green
+    }
+    if (logLevel >= Level.CONFIG.intValue()) {
+      return "#2B60DE"; // blue
+    }
+    if (logLevel >= Level.FINE.intValue()) {
+      return "#F0F"; // purple
+    }
+    if (logLevel >= Level.FINER.intValue()) {
+      return "#F0F"; // purple
+    }
+    if (logLevel >= Level.FINEST.intValue()) {
+      return "#F0F"; // purple
+    }
+    return "#000"; // black
+  }
+
   // TODO(unnurg): There must be a cleaner way to do this...
   private String getEscapedMessage(LogRecord event) {
     String text = event.getMessage();
@@ -61,4 +93,5 @@
     text = text.replaceAll(">", "&gt;");
     return text;
   }
-}
+
+}
=======================================
--- /branches/2.1/user/src/com/google/gwt/logging/client/LogConfiguration.java Thu May 6 12:01:07 2010 +++ /branches/2.1/user/src/com/google/gwt/logging/client/LogConfiguration.java Wed May 12 21:49:14 2010
@@ -98,7 +98,7 @@
       addHandlerIfNotNull(l, firebug);
       Handler system = GWT.create(SystemLogHandler.class);
       addHandlerIfNotNull(l, system);
-      HasWidgets loggingWidget = GWT.create(BasicLoggingPopup.class);
+      HasWidgets loggingWidget = GWT.create(LoggingPopup.class);
       if (!(loggingWidget instanceof NullLoggingPopup)) {
         addHandlerIfNotNull(l, new HasWidgetsLogHandler(loggingWidget));
       }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to