]> source.dussan.org Git - vaadin-framework.git/commitdiff
Makes Window and Panel Focusable. #4384
authorJohn Alhroos <john.ahlroos@itmill.com>
Fri, 11 Mar 2011 13:57:51 +0000 (13:57 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Fri, 11 Mar 2011 13:57:51 +0000 (13:57 +0000)
svn changeset:17727/svn branch:6.5

src/com/vaadin/terminal/gwt/client/ui/VPanel.java
src/com/vaadin/terminal/gwt/client/ui/VView.java
src/com/vaadin/terminal/gwt/client/ui/VWindow.java
src/com/vaadin/ui/Panel.java
src/com/vaadin/ui/Window.java

index e0ade0c4287f398e7e8b4bbb79a70005b23304f7..7960a741862c64f95f1b587775ef57820f6a30ba 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
 @ITMillApache2LicenseForJavaFiles@
  */
 
@@ -19,6 +19,7 @@ import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.BrowserInfo;
 import com.vaadin.terminal.gwt.client.Container;
+import com.vaadin.terminal.gwt.client.Focusable;
 import com.vaadin.terminal.gwt.client.Paintable;
 import com.vaadin.terminal.gwt.client.RenderInformation;
 import com.vaadin.terminal.gwt.client.RenderSpace;
@@ -27,7 +28,7 @@ import com.vaadin.terminal.gwt.client.Util;
 import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
 
 public class VPanel extends SimplePanel implements Container,
-        ShortcutActionHandlerOwner {
+        ShortcutActionHandlerOwner, Focusable {
 
     public static final String CLICK_EVENT_IDENTIFIER = "click";
     public static final String CLASSNAME = "v-panel";
@@ -100,7 +101,16 @@ public class VPanel extends SimplePanel implements Container,
         bottomDecoration.setClassName(CLASSNAME + "-deco");
 
         getElement().appendChild(captionWrap);
+
+        /*
+         * Make contentNode focusable only by using the setFocus() method. This
+         * behaviour can be changed by invoking setTabIndex() in the serverside
+         * implementation
+         */
+        contentNode.setTabIndex(-1);
+
         getElement().appendChild(contentNode);
+
         getElement().appendChild(bottomDecoration);
         setStyleName(CLASSNAME);
         DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
@@ -110,6 +120,30 @@ public class VPanel extends SimplePanel implements Container,
 
     }
 
+    /**
+     * Sets the keyboard focus on the Panel
+     * 
+     * @param focus
+     *            Should the panel have focus or not.
+     */
+    public void setFocus(boolean focus) {
+        if (focus) {
+            getContainerElement().focus();
+        } else {
+            getContainerElement().blur();
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.terminal.gwt.client.Focusable#focus()
+     */
+    public void focus() {
+        setFocus(true);
+
+    }
+
     @Override
     protected Element getContainerElement() {
         return contentNode;
@@ -229,6 +263,11 @@ public class VPanel extends SimplePanel implements Container,
         // scrollTop
         runHacks(false);
 
+        // And apply tab index
+        if (uidl.hasVariable("tabindex")) {
+            contentNode.setTabIndex(uidl.getIntVariable("tabindex"));
+        }
+
         rendering = false;
 
     }
index ea43deba9d3cf6bb44287edc1d2048ccb4c69d72..5d7cc1894599762b83230e07455cf0638c74ba8c 100644 (file)
@@ -43,7 +43,7 @@ import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHan
  *
  */
 public class VView extends SimplePanel implements Container, ResizeHandler,
-        Window.ClosingHandler, ShortcutActionHandlerOwner {
+        Window.ClosingHandler, ShortcutActionHandlerOwner, Focusable {
 
     private static final String CLASSNAME = "v-view";
 
@@ -110,6 +110,10 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
     public VView() {
         super();
         setStyleName(CLASSNAME);
+
+        // Allow focusing the view by using the focus() method, the view
+        // should not be in the document focus flow
+        getElement().setTabIndex(-1);
     }
 
     /**
@@ -691,9 +695,9 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
             DOM.setStyleAttribute(fElem, "position", "absolute");
             DOM.setStyleAttribute(fElem, "opacity", "0.1");
             DOM.appendChild(getElement(), fElem);
-            Util.focus(fElem);
+            fElem.focus();
         } else {
-            Util.focus(getElement());
+            getElement().focus();
         }
 
         parentFrame = getParentFrame();
@@ -703,4 +707,8 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
         return actionHandler;
     }
 
+    public void focus() {
+        getElement().focus();       
+    }
+
 }
index 68501ae082169ef25ee48b604cf147de72b9b108..c564a594234bd0f70d95e7eca55996c2f0f3ec5e 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
 @ITMillApache2LicenseForJavaFiles@
  */
 
@@ -35,6 +35,7 @@ import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.BrowserInfo;
 import com.vaadin.terminal.gwt.client.Container;
 import com.vaadin.terminal.gwt.client.EventId;
+import com.vaadin.terminal.gwt.client.Focusable;
 import com.vaadin.terminal.gwt.client.Paintable;
 import com.vaadin.terminal.gwt.client.RenderSpace;
 import com.vaadin.terminal.gwt.client.UIDL;
@@ -50,7 +51,7 @@ import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHan
  */
 public class VWindow extends VOverlay implements Container,
         ShortcutActionHandlerOwner, ScrollHandler, KeyDownHandler,
-        FocusHandler, BlurHandler, BeforeShortcutActionListener {
+        FocusHandler, BlurHandler, BeforeShortcutActionListener, Focusable {
 
     /**
      * Minimum allowed height of a window. This refers to the content area, not
@@ -1295,4 +1296,8 @@ public class VWindow extends VOverlay implements Container,
         // blur/focus )
     }
 
+    public void focus() {
+        contentPanel.focus();
+    }
+
 }
index 4d00cddfddb4e02f53059e86f45e12aa718d47b2..7ffa11a325415dbae576ee4dd1af00b9928fff5c 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
 @ITMillApache2LicenseForJavaFiles@
  */
 
@@ -17,6 +17,7 @@ import com.vaadin.terminal.PaintTarget;
 import com.vaadin.terminal.Scrollable;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
 import com.vaadin.terminal.gwt.client.ui.VPanel;
+import com.vaadin.ui.Component.Focusable;
 import com.vaadin.ui.themes.Reindeer;
 import com.vaadin.ui.themes.Runo;
 
@@ -32,7 +33,7 @@ import com.vaadin.ui.themes.Runo;
 @ClientWidget(VPanel.class)
 public class Panel extends AbstractComponentContainer implements Scrollable,
         ComponentContainer.ComponentAttachListener,
-        ComponentContainer.ComponentDetachListener, Action.Notifier {
+        ComponentContainer.ComponentDetachListener, Action.Notifier, Focusable {
 
     private static final String CLICK_EVENT = VPanel.CLICK_EVENT_IDENTIFIER;
 
@@ -74,6 +75,13 @@ public class Panel extends AbstractComponentContainer implements Scrollable,
      */
     protected ActionManager actionManager;
 
+    /**
+     * By default the Panel is not in the normal document focus flow and can
+     * only be focused by using the focus()-method. Change this to 0 if you want
+     * to have the Panel in the normal focus flow.
+     */
+    private int tabIndex = -1;
+
     /**
      * Creates a new empty panel. A VerticalLayout is used as content.
      */
@@ -234,6 +242,8 @@ public class Panel extends AbstractComponentContainer implements Scrollable,
     public void paintContent(PaintTarget target) throws PaintException {
         content.paint(target);
 
+        target.addVariable(this, "tabindex", getTabIndex());
+
         if (isScrollable()) {
             target.addVariable(this, "scrollLeft", getScrollLeft());
             target.addVariable(this, "scrollTop", getScrollTop());
@@ -582,4 +592,29 @@ public class Panel extends AbstractComponentContainer implements Scrollable,
         fireEvent(new ClickEvent(this, mouseDetails));
     }
 
+    
+    /**
+     * {@inheritDoc}
+     */
+    public int getTabIndex() {
+        return tabIndex;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setTabIndex(int tabIndex) {
+        this.tabIndex = tabIndex;
+        requestRepaint();
+    }
+
+    /**
+     * Moves keyboard focus to the component. {@see Focusable#focus()}
+     * 
+     */
+    @Override
+    public void focus() {
+        super.focus();
+    }
+
 }
index b31255cb26a59456cbe1edff231715b3bd13c321..5c2769c7567dac05874ed1b1726b5020f06e47fe 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
 @ITMillApache2LicenseForJavaFiles@
  */
 
@@ -1120,6 +1120,10 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         if (parent == null) {
             fireClose();
         } else {
+
+            // focus is restored to the parent window
+            parent.focus();
+            
             // subwindow is removed from parent
             parent.removeWindow(this);
         }
@@ -2089,7 +2093,7 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
      * <code>
      *  // within the window using helper
      *  subWindow.setCloseShortcut(KeyCode.ESCAPE, null);
-     *  
+     * 
      *  // or globally
      *  getWindow().addAction(new Window.CloseShortcut(subWindow, KeyCode.ESCAPE));
      * </code>
@@ -2180,4 +2184,24 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * If the window is a sub-window focusing will cause the sub-window to be
+     * brought on top of other sub-windows on gain keyboard focus.
+     */
+    @Override
+    public void focus() {
+        if (getParent() != null) {
+            /*
+             * When focusing a sub-window it basically means it should be
+             * brought to the front. Instead of just moving the keyboard focus
+             * we focus the window and bring it top-most.
+             */
+            bringToFront();
+        } else {
+            super.focus();
+        }
+    }
+
 }