Skip to content

Commit

Permalink
Merge pull request #244 from Wolfsblvt/Save_Sortings
Browse files Browse the repository at this point in the history
Fixes Sorting lost after refresh + Sorting saved
  • Loading branch information
Ljaysoft committed Aug 14, 2016
2 parents f407691 + baa78d8 commit bab0927
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/me/corriekay/pokegoutil/utils/ConfigKey.java
@@ -1,5 +1,7 @@
package me.corriekay.pokegoutil.utils;

import javax.swing.*;

public enum ConfigKey {

DEVELOPFLAG("develop", false, Boolean.class),
Expand All @@ -14,6 +16,11 @@ public enum ConfigKey {
WINDOW_POS_X("options.window.posx", 0, Integer.class),
WINDOW_POS_Y("options.window.posy", 0, Integer.class),

SORT_COLINDEX_1("options.sort.1.colIndex", 0, Integer.class),
SORT_ORDER_1("options.sort.1.order", SortOrder.ASCENDING.toString(), String.class),
SORT_COLINDEX_2("options.sort.2.colIndex", 12, Integer.class),
SORT_ORDER_2("options.sort.2.order", SortOrder.DESCENDING.toString(), String.class),

TRANSFER_AFTER_EVOLVE("settings.transferAfterEvolve", false, Boolean.class),
SHOW_BULK_POPUP("settings.popupAfterBulk", true, Boolean.class),
INCLUDE_FAMILY("settings.includeFamily", true, Boolean.class),
Expand Down
65 changes: 53 additions & 12 deletions src/me/corriekay/pokegoutil/windows/PokemonTab.java
Expand Up @@ -9,13 +9,17 @@
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.map.pokemon.EvolutionResult;
import com.pokegoapi.api.player.PlayerProfile.Currency;
import com.pokegoapi.api.pokemon.*;
import me.corriekay.pokegoutil.utils.*;
import com.pokegoapi.api.pokemon.Pokemon;
import com.pokegoapi.api.pokemon.PokemonMeta;
import com.pokegoapi.api.pokemon.PokemonMetaRegistry;
import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.Utilities;
import me.corriekay.pokegoutil.utils.helpers.DateHelper;
import me.corriekay.pokegoutil.utils.helpers.JTableColumnPacker;
import me.corriekay.pokegoutil.utils.helpers.LDocumentListener;
import me.corriekay.pokegoutil.utils.pokemon.PokeHandler;
import me.corriekay.pokegoutil.utils.pokemon.PokemonCpUtils;
import me.corriekay.pokegoutil.utils.helpers.DateHelper;
import me.corriekay.pokegoutil.utils.pokemon.PokemonUtils;
import me.corriekay.pokegoutil.utils.ui.GhostText;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -27,18 +31,14 @@
import javax.swing.RowSorter.SortKey;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import java.util.*;
import java.util.List;
import java.util.function.BiConsumer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

@SuppressWarnings("serial")
public class PokemonTab extends JPanel {
Expand Down Expand Up @@ -727,12 +727,26 @@ private static class PokemonTable extends JTable {
* 27 Double - Move 1 Rating
* 28 Double - Move 2 Rating
*/
int sortColIndex = 0;
SortOrder so = SortOrder.ASCENDING;
ConfigNew config = ConfigNew.getConfig();

int sortColIndex1, sortColIndex2;
SortOrder sortOrder1, sortOrder2;

private PokemonTable() {
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setAutoResizeMode(AUTO_RESIZE_OFF);

// Load sort configs
sortColIndex1 = config.getInt(ConfigKey.SORT_COLINDEX_1);
sortColIndex2 = config.getInt(ConfigKey.SORT_COLINDEX_2);
try {
sortOrder1 = SortOrder.valueOf(config.getString(ConfigKey.SORT_ORDER_1));
sortOrder2 = SortOrder.valueOf(config.getString(ConfigKey.SORT_ORDER_2));
} catch (IllegalArgumentException e) {
e.printStackTrace();
sortOrder1 = SortOrder.ASCENDING;
sortOrder2 = SortOrder.ASCENDING;
}
}

private void constructNewTableModel(PokemonGo go, List<Pokemon> pokes) {
Expand Down Expand Up @@ -772,10 +786,37 @@ private void constructNewTableModel(PokemonGo go, List<Pokemon> pokes) {
trs.setComparator(27, cDouble);
trs.setComparator(28, cDouble);
setRowSorter(trs);
trs.toggleSortOrder(sortColIndex);
List<SortKey> sortKeys = new ArrayList<>();
sortKeys.add(new SortKey(sortColIndex, so));
sortKeys.add(new SortKey(sortColIndex1, sortOrder1));
sortKeys.add(new SortKey(sortColIndex2, sortOrder2));
trs.setSortKeys(sortKeys);

// Add listener to save those sorting values
trs.addRowSorterListener(
e -> {
RowSorter sorter = e.getSource();
if (sorter != null) {
List<SortKey> keys = sorter.getSortKeys();
if (keys.size() > 0) {
System.out.println("DEBUG-> Size: " + keys.size());
for (SortKey key : keys) {
System.out.println("SortKey: { column: " + key.getColumn() + ", order: " + key.getSortOrder().toString() + "}");
}
SortKey prim = keys.get(0);
sortOrder1 = prim.getSortOrder();
config.setString(ConfigKey.SORT_ORDER_1, sortOrder1.toString());
sortColIndex1 = prim.getColumn();
config.setInt(ConfigKey.SORT_COLINDEX_1, sortColIndex1);
}
if (keys.size() > 1) {
SortKey sec = keys.get(1);
sortOrder2 = sec.getSortOrder();
config.setString(ConfigKey.SORT_ORDER_2, sortOrder2.toString());
sortColIndex2 = sec.getColumn();
config.setInt(ConfigKey.SORT_COLINDEX_2, sortColIndex2);
}
}
});
}
}

Expand Down

0 comments on commit bab0927

Please sign in to comment.