Project

General

Profile

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

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

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

    
17
import eu.etaxonomy.cdm.common.CdmUtils;
18
import eu.etaxonomy.cdm.model.common.TimePeriod;
19
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
20

    
21
/**
22
 * <p>
23
 * DateDetailSection class.
24
 * </p>
25
 * 
26
 * @author n.hoffmann
27
 * @created Mar 31, 2010
28
 * @version 1.0
29
 */
30
public class DateDetailSection extends AbstractFormSection<TimePeriod> {
31

    
32
	private final TextWithLabelElement text_freeText;
33
	private final PartialElement partialElement_start;
34
	private final PartialElement partialElement_end;
35
	private final TextWithLabelElement text_parseText;
36
	private int cursorPosition;
37

    
38
	/**
39
	 * <p>
40
	 * Constructor for DateDetailSection.
41
	 * </p>
42
	 * 
43
	 * @param formFactory
44
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
45
	 *            object.
46
	 * @param parentElement
47
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
48
	 *            object.
49
	 * @param style
50
	 *            a int.
51
	 */
52
	protected DateDetailSection(CdmFormFactory formFactory,
53
			ICdmFormElement parentElement, int style) {
54
		super(formFactory, parentElement, style);
55

    
56
		partialElement_start = formFactory.createPartialElement(this,
57
				"Start: ", null, style);
58
		partialElement_end = formFactory.createPartialElement(this, "End: ",
59
				null, style);
60

    
61
		text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
62
				null, style);
63
		text_parseText.getMainControl().setLayoutData(
64
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
65

    
66
		text_freeText = formFactory.createTextWithLabelElement(this,
67
				"Freetext", null, style);
68
		text_freeText.getMainControl().setLayoutData(
69
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
70

    
71
		formFactory.addPropertyChangeListener(this);
72
	}
73

    
74
	@Override
75
	public TimePeriod getEntity() {
76
		if(super.getEntity() == null){
77
			setEntity(TimePeriod.NewInstance());
78
		}
79
		return super.getEntity();
80
	}
81
	
82
	/**
83
	 * <p>
84
	 * Setter for the field <code>timePeriod</code>.
85
	 * </p>
86
	 * 
87
	 * @param timePeriod
88
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
89
	 */
90
	@Override
91
	public void setEntity(TimePeriod timePeriod) {
92
		setEntityInternally(timePeriod);
93
		updateTitle();
94
		text_parseText.setText(timePeriod.toString());
95
	}
96
	
97
	/**
98
	 * When setting the entity through parsing we do not want to alter the parse field
99
	 * @param timePeriod
100
	 */
101
	private void setEntityInternally(TimePeriod timePeriod){
102
		Partial start = timePeriod.getStart();
103
		partialElement_start.setPartial(start);
104
		Partial end = timePeriod.getEnd();
105
		partialElement_end.setPartial(end);
106

    
107
		((Text) text_parseText.getMainControl()).setSelection(cursorPosition);
108
		text_freeText.setText(timePeriod.getFreeText());
109

    
110
		super.setEntity(timePeriod);
111
	}
112

    
113
	/** {@inheritDoc} */
114
	@Override
115
	public void propertyChange(PropertyChangeEvent event) {
116
		if (event == null) {
117
			return;
118
		}
119
		Object eventSource = event.getSource();
120

    
121
		if (getElements().contains(eventSource)) {
122
			if (event instanceof CdmPropertyChangeEvent) {
123
				if (((CdmPropertyChangeEvent) event).hasException()) {
124
					handleException((CdmPropertyChangeEvent) event);
125
					return;
126
				}
127
			}
128
			handleEvent(eventSource);
129
		}
130
	}
131

    
132
	/**
133
	 * @param event
134
	 */
135
	private void handleException(CdmPropertyChangeEvent event) {
136
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
137
				event.getException()));
138
	}
139

    
140
	private void handleEvent(Object eventSource) {
141
		if (eventSource == partialElement_start) {
142
			Partial start = partialElement_start.getPartial();
143
			getEntity().setStart(start);
144
		} else if (eventSource == partialElement_end) {
145
			Partial end = partialElement_end.getPartial();
146
			getEntity().setEnd(end);
147
		} else if (eventSource == text_parseText) {
148
			cursorPosition = ((Text) text_parseText.getMainControl())
149
					.getCaretPosition();			
150
			setEntityInternally(TimePeriodParser.parseString(text_parseText.getText()));			
151
		} else if (eventSource == text_freeText) {
152
			getEntity().setFreeText(text_freeText.getText());
153
		}
154
		updateTitle();
155
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
156
	}
157

    
158
	private void updateTitle(){
159
		String title = CdmUtils.Nz(getEntity().toString());
160
		this.setText(title);
161
		layout();
162
	}
163
	
164
	/*
165
	 * (non-Javadoc)
166
	 * 
167
	 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
168
	 */
169
	/** {@inheritDoc} */
170
	@Override
171
	public void dispose() {
172
		formFactory.removePropertyChangeListener(this);
173
		super.dispose();
174
	}
175

    
176
}
(9-9/38)