Project

General

Profile

Download (7.94 KB) Statistics
| Branch: | Tag: | Revision:
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(false), getLocality(), getDateFrom(), getDateTo(), false);
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(boolean biocase) {
157

    
158
        String result = specimenSearchComposite.getTextCountry().getText();
159
        if (result.contains("-") && biocase){
160
            result = result.substring(4).trim();
161
        }else if (result.contains("-")){
162
            result = result.substring(0,2).trim();
163
        }
164
        return result;
165

    
166
//        return specimenSearchComposite.getComboCountry().getItem(specimenSearchComposite.getComboCountry().getSelectionIndex());
167
    }
168

    
169
    /**
170
     * Returns the locality entered in the search view
171
     * @return the locality as a {@link String}
172
     * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getTextLocality()
173
     */
174
    public String getLocality() {
175
        return specimenSearchComposite.getTextLocality().getText();
176
    }
177

    
178
    /**
179
     * Returns the start date entered in the search view.
180
     * @return the date as an instance of {@link Calendar}
181
     * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateFrom()
182
     */
183
    public Calendar getDateFrom() {
184
        if(specimenSearchComposite.getBtnShowDate().getSelection()){
185
            Calendar dateFrom = new GregorianCalendar();
186
            dateFrom.clear();
187
            dateFrom.set(specimenSearchComposite.getDateFrom().getYear(), specimenSearchComposite.getDateFrom().getMonth(), specimenSearchComposite.getDateFrom().getDay());
188
            return dateFrom;
189
        }
190
        return null;
191
    }
192

    
193
    /**
194
     * Returns the end date entered in the search view.
195
     * @return the date as an instance of {@link Calendar}
196
     * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateTo()
197
     */
198
    public Calendar getDateTo() {
199
        if(specimenSearchComposite.getBtnShowDate().getSelection()){
200
            Calendar dateTo = new GregorianCalendar();
201
            dateTo.clear();
202
            dateTo.set(specimenSearchComposite.getDateTo().getYear(), specimenSearchComposite.getDateTo().getMonth(), specimenSearchComposite.getDateTo().getDay());
203
            return dateTo;
204
        }
205
        return null;
206
    }
207

    
208
}
(4-4/4)