forked from Apple-2-Tools/jace
137 lines
4.4 KiB
Java
137 lines
4.4 KiB
Java
/*
|
|
* Copyright (C) 2012 Brendan Robert (BLuRry) brendan.robert@gmail.com.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301 USA
|
|
*/
|
|
package jace.config;
|
|
|
|
import jace.core.Utility;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
/**
|
|
*
|
|
* @author Brendan Robert (BLuRry) brendan.robert@gmail.com
|
|
*/
|
|
|
|
public class ClassSelection extends DynamicSelection<Class> {
|
|
|
|
Class template = null;
|
|
|
|
public ClassSelection(Class supertype, Class defaultValue) {
|
|
super(defaultValue);
|
|
template = supertype;
|
|
}
|
|
|
|
@Override
|
|
public LinkedHashMap<Class, String> getSelections() {
|
|
LinkedHashMap<Class, String> selections = new LinkedHashMap<Class, String>();
|
|
List<? extends Class> allClasses = (List<? extends Class>) Utility.findAllSubclasses(template);
|
|
if (!allClasses.contains(null)) {
|
|
allClasses.add(null);
|
|
}
|
|
List<Entry<Class, String>> values = new ArrayList<Map.Entry<Class, String>>();
|
|
if (allowNull()) {
|
|
values.add(new Entry<Class, String>() {
|
|
|
|
@Override
|
|
public Class getKey() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String getValue() {
|
|
return "***Empty***";
|
|
}
|
|
|
|
@Override
|
|
public String setValue(String v) {
|
|
throw new UnsupportedOperationException("Not supported yet.");
|
|
}
|
|
});
|
|
}
|
|
for (final Class c : allClasses) {
|
|
Entry<Class, String> entry = new Map.Entry<Class, String>() {
|
|
|
|
public Class getKey() {
|
|
return c;
|
|
}
|
|
|
|
public String getValue() {
|
|
if (c == null) {
|
|
return "**Empty**";
|
|
}
|
|
if (c.isAnnotationPresent(Name.class)) {
|
|
return ((Name) c.getAnnotation(Name.class)).value();
|
|
}
|
|
return c.getSimpleName();
|
|
}
|
|
|
|
public String setValue(String value) {
|
|
throw new UnsupportedOperationException("Not supported yet.");
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return getValue();
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return super.equals(obj) || obj == getKey() || getKey() != null && getKey().equals(obj);
|
|
}
|
|
};
|
|
values.add(entry);
|
|
}
|
|
Collections.sort(values, new Comparator<Map.Entry<? extends Class, String>>() {
|
|
public int compare(Entry<? extends Class, String> o1, Entry<? extends Class, String> o2) {
|
|
if (o1.getKey() == null) {
|
|
return -1;
|
|
}
|
|
if (o2.getKey() == null) {
|
|
return 1;
|
|
} else {
|
|
return (o1.getValue().compareTo(o2.getValue()));
|
|
}
|
|
}
|
|
});
|
|
for (Map.Entry<Class, String> entry : values) {
|
|
Class key = entry.getKey();
|
|
selections.put(key, entry.getValue());
|
|
}
|
|
return selections;
|
|
}
|
|
|
|
@Override
|
|
public boolean allowNull() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void setValue(Class value) {
|
|
Object v = value;
|
|
if (v != null && v instanceof String) {
|
|
super.setValueByMatch((String) v);
|
|
return;
|
|
}
|
|
super.setValue(value);
|
|
}
|
|
} |