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