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