Advertisement
mitrakov

DoubleBox with numeric key handling in GWT

Jul 29th, 2019
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package ru.mitrakov.self.adsbidder.client.ui;
  2.  
  3. import java.util.*;
  4. import java.text.ParseException;
  5. import com.google.gwt.dom.client.Document;
  6. import com.google.gwt.event.dom.client.KeyDownHandler;
  7. import com.google.gwt.i18n.client.NumberFormat;
  8. import com.google.gwt.text.shared.AbstractRenderer;
  9. import com.google.gwt.user.client.ui.ValueBox;
  10. import static com.google.gwt.event.dom.client.KeyCodes.*;
  11.  
  12. @SuppressWarnings("WeakerAccess")
  13. public class DoubleBoxEx extends ValueBox<Double> {
  14.     static protected NumberFormat renderFormat = NumberFormat.getFormat("####.##");
  15.     static protected NumberFormat parseFormat = NumberFormat.getFormat("####.##");
  16.  
  17.     protected KeyDownHandler doubleHandler() {
  18.         final Set<Integer> allowedKeys = new HashSet<>(Arrays.asList(
  19.                 KEY_ONE, KEY_NUM_ONE,
  20.                 KEY_TWO, KEY_NUM_TWO,
  21.                 KEY_THREE, KEY_NUM_THREE,
  22.                 KEY_FOUR, KEY_NUM_FOUR,
  23.                 KEY_FIVE, KEY_NUM_FIVE,
  24.                 KEY_SIX, KEY_NUM_SIX,
  25.                 KEY_SEVEN, KEY_NUM_SEVEN,
  26.                 KEY_EIGHT, KEY_NUM_EIGHT,
  27.                 KEY_NINE, KEY_NUM_NINE,
  28.                 KEY_ZERO, KEY_NUM_ZERO,
  29.                 KEY_LEFT, KEY_RIGHT,
  30.                 KEY_END, KEY_HOME,
  31.                 KEY_DELETE, KEY_BACKSPACE,
  32.                 KEY_ENTER, KEY_MAC_ENTER,
  33.                 KEY_TAB,
  34.                 KEY_NUM_MINUS, 173, // 173 is "-"
  35.                 KEY_NUM_PERIOD, 190 // 190 is "."
  36.                 // note that we filter out code 188 (",") because it's a group separator (1,000,000) that may confuse users
  37.         ));
  38.         return event -> {
  39.             boolean isAllowedKey = allowedKeys.contains(event.getNativeKeyCode());
  40.             boolean isPaste1 = event.isControlKeyDown() && event.getNativeKeyCode() == KEY_V;
  41.             boolean isPaste2 = event.isShiftKeyDown() && event.getNativeKeyCode() == KEY_INSERT;
  42.             if (!isAllowedKey && !isPaste1 && !isPaste2)
  43.                 cancelKey();
  44.             // note that mentioned above handling is not exhaustive: user still can copy-paste using context menu
  45.         };
  46.     }
  47.  
  48.     public DoubleBoxEx() {
  49.         super(Document.get().createTextInputElement(), new AbstractRenderer<Double>() {
  50.                 public String render(Double value) { return renderFormat.format(value); }
  51.             }, text -> {
  52.                 try {
  53.                     return parseFormat.parse(text.toString().replace(',', '.'));
  54.                 } catch (NumberFormatException e) {
  55.                     throw new ParseException(e.getMessage(), 0); // throwing is necessary to get ValueBox<T> working
  56.                 }
  57.             }
  58.         );
  59.  
  60.         setStyleName("am-DoubleBox");
  61.         addKeyDownHandler(doubleHandler());
  62.         getElement().setPropertyString("placeholder", "0.0");
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement