From f7a316ebb3461a7579f9b4dc872e3d4f8da5390c Mon Sep 17 00:00:00 2001 From: Jonatan Kronqvist Date: Fri, 1 Apr 2011 07:21:52 +0000 Subject: [PATCH] Merge branch '6197-table-scrolls-on-focus' svn changeset:18068/svn branch:6.5 --- .../terminal/gwt/client/ui/VScrollTable.java | 123 ++++++++++++------ .../table/TableScrollsOnSelection.java | 6 +- 2 files changed, 86 insertions(+), 43 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java index 00a711c389..a607b4617f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VScrollTable.java @@ -881,11 +881,11 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, if (focusedRow != null) { if (!focusedRow.isAttached()) { - // focused row has orphaned, can't focus + // focused row has been orphaned, can't focus focusedRow = null; if (SELECT_MODE_SINGLE == selectMode && selectedRowKeys.size() > 0) { - // try to focusa row currently selected and in viewport + // try to focus a row currently selected and in viewport String selectedRowKey = selectedRowKeys.iterator().next(); if (selectedRowKey != null) { setRowFocus(getRenderedRowByKey(selectedRowKey)); @@ -3708,7 +3708,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, setElement(rowElement); DOM.sinkEvents(getElement(), Event.MOUSEEVENTS | Event.ONDBLCLICK | Event.ONCONTEXTMENU - | Event.ONKEYDOWN); + | Event.KEYEVENTS); } /** @@ -4007,10 +4007,11 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, case Event.ONDBLCLICK: handleClickEvent(event, targetTdOrTr); break; + case Event.ONMOUSEUP: mDown = false; handleClickEvent(event, targetTdOrTr); - scrollBodyPanel.setFocus(true); + // scrollBodyPanel.setFocus(true); if (event.getButton() == Event.BUTTON_LEFT && isSelectable()) { @@ -4094,6 +4095,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, sendSelectedRows(); } break; + case Event.ONMOUSEDOWN: setRowFocus(this); if (dragmode != 0 @@ -4170,11 +4172,59 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, event.stopPropagation(); } - break; + case Event.ONMOUSEOUT: mDown = false; break; + + /* + * The ONKEYDOWN and ONKEYUP are only run in IE, see + * #6197 + */ + case Event.ONKEYDOWN: + if (!enabled) { + // Cancel default keyboard events on a disabled + // Table (prevents scrolling) + event.preventDefault(); + } else { + if (handleNavigation( + event.getKeyCode(), + event.getCtrlKey() + || event.getMetaKey(), + event.getShiftKey())) { + navKeyDown = true; + event.preventDefault(); + } + startScrollingVelocityTimer(); + } + break; + + case Event.ONKEYUP: + int keyCode = event.getKeyCode(); + + if (!isFocusable()) { + cancelScrollingVelocityTimer(); + } else if (isNavigationKey(keyCode)) { + if (keyCode == getNavigationDownKey() + || keyCode == getNavigationUpKey()) { + /* + * in multiselect mode the server may still + * have value from previous page. Clear it + * unless doing multiselection or just + * moving focus. + */ + if (!event.getShiftKey() + && !event.getCtrlKey()) { + instructServerToForgetPreviousSelections(); + } + sendSelectedRows(); + } + cancelScrollingVelocityTimer(); + navKeyDown = false; + } + break; + default: break; } @@ -4470,14 +4520,14 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, selectedRowRanges.clear(); // also notify server that it clears all previous selections (the client // side does not know about the invisible ones) - instructServerToForgotPreviousSelections(); + instructServerToForgetPreviousSelections(); } /** * Used in multiselect mode when the client side knows that all selections * are in the next request. */ - private void instructServerToForgotPreviousSelections() { + private void instructServerToForgetPreviousSelections() { client.updateVariable(paintableId, "clearSelections", true, false); } @@ -5097,6 +5147,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, focusedRow = row; ensureRowIsVisible(row); + row.getElement().focus(); return true; } @@ -5443,16 +5494,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, event.preventDefault(); } - // Start the velocityTimer - if (scrollingVelocityTimer == null) { - scrollingVelocityTimer = new Timer() { - @Override - public void run() { - scrollingVelocity++; - } - }; - scrollingVelocityTimer.scheduleRepeating(100); - } + startScrollingVelocityTimer(); } } @@ -5476,16 +5518,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, event.preventDefault(); } - // Start the velocityTimer - if (scrollingVelocityTimer == null) { - scrollingVelocityTimer = new Timer() { - @Override - public void run() { - scrollingVelocity++; - } - }; - scrollingVelocityTimer.scheduleRepeating(100); - } + startScrollingVelocityTimer(); } } @@ -5620,12 +5653,7 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, int keyCode = event.getNativeKeyCode(); if (!isFocusable()) { - if (scrollingVelocityTimer != null) { - // Remove velocityTimer if it exists and the Table is disabled - scrollingVelocityTimer.cancel(); - scrollingVelocityTimer = null; - scrollingVelocity = 10; - } + cancelScrollingVelocityTimer(); } else if (isNavigationKey(keyCode)) { if (keyCode == getNavigationDownKey() || keyCode == getNavigationUpKey()) { @@ -5636,19 +5664,36 @@ public class VScrollTable extends FlowPanel implements Table, ScrollHandler, */ if (!event.getNativeEvent().getShiftKey() && !event.getNativeEvent().getCtrlKey()) { - instructServerToForgotPreviousSelections(); + instructServerToForgetPreviousSelections(); } sendSelectedRows(); } - if (scrollingVelocityTimer != null) { - scrollingVelocityTimer.cancel(); - scrollingVelocityTimer = null; - scrollingVelocity = 10; - } + cancelScrollingVelocityTimer(); navKeyDown = false; } } + public void startScrollingVelocityTimer() { + if (scrollingVelocityTimer == null) { + scrollingVelocityTimer = new Timer() { + @Override + public void run() { + scrollingVelocity++; + } + }; + scrollingVelocityTimer.scheduleRepeating(100); + } + } + + public void cancelScrollingVelocityTimer() { + if (scrollingVelocityTimer != null) { + // Remove velocityTimer if it exists and the Table is disabled + scrollingVelocityTimer.cancel(); + scrollingVelocityTimer = null; + scrollingVelocity = 10; + } + } + /** * * @param keyCode diff --git a/tests/src/com/vaadin/tests/components/table/TableScrollsOnSelection.java b/tests/src/com/vaadin/tests/components/table/TableScrollsOnSelection.java index 9d3aed00bb..c76fe05d47 100644 --- a/tests/src/com/vaadin/tests/components/table/TableScrollsOnSelection.java +++ b/tests/src/com/vaadin/tests/components/table/TableScrollsOnSelection.java @@ -4,14 +4,12 @@ import com.vaadin.data.Item; import com.vaadin.data.util.IndexedContainer; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Table; -import com.vaadin.ui.Window; public class TableScrollsOnSelection extends TestBase { @Override protected void setup() { - Window mainWindow = new Window("Playground Application"); - setMainWindow(mainWindow); + getMainWindow().getContent().setSizeUndefined(); IndexedContainer cont = new IndexedContainer(); cont.addContainerProperty("number", String.class, null); @@ -23,7 +21,7 @@ public class TableScrollsOnSelection extends TestBase { table.setPageLength(0); table.setContainerDataSource(cont); table.setSelectable(true); - mainWindow.addComponent(table); + addComponent(table); } @Override -- 2.39.5