extract string constant
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / DateDetailSection.java
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
59 period_continue = formFactory.createCheckbox(this, "Continue", false, style);
60 period_continue.getMainControl().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(6,1));
61 partialElement_end = formFactory.createPartialElement(this, "End ",
62 null, style);
63
64 if (includeVerbatim){
65 text_VerbatimDate = formFactory.createTextWithLabelElement(this,
66 "Verbatim Date", null, style);
67
68 text_VerbatimDate.getMainControl().setLayoutData(
69 LayoutConstants.FILL_HORIZONTALLY(6, 1));
70 }
71
72
73
74 text_freeText = formFactory.createTextWithLabelElement(this,
75 "Freetext", null, style);
76 text_freeText.getMainControl().setLayoutData(
77 LayoutConstants.FILL_HORIZONTALLY(6, 1));
78
79
80 formFactory.addPropertyChangeListener(this);
81 }
82
83 @Override
84 public final T getEntity() {
85 if(super.getEntity() == null){
86 T newInstance = newInstance();
87 this.setEntity(newInstance);
88 }
89 return super.getEntity();
90 }
91
92 @SuppressWarnings("unchecked")
93 protected T newInstance(){
94 if (includeVerbatim){
95 return (T)VerbatimTimePeriod.NewVerbatimInstance();
96 }else{
97 return (T)TimePeriod.NewInstance();
98 }
99 }
100
101 @SuppressWarnings("unchecked")
102 protected T parseNewInstance() {
103 if (includeVerbatim){
104 T result = (T)TimePeriodParser.parseStringVerbatim(getText_parseText().getText());
105 return result;
106 }else{
107 T result = (T)TimePeriodParser.parseString(getText_parseText().getText());
108 return result;
109 }
110 }
111
112
113 /**
114 * <p>
115 * Setter for the field <code>timePeriod</code>.
116 * </p>
117 *
118 * @param timePeriod
119 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
120 */
121 @Override
122 public void setEntity(T timePeriod) {
123 setEntityInternally(timePeriod);
124 updateTitle();
125 getText_parseText().setText(timePeriod.toString());
126 }
127
128 /**
129 * When setting the entity through parsing we do not want to alter the parse field
130 * @param timePeriod
131 */
132 protected void setEntityInternally(T timePeriod){
133 Partial start = timePeriod.getStart();
134 partialElement_start.setPartial(start);
135 Partial end = timePeriod.getEnd();
136 partialElement_end.setPartial(end);
137 period_continue.setSelection(timePeriod.isContinued());
138 if (timePeriod.isContinued()){
139 partialElement_end.setEnabled(false);
140
141 }
142 ((Text) getText_parseText().getMainControl()).setSelection(cursorPosition);
143 text_freeText.setText(timePeriod.getFreeText());
144 if (includeVerbatim){
145 ((Text) text_VerbatimDate.getMainControl()).setSelection(cursorPosition);
146 text_VerbatimDate.setText(((VerbatimTimePeriod)timePeriod).getVerbatimDate());
147 }
148
149 super.setEntity(timePeriod);
150 }
151
152 /** {@inheritDoc} */
153 @Override
154 public void propertyChange(PropertyChangeEvent event) {
155 if (event == null) {
156 return;
157 }
158 Object eventSource = event.getSource();
159
160 if (getElements().contains(eventSource)) {
161 if (event instanceof CdmPropertyChangeEvent) {
162 if (((CdmPropertyChangeEvent) event).hasException()) {
163 handleException((CdmPropertyChangeEvent) event);
164 return;
165 }
166 }
167 handleEvent(eventSource);
168 }
169 }
170
171 /**
172 * @param event
173 */
174 private void handleException(CdmPropertyChangeEvent event) {
175 firePropertyChangeEvent(new CdmPropertyChangeEvent(this,
176 event.getException()));
177 }
178
179 protected void handleEvent(Object eventSource) {
180 if (eventSource == partialElement_start) {
181 Partial start = partialElement_start.getPartial();
182 getEntity().setStart(start);
183 } else if (eventSource == partialElement_end) {
184 Partial end = partialElement_end.getPartial();
185 getEntity().setEnd(end);
186
187 } else if (eventSource == getText_parseText()) {
188 cursorPosition = ((Text) getText_parseText().getMainControl())
189 .getCaretPosition();
190 T newInstance = parseNewInstance();
191 setEntityInternally(newInstance);
192 } else if (eventSource == text_freeText) {
193 getEntity().setFreeText(text_freeText.getText());
194 }else if (eventSource == period_continue) {
195 boolean continued = period_continue.getSelection();
196 getEntity().setContinued(continued);
197 if (continued){
198 partialElement_end.setAllNull();
199 partialElement_end.setEnabled(false);
200 }else{
201 partialElement_end.setEnabled(true);
202 }
203 }
204 updateTitle();
205 if (!(eventSource == getText_parseText())){
206 updateParseField();
207 }
208 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
209 }
210
211 // protected abstract T parseNewInstance() ;
212
213 protected void updateTitle(){
214 String title = CdmUtils.Nz(getEntity().toString());
215 this.setText(title);
216 layout();
217 }
218 protected void updateParseField(){
219 getText_parseText().setText(getEntity().toString());
220 layout();
221 }
222
223 /** {@inheritDoc} */
224 @Override
225 public void dispose() {
226 formFactory.removePropertyChangeListener(this);
227 super.dispose();
228 }
229
230 public TextWithLabelElement getText_parseText() {
231 return text_parseText;
232 }
233
234 public void setText_parseText(TextWithLabelElement text_parseText) {
235 this.text_parseText = text_parseText;
236 }
237 }