View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.verifier;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import javax.swing.event.ListDataEvent;
26  import javax.swing.event.ListDataListener;
27  
28  /**
29   * This class implements an adapter; it implements both a Swing ListModel and a VerifierFactoryObserver.
30   *
31   */
32  public class VerifierFactoryListModel implements VerifierFactoryObserver, javax.swing.ListModel<String> {
33  
34      private final List<ListDataListener> listeners = new ArrayList<>();
35      private final Set<String> cache = new TreeSet<>();
36  
37      public VerifierFactoryListModel() {
38          VerifierFactory.attach(this);
39          update(null); // fill cache.
40      }
41  
42      @Override
43      public synchronized void update(final String s) {
44          final Verifier[] verifiers = VerifierFactory.getVerifiers();
45          final int num_of_verifiers = verifiers.length;
46          cache.clear();
47          for (final Verifier verifier : verifiers) {
48              cache.add(verifier.getClassName());
49          }
50          for (final ListDataListener listener : listeners) {
51              final ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1);
52              listener.contentsChanged(e);
53          }
54      }
55  
56      @Override
57      public synchronized void addListDataListener(final ListDataListener l) {
58          listeners.add(l);
59      }
60  
61      @Override
62      public synchronized void removeListDataListener(final javax.swing.event.ListDataListener l) {
63          listeners.remove(l);
64      }
65  
66      @Override
67      public synchronized int getSize() {
68          return cache.size();
69      }
70  
71      @Override
72      public synchronized String getElementAt(final int index) {
73          return cache.toArray(new String[cache.size()])[index];
74      }
75  
76  }