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

Commit

Permalink
rhbz961564 add attention mode timeout controlled by presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed May 14, 2013
1 parent e4fb4d3 commit f91b62c
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 40 deletions.
Expand Up @@ -38,6 +38,8 @@
import org.zanata.webtrans.client.history.WindowLocationImpl;
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.EventWrapperImpl;
import org.zanata.webtrans.client.keys.TimerFactory;
import org.zanata.webtrans.client.keys.TimerFactoryImpl;
import org.zanata.webtrans.client.presenter.AppPresenter;
import org.zanata.webtrans.client.presenter.AttentionKeyShortcutPresenter;
import org.zanata.webtrans.client.presenter.DocumentListOptionsPresenter;
Expand Down Expand Up @@ -162,13 +164,14 @@ protected void configure()
bindPresenter(NotificationPresenter.class, NotificationDisplay.class, NotificationView.class);
bindPresenter(TransUnitsTablePresenter.class, TransUnitsTableDisplay.class, TransUnitsTableView.class);
bindPresenter(SideMenuPresenter.class, SideMenuDisplay.class, SideMenuView.class);

bind(SourceContentsPresenter.class).in(Singleton.class);
bind(TargetContentsDisplay.class).to(TargetContentsView.class);
bind(SourceContentsDisplay.class).to(SourceContentsView.class);
bindPresenter(TransMemoryMergePresenter.class, TransMemoryMergePopupPanelDisplay.class, TransMemoryMergePopupPanelView.class);

bind(EventWrapper.class).to(EventWrapperImpl.class).in(Singleton.class);
bind(TimerFactory.class).to(TimerFactoryImpl.class).in(Singleton.class);
bind(NativeEvent.class).to(NativeEventImpl.class).in(Singleton.class);
bind(History.class).to(HistoryImpl.class).in(Singleton.class);
bind(Window.class).to(WindowImpl.class).in(Singleton.class);
Expand Down
@@ -0,0 +1,32 @@
/*
* 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.keys;

/**
* Used to pass behaviour for a new timer to a {@link TimerFactory}.
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
public interface TimedAction
{
void run();
}
@@ -0,0 +1,55 @@
/*
* 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.keys;

/**
* Wraps a {{@link com.google.gwt.user.client.Timer} to allow testing
* in a non-browser environment.
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
public class Timer
{

private com.google.gwt.user.client.Timer timer;

public Timer(com.google.gwt.user.client.Timer timer)
{
this.timer = timer;
}

/**
* @see {{@link com.google.gwt.user.client.Timer#cancel()}
*/
public void cancel()
{
timer.cancel();
}

/**
* @see {@link com.google.gwt.user.client.Timer#schedule(int)}
*/
public void schedule(int delayMillis)
{
timer.schedule(delayMillis);
}
}
@@ -0,0 +1,33 @@
/*
* 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.keys;

/**
* Factory for wrapped {@link com.google.gwt.user.client.Timer} to allow
* mocking for testing.
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
public interface TimerFactory
{
Timer create(TimedAction timedAction);
}
@@ -0,0 +1,43 @@
/*
* 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.keys;

/**
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
* @see TimerFactory
*/
public class TimerFactoryImpl implements TimerFactory
{

@Override
public Timer create(final TimedAction timedAction)
{
return new Timer(new com.google.gwt.user.client.Timer()
{
@Override
public void run()
{
timedAction.run();
}
});
}

}
Expand Up @@ -39,6 +39,9 @@
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.keys.SurplusKeyListener;
import org.zanata.webtrans.client.keys.TimedAction;
import org.zanata.webtrans.client.keys.Timer;
import org.zanata.webtrans.client.keys.TimerFactory;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;

Expand All @@ -64,10 +67,12 @@
*/
public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutDisplay>
{
private static final int ATTENTION_MODE_DURATION = 5000;

private final KeyShortcutManager shortcutManager;
private final KeyShortcutManager attentionKeyManager;
private boolean isAttentionMode;
private Timer attentionTimer;

private Map<ShortcutContext, Set<SurplusKeyListener>> surplusKeyMap;

Expand All @@ -76,6 +81,7 @@ public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutDisplay>
private WebTransMessages messages;

private final EventWrapper event;
private final TimerFactory timers;

// TODO unregister when user changes attention shortcut
private HandlerRegistration attentionShortcutHandle;
Expand All @@ -84,11 +90,13 @@ public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutDisplay>
public KeyShortcutPresenter(KeyShortcutDisplay display,
EventBus eventBus,
final WebTransMessages webTransMessages,
final EventWrapper event)
final EventWrapper event,
final TimerFactory timer)
{
super(display, eventBus);
this.messages = webTransMessages;
this.event = event;
this.timers = timer;

this.shortcutManager = new KeyShortcutManager(event)
{
Expand Down Expand Up @@ -152,11 +160,6 @@ protected void handleNonMatchedShortcut(NativeEvent evt, KeyShortcutEvent shortc
Log.warn("Unrecognized attention key input");
setAttentionMode(false);
}
else
{
// FIXME remove this logging
Log.info("Ignored non-key-down event in attention mode: " + event.getType(evt));
}
}
};
}
Expand Down Expand Up @@ -257,6 +260,15 @@ public void onKeyShortcut(KeyShortcutEvent event)
register(cancelAttentionShortcut);

// @formatter:on

Log.info("creating attention timer");
attentionTimer = timers.create(new TimedAction() {
@Override
public void run()
{
setAttentionMode(false);
}
});
}

@Override
Expand All @@ -271,19 +283,25 @@ protected void onRevealDisplay()

private void setAttentionMode(boolean active)
{
if (active)
{
// clobbers existing countdown
attentionTimer.schedule(ATTENTION_MODE_DURATION);
}
else
{
attentionTimer.cancel();
}

if (isAttentionMode != active)
{
isAttentionMode = active;
eventBus.fireEvent(new AttentionModeActivationEvent(active));
// TODO expose method for attention key presenter to get what it needs
// to respond properly to this event
}

// TODO (if active) reset attention mode timer

// TODO (on hide) presenter should be hidden at this point, with appropriate message
// Note: only do this if it was active when called with false, so that
// only 1 event is responded to.
// TODO if inactivating, attention key presenter shoud hide
}
}

public void setContextActive(ShortcutContext context, boolean active)
Expand Down
Expand Up @@ -230,28 +230,4 @@ public Widget asWidget()
return this;
}


// FIXME just keeping this stuff for quick reference making timer for AttentionShortcutPresenter

//import com.google.gwt.user.client.Timer;

// private Timer aliasKeyTimer = new Timer()
// {
// public void run()
// {
// listener.setAliasKeyListening(false);
// }
// };

// @Override
// public void cancelMetaKeyTimer()
// {
// aliasKeyTimer.cancel();
// }
//
// @Override
// public void startAliasKeyListen(int delayMillis)
// {
// aliasKeyTimer.schedule(delayMillis); // 5 seconds
// }
}
Expand Up @@ -35,14 +35,13 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.verification.Times;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.TimerFactory;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;

Expand Down Expand Up @@ -82,6 +81,8 @@ public class KeyShortcutPresenterTest
@Mock
private EventBus mockEventBus;
@Mock
private TimerFactory mockTimerFactory;
@Mock
private WebTransMessages mockMessages;

@Captor
Expand All @@ -95,7 +96,7 @@ public void beforeMethod()
MockitoAnnotations.initMocks(this);

keyShortcutPresenter = new KeyShortcutPresenter(mockDisplay,
mockEventBus, mockMessages, mockEventWrapper);
mockEventBus, mockMessages, mockEventWrapper, mockTimerFactory);

when(mockMessages.closeShortcutView()).thenReturn(TEST_MESSAGE_CLOSE_SHORTCUT_VIEW);
when(mockMessages.showAvailableKeyShortcuts()).thenReturn(TEST_MESSAGE_SHOW_AVAILABLE_KEY_SHORTCUTS);
Expand Down

0 comments on commit f91b62c

Please sign in to comment.