Project

General

Profile

Download (7.94 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.taxeditor.ui.element;
10

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

    
15
import eu.etaxonomy.cdm.common.CdmUtils;
16
import eu.etaxonomy.cdm.model.common.TimePeriod;
17
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
18
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
19
import eu.etaxonomy.taxeditor.l10n.Messages;
20

    
21
public class DateDetailSection<T extends TimePeriod>
22
		extends AbstractFormSection<T>
23
        implements ICacheRelevantFormElement, IEnableableFormElement{
24

    
25
	protected TextWithLabelElement text_freeText;
26
	protected PartialElement partialElement_start;
27
	protected PartialElement partialElement_end;
28
	protected CheckboxElement period_continue;
29
	protected TextWithLabelElement text_parseText;
30
    protected TextWithLabelElement text_VerbatimDate = null;
31
	protected int cursorPosition;
32
	protected boolean includeVerbatim;
33

    
34
	boolean isEnabled = true;
35

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

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

    
44
    /**
45
	 * Constructor for DateDetailSection.
46
	 */
47
	protected DateDetailSection(CdmFormFactory formFactory,
48
			ICdmFormElement parentElement, boolean includeVerbatim, int style) {
49
		super(formFactory, parentElement, style);
50

    
51
		this.includeVerbatim = includeVerbatim;
52
		text_parseText = formFactory.createTextWithLabelElement(this, "Parse",
53
                null, style);
54
		text_parseText.getMainControl().setLayoutData(
55
                LayoutConstants.FILL_HORIZONTALLY(6, 1));
56
		text_parseText.getMainControl().setToolTipText(Messages.DateDetail_parseText_tooltip);
57
		partialElement_start = formFactory.createPartialElement(this,
58
				"Start ", null, style);
59

    
60
		period_continue = formFactory.createCheckbox(this, "Continue", false, style);
61
        period_continue.getMainControl().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(6,1));
62
		partialElement_end = formFactory.createPartialElement(this, "End ",
63
				null, style);
64

    
65
		if (includeVerbatim){
66
	        text_VerbatimDate = formFactory.createTextWithLabelElement(this,
67
	                "Verbatim Date", null, style);
68

    
69
	        text_VerbatimDate.getMainControl().setLayoutData(
70
	                LayoutConstants.FILL_HORIZONTALLY(6, 1));
71
		}
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 final T getEntity() {
84
		if(super.getEntity() == null){
85
			T newInstance = newInstance();
86
			this.setEntity(newInstance);
87
		}
88
		return super.getEntity();
89
	}
90

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

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

    
111

    
112
	/**
113
	 * Setter for the field <code>timePeriod</code>.
114
	 */
115
	@Override
116
	public void setEntity(T timePeriod) {
117
		setEntityInternally(timePeriod);
118
		updateTitle();
119
		getText_parseText().setText(timePeriod.toString());
120
	}
121

    
122
	/**
123
	 * When setting the entity through parsing we do not want
124
	 * to alter the parse field
125
	 */
126
	protected void setEntityInternally(T timePeriod){
127
		Partial start = timePeriod.getStart();
128
		partialElement_start.setPartial(start);
129
		Partial end = timePeriod.getEnd();
130
		partialElement_end.setPartial(end);
131
		period_continue.setSelection(timePeriod.isContinued());
132
		if (timePeriod.isContinued()){
133
		    partialElement_end.setEnabled(false);
134

    
135
		}
136
		((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
137
		text_freeText.setText(timePeriod.getFreeText());
138
		if (includeVerbatim){
139
	        ((Text) text_VerbatimDate.getMainControl()).setSelection(cursorPosition);
140
	        text_VerbatimDate.setText(((VerbatimTimePeriod)timePeriod).getVerbatimDate());
141
		}
142

    
143
		super.setEntity(timePeriod);
144
	}
145

    
146
	@Override
147
	public void propertyChange(PropertyChangeEvent event) {
148
		if (event == null) {
149
			return;
150
		}
151
		Object eventSource = event.getSource();
152

    
153
		if (getElements().contains(eventSource)) {
154
			if (event instanceof CdmPropertyChangeEvent) {
155
				if (((CdmPropertyChangeEvent) event).hasException()) {
156
					handleException((CdmPropertyChangeEvent) event);
157
					return;
158
				}
159
			}
160
			handleEvent(eventSource);
161
		}
162
	}
163

    
164
	private void handleException(CdmPropertyChangeEvent event) {
165
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
166
				event.getException()));
167
	}
168

    
169
	protected void handleEvent(Object eventSource) {
170
		if (eventSource == partialElement_start) {
171
			Partial start = partialElement_start.getPartial();
172
			getEntity().setStart(start);
173
		} else if (eventSource == partialElement_end) {
174
			Partial end = partialElement_end.getPartial();
175
			getEntity().setEnd(end);
176

    
177
		} else if (eventSource == getText_parseText()) {
178
			cursorPosition = ((Text) getText_parseText().getMainControl())
179
					.getCaretPosition();
180
			T newInstance = parseNewInstance();
181
			setEntityInternally(newInstance);
182
		} else if (eventSource == text_freeText) {
183
			getEntity().setFreeText(text_freeText.getText());
184
		}else if (eventSource == period_continue) {
185
		    boolean continued = period_continue.getSelection();
186
            getEntity().setContinued(continued);
187
            if (continued){
188
                partialElement_end.setAllNull();
189
                partialElement_end.setEnabled(false);
190
            }else{
191
                partialElement_end.setEnabled(true);
192
            }
193
        }else if (eventSource == text_VerbatimDate) {
194

    
195
            ((VerbatimTimePeriod)getEntity()).setVerbatimDate(text_VerbatimDate.getText());
196
        }
197
		updateTitle();
198
		if (!(eventSource == getText_parseText())){
199
		    updateParseField();
200
		}
201

    
202
		firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
203
	}
204

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

    
215
	@Override
216
	public void dispose() {
217
		formFactory.removePropertyChangeListener(this);
218
		super.dispose();
219
	}
220

    
221
    public TextWithLabelElement getText_parseText() {
222
        return text_parseText;
223
    }
224

    
225
    public void setText_parseText(TextWithLabelElement text_parseText) {
226
        this.text_parseText = text_parseText;
227
    }
228

    
229
    @Override
230
    public void updateCacheRelevance() {
231
        CacheRelevanceHelper.updateCacheRelevanceForSubelements(this);
232
    }
233

    
234
    @Override
235
    public void addDependsOnCache(ToggleableTextElement toggleElement) {
236
        CacheRelevanceHelper.addDependsOnCacheForSubElements(this, toggleElement);
237
    }
238

    
239
    @Override
240
    public CacheRelevance cacheRelevance() {
241
        return null;
242
    }
243

    
244
    @Override
245
    public void setEnabled(boolean enabled){
246
        this.isEnabled = enabled;
247
        for (ICdmFormElement element: getElements()){
248
            if (element instanceof IEnableableFormElement){
249
                IEnableableFormElement enableableElement = (IEnableableFormElement) element;
250
                enableableElement.setEnabled(enabled);
251
            }
252
        }
253
    }
254

    
255
    @Override
256
    public boolean isEnabled() {
257
       return isEnabled;
258
    }
259

    
260
}
(15-15/57)