Project

General

Profile

Download (4.67 KB) Statistics
| Branch: | Tag: | Revision:
1 729887cf n.hoffmann
/**
2 5400463f n.hoffmann
 * Copyright (C) 2007 EDIT
3 f34721b8 Patrick Plitzner
 * European Distributed Institute of Taxonomy
4 5400463f n.hoffmann
 * http://www.e-taxonomy.eu
5 f34721b8 Patrick Plitzner
 *
6 5400463f n.hoffmann
 * 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 729887cf n.hoffmann
10 78222507 n.hoffmann
package eu.etaxonomy.taxeditor.ui.element;
11 729887cf n.hoffmann
12
import org.eclipse.jface.util.PropertyChangeEvent;
13 d917f8d5 n.hoffmann
import org.eclipse.swt.widgets.Text;
14 729887cf n.hoffmann
import org.joda.time.Partial;
15
16 5400463f n.hoffmann
import eu.etaxonomy.cdm.common.CdmUtils;
17 729887cf n.hoffmann
import eu.etaxonomy.cdm.model.common.TimePeriod;
18 c25ddfcf Cherian Mathew
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
19 729887cf n.hoffmann
20
/**
21 5400463f n.hoffmann
 * <p>
22
 * DateDetailSection class.
23
 * </p>
24 f34721b8 Patrick Plitzner
 *
25 729887cf n.hoffmann
 * @author n.hoffmann
26
 * @created Mar 31, 2010
27
 * @version 1.0
28
 */
29 c4c08e81 n.hoffmann
public class DateDetailSection extends AbstractFormSection<TimePeriod> {
30 729887cf n.hoffmann
31 5400463f n.hoffmann
	private final TextWithLabelElement text_freeText;
32
	private final PartialElement partialElement_start;
33
	private final PartialElement partialElement_end;
34
	private final TextWithLabelElement text_parseText;
35 d917f8d5 n.hoffmann
	private int cursorPosition;
36 5400463f n.hoffmann
37 3be6ef3e n.hoffmann
	/**
38 5400463f n.hoffmann
	 * <p>
39
	 * Constructor for DateDetailSection.
40
	 * </p>
41 f34721b8 Patrick Plitzner
	 *
42 5400463f n.hoffmann
	 * @param formFactory
43 78222507 n.hoffmann
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
44 5400463f n.hoffmann
	 *            object.
45
	 * @param parentElement
46 78222507 n.hoffmann
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
47 5400463f n.hoffmann
	 *            object.
48
	 * @param style
49
	 *            a int.
50 3be6ef3e n.hoffmann
	 */
51 5400463f n.hoffmann
	protected DateDetailSection(CdmFormFactory formFactory,
52
			ICdmFormElement parentElement, int style) {
53 021fa6b5 n.hoffmann
		super(formFactory, parentElement, style);
54 5400463f n.hoffmann
55
		partialElement_start = formFactory.createPartialElement(this,
56 f34721b8 Patrick Plitzner
				"Start ", null, style);
57
		partialElement_end = formFactory.createPartialElement(this, "End ",
58 5400463f n.hoffmann
				null, style);
59
60
		text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
61
				null, style);
62
		text_parseText.getMainControl().setLayoutData(
63 23783f7a n.hoffmann
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
64 5400463f n.hoffmann
65
		text_freeText = formFactory.createTextWithLabelElement(this,
66
				"Freetext", null, style);
67
		text_freeText.getMainControl().setLayoutData(
68 23783f7a n.hoffmann
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
69 5400463f n.hoffmann
70 729887cf n.hoffmann
		formFactory.addPropertyChangeListener(this);
71
	}
72
73 73a663a3 n.hoffmann
	@Override
74
	public TimePeriod getEntity() {
75
		if(super.getEntity() == null){
76
			setEntity(TimePeriod.NewInstance());
77
		}
78
		return super.getEntity();
79
	}
80 f34721b8 Patrick Plitzner
81 3be6ef3e n.hoffmann
	/**
82 5400463f n.hoffmann
	 * <p>
83
	 * Setter for the field <code>timePeriod</code>.
84
	 * </p>
85 f34721b8 Patrick Plitzner
	 *
86 5400463f n.hoffmann
	 * @param timePeriod
87
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
88 3be6ef3e n.hoffmann
	 */
89 5400463f n.hoffmann
	@Override
90 c4c08e81 n.hoffmann
	public void setEntity(TimePeriod timePeriod) {
91 f8b7e099 n.hoffmann
		setEntityInternally(timePeriod);
92 c752be48 n.hoffmann
		updateTitle();
93 f8b7e099 n.hoffmann
		text_parseText.setText(timePeriod.toString());
94
	}
95 f34721b8 Patrick Plitzner
96 f8b7e099 n.hoffmann
	/**
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 c4c08e81 n.hoffmann
		Partial start = timePeriod.getStart();
102
		partialElement_start.setPartial(start);
103
		Partial end = timePeriod.getEnd();
104
		partialElement_end.setPartial(end);
105
106 d917f8d5 n.hoffmann
		((Text) text_parseText.getMainControl()).setSelection(cursorPosition);
107
		text_freeText.setText(timePeriod.getFreeText());
108 5400463f n.hoffmann
109 73a663a3 n.hoffmann
		super.setEntity(timePeriod);
110 729887cf n.hoffmann
	}
111 5400463f n.hoffmann
112 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
113 729887cf n.hoffmann
	@Override
114
	public void propertyChange(PropertyChangeEvent event) {
115 5400463f n.hoffmann
		if (event == null) {
116 729887cf n.hoffmann
			return;
117
		}
118
		Object eventSource = event.getSource();
119 5400463f n.hoffmann
120
		if (getElements().contains(eventSource)) {
121
			if (event instanceof CdmPropertyChangeEvent) {
122
				if (((CdmPropertyChangeEvent) event).hasException()) {
123 1d9ed6ce n.hoffmann
					handleException((CdmPropertyChangeEvent) event);
124
					return;
125
				}
126
			}
127
			handleEvent(eventSource);
128
		}
129
	}
130 5400463f n.hoffmann
131 1d9ed6ce n.hoffmann
	/**
132
	 * @param event
133
	 */
134
	private void handleException(CdmPropertyChangeEvent event) {
135 5400463f n.hoffmann
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
136
				event.getException()));
137 1d9ed6ce n.hoffmann
	}
138 5400463f n.hoffmann
139
	private void handleEvent(Object eventSource) {
140
		if (eventSource == partialElement_start) {
141 c4c08e81 n.hoffmann
			Partial start = partialElement_start.getPartial();
142
			getEntity().setStart(start);
143 5400463f n.hoffmann
		} else if (eventSource == partialElement_end) {
144 729887cf n.hoffmann
			Partial end = partialElement_end.getPartial();
145 c4c08e81 n.hoffmann
			getEntity().setEnd(end);
146 5400463f n.hoffmann
		} else if (eventSource == text_parseText) {
147
			cursorPosition = ((Text) text_parseText.getMainControl())
148
					.getCaretPosition();
149 f34721b8 Patrick Plitzner
			setEntityInternally(TimePeriodParser.parseString(text_parseText.getText()));
150 5400463f n.hoffmann
		} else if (eventSource == text_freeText) {
151 d917f8d5 n.hoffmann
			getEntity().setFreeText(text_freeText.getText());
152
		}
153 98ec3084 n.hoffmann
		updateTitle();
154 1d9ed6ce n.hoffmann
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
155 729887cf n.hoffmann
	}
156 5400463f n.hoffmann
157 98ec3084 n.hoffmann
	private void updateTitle(){
158
		String title = CdmUtils.Nz(getEntity().toString());
159
		this.setText(title);
160
		layout();
161
	}
162 f34721b8 Patrick Plitzner
163 5400463f n.hoffmann
	/*
164
	 * (non-Javadoc)
165 f34721b8 Patrick Plitzner
	 *
166 30140758 n.hoffmann
	 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
167
	 */
168 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
169 30140758 n.hoffmann
	@Override
170
	public void dispose() {
171
		formFactory.removePropertyChangeListener(this);
172
		super.dispose();
173
	}
174 5400463f n.hoffmann
175 729887cf n.hoffmann
}