Project

General

Profile

Download (7.04 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.model.common.VerbatimTimePeriod;
10
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
11
import eu.etaxonomy.taxeditor.l10n.Messages;
12

    
13
public class DateDetailSection<T extends TimePeriod>
14
		extends AbstractFormSection<T> {
15
	protected TextWithLabelElement text_freeText;
16
	private PartialElement partialElement_start;
17
	private PartialElement partialElement_end;
18
	private CheckboxElement period_continue;
19
	private TextWithLabelElement text_parseText;
20
    private TextWithLabelElement text_VerbatimDate = null;
21
	private int cursorPosition;
22
	private boolean includeVerbatim;
23

    
24
	public int getCursorPosition() {
25
        return cursorPosition;
26
    }
27

    
28
    public void setCursorPosition(int cursorPosition) {
29
        this.cursorPosition = cursorPosition;
30
    }
31

    
32
    /**
33
	 * <p>
34
	 * Constructor for DateDetailSection.
35
	 * </p>
36
	 *
37
	 * @param formFactory
38
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
39
	 *            object.
40
	 * @param parentElement
41
	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
42
	 *            object.
43
	 * @param style
44
	 *            a int.
45
	 */
46
	protected DateDetailSection(CdmFormFactory formFactory,
47
			ICdmFormElement parentElement, boolean includeVerbatim, int style) {
48
		super(formFactory, parentElement, style);
49

    
50
		this.includeVerbatim = includeVerbatim;
51
		text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
52
                null, style);
53
		text_parseText.getMainControl().setLayoutData(
54
                LayoutConstants.FILL_HORIZONTALLY(6, 1));
55
		text_parseText.getMainControl().setToolTipText(Messages.DateDetail_parseText_tooltip);
56
		partialElement_start = formFactory.createPartialElement(this,
57
				"Start ", null, style);
58
		partialElement_end = formFactory.createPartialElement(this, "End ",
59
				null, style);
60
		period_continue = formFactory.createCheckbox(this, "Continue", false, style);
61
		period_continue.getMainControl().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(6,1));
62
		if (includeVerbatim){
63
	        text_VerbatimDate = formFactory.createTextWithLabelElement(this,
64
	                "Verbatim Date", null, style);
65

    
66
	        text_VerbatimDate.getMainControl().setLayoutData(
67
	                LayoutConstants.FILL_HORIZONTALLY(6, 1));
68
		}
69

    
70

    
71

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

    
77

    
78
		formFactory.addPropertyChangeListener(this);
79
	}
80

    
81
	@Override
82
	public final T getEntity() {
83
		if(super.getEntity() == null){
84
			T newInstance = newInstance();
85
			this.setEntity(newInstance);
86
		}
87
		return super.getEntity();
88
	}
89

    
90
	@SuppressWarnings("unchecked")
91
	protected T newInstance(){
92
		if (includeVerbatim){
93
			return (T)VerbatimTimePeriod.NewVerbatimInstance();
94
		}else{
95
			return (T)TimePeriod.NewInstance();
96
		}
97
	}
98

    
99
	@SuppressWarnings("unchecked")
100
	protected T parseNewInstance() {
101
		if (includeVerbatim){
102
			T result = (T)TimePeriodParser.parseStringVerbatim(getText_parseText().getText());
103
			return result;
104
		}else{
105
			T result = (T)TimePeriodParser.parseString(getText_parseText().getText());
106
			return result;
107
		}
108
	}
109

    
110

    
111
	/**
112
	 * <p>
113
	 * Setter for the field <code>timePeriod</code>.
114
	 * </p>
115
	 *
116
	 * @param timePeriod
117
	 *            a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
118
	 */
119
	@Override
120
	public void setEntity(T timePeriod) {
121
		setEntityInternally(timePeriod);
122
		updateTitle();
123
		getText_parseText().setText(timePeriod.toString());
124
	}
125

    
126
	/**
127
	 * When setting the entity through parsing we do not want to alter the parse field
128
	 * @param timePeriod
129
	 */
130
	protected void setEntityInternally(T timePeriod){
131
		Partial start = timePeriod.getStart();
132
		partialElement_start.setPartial(start);
133
		Partial end = timePeriod.getEnd();
134
		partialElement_end.setPartial(end);
135
		period_continue.setSelection(timePeriod.isContinued());
136
		if (timePeriod.isContinued()){
137
		    partialElement_end.setEnabled(false);
138
		}
139
		((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
140
		text_freeText.setText(timePeriod.getFreeText());
141
		if (includeVerbatim){
142
	        ((Text) text_VerbatimDate.getMainControl()).setSelection(cursorPosition);
143
	        text_VerbatimDate.setText(((VerbatimTimePeriod)timePeriod).getVerbatimDate());
144
		}
145

    
146
		super.setEntity(timePeriod);
147
	}
148

    
149
	/** {@inheritDoc} */
150
	@Override
151
	public void propertyChange(PropertyChangeEvent event) {
152
		if (event == null) {
153
			return;
154
		}
155
		Object eventSource = event.getSource();
156

    
157
		if (getElements().contains(eventSource)) {
158
			if (event instanceof CdmPropertyChangeEvent) {
159
				if (((CdmPropertyChangeEvent) event).hasException()) {
160
					handleException((CdmPropertyChangeEvent) event);
161
					return;
162
				}
163
			}
164
			handleEvent(eventSource);
165
		}
166
	}
167

    
168
	/**
169
	 * @param event
170
	 */
171
	private void handleException(CdmPropertyChangeEvent event) {
172
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
173
				event.getException()));
174
	}
175

    
176
	protected void handleEvent(Object eventSource) {
177
		if (eventSource == partialElement_start) {
178
			Partial start = partialElement_start.getPartial();
179
			getEntity().setStart(start);
180
		} else if (eventSource == partialElement_end) {
181
			Partial end = partialElement_end.getPartial();
182
			getEntity().setEnd(end);
183

    
184
		} else if (eventSource == getText_parseText()) {
185
			cursorPosition = ((Text) getText_parseText().getMainControl())
186
					.getCaretPosition();
187
			T newInstance = parseNewInstance();
188
			setEntityInternally(newInstance);
189
		} else if (eventSource == text_freeText) {
190
			getEntity().setFreeText(text_freeText.getText());
191
		}else if (eventSource == period_continue) {
192
		    boolean continued = period_continue.getSelection();
193
            getEntity().setContinued(continued);
194
            if (continued){
195
                partialElement_end.setAllNull();
196
                partialElement_end.setEnabled(false);
197
            }else{
198
                partialElement_end.setEnabled(true);
199
            }
200
        }
201
		updateTitle();
202
		updateParseField();
203
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
204
	}
205

    
206
//	protected abstract T parseNewInstance() ;
207

    
208
	protected void updateTitle(){
209
		String title = CdmUtils.Nz(getEntity().toString());
210
		this.setText(title);
211
		layout();
212
	}
213
	protected void updateParseField(){
214
	    getText_parseText().setText(getEntity().toString());
215
	    layout();
216
    }
217

    
218
	/** {@inheritDoc} */
219
	@Override
220
	public void dispose() {
221
		formFactory.removePropertyChangeListener(this);
222
		super.dispose();
223
	}
224

    
225
    public TextWithLabelElement getText_parseText() {
226
        return text_parseText;
227
    }
228

    
229
    public void setText_parseText(TextWithLabelElement text_parseText) {
230
        this.text_parseText = text_parseText;
231
    }
232
}
(11-11/47)