From b7ea17498367d6727cd7149534ecca6f5156662e Mon Sep 17 00:00:00 2001 From: SirWayne Date: Tue, 21 Jun 2016 13:42:26 +0200 Subject: [PATCH] Add a shortcut modifier util class to handle cross platform functionality. Signed-off-by: SirWayne --- .../ui/common/table/AbstractTableLayout.java | 20 ++------- .../ui/utils/ShortCutModifierUtils.java | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/ShortCutModifierUtils.java diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/table/AbstractTableLayout.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/table/AbstractTableLayout.java index 905c5917c..11c0083ec 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/table/AbstractTableLayout.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/table/AbstractTableLayout.java @@ -9,12 +9,11 @@ package org.eclipse.hawkbit.ui.common.table; import org.eclipse.hawkbit.ui.common.detailslayout.AbstractTableDetailsLayout; +import org.eclipse.hawkbit.ui.utils.ShortCutModifierUtils; import com.vaadin.event.Action; import com.vaadin.event.Action.Handler; import com.vaadin.event.ShortcutAction; -import com.vaadin.server.Page; -import com.vaadin.server.WebBrowser; import com.vaadin.ui.Alignment; import com.vaadin.ui.Panel; import com.vaadin.ui.VerticalLayout; @@ -109,34 +108,21 @@ public abstract class AbstractTableLayout extends VerticalLayout { private static final String SELECT_ALL_TEXT = "Select All"; private final ShortcutAction selectAllAction = new ShortcutAction(SELECT_ALL_TEXT, ShortcutAction.KeyCode.A, - new int[] { ShortcutAction.ModifierKey.CTRL }); - - private final ShortcutAction selectAllMacAction = new ShortcutAction(SELECT_ALL_TEXT, ShortcutAction.KeyCode.A, - new int[] { ShortcutAction.ModifierKey.META }); + new int[] { ShortCutModifierUtils.getCtrlOrMetaModifier() }); private static final long serialVersionUID = 1L; @Override public void handleAction(final Action action, final Object sender, final Object target) { - if (!isSelecAllAction(action)) { + if (!selectAllAction.equals(action)) { return; } table.selectAll(); publishEvent(); } - private boolean isSelecAllAction(final Action action) { - return selectAllAction.equals(action) || selectAllMacAction.equals(action); - } - @Override public Action[] getActions(final Object target, final Object sender) { - - final WebBrowser webBrowser = Page.getCurrent().getWebBrowser(); - if (webBrowser.isMacOSX()) { - return new Action[] { selectAllMacAction }; - } - return new Action[] { selectAllAction }; } } diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/ShortCutModifierUtils.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/ShortCutModifierUtils.java new file mode 100644 index 000000000..b844caafd --- /dev/null +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/utils/ShortCutModifierUtils.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.ui.utils; + +import com.vaadin.event.ShortcutAction; +import com.vaadin.server.Page; +import com.vaadin.server.WebBrowser; + +/** + * On different systems there are different modifier for short cuts. This + * utility class handles the cross-platform functionality. + */ +public final class ShortCutModifierUtils { + + private ShortCutModifierUtils() { + + } + + /** + * Returns the ctrl or meta modifier depending on the platform. + * + * @return on mac return + * {@link com.vaadin.event.ShortcutAction.ModifierKey#META} other + * platform return + * {@link com.vaadin.event.ShortcutAction.ModifierKey#CTRL} + */ + public static int getCtrlOrMetaModifier() { + final WebBrowser webBrowser = Page.getCurrent().getWebBrowser(); + if (webBrowser.isMacOSX()) { + return ShortcutAction.ModifierKey.META; + } + + return ShortcutAction.ModifierKey.CTRL; + } +}