Project

General

Profile

« Previous | Next » 

Revision d917f8d5

Added by Niels Hoffmann almost 13 years ago

Changed the behavior of !TimePeriodElement and according widgets to better comply with the requirements and library implementation.

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/forms/DateDetailSection.java
11 11
package eu.etaxonomy.taxeditor.ui.forms;
12 12

  
13 13
import org.eclipse.jface.util.PropertyChangeEvent;
14
import org.eclipse.swt.widgets.Text;
14 15
import org.joda.time.Partial;
15 16

  
16 17
import eu.etaxonomy.cdm.model.common.TimePeriod;
......
24 25
 */
25 26
public class DateDetailSection extends AbstractFormSection<TimePeriod> {
26 27

  
28
	private TextWithLabelElement text_freeText;
27 29
	private PartialElement partialElement_start;
28 30
	private PartialElement partialElement_end;
31
	private TextWithLabelElement text_parseText;
32
	private int cursorPosition;
29 33
	
30 34
	/**
31 35
	 * <p>Constructor for DateDetailSection.</p>
......
37 41
	protected DateDetailSection(CdmFormFactory formFactory, ICdmFormElement parentElement,
38 42
			int style) {
39 43
		super(formFactory, null, parentElement, style);
40
		this.setText("Date Details");
41 44
		
42 45
		partialElement_start = formFactory.createPartialElement(this, "Start: ", null, style);
43 46
		partialElement_end = formFactory.createPartialElement(this, "End: ", null, style);
44 47
		
48
		text_parseText = formFactory.createTextWithLabelElement(this, "Parse", null, style);
49
		text_parseText.getMainControl().setLayoutData(CdmFormFactory.FILL_HORIZONTALLY(6, 1));
50
		
51
		text_freeText = formFactory.createTextWithLabelElement(this, "Freetext", null, style);
52
		text_freeText.getMainControl().setLayoutData(CdmFormFactory.FILL_HORIZONTALLY(6, 1));
53
		
45 54
		formFactory.addPropertyChangeListener(this);
46 55
	}
47 56

  
......
56 65
		Partial end = timePeriod.getEnd();
57 66
		partialElement_end.setPartial(end);
58 67

  
68
		text_parseText.setText(timePeriod.toString());
69
		((Text) text_parseText.getMainControl()).setSelection(cursorPosition);
70
		text_freeText.setText(timePeriod.getFreeText());
71
		
72
		this.setText(timePeriod.toString());
73
		
59 74
		super.setEntity(timePeriod);
60 75
	}
61 76
	
......
89 104
		if(eventSource == partialElement_start){
90 105
			Partial start = partialElement_start.getPartial();
91 106
			getEntity().setStart(start);
92
		}else if(eventSource == partialElement_end){
107
		}
108
		else if(eventSource == partialElement_end){
93 109
			Partial end = partialElement_end.getPartial();
94 110
			getEntity().setEnd(end);
95 111
		}
112
		else if(eventSource == text_parseText){
113
			cursorPosition = ((Text) text_parseText.getMainControl()).getCaretPosition();
114
			setEntity(TimePeriod.parseString(text_parseText.getText()));
115
		}
116
		else if(eventSource == text_freeText){
117
			getEntity().setFreeText(text_freeText.getText());
118
		}
96 119
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
97 120
	}
98 121
	
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/forms/PartialElement.java
10 10

  
11 11
package eu.etaxonomy.taxeditor.ui.forms;
12 12

  
13
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.events.SelectionEvent;
15
import org.eclipse.swt.events.SelectionListener;
13
import org.eclipse.jface.util.PropertyChangeEvent;
16 14
import org.eclipse.swt.graphics.Color;
17
import org.eclipse.swt.widgets.DateTime;
18 15
import org.eclipse.swt.widgets.Label;
19
import org.joda.time.DateTimeFieldType;
20 16
import org.joda.time.Partial;
21 17

  
22 18
import eu.etaxonomy.cdm.model.common.TimePeriod;
......
28 24
 * @created Mar 31, 2010
29 25
 * @version 1.0
30 26
 */
31
public class PartialElement extends AbstractCdmFormElement implements SelectionListener{
27
public class PartialElement extends AbstractCdmFormElement {
32 28

  
33 29
	private Label label;
30
	private NumberWithLabelElement number_day;
31
	private NumberWithLabelElement number_month;
32
	private NumberWithLabelElement number_year;
34 33
	private Partial partial;
35
	private DateTime composite_date;
36 34
	
37 35
	/**
38 36
	 * <p>Constructor for PartialElement.</p>
39 37
	 *
40
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
41
	 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
38
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
39
	 * @param formElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
42 40
	 * @param labelString a {@link java.lang.String} object.
43 41
	 * @param style a int.
44 42
	 */
......
51 49
		label = formFactory.createLabel(getLayoutComposite(), labelString);
52 50
		addControl(label);
53 51
		
52
		number_year = formFactory.createIntegerTextWithLabelElement(this, "Year", null, style);
53
		number_year.setLimits(4, -9999, 9999);
54
		
55
		number_month = formFactory.createIntegerTextWithLabelElement(this, "Month", null, style);		
56
		number_month.setLimits(2, 1, 12);
57
		
58
		number_day = formFactory.createIntegerTextWithLabelElement(this, "Day", null, style);
59
		number_day.setLimits(2, 1, 31);
54 60
		
55
		composite_date = new DateTime(getLayoutComposite(), SWT.BORDER | SWT.DATE | SWT.DROP_DOWN);
56
		addControl(composite_date);
57
		composite_date.addSelectionListener(this);
58 61
		
59 62
		formFactory.addPropertyChangeListener(this);		
60 63
	}
......
68 71
		this.partial = partial;
69 72
		
70 73
		if(partial != null){
71
			int year = 0;
72
			int month = 1;
73
			int day = 1;
74
			try{
75
				year = partial.get(DateTimeFieldType.year());
76
			}catch(IllegalArgumentException e){
77
				
78
			}
79
			try{
80
				month = partial.get(DateTimeFieldType.monthOfYear());
81
			}catch(IllegalArgumentException e){
82
				
83
			}
84
			try{
85
				day = partial.get(DateTimeFieldType.dayOfMonth());
86
			}catch(IllegalArgumentException e){
87
				
88
			}
89
			
90
			
91
			composite_date.setDate(year, month, day);
74
			number_day.setInteger(TimePeriod.getPartialValue(partial, TimePeriod.DAY_TYPE));
75
			number_month.setInteger(TimePeriod.getPartialValue(partial, TimePeriod.MONTH_TYPE));
76
			number_year.setInteger(TimePeriod.getPartialValue(partial, TimePeriod.YEAR_TYPE));
92 77
		}
93 78
	}
94 79

  
......
100 85
	public Partial getPartial(){
101 86
		return partial;
102 87
	}
88

  
89
	/* (non-Javadoc)
90
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
91
	 */
92
	/** {@inheritDoc} */
93
	@Override
94
	public void propertyChange(PropertyChangeEvent event) {
95
		if(event == null){
96
			return;
97
		}
98
		
99
		Object eventSource = event.getSource();
100
		
101
		if(getElements().contains(eventSource)){
102
			if(event instanceof CdmPropertyChangeEvent){
103
				if(((CdmPropertyChangeEvent) event).hasException()){
104
					handleException((CdmPropertyChangeEvent) event);
105
					return;
106
				}
107
			}
108
			handleEvent(eventSource);
109
		}		
110
	}
111
	
112
	/**
113
	 * @param event
114
	 */
115
	private void handleException(CdmPropertyChangeEvent event) {
116
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event.getException()));
117
	}
118

  
119
	private void handleEvent(Object eventSource){
120
		if(eventSource == number_month){
121
			partial = TimePeriod.setPartialField(partial, number_month.getInteger(), TimePeriod.MONTH_TYPE);
122
//			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
123
		}else if(eventSource == number_day){
124
			partial = TimePeriod.setPartialField(partial, number_day.getInteger(), TimePeriod.DAY_TYPE);
125
//			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
126
		}else if(eventSource == number_year){
127
			partial = TimePeriod.setPartialField(partial, number_year.getInteger(), TimePeriod.YEAR_TYPE);
128
//			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
129
		}
130
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
131
	}
103 132
	
104 133
	/* (non-Javadoc)
105 134
	 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
......
114 143
	public void setBackground(Color color) {
115 144
		label.setBackground(color);
116 145
	}
117

  
118
	/* (non-Javadoc)
119
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
120
	 */
121
	@Override
122
	public void widgetSelected(SelectionEvent e) {
123
		partial = new Partial()
124
							.with(DateTimeFieldType.year(), composite_date.getYear())
125
							.with(DateTimeFieldType.monthOfYear(), composite_date.getMonth())
126
							.with(DateTimeFieldType.dayOfMonth(), composite_date.getDay());
127
		;
128
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
129
	}
130

  
131
	/* (non-Javadoc)
132
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
133
	 */
134
	@Override
135
	public void widgetDefaultSelected(SelectionEvent e) {}
136 146
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/forms/TimePeriodElement.java
26 26
 */
27 27
public class TimePeriodElement extends AbstractCdmFormElement{
28 28

  
29
	private TextWithLabelElement text_freeText;
30 29
	private TimePeriod timePeriod;
30
	private Label label;
31 31
	private DateDetailSection section_dateDetails;
32
	private Label emptyLabel;
33 32
	
34 33
	/**
35 34
	 * <p>Constructor for TimePeriodElement.</p>
......
42 41
	 */
43 42
	public TimePeriodElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, TimePeriod timePeriod, final int style) {
44 43
		super(formFactory, parentElement);
45
		
46
		formFactory.addPropertyChangeListener(this);
47
		
48
		text_freeText = formFactory.createTextWithLabelElement(this, labelString, null, style);
49
		emptyLabel = formFactory.createLabel(getLayoutComposite(), "");
50
		addControl(emptyLabel);
44

  
45
		label = formFactory.createLabel(getLayoutComposite(), labelString);
51 46
		
52 47
		section_dateDetails = formFactory.createDateDetailSection(this, Section.TWISTIE);
53 48
		addControl(section_dateDetails);
54
		
49
				
55 50
		setTimePeriod(timePeriod);
56 51
		
52
		formFactory.addPropertyChangeListener(this);
57 53
	}
58 54

  
59 55
	/** {@inheritDoc} */
......
85 81
	
86 82
	private void handleEvent(Object eventSource){		
87 83
		
88
		if(eventSource == text_freeText){
89
			String freeText = text_freeText.getText();
90
			timePeriod = TimePeriod.parseString(freeText);
91
			section_dateDetails.setEntity(timePeriod);
92
			
93
		}else if(eventSource == section_dateDetails){
94
			timePeriod = section_dateDetails.getEntity();
95
			String timePeriodString = timePeriod.toString();
96
			text_freeText.setText(timePeriodString);
84
//		if(eventSource == text_freeText){
85
//			String freeText = text_freeText.getText();
86
//			timePeriod = TimePeriod.parseString(freeText);
87
//			section_dateDetails.setEntity(timePeriod);
88
//			
89
		if(eventSource == section_dateDetails){
90
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
97 91
		}
98 92
		
99
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
93
		
100 94
	}
101 95

  
102 96
	/**
......
107 101
	public void setTimePeriod(TimePeriod timePeriod) {
108 102
		this.timePeriod = timePeriod;
109 103
		if(timePeriod != null){
110
			text_freeText.setText(timePeriod.toString());
111 104
			section_dateDetails.setEntity(timePeriod);
112 105
		}
113 106
	}
......
124 117
	/** {@inheritDoc} */
125 118
	@Override
126 119
	public void setBackground(Color color) {
127
		emptyLabel.setBackground(color);
128
		text_freeText.setBackground(color);
120
		label.setBackground(color);
129 121
		section_dateDetails.setBackground(color);
130 122
	}
131 123
}

Also available in: Unified diff