Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Refactor pager widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jul 17, 2013
1 parent 72079ed commit 5c19bd2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 53 deletions.
Expand Up @@ -31,8 +31,6 @@
import org.zanata.webtrans.client.events.RefreshPageEvent;
import org.zanata.webtrans.client.view.TranslationEditorDisplay;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.inject.Inject;

public class TranslationEditorPresenter extends WidgetPresenter<TranslationEditorDisplay> implements PageChangeEventHandler, PageCountChangeEventHandler, TranslationEditorDisplay.Listener
Expand Down Expand Up @@ -68,14 +66,6 @@ protected void onBind()
transUnitNavigationPresenter.bind();
display.setTransUnitNavigation(transUnitNavigationPresenter.getDisplay().asWidget());

registerHandler(display.getPageNavigation().addValueChangeHandler(new ValueChangeHandler<Integer>()
{
@Override
public void onValueChange(ValueChangeEvent<Integer> event)
{
transUnitsTablePresenter.goToPage(event.getValue());
}
}));
registerHandler(eventBus.addHandler(PageChangeEvent.TYPE, this));
registerHandler(eventBus.addHandler(PageCountChangeEvent.TYPE, this));
}
Expand Down Expand Up @@ -133,9 +123,17 @@ public void onPagerBlurred()
{
editorKeyShortcuts.enableEditContext();
}

@Override
public void onPagerValueChanged(Integer pageNumber)
{
transUnitsTablePresenter.goToPage(pageNumber);
}

public void setReadOnly(boolean isReadOnly)
{
display.getResizeButton().setVisible(isReadOnly);
}


}
102 changes: 62 additions & 40 deletions zanata-war/src/main/java/org/zanata/webtrans/client/ui/Pager.java
@@ -1,3 +1,23 @@
/*
* Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.ui;

import org.zanata.webtrans.client.resources.Resources;
Expand All @@ -13,7 +33,6 @@
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
Expand All @@ -27,9 +46,14 @@
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
*/
public class Pager extends Composite implements HasPager
{

public static final int PAGECOUNT_UNKNOWN = -1;

private static PagerUiBinder uiBinder = GWT.create(PagerUiBinder.class);

interface PagerUiBinder extends UiBinder<Widget, Pager>
Expand Down Expand Up @@ -59,11 +83,10 @@ interface Styles extends CssResource
Styles style;

private int pageCount = PAGECOUNT_UNKNOWN;
private int minPageCount = 0;
private int currentPage;
private boolean isFocused;

public static final int PAGECOUNT_UNKNOWN = -1;

public Pager(final WebTransMessages messages, final Resources resources)
{
this.resources = resources;
Expand All @@ -87,29 +110,36 @@ public void onGotoPageBlur(BlurEvent event)
{
isFocused = false;
}

@Override
protected void onLoad()
@UiHandler("gotoPage")
public void onGotoPageKeyDown(KeyDownEvent event)
{
super.onLoad();
gotoPage.addKeyDownHandler(new KeyDownHandler()
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
{
@Override
public void onKeyDown(KeyDownEvent event)
try
{
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
int newValue = Integer.parseInt(gotoPage.getText());
if (newValue < minPageCount)
{
newValue = minPageCount;
}
else if(newValue > pageCount)
{
try
{
int newValue = Integer.parseInt(gotoPage.getText());
setValue(newValue);
}
catch (NumberFormatException nfe)
{
}
newValue = pageCount;
}
setValue(newValue);
}
});
catch (NumberFormatException nfe)
{
Log.error("Invalid page number entered");
}
}
}

@Override
protected void onLoad()
{
super.onLoad();

firstPage.addClickHandler(clickHandler);
lastPage.addClickHandler(clickHandler);
Expand All @@ -122,8 +152,8 @@ private void refresh()
{
String page = pageCount == PAGECOUNT_UNKNOWN ? "" : "of " + pageCount;
pageCountLabel.setText(page);
setEnabled(firstPage, currentPage != 1);
setEnabled(prevPage, currentPage != 1);
setEnabled(firstPage, currentPage > minPageCount);
setEnabled(prevPage, currentPage > minPageCount);
setEnabled(nextPage, currentPage != pageCount);
setEnabled(lastPage, currentPage != pageCount && pageCount != PAGECOUNT_UNKNOWN);

Expand All @@ -134,6 +164,7 @@ private void refresh()
public void setPageCount(int pageCount)
{
this.pageCount = pageCount;
this.minPageCount = pageCount <= 0 ? 0 : 1;
refresh();
}

Expand All @@ -158,7 +189,7 @@ public void setValue(Integer value)
@Override
public void setValue(Integer value, boolean fireEvents)
{
if (value != this.currentPage && (value > 0 && value <= pageCount))
if (value >= minPageCount && value <= pageCount)
{
this.currentPage = value;
if (fireEvents)
Expand All @@ -177,42 +208,33 @@ public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Integer> han

private final ClickHandler clickHandler = new ClickHandler()
{

@Override
public void onClick(ClickEvent event)
{
if (event.getSource() == firstPage)
Widget clickedButton = (Widget) event.getSource();
if (isButtonEnabled(clickedButton))
{
if (isButtonEnabled(firstPage))
if (clickedButton == firstPage)
{
setValue(1);
}
}
else if (event.getSource() == lastPage)
{
if (isButtonEnabled(lastPage))
else if (clickedButton == lastPage)
{
setValue(pageCount);
}
}
else if (event.getSource() == nextPage)
{
if (isButtonEnabled(nextPage))
else if (clickedButton == nextPage)
{
setValue(currentPage + 1);
}
}
else if (event.getSource() == prevPage)
{
if (isButtonEnabled(prevPage))
else if (clickedButton == prevPage)
{
setValue(currentPage - 1);
}
}
}
};

private boolean isButtonEnabled(InlineLabel button)
private boolean isButtonEnabled(Widget button)
{
return button.getStyleName().contains(style.enabled());
}
Expand Down
Expand Up @@ -6,11 +6,11 @@

import com.google.gwt.user.client.ui.HasVisibility;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;



/**
* @author aeng
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
*/
public interface TranslationEditorDisplay extends WidgetDisplay
Expand Down Expand Up @@ -38,6 +38,8 @@ interface Listener
void onPagerFocused();

void onPagerBlurred();

void onPagerValueChanged(Integer pageNumber);
}

HasVisibility getResizeButton();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
Expand Down Expand Up @@ -180,6 +181,12 @@ public void onPagerBlurred(BlurEvent event)
{
listener.onPagerBlurred();
}

@UiHandler("pager")
public void onPagerValueChanged(ValueChangeEvent<Integer> event)
{
listener.onPagerValueChanged(event.getValue());
}

@UiHandler("resize")
public void onResizeIconClick(ClickEvent event)
Expand Down
Expand Up @@ -40,7 +40,6 @@
import org.zanata.service.LocaleService;
import org.zanata.service.ValidationService;
import org.zanata.webtrans.server.ActionHandlerFor;
import org.zanata.webtrans.shared.model.ContentStateGroup;
import org.zanata.webtrans.shared.model.TransUnit;
import org.zanata.webtrans.shared.rpc.GetTransUnitList;
import org.zanata.webtrans.shared.rpc.GetTransUnitListResult;
Expand Down

0 comments on commit 5c19bd2

Please sign in to comment.