Package com.daikit.daikit4gxt.client.ui.fields

Source Code of com.daikit.daikit4gxt.client.ui.fields.MyReferenceSelector

/**
* Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
*
*         Project home : http://code.daikit.com/daikit4gxt
*
* Licensed 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 com.daikit.daikit4gxt.client.ui.fields;

import java.io.Serializable;
import java.util.Set;

import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.model.labelprovider.MyAsSimpleStringLabelProvider;
import com.daikit.daikit4gxt.client.ui.component.Spacer;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent.HasReferenceSelectorHandlers;
import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent.ReferenceSelectorHandler;
import com.daikit.daikit4gxt.client.ui.forms.AbstractDkHideableAdapterField;
import com.daikit.daikit4gxt.client.ui.popup.MyReferenceSelectorPopup;
import com.daikit.daikit4gxt.client.ui.popup.MyReferenceSelectorPopup.MyReferenceSelectorPopupListener;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.data.shared.Converter;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;


/**
* A reference selector. Field with right button allowing to open a reference selector popup
*
* @author tcaselli
* @version $Revision$ Last modifier: $Author$ Last commit: $Date$
* @param <T>
*/
public abstract class MyReferenceSelector<T extends Serializable> extends AbstractDkHideableAdapterField<T> implements
    Converter<T, String>, MyReferenceSelectorPopupListener<T>, HasReferenceSelectorHandlers<T>
{

  @Ignore
  private TextField textField;
  @Ignore
  private TextButton button;
  private LabelProvider<T> labelProvider;
  private boolean enabled = true;
  private T value;

  /**
   * Constructor
   */
  public MyReferenceSelector()
  {
    this(35);
  }

  /**
   * Constructor with custom buttonWidth
   *
   * @param buttonWidth
   */
  public MyReferenceSelector(final int buttonWidth)
  {
    super(null);
    final HorizontalLayoutContainer panel = new HorizontalLayoutContainer();
    textField = new TextField();
    textField.setEmptyText(DkMain.i18n().label_reference_selector_empty_text());
    button = new TextButton(DkMain.i18n().label_reference_selector(), new SelectEvent.SelectHandler()
    {
      @Override
      public void onSelect(final SelectEvent event)
      {
        onButtonClicked(value);
      }
    });
    button.setScale(ButtonScale.NONE);
    panel.add(textField, new HorizontalLayoutData(1, -1));
    panel.add(new Spacer(), new HorizontalLayoutData(5, -1));
    panel.add(button, new HorizontalLayoutData(buttonWidth, 18));
    setWidget(panel);

    labelProvider = new MyAsSimpleStringLabelProvider<T>();
  }

  @Override
  public T getValue()
  {
    return value;
  }

  @Override
  public void setValue(final T value)
  {
    this.value = value;
    textField.setValue(convertModelValue(value));
    onValuSet(value);
    fireEvent(new ReferenceSelectorEvent<T>(value));
  }

  /**
   * Should not be used
   */
  @Deprecated
  @Override
  public T convertFieldValue(final String object)
  {
    return value;
  }

  protected void onButtonClicked(final T currentValue)
  {
    final MyReferenceSelectorPopup<T> referenceSelectorPopup = getReferenceSelectorPopup();
    if (referenceSelectorPopup != null)
    {
      referenceSelectorPopup.setSelectionMultiple(false);
      referenceSelectorPopup.show(this);
    }
  }

  @Override
  public String convertModelValue(final T value)
  {
    return value == null ? null : labelProvider.getLabel(value);
  };

  protected abstract MyReferenceSelectorPopup<T> getReferenceSelectorPopup();

  protected void onValuSet(final T value)
  {
    // Nothing done by default
  }

  @Override
  public void setEnabled(final boolean enabled)
  {
    textField.setEnabled(enabled);
    button.setEnabled(enabled);
    this.enabled = enabled;
  }

  @Override
  public boolean isEnabled()
  {
    return enabled;
  }

  /**
   * Set the button Text
   *
   * @param buttonText
   *           the new button text
   */
  public void setButtonText(final String buttonText)
  {
    button.setText(buttonText);
  }

  @Override
  public void onSelectionOk(final Set<T> selectedItems)
  {
    if (!selectedItems.isEmpty())
    {
      this.setValue(selectedItems.iterator().next());
    }
  }

  /**
   * @return the labelProvider
   */
  public LabelProvider<T> getLabelProvider()
  {
    return labelProvider;
  }

  /**
   * @param labelProvider
   *           the labelProvider to set
   */
  public void setLabelProvider(final LabelProvider<T> labelProvider)
  {
    this.labelProvider = labelProvider;
  }

  /**
   * Add an handler for selection
   */
  @Override
  public HandlerRegistration addReferenceSelectorHandler(final ReferenceSelectorHandler<T> handler)
  {
    return addHandler(handler, ReferenceSelectorEvent.getType());
  }
}
TOP

Related Classes of com.daikit.daikit4gxt.client.ui.fields.MyReferenceSelector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.