package org.shortpasta.smartgwttest.portal.client.layout;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.DateRangeItem;
import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.types.DateFieldLayout;
import com.smartgwt.client.data.DateRange;
import com.smartgwt.client.data.RelativeDate;

/**
 * ShortPasta Foundation
 * http://www.shortpasta.org
 * Copyright 2009 and beyond, Sal Ingrilli at the ShortPasta Software Foundation
 * <p/>
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation as long as:
 * 1. You credit the original author somewhere within your product or website
 * 2. The credit is easily reachable and not burried deep
 * 3. Your end-user can easily see it
 * 4. You register your name (optional) and company/group/org name (required)
 * at http://www.shortpasta.org
 * 5. You do all of the above within 4 weeks of integrating this software
 * 6. You contribute feedback, fixes, and requests for features
 * <p/>
 * If/when you derive a commercial gain from using this software
 * please donate at http://www.shortpasta.org
 * <p/>
 * If prefer or require, contact the author specified above to:
 * 1. Release you from the above requirements
 * 2. Acquire a commercial license
 * 3. Purchase a support contract
 * 4. Request a different license
 * 5. Anything else
 * <p/>
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, similarly
 * to how this is described in the GNU Lesser General Public License.
 * <p/>
 * User: Sal Ingrilli
 * Date: May 5, 2017
 * Time: 8:28:16 PM
 */
public class TestDateRangeItem implements com.google.gwt.core.client.EntryPoint {

  /**
   * The EntryPoint interface
   */
  public void onModuleLoad () {

    // Using:
    // GWT: 2.8.1
    // SmartGWT: 6.0p-2017-05-04
    // Chrome: 58.0.3029.96 (64-bit)
    // Java: 1.8.0_111-b14
    // Reference: http://www.smartclient.com/smartgwt/showcase/#form_controls_various

    // items
    final TextItem nameItem = new TextItem ("name", "Name");
    
    final ColorPickerItem colorPickerItem = new ColorPickerItem ();  
    colorPickerItem.setTitle ("Color Picker"); 

    // *14:08:00.361:MUP9:WARN:DynamicForm:isc_DynamicForm_0:Problem initializing item: {__ref: {GWT Java Obj},
    // __module: {GWT Module Obj},
    // AUTOIDClass: "DateRangeItem",
    // name: "isc_DateRangeItem_0",
    // _autoAssignedName: true,
    // editorType: "DateRangeItem",
    // __sgwtRelink: anonymous(),
    // createCanvas: anonymous(),
    // fromTitle: "From",
    // toTitle: "To",
    // fieldLayout: "horizontal",
    // allowRelativeDates: true,
    // defaultValue: Obj,
    // } - derived FormItem class is: DateRangeItem.  If this is  not a typo, please make sure the relevant module is loaded.  A SpacerItem will be created for this FormItem.
    final DateRangeItem dateRangeItem = new DateRangeItem (); // "dates", "Date Range");
    dateRangeItem.setFromTitle ("From");
    dateRangeItem.setToTitle ("To");
    dateRangeItem.setFieldLayout (DateFieldLayout.HORIZONTAL);

    dateRangeItem.setAllowRelativeDates(true);
        DateRange dateRange = new DateRange();
        dateRange.setRelativeStartDate(RelativeDate.TODAY);
        dateRange.setRelativeEndDate(new RelativeDate("-1m"));
        dateRangeItem.setValue(dateRange);

    /*final DateRange dateRange = new DateRange ();
    dateRange.setStartDate (new Date ());
    dateRange.setEndDate (new Date ());
    dateRangeItem.setValue (dateRange);*/

    // form
    final DynamicForm form = new DynamicForm ();
    form.setFields (nameItem, colorPickerItem, dateRangeItem);

    // layout
    draw (form);
  }

  ////////////////////////////////////////////////////////////////////////////////
  /// The following methods are here so that:
  /// 1. MainLayout can run this test
  /// 2. We can post a standalone EntryPoint test-case to SmartGWT
  ////////////////////////////////////////////////////////////////////////////////

  // my attributes
  private VLayout parentLayout;
  
  /**
   * The EntryPoint interface
   * Overload of the EntryPoint interface so that MainLayout can specify the parent layout
   * @param parentLayout
   */
  public final void onModuleLoad (final VLayout parentLayout) {
    
    // track and delegate
    this.parentLayout = parentLayout;
    onModuleLoad ();
  }
  
  /**
   * Shows the given Canvas
   * @param canvas
   */
  private void draw (final Canvas canvas) {

    // layout
    if (parentLayout != null) {
      parentLayout.addMember (canvas);
    }
    else {
      canvas.draw ();
    }
  }
}