Project

General

Profile

Download (5.08 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
	protected TextWithLabelElement text_freeText;
32
	private PartialElement partialElement_start;
33
	private PartialElement partialElement_end;
34
	private TextWithLabelElement text_parseText;
35
	private int cursorPosition;
36

    
37
	public int getCursorPosition() {
38
        return cursorPosition;
39
    }
40

    
41
    public void setCursorPosition(int cursorPosition) {
42
        this.cursorPosition = cursorPosition;
43
    }
44

    
45
    /**
46
	 * <p>
47
	 * Constructor for DateDetailSection.
48
	 * </p>
49
	 *
50
	 * @param formFactory
51
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
52
	 *            object.
53
	 * @param parentElement
54
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
55
	 *            object.
56
	 * @param style
57
	 *            a int.
58
	 */
59
	protected DateDetailSection(CdmFormFactory formFactory,
60
			ICdmFormElement parentElement, int style) {
61
		super(formFactory, parentElement, style);
62

    
63
		partialElement_start = formFactory.createPartialElement(this,
64
				"Start ", null, style);
65
		partialElement_end = formFactory.createPartialElement(this, "End ",
66
				null, style);
67

    
68
		setText_parseText(formFactory.createTextWithLabelElement(this, "Parse",
69
				null, style));
70
		getText_parseText().getMainControl().setLayoutData(
71
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
72

    
73
		text_freeText = formFactory.createTextWithLabelElement(this,
74
				"Freetext", null, style);
75
		text_freeText.getMainControl().setLayoutData(
76
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
77

    
78

    
79
		formFactory.addPropertyChangeListener(this);
80
	}
81

    
82
	@Override
83
	public TimePeriod getEntity() {
84
		if(super.getEntity() == null){
85
			setEntity(TimePeriod.NewInstance());
86
		}
87
		return super.getEntity();
88
	}
89

    
90
	/**
91
	 * <p>
92
	 * Setter for the field <code>timePeriod</code>.
93
	 * </p>
94
	 *
95
	 * @param timePeriod
96
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
97
	 */
98
	@Override
99
	public void setEntity(TimePeriod timePeriod) {
100
		setEntityInternally(timePeriod);
101
		updateTitle();
102
		getText_parseText().setText(timePeriod.toString());
103
	}
104

    
105
	/**
106
	 * When setting the entity through parsing we do not want to alter the parse field
107
	 * @param timePeriod
108
	 */
109
	protected void setEntityInternally(TimePeriod timePeriod){
110
		Partial start = timePeriod.getStart();
111
		partialElement_start.setPartial(start);
112
		Partial end = timePeriod.getEnd();
113
		partialElement_end.setPartial(end);
114

    
115
		((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
116
		text_freeText.setText(timePeriod.getFreeText());
117

    
118
		super.setEntity(timePeriod);
119
	}
120

    
121
	/** {@inheritDoc} */
122
	@Override
123
	public void propertyChange(PropertyChangeEvent event) {
124
		if (event == null) {
125
			return;
126
		}
127
		Object eventSource = event.getSource();
128

    
129
		if (getElements().contains(eventSource)) {
130
			if (event instanceof CdmPropertyChangeEvent) {
131
				if (((CdmPropertyChangeEvent) event).hasException()) {
132
					handleException((CdmPropertyChangeEvent) event);
133
					return;
134
				}
135
			}
136
			handleEvent(eventSource);
137
		}
138
	}
139

    
140
	/**
141
	 * @param event
142
	 */
143
	private void handleException(CdmPropertyChangeEvent event) {
144
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
145
				event.getException()));
146
	}
147

    
148
	protected void handleEvent(Object eventSource) {
149
		if (eventSource == partialElement_start) {
150
			Partial start = partialElement_start.getPartial();
151
			getEntity().setStart(start);
152
		} else if (eventSource == partialElement_end) {
153
			Partial end = partialElement_end.getPartial();
154
			getEntity().setEnd(end);
155
		} else if (eventSource == getText_parseText()) {
156
			cursorPosition = ((Text) getText_parseText().getMainControl())
157
					.getCaretPosition();
158
			setEntityInternally(TimePeriodParser.parseString(getText_parseText().getText()));
159
		} else if (eventSource == text_freeText) {
160
			getEntity().setFreeText(text_freeText.getText());
161
		}
162
		updateTitle();
163
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
164
	}
165

    
166
	protected void updateTitle(){
167
		String title = CdmUtils.Nz(getEntity().toString());
168
		this.setText(title);
169
		layout();
170
	}
171

    
172
	/*
173
	 * (non-Javadoc)
174
	 *
175
	 * @see eu.etaxonomy.taxeditor.forms.AbstractFormSection#dispose()
176
	 */
177
	/** {@inheritDoc} */
178
	@Override
179
	public void dispose() {
180
		formFactory.removePropertyChangeListener(this);
181
		super.dispose();
182
	}
183

    
184
    public TextWithLabelElement getText_parseText() {
185
        return text_parseText;
186
    }
187

    
188
    public void setText_parseText(TextWithLabelElement text_parseText) {
189
        this.text_parseText = text_parseText;
190
    }
191

    
192
}
(11-11/47)