Package hidb2.gui.util

Source Code of hidb2.gui.util.DlgDateTime

/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PoJamas 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.  See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2.  If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui.util;

import hidb2.kern.HIDBConst;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class DlgDateTime extends Dialog implements HIDBConst
  {
  private int _res = C_FAIL;

  protected DateTime _calDate = null;

  protected DateTime _calTime = null;

  protected Shell _dlg;

  protected Date _curDate = new Date();

  /**
   * Style stands for the CALENDAR Style.
   * - SWT.CALENDAR | SWT.BORDER
   * - SWT.DATE     | SWT.SHORT
   * - SWT.TIME     | SWT.SHORT
   *
   * @param parent
   * @param style
   */
  public DlgDateTime(Shell parent, int style)
    {
    super(parent, SWT.DIALOG_TRIM);

    _dlg = new Shell(parent.getShell(), SWT.DIALOG_TRIM);

    _dlg.setText(getText());

    _dlg.setLayout(new GridLayout(1, false));

    if ((style & SWT.CALENDAR) == SWT.CALENDAR)
      {
      _calDate = new DateTime(_dlg, SWT.CALENDAR | SWT.LONG);

      GridData gdCal = new GridData();
      gdCal.grabExcessHorizontalSpace = true;
      gdCal.horizontalAlignment = SWT.FILL;
      _calDate.setLayoutData(gdCal);
      }

    new Label(_dlg, SWT.NONE);

    if ((style & SWT.TIME) == SWT.TIME)
      {
      _calTime = new DateTime(_dlg, SWT.TIME);

      GridData gdCal = new GridData();
      gdCal.grabExcessHorizontalSpace = true;
      gdCal.horizontalAlignment = SWT.FILL;
      _calTime.setLayoutData(gdCal);
      }

    new Label(_dlg, SWT.NONE);

    Composite panButton = new Group(_dlg, SWT.NONE);

    GridData gdPan = new GridData();
    gdPan.grabExcessHorizontalSpace = true;
    gdPan.horizontalAlignment = SWT.FILL;
    panButton.setLayoutData(gdPan);

    RowLayout rowLayout2 = new RowLayout(SWT.HORIZONTAL);
    rowLayout2.fill = true;
    rowLayout2.wrap = false;
    rowLayout2.justify = true;
    rowLayout2.pack = false;

    panButton.setLayout(rowLayout2);

    Button btnOk = new Button(panButton, SWT.PUSH);
    btnOk.setText("OK");

    btnOk.addSelectionListener(new SelectionAdapter()
      {
        public void widgetSelected(SelectionEvent e)
          {
          Calendar cd = Calendar.getInstance();

          cd.clear();

          if (_calDate != null)
            {
            cd.set(_calDate.getYear(), _calDate.getMonth(), _calDate.getDay());
            }

          if (_calTime != null)
            {
            if (_calDate == null)
              {
              cd.set(cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH), _calTime.getHours(),
                  _calTime.getMinutes(), _calTime.getSeconds());
              }
            else
              {
              cd.set(_calDate.getYear(), _calDate.getMonth(), _calDate.getDay(), _calTime.getHours(), _calTime
                  .getMinutes(), _calTime.getSeconds());
              }
            }

          _curDate.setTime(cd.getTimeInMillis());

          _res = C_OK;
          _dlg.close();
          }
      });

    Button btnCancel = new Button(panButton, SWT.PUSH);
    btnCancel.setText("Cancel");

    btnCancel.addSelectionListener(new SelectionAdapter()
      {
        public void widgetSelected(SelectionEvent e)
          {
          _res = C_FAIL;
          _dlg.close();
          }
      });

    _dlg.setDefaultButton(btnOk);
    _dlg.pack();
    }

  /**
   * Open a Date/Time selection dialog and the return the selected date.
   * @param     initialeDate    Set the dialog with the given date and use it for output.
   * @return    The selected date, null if dialog cancelled.
   */
  public Date open(Date initialeDate)
    {
    if (initialeDate != null)
      {
      _curDate = initialeDate;

      Calendar cd = Calendar.getInstance();

      cd.clear();

      cd.setTime(initialeDate);

      if (_calDate != null)
        {
        _calDate.setDate(cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH));
        }

      if (_calTime != null)
        {
        _calTime.setTime(cd.get(Calendar.HOUR_OF_DAY), cd.get(Calendar.MINUTE), cd.get(Calendar.SECOND));
        }
      }

    _dlg.open();

    while (!_dlg.isDisposed())
      {
      if (!_dlg.getDisplay().readAndDispatch())
        _dlg.getDisplay().sleep();
      }

    _dlg.dispose();

    // System.out.println("Res:" + _curDate);
    return (_res == C_OK) ? _curDate : null;
    }

  }
TOP

Related Classes of hidb2.gui.util.DlgDateTime

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.