    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.verifier;
    019
    020import java.util.ArrayList;
    021import java.util.List;
    022import java.util.Set;
    023import java.util.TreeSet;
    024
    025import javax.swing.event.ListDataEvent;
    026import javax.swing.event.ListDataListener;
    027
    028/**
    029 * This class implements an adapter; it implements both a Swing ListModel and a VerifierFactoryObserver.
    030 *
    031 * @version $Id: VerifierFactoryListModel.java 1749611 2016-06-21 21:53:40Z ggregory $
    032 */
    033public class VerifierFactoryListModel implements VerifierFactoryObserver, javax.swing.ListModel<String> {
    034
    035    private final List<ListDataListener> listeners = new ArrayList<>();
    036    private final Set<String> cache = new TreeSet<>();
    037
    038    public VerifierFactoryListModel() {
    039        VerifierFactory.attach(this);
    040        update(null); // fill cache.
    041    }
    042
    043    @Override
    044    public synchronized void update(final String s) {
    045        final Verifier[] verifiers = VerifierFactory.getVerifiers();
    046        final int num_of_verifiers = verifiers.length;
    047        cache.clear();
    048        for (final Verifier verifier : verifiers) {
    049            cache.add(verifier.getClassName());
    050        }
    051        for (final ListDataListener listener : listeners) {
    052            final ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1);
    053            listener.contentsChanged(e);
    054        }
    055    }
    056
    057    @Override
    058    public synchronized void addListDataListener(final ListDataListener l) {
    059        listeners.add(l);
    060    }
    061
    062    @Override
    063    public synchronized void removeListDataListener(final javax.swing.event.ListDataListener l) {
    064        listeners.remove(l);
    065    }
    066
    067    @Override
    068    public synchronized int getSize() {
    069        return cache.size();
    070    }
    071
    072    @Override
    073    public synchronized String getElementAt(final int index) {
    074        return cache.toArray(new String[cache.size()])[index];
    075    }
    076
    077}