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  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   * This class implements an adapter; it implements both a Swing ListModel and a VerifierFactoryObserver.
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); // fill cache.
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  }