including changes from cdmlib-print
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / forms / 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.forms;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.widgets.DateTime;
18 import org.eclipse.swt.widgets.Label;
19 import org.joda.time.DateTimeFieldType;
20 import org.joda.time.Partial;
21
22 import eu.etaxonomy.cdm.model.common.TimePeriod;
23
24 /**
25 * <p>PartialElement class.</p>
26 *
27 * @author n.hoffmann
28 * @created Mar 31, 2010
29 * @version 1.0
30 */
31 public class PartialElement extends AbstractCdmFormElement implements SelectionListener{
32
33 private Label label;
34 private Partial partial;
35 private DateTime composite_date;
36
37 /**
38 * <p>Constructor for PartialElement.</p>
39 *
40 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
41 * @param formElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
42 * @param labelString a {@link java.lang.String} object.
43 * @param style a int.
44 */
45 public PartialElement(CdmFormFactory formFactory,
46 ICdmFormElement formElement, String labelString, int style) {
47 super(formFactory, formElement);
48
49 formElement.getLayoutComposite().setLayout(CdmFormFactory.LAYOUT(7, false));
50
51 label = formFactory.createLabel(getLayoutComposite(), labelString);
52 addControl(label);
53
54
55 composite_date = new DateTime(getLayoutComposite(), SWT.BORDER | SWT.DATE | SWT.DROP_DOWN);
56 addControl(composite_date);
57 composite_date.addSelectionListener(this);
58
59 formFactory.addPropertyChangeListener(this);
60 }
61
62 /**
63 * <p>Setter for the field <code>partial</code>.</p>
64 *
65 * @param partial a {@link org.joda.time.Partial} object.
66 */
67 public void setPartial(Partial partial){
68 this.partial = partial;
69
70 if(partial != null){
71 int year = 0;
72 int month = 1;
73 int day = 1;
74 try{
75 year = partial.get(DateTimeFieldType.year());
76 }catch(IllegalArgumentException e){
77
78 }
79 try{
80 month = partial.get(DateTimeFieldType.monthOfYear());
81 }catch(IllegalArgumentException e){
82
83 }
84 try{
85 day = partial.get(DateTimeFieldType.dayOfMonth());
86 }catch(IllegalArgumentException e){
87
88 }
89
90
91 composite_date.setDate(year, month, day);
92 }
93 }
94
95 /**
96 * <p>Getter for the field <code>partial</code>.</p>
97 *
98 * @return a {@link org.joda.time.Partial} object.
99 */
100 public Partial getPartial(){
101 return partial;
102 }
103
104 /* (non-Javadoc)
105 * @see eu.etaxonomy.taxeditor.forms.ISelectable#setSelected(boolean)
106 */
107 /** {@inheritDoc} */
108 public void setSelected(boolean selected) {
109 setBackground(getColor(selected));
110 }
111
112 /** {@inheritDoc} */
113 @Override
114 public void setBackground(Color color) {
115 label.setBackground(color);
116 }
117
118 /* (non-Javadoc)
119 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
120 */
121 @Override
122 public void widgetSelected(SelectionEvent e) {
123 partial = new Partial()
124 .with(DateTimeFieldType.year(), composite_date.getYear())
125 .with(DateTimeFieldType.monthOfYear(), composite_date.getMonth())
126 .with(DateTimeFieldType.dayOfMonth(), composite_date.getDay());
127 ;
128 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
129 }
130
131 /* (non-Javadoc)
132 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
133 */
134 @Override
135 public void widgetDefaultSelected(SelectionEvent e) {}
136 }