Remember column order in Deck Editor

This commit is contained in:
drdev
2013-11-03 17:47:57 +00:00
parent f3560238fe
commit bde5967ef4
4 changed files with 70 additions and 4 deletions

View File

@@ -7,7 +7,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JTable;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
@@ -37,6 +36,7 @@ public class SItemManagerIO {
enumval, /** */
identifier, /** */
show, /** */
index, /** */
sortpriority, /** */
sortstate, /** */
width
@@ -135,6 +135,7 @@ public class SItemManagerIO {
if (index == -1) {
index = COLS.get(c).getModelIndex();
}
COLS.get(c).setIndex(index); //update index property of column
writer.add(TAB);
writer.add(EVENT_FACTORY.createStartElement("", "", "col"));
@@ -144,6 +145,8 @@ public class SItemManagerIO {
ColumnProperty.identifier.toString(), COLS.get(c).getIdentifier().toString()));
writer.add(EVENT_FACTORY.createAttribute(
ColumnProperty.show.toString(), String.valueOf(COLS.get(c).isShowing())));
writer.add(EVENT_FACTORY.createAttribute(
ColumnProperty.index.toString(), String.valueOf(index)));
writer.add(EVENT_FACTORY.createAttribute(
ColumnProperty.sortpriority.toString(), String.valueOf(COLS.get(c).getSortPriority())));
writer.add(EVENT_FACTORY.createAttribute(
@@ -228,6 +231,9 @@ public class SItemManagerIO {
else if (attribute.getName().toString().equals(ColumnProperty.show.toString())) {
tempcol.setShowing(Boolean.valueOf(attribute.getValue()));
}
else if (attribute.getName().toString().equals(ColumnProperty.index.toString())) {
tempcol.setIndex(Integer.valueOf(attribute.getValue()));
}
else if (attribute.getName().toString().equals(ColumnProperty.sortpriority.toString())) {
tempcol.setSortPriority(Integer.valueOf(attribute.getValue()));
}

View File

@@ -26,6 +26,8 @@ import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JComponent;
@@ -119,6 +121,14 @@ public final class ItemTable<T extends InventoryItem> extends JTable {
public void setup(final List<TableColumnInfo<InventoryItem>> cols) {
final DefaultTableColumnModel colmodel = new DefaultTableColumnModel();
//ensure columns ordered properly
Collections.sort(cols, new Comparator<TableColumnInfo<InventoryItem>>() {
@Override
public int compare(TableColumnInfo<InventoryItem> arg0, TableColumnInfo<InventoryItem> arg1) {
return Integer.compare(arg0.getIndex(), arg1.getIndex());
}
});
for (TableColumnInfo<InventoryItem> item : cols) {
item.setModelIndex(colmodel.getColumnCount());
if (item.isShowing()) { colmodel.addColumn(item); }

View File

@@ -39,6 +39,7 @@ public class TableColumnInfo<T> extends TableColumn {
private int sortPriority = 0;
private boolean show = true;
private String enumval;
private int index = 0;
private Function<Entry<T, Integer>, Comparable<?>> fnSort;
private Function<Entry<T, Integer>, Object> fnDisplay;
@@ -66,6 +67,24 @@ public class TableColumnInfo<T> extends TableColumn {
this.enumval = val0;
}
/**
* Index within table columns
*
* @return int
*/
public int getIndex() {
return this.index;
}
/**
* Index within table columns
*
* @param index0 &emsp; int
*/
public void setIndex(final int index0) {
this.index = index0;
}
/**
* Position in sort cascade, 0 for no priority.
*
@@ -78,10 +97,10 @@ public class TableColumnInfo<T> extends TableColumn {
/**
* Position in sort cascade, 0 for no priority.
*
* @param position0 &emsp; int
* @param sortPriority0 &emsp; int
*/
public void setSortPriority(final int position0) {
this.sortPriority = position0;
public void setSortPriority(final int sortPriority0) {
this.sortPriority = sortPriority0;
}
/** @return {@link forge.gui.toolbox.itemmanager.ItemTableModel.SortState} */

View File

@@ -3,6 +3,13 @@ package forge.util;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TypeUtil {
@@ -44,4 +51,28 @@ public class TypeUtil {
}
}
}
public static <K, V extends Comparable<V>> Map<K, V> sortMap(Map<K, V> unsortedMap, final boolean ascending){
List<Entry<K, V>> list = new LinkedList<Entry<K, V>>(unsortedMap.entrySet());
//sort the list based on values
Collections.sort(list, new Comparator<Entry<K, V>>() {
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
if (ascending) {
return o1.getValue().compareTo(o2.getValue());
}
else {
return o2.getValue().compareTo(o1.getValue());
}
}
});
//maintain insertion order with the help of LinkedList
Map<K, V> sortedMap = new LinkedHashMap<K, V>();
for (Entry<K, V> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}