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