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 /** 041 * Constructs a VerifierFactoryListModel. 042 */ 043 public VerifierFactoryListModel() { 044 VerifierFactory.attach(this); 045 update(null); // fill cache. 046 } 047 048 @Override 049 public synchronized void addListDataListener(final ListDataListener l) { 050 listeners.add(l); 051 } 052 053 @Override 054 public synchronized String getElementAt(final int index) { 055 return cache.toArray(ArrayUtils.EMPTY_STRING_ARRAY)[index]; 056 } 057 058 @Override 059 public synchronized int getSize() { 060 return cache.size(); 061 } 062 063 @Override 064 public synchronized void removeListDataListener(final ListDataListener l) { 065 listeners.remove(l); 066 } 067 068 @Override 069 public synchronized void update(final String s) { 070 final Verifier[] verifiers = VerifierFactory.getVerifiers(); 071 final int verifierLen = verifiers.length; 072 cache.clear(); 073 for (final Verifier verifier : verifiers) { 074 cache.add(verifier.getClassName()); 075 } 076 for (final ListDataListener listener : listeners) { 077 listener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, verifierLen - 1)); 078 } 079 } 080 081}