Adds filter by pattern to the interface terminals

Some code cleanup
This commit is contained in:
yueh 2014-09-30 17:52:28 +02:00
parent 185500c017
commit 79837cf35f

View file

@ -4,8 +4,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -31,22 +31,19 @@ public class GuiInterfaceTerminal extends AEBaseGui
// TODO: copied from GuiMEMonitorable. It looks not changed, maybe unneeded? // TODO: copied from GuiMEMonitorable. It looks not changed, maybe unneeded?
int offsetX = 9; int offsetX = 9;
final HashMap<Long, ClientDCInternalInv> byId = new HashMap<Long, ClientDCInternalInv>(); private final HashMap<Long, ClientDCInternalInv> byId = new HashMap<Long, ClientDCInternalInv>();
final HashMultimap<String, ClientDCInternalInv> byName = HashMultimap.create(); private final HashMultimap<String, ClientDCInternalInv> byName = HashMultimap.create();
final ArrayList<String> names = new ArrayList<String>(); private final ArrayList<String> names = new ArrayList<String>();
private final ArrayList<Object> lines = new ArrayList<Object>();
private final EntityPlayer player;
ArrayList<Object> lines = new ArrayList<Object>(); private boolean refreshList = false;
LinkedList<SlotDisconnected> dcSlots = new LinkedList<SlotDisconnected>();
private MEGuiTextField searchField; private MEGuiTextField searchField;
private int getMaxRows()
{
return names.size() + byId.size();// unique names, and each inv row.
}
public GuiInterfaceTerminal(InventoryPlayer inventoryPlayer, PartMonitor te) public GuiInterfaceTerminal(InventoryPlayer inventoryPlayer, PartMonitor te)
{ {
super( new ContainerInterfaceTerminal( inventoryPlayer, te ) ); super( new ContainerInterfaceTerminal( inventoryPlayer, te ) );
this.player = inventoryPlayer.player;
myScrollBar = new GuiScrollbar(); myScrollBar = new GuiScrollbar();
xSize = 195; xSize = 195;
ySize = 222; ySize = 222;
@ -56,6 +53,7 @@ public class GuiInterfaceTerminal extends AEBaseGui
public void initGui() public void initGui()
{ {
super.initGui(); super.initGui();
myScrollBar.setLeft( 175 ); myScrollBar.setLeft( 175 );
myScrollBar.setHeight( 106 ); myScrollBar.setHeight( 106 );
myScrollBar.setTop( 18 ); myScrollBar.setTop( 18 );
@ -65,6 +63,7 @@ public class GuiInterfaceTerminal extends AEBaseGui
searchField.setMaxStringLength( 25 ); searchField.setMaxStringLength( 25 );
searchField.setTextColor( 0xFFFFFF ); searchField.setTextColor( 0xFFFFFF );
searchField.setVisible( true ); searchField.setVisible( true );
searchField.setFocused( true );
} }
@Override @Override
@ -170,8 +169,6 @@ public class GuiInterfaceTerminal extends AEBaseGui
} }
} }
boolean refreshList = false;
public void postUpdate(NBTTagCompound in) public void postUpdate(NBTTagCompound in)
{ {
if ( in.getBoolean( "clear" ) ) if ( in.getBoolean( "clear" ) )
@ -211,18 +208,42 @@ public class GuiInterfaceTerminal extends AEBaseGui
} }
} }
/**
* rebuilds the list of interfaces.
*
* Respects a search term if present (ignores case) and adding only matching patterns.
*/
private void refreshList() private void refreshList()
{ {
byName.clear(); byName.clear();
String searchFilterLowerCase = searchField.getText().toLowerCase();
String itemNameLowerCase; final String searchFilterLowerCase = searchField.getText().toLowerCase();
for (ClientDCInternalInv entry : byId.values()) for (ClientDCInternalInv entry : byId.values())
{ {
itemNameLowerCase = entry.getName().toLowerCase(); // Shortcut to skip any filter if search term is ""/empty
if ( !searchFilterLowerCase.equals( "" ) && !itemNameLowerCase.contains( searchFilterLowerCase ) ) boolean found = searchFilterLowerCase.isEmpty();
continue;
byName.put( entry.getName(), entry ); // Search if the current inventory holds a pattern containing the search term.
if ( !found && !searchFilterLowerCase.equals( "" ) )
{
for (ItemStack itemStack : entry.inv)
{
if ( itemStack != null )
{
String tooltipLowerCase = String.valueOf( itemStack.getTooltip( player, false ) ).toLowerCase();
if ( tooltipLowerCase.contains( searchFilterLowerCase ) )
{
found = true;
break;
}
}
}
}
// if found, filter skipped or machine name matching the search term, add it
if ( found || entry.getName().toLowerCase().contains( searchFilterLowerCase ) )
byName.put( entry.getName(), entry );
} }
names.clear(); names.clear();
@ -230,7 +251,9 @@ public class GuiInterfaceTerminal extends AEBaseGui
Collections.sort( names ); Collections.sort( names );
lines = new ArrayList<Object>( getMaxRows() ); lines.clear();
lines.ensureCapacity( getMaxRows() );
for (String n : names) for (String n : names)
{ {
lines.add( n ); lines.add( n );
@ -245,6 +268,16 @@ public class GuiInterfaceTerminal extends AEBaseGui
myScrollBar.setRange( 0, lines.size() - LINES_ON_PAGE, 2 ); myScrollBar.setRange( 0, lines.size() - LINES_ON_PAGE, 2 );
} }
/**
* The max amount of unique names and each inv row. Not affected by the filtering.
*
* @return
*/
private int getMaxRows()
{
return names.size() + byId.size();
}
private ClientDCInternalInv getById(long id, long sortBy, String string) private ClientDCInternalInv getById(long id, long sortBy, String string)
{ {
ClientDCInternalInv o = byId.get( id ); ClientDCInternalInv o = byId.get( id );