Project

General

Profile

Download (4.83 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.ui.element;
2

    
3
import org.eclipse.jface.util.PropertyChangeEvent;
4
import org.eclipse.swt.widgets.Text;
5
import org.joda.time.Partial;
6

    
7
import eu.etaxonomy.cdm.common.CdmUtils;
8
import eu.etaxonomy.cdm.model.common.TimePeriod;
9
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
10

    
11
public abstract class DateDetailSectionBase<T extends TimePeriod> 
12
		extends AbstractFormSection<T> {
13
	protected TextWithLabelElement text_freeText;
14
	private PartialElement partialElement_start;
15
	private PartialElement partialElement_end;
16
	private TextWithLabelElement text_parseText;
17
	private int cursorPosition;
18

    
19
	public int getCursorPosition() {
20
        return cursorPosition;
21
    }
22

    
23
    public void setCursorPosition(int cursorPosition) {
24
        this.cursorPosition = cursorPosition;
25
    }
26

    
27
    /**
28
	 * <p>
29
	 * Constructor for DateDetailSection.
30
	 * </p>
31
	 *
32
	 * @param formFactory
33
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
34
	 *            object.
35
	 * @param parentElement
36
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
37
	 *            object.
38
	 * @param style
39
	 *            a int.
40
	 */
41
	protected DateDetailSectionBase(CdmFormFactory formFactory,
42
			ICdmFormElement parentElement, int style) {
43
		super(formFactory, parentElement, style);
44

    
45
		partialElement_start = formFactory.createPartialElement(this,
46
				"Start ", null, style);
47
		partialElement_end = formFactory.createPartialElement(this, "End ",
48
				null, style);
49

    
50
		setText_parseText(formFactory.createTextWithLabelElement(this, "Parse",
51
				null, style));
52
		getText_parseText().getMainControl().setLayoutData(
53
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
54

    
55
		text_freeText = formFactory.createTextWithLabelElement(this,
56
				"Freetext", null, style);
57
		text_freeText.getMainControl().setLayoutData(
58
				LayoutConstants.FILL_HORIZONTALLY(6, 1));
59

    
60

    
61
		formFactory.addPropertyChangeListener(this);
62
	}
63

    
64
	@Override
65
	public final T getEntity() {
66
		if(super.getEntity() == null){
67
			T newInstance = newInstance();
68
			this.setEntity(newInstance);
69
		}
70
		return super.getEntity();
71
	}
72
	
73
	protected abstract T newInstance();
74

    
75
	/**
76
	 * <p>
77
	 * Setter for the field <code>timePeriod</code>.
78
	 * </p>
79
	 *
80
	 * @param timePeriod
81
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
82
	 */
83
	@Override
84
	public void setEntity(T timePeriod) {
85
		setEntityInternally(timePeriod);
86
		updateTitle();
87
		getText_parseText().setText(timePeriod.toString());
88
	}
89

    
90
	/**
91
	 * When setting the entity through parsing we do not want to alter the parse field
92
	 * @param timePeriod
93
	 */
94
	protected void setEntityInternally(T timePeriod){
95
		Partial start = timePeriod.getStart();
96
		partialElement_start.setPartial(start);
97
		Partial end = timePeriod.getEnd();
98
		partialElement_end.setPartial(end);
99

    
100
		((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
101
		text_freeText.setText(timePeriod.getFreeText());
102

    
103
		super.setEntity(timePeriod);
104
	}
105

    
106
	/** {@inheritDoc} */
107
	@Override
108
	public void propertyChange(PropertyChangeEvent event) {
109
		if (event == null) {
110
			return;
111
		}
112
		Object eventSource = event.getSource();
113

    
114
		if (getElements().contains(eventSource)) {
115
			if (event instanceof CdmPropertyChangeEvent) {
116
				if (((CdmPropertyChangeEvent) event).hasException()) {
117
					handleException((CdmPropertyChangeEvent) event);
118
					return;
119
				}
120
			}
121
			handleEvent(eventSource);
122
		}
123
	}
124

    
125
	/**
126
	 * @param event
127
	 */
128
	private void handleException(CdmPropertyChangeEvent event) {
129
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
130
				event.getException()));
131
	}
132

    
133
	protected void handleEvent(Object eventSource) {
134
		if (eventSource == partialElement_start) {
135
			Partial start = partialElement_start.getPartial();
136
			getEntity().setStart(start);
137
		} else if (eventSource == partialElement_end) {
138
			Partial end = partialElement_end.getPartial();
139
			getEntity().setEnd(end);
140
		} else if (eventSource == getText_parseText()) {
141
			cursorPosition = ((Text) getText_parseText().getMainControl())
142
					.getCaretPosition();
143
			T newInstance = parseNewInstance();
144
			setEntityInternally(newInstance);
145
		} else if (eventSource == text_freeText) {
146
			getEntity().setFreeText(text_freeText.getText());
147
		}
148
		updateTitle();
149
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
150
	}
151

    
152
	protected T parseNewInstance() {
153
		@SuppressWarnings("unchecked")
154
		T result = (T)TimePeriodParser.parseString(getText_parseText().getText());
155
		return result;
156
	}
157

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

    
164
	/** {@inheritDoc} */
165
	@Override
166
	public void dispose() {
167
		formFactory.removePropertyChangeListener(this);
168
		super.dispose();
169
	}
170

    
171
    public TextWithLabelElement getText_parseText() {
172
        return text_parseText;
173
    }
174

    
175
    public void setText_parseText(TextWithLabelElement text_parseText) {
176
        this.text_parseText = text_parseText;
177
    }
178
}
(12-12/49)