Package com.google.gwt.cell.client

Source Code of com.google.gwt.cell.client.DatePickerCell

/*
* Copyright 2010 Google Inc.
*
* 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.google.gwt.cell.client;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.datepicker.client.DatePicker;

import java.util.Date;

/**
* A {@link Cell} used to render and edit {@link Date}s. When a cell is selected
* by clicking on it, a {@link DatePicker} is popped up. When a date is selected
* using the DatePicker, the new date is passed to the
* {@link ValueUpdater#update update} method of the {@link ValueUpdater} that
* was passed to {@link #onBrowserEvent} for the click event. Note that this
* means that the call to ValueUpdater.update will occur after onBrowserEvent
* has returned. Pressing the 'escape' key dismisses the DatePicker popup
* without calling ValueUpdater.update.
*
* <p>
* Each DatePickerCell has a unique DatePicker popup associated with it; thus,
* if a single DatePickerCell is used as the cell for a column in a table, only
* one entry in that column will be editable at a given time.
* </p>
*
* <p>
* Note: This class is new and its interface subject to change.
* </p>
*/
public class DatePickerCell extends AbstractCell<Date> {

  private static final int ESCAPE = 27;

  private final DatePicker datePicker;
  private final DateTimeFormat format;
  private int offsetX = 10;
  private int offsetY = 10;
  private PopupPanel panel;
  private ValueUpdater<Date> valueUpdater;

  /**
   * Constructs a new DatePickerCell that uses the date/time format
   * given by {@link DateTimeFormat#getFullDateFormat}.
   */
  public DatePickerCell() {
    this(DateTimeFormat.getFullDateFormat());
  }

  /**
   * Constructs a new DatePickerCell that uses the given date/time format.
   */
  public DatePickerCell(DateTimeFormat format) {
    this.format = format;

    this.datePicker = new DatePicker();
    this.panel = new PopupPanel(true, true) {
      @Override
      protected void onPreviewNativeEvent(NativePreviewEvent event) {
        if (Event.ONKEYUP == event.getTypeInt()) {
          if (event.getNativeEvent().getKeyCode() == ESCAPE) {
            // Dismiss when escape is pressed
            panel.hide();
          }
        }
      }
    };
    panel.add(datePicker);

    // Hide the panel and call valueUpdater.update when a date is selected
    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
      public void onValueChange(ValueChangeEvent<Date> event) {
        panel.hide();
        valueUpdater.update(event.getValue());
      }
    });
  }

  @Override
  public Object onBrowserEvent(final Element parent, Date value, Object viewData,
      NativeEvent event, ValueUpdater<Date> valueUpdater) {
    if (event.getType().equals("click")) {
      this.valueUpdater = valueUpdater;

      datePicker.setCurrentMonth(value);
      datePicker.setValue(value);
      panel.setPopupPositionAndShow(new PositionCallback() {
        public void setPosition(int offsetWidth, int offsetHeight) {
          panel.setPopupPosition(parent.getAbsoluteLeft() + offsetX,
              parent.getAbsoluteTop() + offsetY);
        }
      });
    }
    return viewData;
  }

  @Override
  public void render(Date value, Object viewData, StringBuilder sb) {
    if (value != null) {
      sb.append(format.format(value));
    }
  }
}
TOP

Related Classes of com.google.gwt.cell.client.DatePickerCell

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.