Project

General

Profile

Download (6.53 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.ui.mvc.element;
10

    
11

    
12

    
13

    
14
import org.apache.lucene.analysis.shingle.ShingleMatrixFilter.OneDimensionalNonWeightedTokenSettingsCodec;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.events.DisposeEvent;
17
import org.eclipse.swt.events.DisposeListener;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.DateTime;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Label;
28
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.swt.widgets.Text;
30
import org.eclipse.ui.forms.widgets.FormToolkit;
31
import org.eclipse.ui.forms.widgets.TableWrapLayout;
32
import org.joda.time.DateTimeZone;
33
import org.joda.time.format.DateTimeFormatter;
34

    
35
import eu.etaxonomy.taxeditor.model.ImageResources;
36
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
37
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
38
import eu.etaxonomy.taxeditor.ui.mvc.AbstractCdmComposite;
39

    
40
/**
41
 * @author pplitzner
42
 * @date 16.06.2014
43
 *
44
 */
45
public class DateElement extends AbstractCdmComposite<DateElementController>  {
46

    
47
    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
48
    private org.joda.time.DateTime initialDateTime;
49
    private Text textDate;
50
    private final Button openDateDialog;
51
    
52
    private static final String pattern = "yyyy-MM-dd HH:mm";
53
   
54

    
55
    /**
56
     * Create the composite.
57
     * @param parent
58
     * @param style
59
     */
60
    public DateElement(Composite parent, org.joda.time.DateTime initDateTime, String label, int style, boolean editableText) {
61
        super(parent, style);
62
        if (initDateTime != null){
63
        	this.initialDateTime = initDateTime.toDateTime(DateTimeZone.UTC);
64
        }
65
        
66
        
67
        addDisposeListener(new DisposeListener() {
68
            @Override
69
            public void widgetDisposed(DisposeEvent e) {
70
                toolkit.dispose();
71
            }
72
        });
73
        toolkit.adapt(this);
74
        toolkit.paintBordersFor(this);
75
        {
76
            TableWrapLayout tableWrapLayout = new TableWrapLayout();
77
            tableWrapLayout.verticalSpacing = 0;
78
            tableWrapLayout.topMargin = 0;
79
            tableWrapLayout.rightMargin = 0;
80
            tableWrapLayout.leftMargin = 0;
81
            tableWrapLayout.horizontalSpacing = 0;
82
            tableWrapLayout.bottomMargin = 0;
83
            setLayout(tableWrapLayout);
84
        }
85
     
86
        Composite composite = new Composite (this, SWT.NONE);
87
    	GridLayout layout = new GridLayout ();
88
    
89
    	layout.numColumns = 2;
90
    	 	
91
    	composite.setLayout(layout);
92
    	
93
    	
94
       // textDate = new Text(composite, SWT.BORDER|SWT.FILL);new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
95
    	textDate = toolkit.createText(composite, null, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
96
    	
97
        textDate.setEditable(editableText);
98
       
99
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
100
        gridData.minimumWidth = 100;
101
                       
102
        textDate.setLayoutData(gridData);
103
        openDateDialog = new Button (composite, SWT.PUSH);
104
        Image image = ImageResources.getImage(ImageResources.DATE);
105
   //     openDateDialog.setText ("Open Date Dialog");
106
        openDateDialog.setImage(image);
107
       
108
        openDateDialog.addSelectionListener (new SelectionAdapter() {
109
        	@Override
110
        	public void widgetSelected(SelectionEvent e){
111
        		final Shell dialog = new Shell (parent.getShell(), SWT.DIALOG_TRIM);
112
        	
113
    			dialog.setLayout (new GridLayout (3, false));
114
    			dialog.setText(label);
115

    
116
    			final DateTime calendar = new DateTime (dialog, SWT.CALENDAR | SWT.BORDER);
117
    			
118
    			final DateTime time = new DateTime (dialog, SWT.TIME | SWT.SHORT);
119
    			if (initialDateTime != null){
120
    				calendar.setDate(initialDateTime.getYear(), initialDateTime.getMonthOfYear()-1, initialDateTime.getDayOfMonth());
121
    				time.setHours(initialDateTime.getHourOfDay());
122
        			time.setMinutes(initialDateTime.getMinuteOfHour());
123
//        			time.setSeconds(initialDateTime.getSecondOfMinute());
124
    			}
125
    			
126

    
127
    			new Label (dialog, SWT.NONE);
128
    			new Label (dialog, SWT.NONE);
129
    			Button ok = new Button (dialog, SWT.PUSH);
130
    			ok.setText ("OK");
131
    			ok.setLayoutData(new GridData (SWT.FILL, SWT.CENTER, false, false));
132
    			ok.addSelectionListener (new SelectionAdapter() {
133
    	        	@Override
134
    	        	public void widgetSelected(SelectionEvent e){
135
    	        		setData(makeZonedTimeFromSWT(calendar, time));
136
    	        		dialog.close ();
137
    				}
138
    			});
139
    			dialog.setDefaultButton (ok);
140
    			dialog.pack ();
141
    			dialog.open ();
142
    		}
143
        });
144
       
145
        toolkit.adapt(textDate, true, true);
146
    }
147

    
148
    @Override
149
    protected void initInternalController(CdmFormFactory formFactory, ICdmFormElement parentElement) {
150
        controller = new DateElementController(this, formFactory, parentElement, initialDateTime);
151
        formFactory.adapt(controller);
152
    }
153
    public Text getTextDate() {
154
        return textDate;
155
    }
156
    
157
   
158
    
159
	
160
    public static org.joda.time.DateTime makeZonedTimeFromSWT(
161
	                                org.eclipse.swt.widgets.DateTime widget, 
162
	                                org.eclipse.swt.widgets.DateTime dateTime) {
163
		  //the first month of the year is 0			  
164
		  return new org.joda.time.DateTime(widget.getYear(),
165
				  widget.getMonth()+1,
166
				  widget.getDay(),
167
				  dateTime.getHours(),
168
				  dateTime.getMinutes(),
169
                  0,
170
                  0,
171
                  DateTimeZone.UTC);
172
	}
173

    
174
	public static void updateSWTwithJoda(
175
	                                org.eclipse.swt.widgets.DateTime widget,
176
	                                DateTime dateTime) {
177
		  widget.setYear(dateTime.getYear());
178
		  widget.setMonth(dateTime.getMonth() -1);
179
		  widget.setDay(dateTime.getDay());
180
		  widget.setHours(dateTime.getHours());
181
		  widget.setMinutes(dateTime.getMinutes());
182
		  
183
	}
184
		
185
	public void setData (org.joda.time.DateTime data) {
186
			this.initialDateTime = data;
187
			this.controller.setDateTime(data);
188
			this.textDate.setText(initialDateTime.toString(pattern));
189
	}
190

    
191
}
(1-1/4)