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

at.spardat.xma.widgets.DatePicker Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#) $Id: DatePicker.java 10833 2013-06-26 05:12:28Z hoenninger $
 *
 *
 *
 *
 *
 */
package at.spardat.xma.widgets;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.vafada.swtcalendar.SWTCalendar;

import at.spardat.xma.mdl.IWModelClient;
import at.spardat.xma.mdl.UIDelegateClient;
import at.spardat.xma.mdl.WModel;
import at.spardat.xma.mdl.simple.ISimpleWM;
import at.spardat.xma.page.Scaler;

/**
 * The DatePicker widget consists of a Text field and a Button.
 * Usually the text field is to attach to a simple widget model, on which a Date validator has to be set.
 * On a button click a date-chooser dialog is started, after date selection the
 * dialog is closed and the selected date is written to the widget model.
 * @author S3460
 * @since version_number
 */
public class DatePicker extends Composite {

    private static final String IMAGE_CALENDAR = "at/spardat/xma/widgets/icons/Calendar_scheduleHS.png";
    /**
     * Style constant for making Sundays red.
     */
    public static final int RED_SUNDAY = SWTCalendar.RED_SUNDAY;
    /**
     * Style constant for making weekends red.
     */
    public static final int RED_WEEKEND = SWTCalendar.RED_WEEKEND;

    /**
     * The text field showing the date. This field is to attach to a SimpleWMClient model.
     */
    private Text dateTextField_ = null;

    /**
     * The drop down button starting the date-picker shell.
     */
    private Button datePickerButton_ = null;

    /**
     * The style for the dateField field.
     */
    int dateFieldStyle_ = (SWT.SINGLE|SWT.BORDER|SWT.LEFT);

    /**
     * The style for the date-picker composite.
     * Can be any style valid for a Composite and RED_WEEKEND, RED_SUNDAY.
     */
    int datePickerStyle_ = SWT.NONE;

    /**
     * The Locale used by the date-picker shell and its controls.
     */
    private Locale locale_ = Locale.getDefault();

    /**
     * The Font used by the date-picker shell and its controls.
     */
    private Font datepickerFont_;

    /**
     * Stores the selected date.
     */
    private Calendar pickedCalendar_;

    private boolean editable_ = true;
    
    boolean activateRepeator=true;       

    /**
     *
     * @param parent
     * @param dateFieldStyle The style for the Text dateTextField. Any style valid or a Text widget.
     * @param datePickerStyle The style for the date-picker composite. Can be any style valid for a Composite and RED_WEEKEND, RED_SUNDAY.
     */
    public DatePicker(Composite parent, int dateFieldStyle, int datePickerStyle) {
        this(parent,dateFieldStyle,dateFieldStyle,true);
    }
        
    public DatePicker(Composite parent, int dateFieldStyle, int datePickerStyle, boolean activateRepeator) {        
        super(parent, SWT.NONE);
        this.dateFieldStyle_ = dateFieldStyle;
        this.datePickerStyle_ = datePickerStyle;
        this.activateRepeator = activateRepeator;
        createWidgets();
    }

    /**
     *
     * @param parent
     * @param dateFieldStyle The style for the Text dateTextField. Any style valid or a Text widget.
     */
    public DatePicker(Composite parent, int dateFieldStyle) {
        super(parent, SWT.NONE);
        this.dateFieldStyle_ = dateFieldStyle;
        createWidgets();
    }

    /**
     *
     * @param parent
     */
    public DatePicker(Composite parent) {
        super(parent, SWT.NONE);
        createWidgets();
    }

    /**
     * Returns the text field used by the DatePicker.
     * Usually this text field is attached to a widget model (ISimpleWM) with a date validator.
     * @return Returns the dateTextField.
     */
    public Text getDateTextField() {
        return dateTextField_;
    }

    /**
     * Returns the calendar set with the date as chosen by the date-chosser dialog.
     * @return Returns the picked date as a Calendar.
     * Returns null if called before anything was picked or set.
     */
    public Calendar getPickedCalendar() {
        return pickedCalendar_;
    }

    /**
     * Sets the date preselected at the date-chooser dialog.
     * Note that if this widget is attached to a model, then the model's date is preselected (if the model has a value).
     * This method does not set the date in the dateTextField!
     * @param aDate
     * @since version_number
     * @author S3460
     */
    public void setDatePickerDate(Date aDate){
        if(pickedCalendar_ == null){
            pickedCalendar_ = Calendar.getInstance(locale_);
        }

        pickedCalendar_.setTime(aDate);
    }

    /**
     * Sets the Font of the date-chooser dialog and its controls.
     * (Not of the DateTextField, its properties can be set by getDateTextField())
     * Optional - if not set then the controls' default Fonts are used.
     */
    public void setDatePickerFont(Font font) {
        datepickerFont_ = font;
    }

    /**
     * Sets the Locale of the date-chooser dialog and its controls.
     * (Not of the DateTextField. The text field's Locale is set by the Wiget model)
     * Optional - if not set then the default Locale is used.
     */
    public void setDatePickerLocale(Locale locale) {
        locale_ = locale;
    }

    /**
     * Initializes arrowDownButton_ , dateTextField_
     * and the SelectionListener for arrowDownButton_ to start the date-picker shell.
     *
     * @since version_number
     * @author S3460
     */
    private void createWidgets(){
        final Scaler scaler = Scaler.getInstance(this);
        datePickerButton_ = new Button (this, SWT.PUSH);
        Image calendarIcon = new Image(getShell().getDisplay(), DatePicker.class.getClassLoader().getResourceAsStream(IMAGE_CALENDAR));
        datePickerButton_.setImage(calendarIcon);
        dateTextField_ = new Text(this, dateFieldStyle_);

        FormLayout layout = new FormLayout();
        layout.marginHeight = scaler.convertYToCurrent(0);
        layout.marginWidth = scaler.convertXToCurrent(0);
        this.setLayout(layout);

        FormData data = new FormData();
        data.left = new FormAttachment(0,100,scaler.convertXToCurrent(0));
        data.right = new FormAttachment(datePickerButton_,scaler.convertXToCurrent(-1),SWT.LEFT);
        data.top = new FormAttachment(0,100,scaler.convertYToCurrent(0));
        dateTextField_.setLayoutData(data);

        dateTextField_.addKeyListener(new KeyListener(){
            public void keyPressed(KeyEvent e) {
                if(isEditable() && (e.keyCode == SWT.ARROW_DOWN || e.keyCode == SWT.ARROW_UP)){
                    showDatePicker();
                }
            }

            public void keyReleased(KeyEvent e) {
            }
        });

        data = new FormData();
      
        //data.width = scaler.convertXToCurrent(16); // do not anymore explicitly set the width, allow the button to take the needed width
        data.right = new FormAttachment(100,100,scaler.convertXToCurrent(0));
        data.top = new FormAttachment(dateTextField_,scaler.convertYToCurrent(0),SWT.TOP);
        data.bottom = new FormAttachment(dateTextField_,scaler.convertYToCurrent(0),SWT.BOTTOM);
        datePickerButton_.setLayoutData(data);
        datePickerButton_.pack();

        datePickerButton_.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent event) {
                showDatePicker();
            }

            public void widgetDefaultSelected(SelectionEvent event) {
            }
        });
    }

    /**
     * Starts the DatePicker shell at the loacation x,y.
     * @param x
     * @param y
     * @since version_number
     * @author S3460
     */
    private void showDatePicker() {
        Shell pickerShell = null;

        try {
            if(pickedCalendar_ == null){
                pickedCalendar_ = Calendar.getInstance(locale_);
            }

            ISimpleWM dateTextFieldM = getWModel(dateTextField_);

            pickerShell = new Shell(this.getShell(), SWT.APPLICATION_MODAL);
            pickerShell.setLayout(new FillLayout());
                    
            SWTCalendar swtCalendar = new SWTCalendar(pickerShell, datePickerStyle_,activateRepeator);

            //set date from dateTextFieldM model for picker
            if(dateTextFieldM != null){
                Date date = dateTextFieldM.toDate();
                if(date != null){
                    pickedCalendar_.setTime(date);
                }
            }

//          Always set the Locale on swtCalendar before the calender as it refreshes the Calendar
            swtCalendar.setLocale(locale_);
            swtCalendar.setCalendar(pickedCalendar_);

            if(datepickerFont_ != null)
                swtCalendar.setFont(datepickerFont_);

            pickerShell.pack();

            //set the location of the pickerShell
            Rectangle bounds = datePickerButton_.getBounds();
            //Get absolute location of datePickerButton_
            Point point = datePickerButton_.getParent().toDisplay(bounds.x, bounds.y);
            //shift pickerShell to the left so that its right upper corner is under datePickerButton_
            point.x -= (pickerShell.getSize().x - bounds.width);
            //shift pickerShell down so that it is 0 pixels under datePickerButton_
            point.y += bounds.height;

            pickerShell.setLocation(point);
            pickerShell.open();

            Display display = Display.getCurrent();
            while (!pickerShell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }

            if ( ! swtCalendar.wasCanceled() ) {
                //get picked Calendar
                pickedCalendar_ = swtCalendar.getCalendar();
                //set picked date at model
                if(dateTextFieldM != null){
                    dateTextFieldM.set(pickedCalendar_.getTime());
                    //status bar message is not updated (e.g. mandatory) as the following methods are called in this order:
                    //DialogPage.updateErrorStatus() - set the status bar, triggered by set of value at Text widget by model
                    //SimpleUIDelegateClient.updateErrorState() - actualizes the widget's error state, called by model
                    //So the handleModelChangeEvent() is called again (to call DialogPage.updateErrorStatus()).
                    ((IWModelClient)dateTextFieldM).getUIDelegate().handleModelChangeEvent(null);
                }
            }

        } finally {
            if(pickerShell!= null)
                pickerShell.dispose();
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
     */
    public void setBackground(Color color) {
        super.setBackground(color);
        dateTextField_.setBackground(color);
    }

    /* (non-Javadoc)
     * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
     * Also enables/disables childs of DatePicker.
     */
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.dateTextField_.setEnabled(enabled);
        if(isEditable()){
            this.datePickerButton_.setEnabled(enabled);
        } else if (!enabled){
            this.datePickerButton_.setEnabled(enabled);
        }
    }

    /**
     * Editables text field of DatePicker and enables/disables Button.
     * @param editable
     * @since version_number
     * @author S3460
     */
    public void setEditable(boolean editable) {
        editable_ = editable;
        this.dateTextField_.setEditable(editable);
        if(getEnabled()){
            this.datePickerButton_.setEnabled(editable);
        }else if (!editable){
            this.datePickerButton_.setEnabled(editable);
        }
    }

    public boolean isEditable(){
        return editable_;
    }

    /**
     * @param widget
     * @return The model attached to widget. Returns null if no model is attached.
     */
    private static ISimpleWM getWModel(Widget widget) {
        ISimpleWM simpleWM = null;
        Object uiDelegate = (UIDelegateClient) widget.getData();

        if (uiDelegate != null && uiDelegate instanceof UIDelegateClient) {
            WModel model = ((UIDelegateClient)uiDelegate).getWModel();
            if(model instanceof ISimpleWM){
                simpleWM = (ISimpleWM) model;
            }
        }

        return simpleWM;
    }

    /**
     * Sets the receiver's tool tip text to the argument, which
     * may be null indicating that no tool tip text should be shown.
     */
    public void setToolTipText(String string) {
        super.setToolTipText(string);
        dateTextField_.setToolTipText(string);
        datePickerButton_.setToolTipText(string);
    }

//  /* Sets the text of the text field.
//  * (non-Javadoc)
//  * @see org.eclipse.swt.widgets.Text#setText(java.lang.String)
//  */
// public void setText(String string) {
//     dateTextField_.setText(string);
// }
//
// /* Get the text of text field.
//  * (non-Javadoc)
//  * @see org.eclipse.swt.widgets.Text#getText()
//  */
// public String getText() {
//     return dateTextField_.getText();
// }

///**
//* Returns the button starting the date-chooser dialog.
//* @return Returns the date-picker Button.
//*/
//private Button getButton() {
//  return datePickerButton_;
//}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy