autoselect first item when choosing order of simultaneous abilities and autoselect next item when current item is added/removed

This commit is contained in:
myk
2013-02-02 11:41:37 +00:00
parent e2f62c40fb
commit d6212b16ca
2 changed files with 73 additions and 28 deletions

View File

@@ -11,6 +11,9 @@ import java.util.List;
import javax.swing.JDialog;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
@@ -66,8 +69,15 @@ public class DualListBox<T> extends FPanel {
this.remainingObjects = remainingObjects;
initScreen();
orderedLabel.setText(label);
if (sourceElements != null) {
if (sourceElements != null && !sourceElements.isEmpty()) {
addSourceElements(sourceElements);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
sourceList.setSelectedIndex(0);
}
});
}
if (destElements != null) {
addDestinationElements(destElements);
@@ -135,19 +145,17 @@ public class DualListBox<T> extends FPanel {
}
private void clearSourceSelected() {
Object[] selected = sourceList.getSelectedValues();
int[] selected = sourceList.getSelectedIndices();
for (int i = selected.length - 1; i >= 0; --i) {
sourceListModel.removeElement(selected[i]);
}
sourceList.getSelectionModel().clearSelection();
}
private void clearDestinationSelected() {
Object[] selected = destList.getSelectedValues();
int[] selected = destList.getSelectedIndices();
for (int i = selected.length - 1; i >= 0; --i) {
destListModel.removeElement(selected[i]);
}
destList.getSelectionModel().clearSelection();
}
public List<T> getOrderedList() {
@@ -159,25 +167,64 @@ public class DualListBox<T> extends FPanel {
return sourceListModel.model;
}
private static void showSelectedCard(Object obj) {
Card card = null;
if (obj instanceof Card) {
card = (Card) obj;
} else if (obj instanceof SpellAbility) {
card = ((SpellAbility) obj).getSourceCard();
} else if (obj instanceof CardPrinted) {
card = ((CardPrinted) obj).getMatchingForgeCard();
}
GuiUtils.clearPanelSelections();
if (card != null) {
CMatchUI.SINGLETON_INSTANCE.setCard(card);
GuiUtils.setPanelSelection(card);
}
}
private static void addCardViewListener(final FList list) {
list.getModel().addListDataListener(new ListDataListener() {
@Override
public void intervalRemoved(ListDataEvent e) {
ListModel model = list.getModel();
if (0 == model.getSize()) {
// nothing left to show
return;
}
int cardIdxPre = e.getIndex0();
if (model.getSize() <= cardIdxPre) {
// the last element got removed, get the one above it
--cardIdxPre;
}
final int cardIdx = cardIdxPre;
showSelectedCard(model.getElementAt(cardIdx));
// invoke this later since the list is out of sync with the model
// at this moment.
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
list.setSelectedIndex(cardIdx);
}
});
}
@Override
public void intervalAdded(ListDataEvent e) {
}
@Override
public void contentsChanged(ListDataEvent e) {
}
});
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(final ListSelectionEvent ev) {
Card card = null;
Object obj = list.getSelectedValue();
if (obj instanceof Card) {
card = (Card) obj;
} else if (obj instanceof SpellAbility) {
card = ((SpellAbility) obj).getSourceCard();
} else if (obj instanceof CardPrinted) {
card = ((CardPrinted) obj).getMatchingForgeCard();
}
GuiUtils.clearPanelSelections();
if (card != null) {
CMatchUI.SINGLETON_INSTANCE.setCard(card);
GuiUtils.setPanelSelection(card);
}
showSelectedCard(list.getSelectedValue());
}
});
}

View File

@@ -1,8 +1,8 @@
package forge.gui;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractListModel;
@@ -13,7 +13,7 @@ public class UnsortedListModel<T> extends AbstractListModel { // Java 7 has a ge
List<T> model;
public UnsortedListModel() {
model = new LinkedList<T>();
model = new ArrayList<T>();
}
@Override
@@ -58,11 +58,9 @@ public class UnsortedListModel<T> extends AbstractListModel { // Java 7 has a ge
return model.iterator();
}
public boolean removeElement(Object element) {
boolean removed = model.remove(element);
if (removed) {
fireContentsChanged(this, 0, getSize());
}
return removed;
public void removeElement(int idx) {
model.remove(idx);
fireIntervalRemoved(this, idx, idx);
fireContentsChanged(this, 0, getSize());
}
}