Fixes a problem with TimePeriodElement
[taxeditor.git] / eu.etaxonomy.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>
22 * TimePeriodElement class.
23 * </p>
24 *
25 * @author n.hoffmann
26 * @created Nov 17, 2009
27 * @version 1.0
28 */
29 public class TimePeriodElement extends AbstractCdmFormElement {
30
31 private TimePeriod timePeriod;
32 private final Label label;
33 private final DateDetailSection section_dateDetails;
34
35 /**
36 * <p>
37 * Constructor for TimePeriodElement.
38 * </p>
39 *
40 * @param style
41 * a int.
42 * @param formFactory
43 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
44 * object.
45 * @param parentElement
46 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
47 * object.
48 * @param labelString
49 * a {@link java.lang.String} object.
50 * @param timePeriod
51 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
52 */
53 public TimePeriodElement(CdmFormFactory formFactory,
54 ICdmFormElement parentElement, String labelString,
55 TimePeriod timePeriod, final int style) {
56 super(formFactory, parentElement);
57
58 label = formFactory.createLabel(getLayoutComposite(), labelString);
59 addControl(label);
60
61 section_dateDetails = formFactory.createDateDetailSection(this,
62 Section.TWISTIE);
63 addControl(section_dateDetails);
64
65 setTimePeriod(timePeriod);
66
67 formFactory.addPropertyChangeListener(this);
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public void setSelected(boolean selected) {
73 setBackground(getColor(selected));
74 }
75
76 /**
77 * <p>
78 * setEntity
79 * </p>
80 *
81 * @param timePeriod
82 * a {@link eu.etaxonomy.cdm.model.common.TimePeriod} object.
83 */
84 public void setEntity(TimePeriod timePeriod) {
85 setTimePeriod(timePeriod);
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public void propertyChange(PropertyChangeEvent event) {
91 if (event == null) {
92 return;
93 }
94 Object eventSource = event.getSource();
95
96 if (getElements().contains(eventSource)) {
97 handleEvent(eventSource);
98 }
99 }
100
101 private void handleEvent(Object eventSource) {
102
103 // if(eventSource == text_freeText){
104 // String freeText = text_freeText.getText();
105 // timePeriod = TimePeriod.parseString(freeText);
106 // section_dateDetails.setEntity(timePeriod);
107 //
108 if (eventSource == section_dateDetails) {
109 timePeriod = section_dateDetails.getEntity();
110 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
111 }
112
113 }
114
115 /**
116 * <p>
117 * Setter for the field <code>timePeriod</code>.
118 * </p>
119 *
120 * @param timePeriod
121 * the timePeriod to set
122 */
123 public void setTimePeriod(TimePeriod timePeriod) {
124 this.timePeriod = timePeriod;
125 if (timePeriod != null) {
126 section_dateDetails.setEntity(timePeriod);
127 }
128 }
129
130 /**
131 * <p>
132 * Getter for the field <code>timePeriod</code>.
133 * </p>
134 *
135 * @return the timePeriod
136 */
137 public TimePeriod getTimePeriod() {
138 return timePeriod;
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 public void setBackground(Color color) {
144 label.setBackground(color);
145 section_dateDetails.setBackground(color);
146 }
147 }