ref #5616 Generalize evaluation of available CdmViewers
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / specimenSearch / SpecimenSearchController.java
index 86137de524381b79800836e9c9d5004764892273..33f1e6ea5e61caf61013c1ffc113849f90f075e9 100644 (file)
@@ -13,7 +13,12 @@ import java.util.Calendar;
 import java.util.GregorianCalendar;
 
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+
+import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
 
 /**
  * Controller class for handling a {@link SpecimenSearchComposite}.
@@ -21,16 +26,74 @@ import org.eclipse.swt.widgets.Composite;
  * @date 03.09.2013
  *
  */
-public class SpecimenSearchController {
+public class SpecimenSearchController implements Listener{
+
+    private SpecimenSearchComposite specimenSearchComposite;
+    private OccurenceQuery lastQuery = null;
 
-    private final SpecimenSearchComposite specimenSearchComposite;
+    private static SpecimenSearchController instance = null;
+
+    public static SpecimenSearchController getInstance(Composite parent){
+        if(instance==null){
+            instance = new SpecimenSearchController(parent);
+            return instance;
+        }
+        instance.init(parent);
+        return instance;
+    }
 
     /**
      * Constructs a new controller which will itself construct the composite
      * @param parent the parent {@link Composite} for the one handles by this controller
      */
-    public SpecimenSearchController(Composite parent) {
+    private SpecimenSearchController(Composite parent) {
+        init(parent);
+    }
+
+    private void init(Composite parent){
         this.specimenSearchComposite = new SpecimenSearchComposite(parent, SWT.NONE);
+        this.specimenSearchComposite.addListener(SWT.Selection, this);
+        specimenSearchComposite.getBtnShowDate().addListener(SWT.Selection, this);
+        specimenSearchComposite.getBtnShowDate().setSelection(false);
+        specimenSearchComposite.getDateFrom().setEnabled(false);
+        specimenSearchComposite.getDateTo().setEnabled(false);
+
+        loadLastState();
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
+     */
+    @Override
+    public void handleEvent(Event event) {
+        Button btnShowDate = specimenSearchComposite.getBtnShowDate();
+        if(event.widget==btnShowDate){
+            specimenSearchComposite.getDateFrom().setEnabled(btnShowDate.getSelection());
+            specimenSearchComposite.getDateTo().setEnabled(btnShowDate.getSelection());
+        }
+    }
+
+
+    private void loadLastState(){
+        if(lastQuery!=null){
+            specimenSearchComposite.getTextAccessionNumber().setText(lastQuery.accessionNumber);
+            specimenSearchComposite.getTextCollector().setText(lastQuery.collector);
+            specimenSearchComposite.getTextCollectorNumber().setText(lastQuery.collectorsNumber);
+            specimenSearchComposite.getTextCountry().setText(lastQuery.country);
+            specimenSearchComposite.getTextHerbarium().setText(lastQuery.herbarium);
+            specimenSearchComposite.getTextLocality().setText(lastQuery.locality);
+            specimenSearchComposite.getTextTaxonName().setText(lastQuery.taxonName);
+            if(lastQuery.dateFrom!=null){
+                specimenSearchComposite.getDateFrom().setDate(lastQuery.dateFrom.get(Calendar.YEAR), lastQuery.dateFrom.get(Calendar.MONTH), lastQuery.dateFrom.get(Calendar.DAY_OF_MONTH));
+            }
+            if(lastQuery.dateTo!=null){
+                specimenSearchComposite.getDateTo().setDate(lastQuery.dateTo.get(Calendar.YEAR), lastQuery.dateTo.get(Calendar.MONTH), lastQuery.dateTo.get(Calendar.DAY_OF_MONTH));
+            }
+        }
+    }
+
+    public void saveLastSate() {
+        lastQuery = new OccurenceQuery(getTaxonName(), getCollector(), getCollectorNumber(), getAccessionNumber(), getHerbarium(), getCountry(), getLocality(), getDateFrom(), getDateTo());
     }
 
     /**
@@ -84,7 +147,6 @@ public class SpecimenSearchController {
      */
     public String getHerbarium() {
         return specimenSearchComposite.getTextHerbarium().getText();
-//        return specimenSearchComposite.getComboHerbarium().getItem(specimenSearchComposite.getComboHerbarium().getSelectionIndex());
     }
 
     /**
@@ -112,10 +174,13 @@ public class SpecimenSearchController {
      * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateFrom()
      */
     public Calendar getDateFrom() {
-        Calendar dateFrom = new GregorianCalendar();
-        dateFrom.clear();
-        dateFrom.set(specimenSearchComposite.getDateFrom().getYear(), specimenSearchComposite.getDateFrom().getMonth(), specimenSearchComposite.getDateFrom().getDay());
-        return dateFrom;
+        if(specimenSearchComposite.getBtnShowDate().getSelection()){
+            Calendar dateFrom = new GregorianCalendar();
+            dateFrom.clear();
+            dateFrom.set(specimenSearchComposite.getDateFrom().getYear(), specimenSearchComposite.getDateFrom().getMonth(), specimenSearchComposite.getDateFrom().getDay());
+            return dateFrom;
+        }
+        return null;
     }
 
     /**
@@ -124,10 +189,13 @@ public class SpecimenSearchController {
      * @see eu.etaxonomy.taxeditor.view.specimenSearch.SpecimenSearchComposite#getDateTo()
      */
     public Calendar getDateTo() {
-        Calendar dateTo = new GregorianCalendar();
-        dateTo.clear();
-        dateTo.set(specimenSearchComposite.getDateTo().getYear(), specimenSearchComposite.getDateTo().getMonth(), specimenSearchComposite.getDateTo().getDay());
-        return dateTo;
+        if(specimenSearchComposite.getBtnShowDate().getSelection()){
+            Calendar dateTo = new GregorianCalendar();
+            dateTo.clear();
+            dateTo.set(specimenSearchComposite.getDateTo().getYear(), specimenSearchComposite.getDateTo().getMonth(), specimenSearchComposite.getDateTo().getDay());
+            return dateTo;
+        }
+        return null;
     }
 
 }
\ No newline at end of file