8149f3aad97c705becace2e52791c88f5976f531
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / PartialElement.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.element;
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.joda.time.Partial;
17
18 import eu.etaxonomy.cdm.model.common.TimePeriod;
19
20 /**
21 * <p>PartialElement class.</p>
22 *
23 * @author n.hoffmann
24 * @created Mar 31, 2010
25 * @version 1.0
26 */
27 public class PartialElement extends AbstractCdmFormElement implements ISelectable {
28
29 private Label label;
30 private NumberWithLabelElement number_day;
31 private NumberWithLabelElement number_month;
32 private NumberWithLabelElement number_year;
33 private Partial partial;
34
35 /**
36 * <p>Constructor for PartialElement.</p>
37 *
38 * @param formFactory a {@link eu.eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
39 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.forms.ICdmFormElement} object.
40 * @param labelString a {@link java.lang.String} object.
41 * @param style a int.
42 */
43 public PartialElement(CdmFormFactory formFactory,
44 ICdmFormElement formElement, String labelString, int style) {
45 super(formFactory, formElement);
46
47 formElement.getLayoutComposite().setLayout(LayoutConstants.LAYOUT(7, false));
48
49 label = formFactory.createLabel(getLayoutComposite(), labelString);
50 addControl(label);
51
52 number_year = formFactory.createNumberTextWithLabelElement(this, "Year", null, style);
53 number_year.setLimits(4, -9999, 9999);
54
55 number_month = formFactory.createNumberTextWithLabelElement(this, "Month", null, style);
56 number_month.setLimits(2, 1, 12);
57
58 number_day = formFactory.createNumberTextWithLabelElement(this, "Day", null, style);
59 number_day.setLimits(2, 1, 31);
60
61
62 formFactory.addPropertyChangeListener(this);
63 }
64
65 /**
66 * <p>Setter for the field <code>partial</code>.</p>
67 *
68 * @param partial a {@link org.joda.time.Partial} object.
69 */
70 public void setPartial(Partial partial){
71 this.partial = partial;
72
73 if(partial != null){
74 number_day.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.DAY_TYPE));
75 number_month.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.MONTH_TYPE));
76 number_year.setNumber(TimePeriod.getPartialValue(partial, TimePeriod.YEAR_TYPE));
77 }
78 }
79
80 /**
81 * <p>Getter for the field <code>partial</code>.</p>
82 *
83 * @return a {@link org.joda.time.Partial} object.
84 */
85 public Partial getPartial(){
86 return partial;
87 }
88
89 /* (non-Javadoc)
90 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
91 */
92 /** {@inheritDoc} */
93 @Override
94 public void propertyChange(PropertyChangeEvent event) {
95 if(event == null){
96 return;
97 }
98
99 Object eventSource = event.getSource();
100
101 if(getElements().contains(eventSource)){
102 if(event instanceof CdmPropertyChangeEvent){
103 if(((CdmPropertyChangeEvent) event).hasException()){
104 handleException((CdmPropertyChangeEvent) event);
105 return;
106 }
107 }
108 handleEvent(eventSource);
109 }
110 }
111
112 /**
113 * @param event
114 */
115 private void handleException(CdmPropertyChangeEvent event) {
116 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event.getException()));
117 }
118
119 private void handleEvent(Object eventSource){
120 if(eventSource == number_month){
121 partial = TimePeriod.setPartialField(partial, number_month.getInteger(), TimePeriod.MONTH_TYPE);
122 }else if(eventSource == number_day){
123 partial = TimePeriod.setPartialField(partial, number_day.getInteger(), TimePeriod.DAY_TYPE);
124 }else if(eventSource == number_year){
125 partial = TimePeriod.setPartialField(partial, number_year.getInteger(), TimePeriod.YEAR_TYPE);
126 }
127 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 public void setBackground(Color color) {
133 label.setBackground(color);
134 }
135
136 @Override
137 public void setSelected(boolean selected) {
138 setBackground(selected ? SELECTED : getPersistentBackground());
139 }
140 }