merge from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / specimenSearch / SpecimenSearchController.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 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 package eu.etaxonomy.taxeditor.view.specimenSearch;
11
12 import java.util.Calendar;
13 import java.util.GregorianCalendar;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.swt.widgets.Listener;
20
21 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
22
23 /**
24 * Controller class for handling a {@link SpecimenSearchComposite}.
25 * @author pplitzner
26 * @date 03.09.2013
27 *
28 */
29 public class SpecimenSearchController implements Listener{
30
31 private SpecimenSearchComposite specimenSearchComposite;
32 private OccurenceQuery lastQuery = null;
33
34 private static SpecimenSearchController instance = null;
35
36 public static SpecimenSearchController getInstance(Composite parent){
37 if(instance==null){
38 instance = new SpecimenSearchController(parent);
39 return instance;
40 }
41 instance.init(parent);
42 return instance;
43 }
44
45 /**
46 * Constructs a new controller which will itself construct the composite
47 * @param parent the parent {@link Composite} for the one handles by this controller
48 */
49 private SpecimenSearchController(Composite parent) {
50 init(parent);
51 }
52
53 private void init(Composite parent){
54 this.specimenSearchComposite = new SpecimenSearchComposite(parent, SWT.NONE);
55 this.specimenSearchComposite.addListener(SWT.Selection, this);
56 specimenSearchComposite.getBtnShowDate().addListener(SWT.Selection, this);
57 specimenSearchComposite.getBtnShowDate().setSelection(false);
58 specimenSearchComposite.getDateFrom().setEnabled(false);
59 specimenSearchComposite.getDateTo().setEnabled(false);
60
61 loadLastState();
62 }
63
64 /* (non-Javadoc)
65 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
66 */
67 @Override
68 public void handleEvent(Event event) {
69 Button btnShowDate = specimenSearchComposite.getBtnShowDate();
70 if(event.widget==btnShowDate){
71 specimenSearchComposite.getDateFrom().setEnabled(btnShowDate.getSelection());
72 specimenSearchComposite.getDateTo().setEnabled(btnShowDate.getSelection());
73 }
74 }
75
76
77 private void loadLastState(){
78 if(lastQuery!=null){
79 specimenSearchComposite.getTextAccessionNumber().setText(lastQuery.accessionNumber);
80 specimenSearchComposite.getTextCollector().setText(lastQuery.collector);
81 specimenSearchComposite.getTextCollectorNumber().setText(lastQuery.collectorsNumber);
82 specimenSearchComposite.getTextCountry().setText(lastQuery.country);
83 specimenSearchComposite.getTextHerbarium().setText(lastQuery.herbarium);
84 specimenSearchComposite.getTextLocality().setText(lastQuery.locality);
85 specimenSearchComposite.getTextTaxonName().setText(lastQuery.taxonName);
86 if(lastQuery.dateFrom!=null){
87 specimenSearchComposite.getDateFrom().setDate(lastQuery.dateFrom.get(Calendar.YEAR), lastQuery.dateFrom.get(Calendar.MONTH), lastQuery.dateFrom.get(Calendar.DAY_OF_MONTH));
88 }
89 if(lastQuery.dateTo!=null){
90 specimenSearchComposite.getDateTo().setDate(lastQuery.dateTo.get(Calendar.YEAR), lastQuery.dateTo.get(Calendar.MONTH), lastQuery.dateTo.get(Calendar.DAY_OF_MONTH));
91 }
92 }
93 }
94
95 public void saveLastSate() {
96 lastQuery = new OccurenceQuery(getTaxonName(), getCollector(), getCollectorNumber(), getAccessionNumber(), getHerbarium(), getCountry(), getLocality(), getDateFrom(), getDateTo());
97 }
98
99 /**
100 * Returns the {@link Composite} handled by this controller
101 * @return
102 */
103 public Composite getComposite() {
104 return specimenSearchComposite;
105 }
106
107 /**
108 * Returns the taxon name entered in the search view
109 * @return the taxon name as a {@link String}
110 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextTaxonName()
111 */
112 public String getTaxonName() {
113 return specimenSearchComposite.getTextTaxonName().getText();
114 }
115
116 /**
117 * Returns the collector entered in the search view
118 * @return the collector as a {@link String}
119 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextCollector()
120 */
121 public String getCollector() {
122 return specimenSearchComposite.getTextCollector().getText();
123 }
124
125 /**
126 * Returns the collecting number entered in the search view
127 * @return the collecting as a {@link String}
128 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextCollectorNumber()
129 */
130 public String getCollectorNumber() {
131 return specimenSearchComposite.getTextCollectorNumber().getText();
132 }
133
134 /**
135 * Returns the accession number entered in the search view
136 * @return the accession number as a {@link String}
137 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextAccessionNumber()
138 */
139 public String getAccessionNumber() {
140 return specimenSearchComposite.getTextAccessionNumber().getText();
141 }
142
143 /**
144 * Returns the herbarium entered in the search view
145 * @return the herbarium as a {@link String}
146 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getComboHerbarium()
147 */
148 public String getHerbarium() {
149 return specimenSearchComposite.getTextHerbarium().getText();
150 }
151
152 /**
153 * Returns the country entered in the search view
154 * @return the country as a {@link String}
155 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getComboCountry()
156 */
157 public String getCountry() {
158 return specimenSearchComposite.getTextCountry().getText();
159 // return specimenSearchComposite.getComboCountry().getItem(specimenSearchComposite.getComboCountry().getSelectionIndex());
160 }
161
162 /**
163 * Returns the locality entered in the search view
164 * @return the locality as a {@link String}
165 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextLocality()
166 */
167 public String getLocality() {
168 return specimenSearchComposite.getTextLocality().getText();
169 }
170
171 /**
172 * Returns the start date entered in the search view.
173 * @return the date as an instance of {@link Calendar}
174 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateFrom()
175 */
176 public Calendar getDateFrom() {
177 if(specimenSearchComposite.getBtnShowDate().getSelection()){
178 Calendar dateFrom = new GregorianCalendar();
179 dateFrom.clear();
180 dateFrom.set(specimenSearchComposite.getDateFrom().getYear(), specimenSearchComposite.getDateFrom().getMonth(), specimenSearchComposite.getDateFrom().getDay());
181 return dateFrom;
182 }
183 return null;
184 }
185
186 /**
187 * Returns the end date entered in the search view.
188 * @return the date as an instance of {@link Calendar}
189 * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateTo()
190 */
191 public Calendar getDateTo() {
192 if(specimenSearchComposite.getBtnShowDate().getSelection()){
193 Calendar dateTo = new GregorianCalendar();
194 dateTo.clear();
195 dateTo.set(specimenSearchComposite.getDateTo().getYear(), specimenSearchComposite.getDateTo().getMonth(), specimenSearchComposite.getDateTo().getDay());
196 return dateTo;
197 }
198 return null;
199 }
200
201 }