Project

General

Profile

Download (2.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.cdm.vaadin.event;
10

    
11
import org.vaadin.viritin.fields.LazyComboBox;
12

    
13
import com.vaadin.data.Property.ValueChangeEvent;
14
import com.vaadin.data.Property.ValueChangeListener;
15

    
16
import eu.etaxonomy.cdm.cache.EntityCache;
17
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
20

    
21
/**
22
 *
23
 * @author a.kohlbecker
24
 * @since 19.10.2017
25
 *
26
 */
27
public class ToOneRelatedEntityReloader<CDM extends CdmBase> implements ValueChangeListener {
28

    
29
    private static final long serialVersionUID = -1141764783149024788L;
30

    
31
    LazyComboBox<CDM>  toOneRelatedEntityField;
32

    
33
    CachingPresenter cachingPresenter;
34

    
35
    public ToOneRelatedEntityReloader( LazyComboBox<CDM> toOneRelatedEntityField, CachingPresenter entityCache){
36
        this.toOneRelatedEntityField = toOneRelatedEntityField;
37
        this.cachingPresenter = entityCache;
38
    }
39

    
40
    /**
41
     * {@inheritDoc}
42
     */
43
    @Override
44
    public void valueChange(ValueChangeEvent event) {
45

    
46

    
47
        CDM value = (CDM)event.getProperty().getValue();
48
        if(value == null) {
49
            return;
50
        }
51
        value = HibernateProxyHelper.deproxy(value);
52

    
53
        EntityCache cache = cachingPresenter.getCache();
54
        if(cache != null){
55
            cache.update();
56
            CDM cachedEntity = cache.find(value);
57
            if(cachedEntity == null){
58
                cache.add(value);
59
            } else if(cachedEntity != value){
60
                toOneRelatedEntityField.removeValueChangeListener(this);
61
                toOneRelatedEntityField.setValue(null); // reset to trick equals check in vaadin
62
                toOneRelatedEntityField.setValue(cachedEntity);
63
                toOneRelatedEntityField.addValueChangeListener(this);
64

    
65
            }
66
        }
67
    }
68

    
69
}
(16-16/18)