1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.verifier;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.TreeSet;
23
24 import javax.swing.ListModel;
25 import javax.swing.event.ListDataEvent;
26 import javax.swing.event.ListDataListener;
27
28 import org.apache.commons.lang3.ArrayUtils;
29
30
31
32
33 public class VerifierFactoryListModel implements VerifierFactoryObserver, ListModel<String> {
34
35 private final List<ListDataListener> listeners = new ArrayList<>();
36 private final Set<String> cache = new TreeSet<>();
37
38 public VerifierFactoryListModel() {
39 VerifierFactory.attach(this);
40 update(null);
41 }
42
43 @Override
44 public synchronized void addListDataListener(final ListDataListener l) {
45 listeners.add(l);
46 }
47
48 @Override
49 public synchronized String getElementAt(final int index) {
50 return cache.toArray(ArrayUtils.EMPTY_STRING_ARRAY)[index];
51 }
52
53 @Override
54 public synchronized int getSize() {
55 return cache.size();
56 }
57
58 @Override
59 public synchronized void removeListDataListener(final ListDataListener l) {
60 listeners.remove(l);
61 }
62
63 @Override
64 public synchronized void update(final String s) {
65 final Verifier[] verifiers = VerifierFactory.getVerifiers();
66 final int verifierLen = verifiers.length;
67 cache.clear();
68 for (final Verifier verifier : verifiers) {
69 cache.add(verifier.getClassName());
70 }
71 for (final ListDataListener listener : listeners) {
72 listener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, verifierLen - 1));
73 }
74 }
75
76 }