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   * @version $Id: VerifierFactoryListModel.html 1018313 2017-09-18 09:03:04Z britter $
32   */
33  public class VerifierFactoryListModel implements VerifierFactoryObserver, javax.swing.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); // fill cache.
41      }
42  
43      @Override
44      public synchronized void update(final String s) {
45          final Verifier[] verifiers = VerifierFactory.getVerifiers();
46          final int num_of_verifiers = verifiers.length;
47          cache.clear();
48          for (final Verifier verifier : verifiers) {
49              cache.add(verifier.getClassName());
50          }
51          for (final ListDataListener listener : listeners) {
52              final ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1);
53              listener.contentsChanged(e);
54          }
55      }
56  
57      @Override
58      public synchronized void addListDataListener(final ListDataListener l) {
59          listeners.add(l);
60      }
61  
62      @Override
63      public synchronized void removeListDataListener(final javax.swing.event.ListDataListener l) {
64          listeners.remove(l);
65      }
66  
67      @Override
68      public synchronized int getSize() {
69          return cache.size();
70      }
71  
72      @Override
73      public synchronized String getElementAt(final int index) {
74          return cache.toArray(new String[cache.size()])[index];
75      }
76  
77  }