OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 112 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 ilm 1
package org.openconcerto.modules.operation;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import javax.swing.AbstractListModel;
7
import javax.swing.SwingUtilities;
147 ilm 8
import javax.swing.SwingWorker;
112 ilm 9
 
10
import org.openconcerto.ui.list.CheckListItem;
11
 
12
public abstract class CheckListModel extends AbstractListModel {
13
    private List<CheckListItem> items = new ArrayList<CheckListItem>();
14
 
15
    @Override
16
    public int getSize() {
17
        return items.size();
18
    }
19
 
20
    @Override
21
    public Object getElementAt(int index) {
22
        return items.get(index);
23
    }
24
 
25
    public void loadContent() {
26
        assert SwingUtilities.isEventDispatchThread();
147 ilm 27
        SwingWorker<List<CheckListItem>, Object> worker = new SwingWorker<List<CheckListItem>, Object>() {
112 ilm 28
 
29
            @Override
30
            protected List<CheckListItem> doInBackground() throws Exception {
31
                return loadItems();
32
            }
33
 
34
            @Override
35
            protected void done() {
36
                try {
37
                    final List<CheckListItem> l = get();
38
                    if (items.isEmpty()) {
39
                        // On ajoute tout, et on sélectionne tout
40
                        items.clear();
41
                        items.addAll(l);
42
                        for (CheckListItem item : items) {
43
                            item.setSelected(true);
44
                        }
45
                    } else {
46
                        List<Object> previouslySelectedObject = new ArrayList<Object>();
47
                        for (CheckListItem item : items) {
48
                            if (item.isSelected()) {
49
                                previouslySelectedObject.add(item.getObject());
50
                            }
51
                        }
52
                        items.clear();
53
                        items.addAll(l);
54
                        // restaure la sélection
55
                        for (CheckListItem item : items) {
56
                            final boolean wasSelected = previouslySelectedObject.contains(item.getObject());
57
                            item.setSelected(wasSelected);
58
                        }
59
                    }
60
                    refresh();
61
                } catch (Exception e) {
62
                    e.printStackTrace();
63
                }
64
 
65
            }
66
        };
67
        worker.execute();
68
    }
69
 
70
    public abstract List<CheckListItem> loadItems();
71
 
72
    public void refresh() {
73
        fireContentsChanged(this, 0, items.size());
74
    }
75
}