Skip to content

Commit

Permalink
gwt: Add support for scrolling the popup contents and closing with th…
Browse files Browse the repository at this point in the history
…e ESC key.
  • Loading branch information
calin-iorgulescu committed Mar 10, 2015
1 parent 5ccdd56 commit 9e260f5
Showing 1 changed file with 109 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.core.client.Scheduler;

/**
* Wrapper over the classic PopupPanel
Expand All @@ -25,11 +29,12 @@ public class VmcheckerPopup extends PopupPanel {
private static VmcheckerConstants constants = GWT
.create(VmcheckerConstants.class);
private FlowPanel detailsPopupContainer = new FlowPanel();
private SimplePanel detailsPopupContent = new SimplePanel();
private ScrollPanel detailsPopupContent = new ScrollPanel();
private Anchor popupCloseButton = new Anchor();

public VmcheckerPopup() {
super(true, true);
// super(true, true);
super(true, false);
setup();
}

Expand All @@ -56,11 +61,112 @@ public void onClick(ClickEvent event) {

}

/*
private void focusContentPanel() {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
detailsPopupContent.getElement().focus();
}
});
}
*/

public void showContent(String htmlContent) {
detailsPopupContent.clear();
detailsPopupContent.add(new HTML(htmlContent));
center();
show();
// focusContentPanel();
}

@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {
super.onPreviewNativeEvent(event);
switch (event.getTypeInt()) {
case Event.ONKEYDOWN:
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
hide();
}
// XXX: It seems that on Chrome or Safari focusing the scrollpanel doesn't work.
// There's probably a better way to do this, but for now, it'll do.
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_DOWN) {
int scrollIncrement = (detailsPopupContent.getMaximumVerticalScrollPosition() -
detailsPopupContent.getMinimumVerticalScrollPosition()) / 200;
if (detailsPopupContent.getVerticalScrollPosition() <
detailsPopupContent.getMaximumVerticalScrollPosition()) {
detailsPopupContent.setVerticalScrollPosition(
detailsPopupContent.getVerticalScrollPosition() + scrollIncrement);
}
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_UP) {
int scrollIncrement = (detailsPopupContent.getMaximumVerticalScrollPosition() -
detailsPopupContent.getMinimumVerticalScrollPosition()) / 200;
if (detailsPopupContent.getVerticalScrollPosition() >
detailsPopupContent.getMinimumVerticalScrollPosition()) {
detailsPopupContent.setVerticalScrollPosition(
detailsPopupContent.getVerticalScrollPosition() - scrollIncrement);
}
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_PAGEDOWN) {
int scrollIncrement = (detailsPopupContent.getMaximumVerticalScrollPosition() -
detailsPopupContent.getMinimumVerticalScrollPosition()) / 20;
if (detailsPopupContent.getVerticalScrollPosition() <
detailsPopupContent.getMaximumVerticalScrollPosition()) {
detailsPopupContent.setVerticalScrollPosition(
detailsPopupContent.getVerticalScrollPosition() + scrollIncrement);
}
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_PAGEUP) {
int scrollIncrement = (detailsPopupContent.getMaximumVerticalScrollPosition() -
detailsPopupContent.getMinimumVerticalScrollPosition()) / 20;
if (detailsPopupContent.getVerticalScrollPosition() >
detailsPopupContent.getMinimumVerticalScrollPosition()) {
detailsPopupContent.setVerticalScrollPosition(
detailsPopupContent.getVerticalScrollPosition() - scrollIncrement);
}
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_END) {
detailsPopupContent.scrollToBottom();
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_HOME) {
detailsPopupContent.scrollToTop();
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_RIGHT) {
int scrollIncrement = (detailsPopupContent.getMaximumHorizontalScrollPosition() -
detailsPopupContent.getMinimumHorizontalScrollPosition()) / 20;
if (detailsPopupContent.getHorizontalScrollPosition() <
detailsPopupContent.getMaximumHorizontalScrollPosition()) {
detailsPopupContent.setHorizontalScrollPosition(
detailsPopupContent.getHorizontalScrollPosition() + scrollIncrement);
}
}

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_LEFT) {
int scrollIncrement = (detailsPopupContent.getMaximumHorizontalScrollPosition() -
detailsPopupContent.getMinimumHorizontalScrollPosition()) / 20;
if (detailsPopupContent.getHorizontalScrollPosition() >
detailsPopupContent.getMinimumHorizontalScrollPosition()) {
detailsPopupContent.setHorizontalScrollPosition(
detailsPopupContent.getHorizontalScrollPosition() - scrollIncrement);
}
}

break;
/*
case Event.ONCLICK:
// XXX: Kind of hack-ish, but if the scrollpanel doesn't have focus
// it won't allow scrolling with keys.
focusContentPanel();
break;
*/
}
}

}

0 comments on commit 9e260f5

Please sign in to comment.