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