All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.mtwidgets.client.ui.widget.AbstractPhoneNumberRestSuggestBox Maven / Gradle / Ivy

Go to download

A set of widgets and handlers using server calls for gwt applications using gwt-bean-validators-spring-gwtp.

There is a newer version: 2.3.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package de.knightsoftnet.mtwidgets.client.ui.widget;

import de.knightsoftnet.validators.client.rest.helper.FutureResult;
import de.knightsoftnet.validators.shared.data.ValueWithPos;
import de.knightsoftnet.validators.shared.data.ValueWithPosAndCountry;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.SuggestOracle;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * abstract phone number suggest widget with rest calls.
 *
 * @author Manfred Tremmel
 *
 */
public abstract class AbstractPhoneNumberRestSuggestBox extends AbstractFormatingSuggestBox {

  protected final AsyncCallback> callback;
  protected HasValue countryCodeField;

  /**
   * cache map.
   */
  final LoadingCache, FutureResult>> cache;


  /**
   * default constructor.
   */
  public AbstractPhoneNumberRestSuggestBox(final SuggestOracle poracle) {
    super(poracle);
    this.callback = new AsyncCallback>() {

      @Override
      public void onFailure(final Throwable pcaught) {
        GWT.log(pcaught.getMessage(), pcaught);
      }

      @Override
      public void onSuccess(final ValueWithPos presponse) {
        if (presponse != null && StringUtils.isNotEmpty(presponse.getValue())) {
          AbstractPhoneNumberRestSuggestBox.this.setTextWithPos(presponse);
        }
      }

    };
    this.cache =
        CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.DAYS).build(
            new CacheLoader, FutureResult>>() {

              @Override
              public FutureResult> load(
                  final ValueWithPosAndCountry pkey) {
                final FutureResult> result = new FutureResult<>();
                result.addCallback(AbstractPhoneNumberRestSuggestBox.this.callback);
                try {
                  AbstractPhoneNumberRestSuggestBox.this.formatValue(pkey, result);
                } catch (final ExecutionException e) {
                  GWT.log(e.getMessage(), e);
                }
                return result;
              }
            });
  }

  @Override
  public void formatValue(final ValueWithPos pvalue) {
    if (pvalue == null || StringUtils.isEmpty(pvalue.getValue())) {
      this.setValue(StringUtils.EMPTY);
    } else {
      final ValueWithPosAndCountry unformatedEntry = new ValueWithPosAndCountry(
          pvalue.getValue(), pvalue.getPos(), Objects.toString(this.countryCodeField.getValue()),
          LocaleInfo.getCurrentLocale().getLocaleName());
      try {
        final FutureResult> result = this.cache.get(unformatedEntry);
        if (result.isDone()) {
          this.setTextWithPos(result.get());
        }
      } catch (final ExecutionException e) {
        GWT.log(e.getMessage(), e);
      }
    }
  }

  public abstract void formatValue(ValueWithPosAndCountry pkey,
      FutureResult> presult) throws ExecutionException;

  /**
   * set reference to a field which contains the country code.
   *
   * @param pcountryCodeField field which contains the country code
   */
  public final void setCountryCodeReferenceField(final HasValue pcountryCodeField) {
    this.countryCodeField = pcountryCodeField;
  }

  @Override
  public boolean isAllowedCharacter(final char pcharacter) {
    return pcharacter >= '0' && pcharacter <= '9' || this.isFormatingCharacter(pcharacter);
  }

  @Override
  public boolean isCharacterToReplace(final char pcharacter) {
    return false;
  }

  @Override
  public char replaceCharacter(final char pcharacter) {
    return pcharacter;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy