Skip to content

Commit

Permalink
add multiselection and less than search
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Oct 26, 2024
1 parent 7249e29 commit 8a8023b
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
*/
abstract class EquipmentFilterToken implements FilterToken {

int qty;
boolean atleast;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@
package megamek.client.ui.advancedsearch;

import megamek.common.EquipmentType;
import megamek.common.MiscType;
import megamek.common.TechConstants;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* A table model for the advanced search weapon tab's equipment list
*/
class EquipmentTableModel extends AbstractTableModel {

static final int COL_QTY = 0;
static final int COL_NAME = 1;
static final int COL_COST = 2;
static final int COL_IS_CLAN = 3;
static final int COL_LEVEL = 4;
static final int N_COL = 5;
static final int COL_NAME = 0;
static final int COL_COST = 1;
static final int COL_IS_CLAN = 2;
static final int COL_LEVEL = 3;
static final int N_COL = 4;
static final int COL_INTERNAL_NAME = 5;

private final TWAdvancedSearchPanel twAdvancedSearchPanel;
private int[] qty;
private final List<EquipmentType> equipment = new ArrayList<>();
private final List<MiscType> equipment = new ArrayList<>();

EquipmentTableModel(TWAdvancedSearchPanel twAdvancedSearchPanel) {
this.twAdvancedSearchPanel = twAdvancedSearchPanel;
Expand All @@ -59,7 +57,6 @@ public int getColumnCount() {

int getPreferredWidth(int col) {
return switch (col) {
case COL_QTY -> 40;
case COL_NAME -> 400;
case COL_IS_CLAN -> 75;
case COL_COST -> 175;
Expand All @@ -71,7 +68,6 @@ int getPreferredWidth(int col) {
@Override
public String getColumnName(int column) {
return switch (column) {
case COL_QTY -> "Qty";
case COL_NAME -> "Name";
case COL_IS_CLAN -> "IS/Clan";
case COL_COST -> "Cost";
Expand All @@ -85,20 +81,13 @@ public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

@Override
public boolean isCellEditable(int row, int col) {
return col == COL_QTY;
}

void setData(List<EquipmentType> eq) {
void setData(List<MiscType> eq) {
equipment.clear();
equipment.addAll(eq);
qty = new int[eq.size()];
Arrays.fill(qty, 1);
fireTableDataChanged();
}

EquipmentType getEquipmentTypeAt(int row) {
MiscType getEquipmentTypeAt(int row) {
return equipment.get(row);
}

Expand All @@ -109,7 +98,6 @@ public Object getValueAt(int row, int col) {
}
EquipmentType eq = equipment.get(row);
return switch (col) {
case COL_QTY -> qty[row] + "";
case COL_NAME -> eq.getName();
case COL_IS_CLAN -> TechConstants.getTechName(eq.getTechLevel(twAdvancedSearchPanel.gameYear));
case COL_COST -> eq.getRawCost();
Expand All @@ -119,12 +107,4 @@ public Object getValueAt(int row, int col) {
default -> "?";
};
}

@Override
public void setValueAt(Object value, int row, int col) {
if (col == COL_QTY) {
qty[row] = Integer.parseInt((String) value);
fireTableCellUpdated(row, col);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ class EquipmentTypeFT extends EquipmentFilterToken {

String internalName;
String fullName;
int qty;

EquipmentTypeFT(String in, String fn, int q) {
this(in, fn, q, true);
}

EquipmentTypeFT(String in, String fn, int q, boolean atleast) {
internalName = in;
fullName = fn;
qty = q;
this.atleast = atleast;
}

@Override
public String toString() {
return qty + " " + fullName + ((qty != 1) ? "s" : "");
return (atleast ? "" : "less than ") + qty + " " + fullName + ((qty != 1) ? "s" : "");
}
}
23 changes: 13 additions & 10 deletions megamek/src/megamek/client/ui/advancedsearch/MekSearchFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private ExpNode createFTFromTokensRecursively(Iterator<FilterToken> toks, ExpNod
if (currNode == null) {
currNode = new ExpNode();
}
ExpNode newChild = new ExpNode(ft.internalName, ft.qty);
ExpNode newChild = new ExpNode(ft.internalName, ft.qty, ft.atleast);
currNode.children.add(newChild);
return createFTFromTokensRecursively(toks, currNode);

Expand All @@ -331,7 +331,7 @@ private ExpNode createFTFromTokensRecursively(Iterator<FilterToken> toks, ExpNod
currNode = new ExpNode();
}

ExpNode newChild = new ExpNode(ft.weaponClass, ft.qty);
ExpNode newChild = new ExpNode(ft.weaponClass, ft.qty, ft.atleast);
currNode.children.add(newChild);
return createFTFromTokensRecursively(toks, currNode);
}
Expand Down Expand Up @@ -988,10 +988,10 @@ private boolean evaluate(List<String> eq, List<Integer> qty, ExpNode n) {
// If the requested quantity is 0, then we match if and only if the total number
// of matching equipment is also 0.
// Otherwise, we match if the total equals or exceeds the requested amount.
if (n.qty == 0) {
return total == 0;
} else {
if (n.atleast) {
return total >= n.qty;
} else {
return total < n.qty;
}

} else {
Expand Down Expand Up @@ -1022,9 +1022,9 @@ private boolean evaluate(List<String> eq, List<Integer> qty, ExpNode n) {
// means that the unit isn't a match for the filter, as it has a
// weapon/equipment that is required to
// NOT be there.
if (currEq.equals(n.name) && n.qty > 0 && currQty >= n.qty) {
if (currEq.equals(n.name) && n.atleast && (currQty >= n.qty)) {
return true;
} else if (currEq.equals(n.name) && n.qty == 0) {
} else if (currEq.equals(n.name) && !n.atleast && (currQty >= n.qty)) {
return false;
}

Expand All @@ -1035,7 +1035,7 @@ private boolean evaluate(List<String> eq, List<Integer> qty, ExpNode n) {
// If the leaf quantity is 0, that means that the mek is a match. If the leaf
// quantity is non-zero, that means the mek isn't
// a match.
return n.qty == 0;
return !n.atleast;
}
}
// Otherwise, recurse on all the children and either AND the results
Expand Down Expand Up @@ -1091,6 +1091,7 @@ public static class ExpNode {
public WeaponClass weaponClass;
public int qty;
public List<ExpNode> children;
public boolean atleast;

public ExpNode() {
operation = BoolOp.NOP;
Expand Down Expand Up @@ -1118,22 +1119,24 @@ public ExpNode(ExpNode e) {
}
}

public ExpNode(String n, int q) {
public ExpNode(String n, int q, boolean atleast) {
parent = null;
name = n;
weaponClass = null;
qty = q;
operation = BoolOp.NOP;
children = new LinkedList<>();
this.atleast = atleast;
}

public ExpNode(WeaponClass n, int q) {
public ExpNode(WeaponClass n, int q, boolean atleast) {
parent = null;
name = null;
weaponClass = n;
qty = q;
operation = BoolOp.NOP;
children = new LinkedList<>();
this.atleast = atleast;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private JPanel createBaseComboBoxes() {
listTechLevel = new TriStateItemList(SimpleTechLevel.getAllSimpleTechLevelCodeName(), 5);

listTechBase = new TriStateItemList(Entity.getTechBaseDescriptions(), 4);
List<String> moveModes = Arrays.stream(EntityMovementMode.values()).map(EntityMovementMode::toString).toList();
List<String> moveModes = Arrays.stream(EntityMovementMode.values()).map(EntityMovementMode::toString).distinct().toList();
listMoveMode = new TriStateItemList(moveModes, 13);

JPanel baseComboBoxesPanel = new JPanel();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package megamek.client.ui.swing.table;
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.client.ui.advancedsearch;

import javax.swing.*;
import javax.swing.table.TableColumnModel;
Expand All @@ -7,71 +25,69 @@
import java.awt.event.MouseEvent;
import java.util.Vector;

public class MegaMekTable extends JTable {
private static final long serialVersionUID = 1L;
class SearchableTable extends JTable {

private static final int KEY_TIMEOUT = 1000;

private long lastSearch;

StringBuffer searchBuffer;

/**
* Determines which column in the table model will be used for searches.
*/
protected int searchColumn;

public MegaMekTable() {
public SearchableTable() {
super();
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(int numRows, int numColumns) {
public SearchableTable(int numRows, int numColumns) {
super(numRows, numColumns);
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(Object[][] rowData, Object[] columnNames) {
public SearchableTable(Object[][] rowData, Object[] columnNames) {
super(rowData, columnNames);
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(TableModel dm) {
public SearchableTable(TableModel dm) {
super(dm);
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(TableModel dm, int sc) {
public SearchableTable(TableModel dm, int sc) {
super(dm);
lastSearch = 0;
searchColumn = sc;
searchBuffer = new StringBuffer();
}

public MegaMekTable(TableModel dm, TableColumnModel cm) {
public SearchableTable(TableModel dm, TableColumnModel cm) {
super(dm, cm);
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(TableModel dm, TableColumnModel cm,
ListSelectionModel sm) {
public SearchableTable(TableModel dm, TableColumnModel cm,
ListSelectionModel sm) {
super(dm, cm, sm);
lastSearch = 0;
searchColumn = 0;
searchBuffer = new StringBuffer();
}

public MegaMekTable(Vector<Vector<String>> rowData, Vector<String> columnNames) {
public SearchableTable(Vector<Vector<String>> rowData, Vector<String> columnNames) {
super(rowData, columnNames);
lastSearch = 0;
searchColumn = 0;
Expand All @@ -96,7 +112,9 @@ public String getToolTipText(MouseEvent e) {
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
tip = getValueAt(rowIndex, colIndex).toString();
if (rowIndex >= 0) {
tip = getValueAt(rowIndex, colIndex).toString();
}
return tip;
}

Expand Down
12 changes: 6 additions & 6 deletions megamek/src/megamek/client/ui/advancedsearch/WeaponClassFT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
class WeaponClassFT extends EquipmentFilterToken {

WeaponClass weaponClass;
int qty;

WeaponClassFT(WeaponClass in_class, int in_qty) {
this(in_class, in_qty, true);
}

WeaponClassFT(WeaponClass in_class, int in_qty, boolean atleast) {
weaponClass = in_class;
qty = in_qty;
this.atleast = atleast;
}

@Override
public String toString() {
if (qty == 1) {
return qty + " " + weaponClass.toString();
} else {
return qty + " " + weaponClass.toString() + "s";
}
return (atleast ? "" : "less than ") + qty + " " + weaponClass.toString() + ((qty != 1) ? "s" : "");
}
}
Loading

0 comments on commit 8a8023b

Please sign in to comment.