Merged refactoring from development branch.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / ui / forms / TimePeriodElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.forms;
12
13 import org.eclipse.jface.util.PropertyChangeEvent;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.ui.forms.widgets.Section;
17
18 import eu.etaxonomy.cdm.model.common.TimePeriod;
19
20 /**
21 * <p>TimePeriodElement class.</p>
22 *
23 * @author n.hoffmann
24 * @created Nov 17, 2009
25 * @version 1.0
26 */
27 public class TimePeriodElement extends AbstractCdmFormElement{
28
29 private TextWithLabelElement text_freeText;
30 private TimePeriod timePeriod;
31 private DateDetailSection section_dateDetails;
32 private Label emptyLabel;
33
34 /**
35 * <p>Constructor for TimePeriodElement.</p>
36 *
37 * @param style a int.
38 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
39 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
40 * @param labelString a {@link java.lang.String} object.
41 * @param timePeriod a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
42 */
43 public TimePeriodElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, TimePeriod timePeriod, final int style) {
44 super(formFactory, parentElement);
45
46 formFactory.addPropertyChangeListener(this);
47
48 text_freeText = formFactory.createTextWithLabelElement(this, labelString, null, style);
49 emptyLabel = formFactory.createLabel(getLayoutComposite(), "");
50 addControl(emptyLabel);
51
52 section_dateDetails = formFactory.createDateDetailSection(this, Section.TWISTIE);
53 addControl(section_dateDetails);
54
55 setTimePeriod(timePeriod);
56
57 }
58
59 /** {@inheritDoc} */
60 public void setSelected(boolean selected) {
61 setBackground(getColor(selected));
62 }
63
64 /**
65 * <p>setEntity</p>
66 *
67 * @param timePeriod a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
68 */
69 public void setEntity(TimePeriod timePeriod) {
70 setTimePeriod(timePeriod);
71 }
72
73 /** {@inheritDoc} */
74 @Override
75 public void propertyChange(PropertyChangeEvent event) {
76 if(event == null){
77 return;
78 }
79 Object eventSource = event.getSource();
80
81 if(getElements().contains(eventSource)){
82 handleEvent(eventSource);
83 }
84 }
85
86 private void handleEvent(Object eventSource){
87
88 if(eventSource == text_freeText){
89 String freeText = text_freeText.getText();
90 timePeriod = TimePeriod.parseString(freeText);
91 section_dateDetails.setEntity(timePeriod);
92
93 }else if(eventSource == section_dateDetails){
94 timePeriod = section_dateDetails.getEntity();
95 String timePeriodString = timePeriod.toString();
96 text_freeText.setText(timePeriodString);
97 }
98
99 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
100 }
101
102 /**
103 * <p>Setter for the field <code>timePeriod</code>.</p>
104 *
105 * @param timePeriod the timePeriod to set
106 */
107 public void setTimePeriod(TimePeriod timePeriod) {
108 this.timePeriod = timePeriod;
109 if(timePeriod != null){
110 text_freeText.setText(timePeriod.toString());
111 section_dateDetails.setEntity(timePeriod);
112 }
113 }
114
115 /**
116 * <p>Getter for the field <code>timePeriod</code>.</p>
117 *
118 * @return the timePeriod
119 */
120 public TimePeriod getTimePeriod() {
121 return timePeriod;
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public void setBackground(Color color) {
127 emptyLabel.setBackground(color);
128 text_freeText.setBackground(color);
129 section_dateDetails.setBackground(color);
130 }
131 }