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

Commit

Permalink
add global key shortcut handler and some shortcuts for search page. A…
Browse files Browse the repository at this point in the history
…lt+K to show shortcuts.
  • Loading branch information
davidmason committed May 27, 2012
1 parent e222b42 commit 49d66e5
Show file tree
Hide file tree
Showing 19 changed files with 957 additions and 36 deletions.
Expand Up @@ -60,14 +60,6 @@ public interface CheckKey
static final int KEY_4 = 52;
static final int KEY_4_NUM = 37;

public static enum Context
{
/**
* Edit: InlineTargetCellEditor Navigation: TableEditorPresenter
*/
Navigation, Edit;
}

void init(NativeEvent event);


Expand Down
Expand Up @@ -20,6 +20,8 @@
*/
package org.zanata.webtrans.client.editor;

import org.zanata.webtrans.client.keys.ShortcutContext;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;

Expand All @@ -32,11 +34,11 @@
**/
public final class CheckKeyImpl implements CheckKey
{
private Context context;
private ShortcutContext context;
private int keyCode;
private boolean shiftKey, altKey, ctrlKey;

public CheckKeyImpl(Context context)
public CheckKeyImpl(ShortcutContext context)
{
this.context = context;
}
Expand Down Expand Up @@ -71,7 +73,7 @@ public boolean isNextStateEntryKey()
@Override
public boolean isPreviousEntryKey()
{
if (context == Context.Edit)
if (context == ShortcutContext.Edit)
{
return altKey && (keyCode == KeyCodes.KEY_UP || keyCode == KEY_J);
}
Expand All @@ -84,7 +86,7 @@ public boolean isPreviousEntryKey()
@Override
public boolean isNextEntryKey()
{
if (context == Context.Edit)
if (context == ShortcutContext.Edit)
{
return altKey && (keyCode == KeyCodes.KEY_DOWN || keyCode == KEY_K);
}
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.zanata.webtrans.client.events.TransUnitEditEventHandler;
import org.zanata.webtrans.client.events.UserConfigChangeEvent;
import org.zanata.webtrans.client.events.UserConfigChangeHandler;
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.presenter.SourceContentsPresenter;
import org.zanata.webtrans.client.presenter.UserConfigHolder;
import org.zanata.webtrans.client.resources.TableEditorMessages;
Expand Down Expand Up @@ -118,7 +119,7 @@ public TargetContentsPresenter(Provider<TargetContentsDisplay> displayProvider,
this.identity = identity;
this.dispatcher = dispatcher;

checkKey = new CheckKeyImpl(CheckKeyImpl.Context.Edit);
checkKey = new CheckKeyImpl(ShortcutContext.Edit);
eventBus.addHandler(UserConfigChangeEvent.getType(), this);
eventBus.addHandler(RequestValidationEvent.getType(), this);
eventBus.addHandler(InsertStringInEditorEvent.getType(), this);
Expand Down
@@ -0,0 +1,78 @@
/*
* Copyright 2012, 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.events;

import com.google.gwt.event.shared.GwtEvent;

/**
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a> *
*/
public class KeyShortcutEvent extends GwtEvent<KeyShortcutEventHandler>
{

private final int modifiers;
private final int keyCode;

public int getModifiers()
{
return modifiers;
}

public int getKeyCode()
{
return keyCode;
}

public KeyShortcutEvent(int modifiers, int keyCode)
{
this.modifiers = modifiers;
this.keyCode = keyCode;
}

@Override
protected void dispatch(KeyShortcutEventHandler handler)
{
handler.onKeyShortcut(this);
}


/**
* Handler type.
*/
private static Type<KeyShortcutEventHandler> TYPE;

/**
* Gets the type associated with this event.
*
* @return returns the handler type
*/
public static Type<KeyShortcutEventHandler> getType()
{
return TYPE != null ? TYPE : (TYPE = new Type<KeyShortcutEventHandler>());
}

@Override
public Type<KeyShortcutEventHandler> getAssociatedType()
{
return getType();
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2012, 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.events;

import com.google.gwt.event.shared.EventHandler;

/**
*
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a> *
*
* @see KeyShortcutEvent
*/
public interface KeyShortcutEventHandler extends EventHandler
{
void onKeyShortcut(KeyShortcutEvent event);
}
Expand Up @@ -49,6 +49,7 @@
import org.zanata.webtrans.client.presenter.DocumentListPresenter;
import org.zanata.webtrans.client.presenter.GlossaryDetailsPresenter;
import org.zanata.webtrans.client.presenter.GlossaryPresenter;
import org.zanata.webtrans.client.presenter.KeyShortcutPresenter;
import org.zanata.webtrans.client.presenter.NotificationPresenter;
import org.zanata.webtrans.client.presenter.OptionsPanelPresenter;
import org.zanata.webtrans.client.presenter.SearchResultsPresenter;
Expand All @@ -72,6 +73,7 @@
import org.zanata.webtrans.client.view.DocumentListView;
import org.zanata.webtrans.client.view.GlossaryDetailsView;
import org.zanata.webtrans.client.view.GlossaryView;
import org.zanata.webtrans.client.view.KeyShortcutView;
import org.zanata.webtrans.client.view.NotificationView;
import org.zanata.webtrans.client.view.SearchResultsView;
import org.zanata.webtrans.client.view.TransMemoryDetailsView;
Expand Down Expand Up @@ -103,6 +105,7 @@ protected void configure()
bind(ValidationService.class).in(Singleton.class);

bindPresenter(AppPresenter.class, AppPresenter.Display.class, AppView.class);
bindPresenter(KeyShortcutPresenter.class, KeyShortcutPresenter.Display.class, KeyShortcutView.class);
bindPresenter(DocumentListPresenter.class, DocumentListPresenter.Display.class, DocumentListView.class);
bindPresenter(SearchResultsPresenter.class, SearchResultsPresenter.Display.class, SearchResultsView.class);
bindPresenter(TranslationPresenter.class, TranslationPresenter.Display.class, TranslationView.class);
Expand Down
@@ -0,0 +1,140 @@
/*
* Copyright 2012, 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;

import org.zanata.webtrans.client.events.KeyShortcutEventHandler;
import org.zanata.webtrans.client.presenter.KeyShortcutPresenter;

/**
* Represents a key shortcut for registration with {@link KeyShortcutPresenter}.
*
* @author David Mason, <a
* href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
public class KeyShortcut
{
public static final int ALT_KEY = 0x1;
public static final int SHIFT_KEY = 0x2;
public static final int CTRL_KEY = 0x4;
public static final int META_KEY = 0x8;
public static final int SHIFT_ALT_KEYS = ALT_KEY | SHIFT_KEY;

private final int modifiers;
private final int keyCode;
private final ShortcutContext context;
private String description;
private final KeyShortcutEventHandler handler;

/**
* Construct a KeyShortcut.
*
* @param modifiers keys such as Shift and Alt that must be depressed for the
* shortcut to fire.
* <p>
* Use {@link #ALT_KEY}, {@link #SHIFT_KEY},
* {@link #SHIFT_ALT_KEYS}, {@link #META_KEY} and {@link #CTRL_KEY}
* to generate this. ( e.g. {@code} CTRL_KEY | ALT_KEY )
* </p>
* @param keyCode the integer code for the key.
* <p>
* This may be an uppercase character, but results may vary so test
* thoroughly in the targeted browsers.
* </p>
* <p>
* Note that for keypress events, the key code depends on Shift and
* CapsLock and will give the lowercase or uppercase ASCII code as
* expected. keydown and keyup events appear always to give the
* uppercase key code (keydown is currently used for all shortcuts.
* </p>
* @param context see
* {@link KeyShortcutPresenter#setContextActive(ShortcutContext, boolean)}
* @param description shown to the user in the key shortcut summary pane
*/
public KeyShortcut(int modifiers, int keyCode, ShortcutContext context, String description, KeyShortcutEventHandler handler)
{
this.modifiers = modifiers;
this.keyCode = keyCode;
this.context = context;
this.description = description;
this.handler = handler;
}

public int getModifiers()
{
return modifiers;
}

public int getKeyCode()
{
return keyCode;
}

public ShortcutContext getContext()
{
return context;
}

public String getDescription()
{
return description;
}

public KeyShortcutEventHandler getHandler()
{
return handler;
}

/**
* Return a hash for just the user input part of the shortcut, without
* context.
*
* @return a hash that is unique for a set of modifiers + key code
*/
public int keysHash()
{
return keyCode * 8 + modifiers;
}

@Override
public int hashCode()
{
int hash = context.ordinal();
hash = hash * 256 + keyCode;
hash = hash * 8 + modifiers;
return hash;
}

/**
* Two {@link KeyShortcut} objects are equal if they have the same modifier
* keys, key code and context.
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (!(obj instanceof KeyShortcut))
return false;
KeyShortcut other = (KeyShortcut) obj;
return modifiers == other.modifiers && keyCode == other.keyCode && context == other.context;
}
}

0 comments on commit 49d66e5

Please sign in to comment.