Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / search / specimen / SpecimenProviderSelectionController.java
1 /**
2 * Copyright (C) 2014 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.search.specimen;
10
11 import org.eclipse.jface.wizard.IWizard;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.widgets.Combo;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Event;
16 import org.eclipse.swt.widgets.Listener;
17
18 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
19
20 /**
21 * Controller class for handling {@link SpecimenProviderSelectionComposite}
22 * @author pplitzner
23 * @date 22.05.2014
24 *
25 */
26 public class SpecimenProviderSelectionController implements Listener{
27
28 private SpecimenProviderSelectionComposite composite;
29 private IWizard wizard;
30 private String lastAccessPoint;
31
32 private static SpecimenProviderSelectionController instance;
33
34 public static SpecimenProviderSelectionController getInstance(Composite parent, IWizard wizard){
35 if(instance==null){
36 instance = new SpecimenProviderSelectionController(parent, wizard);
37 return instance;
38 }
39 instance.init(parent, wizard);
40 return instance;
41 }
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 SpecimenProviderSelectionController(Composite parent, IWizard wizard) {
49 init(parent, wizard);
50 }
51
52 /**
53 * @param parent
54 * @param wizard
55 */
56 private void init(Composite parent, IWizard wizard) {
57 this.wizard = wizard;
58 composite = new SpecimenProviderSelectionComposite(parent, SWT.NONE);
59 composite.getBtnBioCaseProvider().addListener(SWT.Selection, this);
60 composite.getBtnGbif().addListener(SWT.Selection, this);
61 composite.getTxtAccessPoint().addListener(SWT.Modify, this);
62 composite.getBtnGbif().setSelection(true);
63 composite.getTxtAccessPoint().setEnabled(false);
64 loadLastState();
65 }
66
67 private void loadLastState() {
68 if (lastAccessPoint == null){
69 String lastSelected = PreferencesUtil.getStringValue(PreferencesUtil.LAST_USED_BIOCASE_PROVIDER, true);
70 lastAccessPoint = lastSelected;
71 }
72 if(lastAccessPoint!=null){
73 Combo text = composite.getTxtAccessPoint();
74 Listener[] listeners = text.getListeners(SWT.Modify);
75 for (int i = 0; i < listeners.length; i++) {
76 text.removeListener(SWT.Modify, listeners[i]);
77 }
78 text.setText(lastAccessPoint);
79 for (int i = 0; i < listeners.length; i++) {
80 text.addListener(SWT.Modify, listeners[i]);
81 }
82 composite.getBtnGbif().setSelection(false);
83 composite.getTxtAccessPoint().setEnabled(true);
84
85
86 }
87 }
88
89
90 /* (non-Javadoc)
91 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
92 */
93 @Override
94 public void handleEvent(Event event) {
95 if(event.widget==composite.getBtnBioCaseProvider()){
96 composite.getTxtAccessPoint().setEnabled(true);
97 }
98 else if(event.widget==composite.getBtnGbif()){
99 composite.getTxtAccessPoint().setEnabled(false);
100 }
101 wizard.getContainer().updateButtons();
102 }
103
104 /**
105 * @return the composite
106 */
107 public SpecimenProviderSelectionComposite getComposite() {
108 return composite;
109 }
110
111
112 public void saveLastState() {
113 lastAccessPoint = composite.getTxtAccessPoint().getText();
114 PreferencesUtil.setLastSelectedBiocaseProvider(lastAccessPoint);
115 }
116
117 }