Project

General

Profile

Download (2.91 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.vaadin.util;
11

    
12
import java.util.Iterator;
13
import java.util.List;
14
import java.util.Set;
15

    
16
import com.vaadin.ui.AbstractSelect;
17
import com.vaadin.ui.Component;
18
import com.vaadin.ui.HasComponents;
19
import com.vaadin.ui.UI;
20

    
21
import eu.etaxonomy.cdm.vaadin.component.CdmProgressComponent;
22

    
23
/**
24
 * @author cmathew
25
 * @date 7 Apr 2015
26
 *
27
 */
28
public class CdmVaadinUtilities {
29

    
30
    /**
31
     * Thread safe method to asynchronously perform an update.
32
     *
33
     * @param update
34
     */
35
    public static void asyncExec(Runnable update) {
36
        UI currentUI = UI.getCurrent();
37
        Thread t = new Thread(update);
38
        if(currentUI != null) {
39
            synchronized(currentUI) {
40
                t.start();
41
            }
42
        } else {
43
            throw new UnsupportedOperationException("Cannot execute update on a UI which is null");
44
        }
45
    }
46

    
47
    public static void exec(CdmVaadinOperation op) {
48
        if(op.isAsync()) {
49
            asyncExec(op);
50
        } else {
51
            op.run();
52
        }
53
    }
54

    
55

    
56
    public static void setEnabled(Component root, boolean isEnabled, List<? extends Component> exceptions) {
57
        if(exceptions != null && exceptions.contains(root)) {
58
            return;
59
        }
60
        if(root instanceof HasComponents) {
61
            HasComponents hc = (HasComponents) root;
62
            Iterator<Component> iterator = hc.iterator();
63
            while(iterator.hasNext()) {
64
                setEnabled(iterator.next(), isEnabled, exceptions);
65
            }
66
        } else {
67
            root.setEnabled(isEnabled);
68
        }
69
    }
70

    
71
    public static void enableOnlyProgressBar(Component component) {
72
        if(component instanceof CdmProgressComponent) {
73
            component.setEnabled(true);
74
            return;
75
        }
76
        if(component instanceof HasComponents) {
77
            HasComponents hc = (HasComponents) component;
78
            Iterator<Component> iterator = hc.iterator();
79
            while(iterator.hasNext()) {
80
                enableOnlyProgressBar(iterator.next());
81
            }
82
        } else {
83
            component.setEnabled(false);
84
        }
85
    }
86

    
87
    public static boolean isSelected(AbstractSelect select, Object itemId) {
88
        if(select.getValue() != null) {
89
            if(select.isMultiSelect()) {
90
                Set<Object> selectedItemIds = (Set<Object>)select.getValue();
91
                for(Object selectedItemId : selectedItemIds) {
92
                    if(selectedItemId.equals(itemId)) {
93
                        return true;
94
                    }
95
                }
96
            } else {
97
                return (select.getValue().equals(itemId));
98
            }
99
        }
100
        return false;
101
    }
102

    
103

    
104
}
(7-7/8)