Skip to content

Commit

Permalink
UI: tables with free seats always shows on top of the games list;
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDi85 committed Feb 1, 2019
1 parent cd23125 commit c7ccc33
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions Mage.Client/src/main/java/mage/client/table/TablesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -175,8 +176,8 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
defaultLabel.setHorizontalAlignment(JLabel.CENTER);
// colors
String val = (String) value;
String[] valsList = val.split("/");
if (valsList.length == 2 && !valsList[0].equals(valsList[1])) {
int[] seats = parseSeatsInfo(val);
if (seats[0] != seats[1]) {
// green draw
Color defaultBack = defaultLabel.getBackground();
greenLabel.setText(val);
Expand All @@ -194,6 +195,17 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
}
};

private int[] parseSeatsInfo(String info) {
String[] valsList = info.split("/");
int[] res = {0, 0};
if (valsList.length == 2) {
res[0] = Integer.parseInt(valsList[0]);
res[1] = Integer.parseInt(valsList[1]);
}
return res;
}


/**
* Creates new form TablesPanel
*/
Expand All @@ -213,7 +225,27 @@ public TablesPanel() {

// 1. TABLE CURRENT
tableTables.createDefaultColumnsFromModel();
activeTablesSorter = new MageTableRowSorter(tableModel);
activeTablesSorter = new MageTableRowSorter(tableModel) {
@Override
public void toggleSortOrder(int column) {
// special sort for created and seat column
if (column == TablesTableModel.COLUMN_CREATED || column == TablesTableModel.COLUMN_SEATS) {
List<? extends SortKey> sortKeys = getSortKeys();
if (!sortKeys.isEmpty() && sortKeys.size() == 2) {
// clear sort on second click
setSortKeys(null);
} else {
// setup sort on first click
ArrayList list = new ArrayList();
list.add(new RowSorter.SortKey(TablesTableModel.COLUMN_SEATS, SortOrder.ASCENDING));
list.add(new RowSorter.SortKey(TablesTableModel.COLUMN_CREATED, SortOrder.DESCENDING));
setSortKeys(list);
}
} else {
super.toggleSortOrder(column);
}
}
};
tableTables.setRowSorter(activeTablesSorter);

// time ago
Expand All @@ -232,8 +264,28 @@ public int compare(Date v1, Date v2) {
});*/

// seats sorter (free tables must be first)
activeTablesSorter.setComparator(TablesTableModel.COLUMN_SEATS, new Comparator<String>() {
@Override
public int compare(String v1, String v2) {
int[] seats1 = parseSeatsInfo(v1);
int[] seats2 = parseSeatsInfo(v2);
boolean free1 = seats1[0] != seats1[1];
boolean free2 = seats2[0] != seats2[1];

// free seats go first
if (free1 || free2) {
return Boolean.compare(free2, free1);
}

// all other seats go without sorts
return 0;
}
});

// default sort by created date (last games from above)
ArrayList list = new ArrayList();
list.add(new RowSorter.SortKey(TablesTableModel.COLUMN_SEATS, SortOrder.ASCENDING));
list.add(new RowSorter.SortKey(TablesTableModel.COLUMN_CREATED, SortOrder.DESCENDING));
activeTablesSorter.setSortKeys(list);

Expand Down Expand Up @@ -1604,5 +1656,4 @@ public void actionPerformed(ActionEvent e) {
}

}

}

0 comments on commit c7ccc33

Please sign in to comment.