Project

General

Profile

Download (4.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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

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

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

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

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

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

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

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

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

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

    
70
		formFactory.addPropertyChangeListener(this);
71
	}
72

    
73
	@Override
74
	public TimePeriod getEntity() {
75
		if(super.getEntity() == null){
76
			setEntity(TimePeriod.NewInstance());
77
		}
78
		return super.getEntity();
79
	}
80

    
81
	/**
82
	 * <p>
83
	 * Setter for the field <code>timePeriod</code>.
84
	 * </p>
85
	 *
86
	 * @param timePeriod
87
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
88
	 */
89
	@Override
90
	public void setEntity(TimePeriod timePeriod) {
91
		setEntityInternally(timePeriod);
92
		updateTitle();
93
		text_parseText.setText(timePeriod.toString());
94
	}
95

    
96
	/**
97
	 * When setting the entity through parsing we do not want to alter the parse field
98
	 * @param timePeriod
99
	 */
100
	private void setEntityInternally(TimePeriod timePeriod){
101
		Partial start = timePeriod.getStart();
102
		partialElement_start.setPartial(start);
103
		Partial end = timePeriod.getEnd();
104
		partialElement_end.setPartial(end);
105

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

    
109
		super.setEntity(timePeriod);
110
	}
111

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

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

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

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

    
157
	private void updateTitle(){
158
		String title = CdmUtils.Nz(getEntity().toString());
159
		this.setText(title);
160
		layout();
161
	}
162

    
163
	/*
164
	 * (non-Javadoc)
165
	 *
166
	 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
167
	 */
168
	/** {@inheritDoc} */
169
	@Override
170
	public void dispose() {
171
		formFactory.removePropertyChangeListener(this);
172
		super.dispose();
173
	}
174

    
175
}
(11-11/44)