Source code

    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 */
    032public class VerifierFactoryListModel implements VerifierFactoryObserver, javax.swing.ListModel<String> {
    033
    034    private final List<ListDataListener> listeners = new ArrayList<>();
    035    private final Set<String> cache = new TreeSet<>();
    036
    037    public VerifierFactoryListModel() {
    038        VerifierFactory.attach(this);
    039        update(null); // fill cache.
    040    }
    041
    042    @Override
    043    public synchronized void update(final String s) {
    044        final Verifier[] verifiers = VerifierFactory.getVerifiers();
    045        final int num_of_verifiers = verifiers.length;
    046        cache.clear();
    047        for (final Verifier verifier : verifiers) {
    048            cache.add(verifier.getClassName());
    049        }
    050        for (final ListDataListener listener : listeners) {
    051            final ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, num_of_verifiers - 1);
    052            listener.contentsChanged(e);
    053        }
    054    }
    055
    056    @Override
    057    public synchronized void addListDataListener(final ListDataListener l) {
    058        listeners.add(l);
    059    }
    060
    061    @Override
    062    public synchronized void removeListDataListener(final javax.swing.event.ListDataListener l) {
    063        listeners.remove(l);
    064    }
    065
    066    @Override
    067    public synchronized int getSize() {
    068        return cache.size();
    069    }
    070
    071    @Override
    072    public synchronized String getElementAt(final int index) {
    073        return cache.toArray(new String[cache.size()])[index];
    074    }
    075
    076}