Sort Abstract Assets in ComboBox.

This commit is contained in:
7u83 2018-11-30 14:48:30 +01:00
parent cde8670e97
commit 61a87f3a4d

View File

@ -29,6 +29,8 @@ import java.awt.Dialog;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector; import java.util.Vector;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -46,34 +48,52 @@ import opensesim.sesim.interfaces.Asset;
*/ */
public class AssetEditorPanel extends javax.swing.JPanel { public class AssetEditorPanel extends javax.swing.JPanel {
ArrayList<Class<AbstractAsset>> asset_types;
/** /**
* Creates new form AssetEditor * Creates new form AssetEditor
*/ */
public AssetEditorPanel() { public AssetEditorPanel() {
super(); super();
asset_types = Globals.getAvailableAssetsTypes(); asset_types = Globals.getAvailableAssetsTypes();
asset_types.sort(new Comparator<Class<AbstractAsset>>() {
@Override
public int compare(Class<AbstractAsset> o1, Class<AbstractAsset> o2) {
AbstractAsset a1, a2;
try {
a1 = o1.newInstance();
a2 = o2.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
return 0;
}
String t1, t2;
t1 = a1.getTypeName();
t2 = a2.getTypeName();
return t1.compareToIgnoreCase(t2);
}
});
initComponents(); initComponents();
symField.setLimit(Globals.MAX.SYMLEN); symField.setLimit(Globals.MAX.SYMLEN);
nameField.setLimit(Globals.MAX.NAMELEN); nameField.setLimit(Globals.MAX.NAMELEN);
} }
void initFields(AbstractAsset asset) {
void initFields(AbstractAsset asset){ if (asset == null) {
if(asset==null)
return; return;
}
symField.setText(asset.getSymbol()); symField.setText(asset.getSymbol());
nameField.setText(asset.getName()); nameField.setText(asset.getName());
} }
public JDialog dialog; public JDialog dialog;
ArrayList<Class<AbstractAsset>> asset_types;
ComboBoxModel getComboBoxModel() { ComboBoxModel getComboBoxModel() {
ArrayList vector = new ArrayList(); ArrayList vector = new ArrayList();
@ -90,10 +110,10 @@ public class AssetEditorPanel extends javax.swing.JPanel {
for (i = 0; i < asset_types.size(); i++) { for (i = 0; i < asset_types.size(); i++) {
AbstractAsset ait; AbstractAsset ait;
Class<?> asset_type = asset_types.get(i); Class <AbstractAsset> asset_type = asset_types.get(i);
try { try {
ait = (AbstractAsset) asset_type.newInstance(); ait = asset_type.newInstance();
vector.add(i, ait.getTypeName()); vector.add(i, ait.getTypeName());
// assetTypesComboBox.addItem(ait.getTypeName()); // assetTypesComboBox.addItem(ait.getTypeName());