    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.ListModel;
    026import javax.swing.event.ListDataEvent;
    027import javax.swing.event.ListDataListener;
    028
    029import org.apache.commons.lang3.ArrayUtils;
    030
    031/**
    032 * This class implements an adapter; it implements both a Swing ListModel and a VerifierFactoryObserver.
    033 *
    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 num_of_verifiers = 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, num_of_verifiers - 1));
    075        }
    076    }
    077
    078}