mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 19:28:01 +00:00
GuiChoose.order is also always invoked by EDT.
Found a right wrapper class to pass Callables to EDT,
This commit is contained in:
@@ -16,13 +16,6 @@ import forge.control.input.InputSynchronized;
|
||||
*/
|
||||
public class FThreads {
|
||||
|
||||
// This could be some bad copy of SwingWorker
|
||||
public abstract static class RunnableWithResult<T> implements Runnable {
|
||||
protected T result = null;
|
||||
public T getResult() { return result; }
|
||||
|
||||
}
|
||||
|
||||
static {
|
||||
System.out.printf("(FThreads static ctor): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() );
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JFrame;
|
||||
@@ -104,10 +107,9 @@ public class GuiChoose {
|
||||
}
|
||||
}
|
||||
|
||||
FThreads.RunnableWithResult<List<T>> showChoice = new FThreads.RunnableWithResult<List<T>>() {
|
||||
|
||||
Callable<List<T>> showChoice = new Callable<List<T>>() {
|
||||
@Override
|
||||
public void run() {
|
||||
public List<T> call() {
|
||||
ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
|
||||
final JList list = c.getJList();
|
||||
list.addListSelectionListener(new ListSelectionListener() {
|
||||
@@ -126,12 +128,18 @@ public class GuiChoose {
|
||||
});
|
||||
c.show();
|
||||
GuiUtils.clearPanelSelections();
|
||||
result = c.getSelectedValues();
|
||||
return c.getSelectedValues();
|
||||
}
|
||||
};
|
||||
|
||||
FThreads.invokeInEDTAndWait(showChoice);
|
||||
return showChoice.getResult();
|
||||
FutureTask<List<T>> future = new FutureTask<List<T>>(showChoice);
|
||||
FThreads.invokeInEDTAndWait(future);
|
||||
try {
|
||||
return future.get();
|
||||
} catch (Exception e) { // should be no exception here
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Nothing to choose here. Code uses this to just show a card.
|
||||
@@ -153,39 +161,54 @@ public class GuiChoose {
|
||||
}
|
||||
|
||||
|
||||
public static <T> List<T> order(final String title, final String top, int remainingObjects,
|
||||
final List<T> sourceChoices, List<T> destChoices, Card referenceCard, boolean sideboardingMode) {
|
||||
public static <T> List<T> order(final String title, final String top, final int remainingObjects,
|
||||
final List<T> sourceChoices, final List<T> destChoices, final Card referenceCard, final boolean sideboardingMode) {
|
||||
// An input box for handling the order of choices.
|
||||
final JFrame frame = new JFrame();
|
||||
DualListBox<T> dual = new DualListBox<T>(remainingObjects, sourceChoices, destChoices);
|
||||
dual.setSecondColumnLabelText(top);
|
||||
|
||||
Callable<List<T>> callable = new Callable<List<T>>() {
|
||||
@Override
|
||||
public List<T> call() throws Exception {
|
||||
final JFrame frame = new JFrame();
|
||||
DualListBox<T> dual = new DualListBox<T>(remainingObjects, sourceChoices, destChoices);
|
||||
dual.setSecondColumnLabelText(top);
|
||||
|
||||
frame.setLayout(new BorderLayout());
|
||||
frame.setSize(dual.getPreferredSize());
|
||||
frame.add(dual);
|
||||
frame.setTitle(title);
|
||||
frame.setVisible(false);
|
||||
|
||||
dual.setSideboardMode(sideboardingMode);
|
||||
|
||||
final JDialog dialog = new JDialog(frame, true);
|
||||
dialog.setTitle(title);
|
||||
dialog.setContentPane(dual);
|
||||
dialog.setSize(dual.getPreferredSize());
|
||||
dialog.setLocationRelativeTo(null);
|
||||
dialog.pack();
|
||||
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
if (referenceCard != null) {
|
||||
CMatchUI.SINGLETON_INSTANCE.setCard(referenceCard);
|
||||
// MARKED FOR UPDATE
|
||||
}
|
||||
dialog.setVisible(true);
|
||||
|
||||
List<T> objects = dual.getOrderedList();
|
||||
|
||||
dialog.dispose();
|
||||
GuiUtils.clearPanelSelections();
|
||||
return objects;
|
||||
}
|
||||
};
|
||||
|
||||
frame.setLayout(new BorderLayout());
|
||||
frame.setSize(dual.getPreferredSize());
|
||||
frame.add(dual);
|
||||
frame.setTitle(title);
|
||||
frame.setVisible(false);
|
||||
|
||||
dual.setSideboardMode(sideboardingMode);
|
||||
|
||||
final JDialog dialog = new JDialog(frame, true);
|
||||
dialog.setTitle(title);
|
||||
dialog.setContentPane(dual);
|
||||
dialog.setSize(dual.getPreferredSize());
|
||||
dialog.setLocationRelativeTo(null);
|
||||
dialog.pack();
|
||||
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
if (referenceCard != null) {
|
||||
CMatchUI.SINGLETON_INSTANCE.setCard(referenceCard);
|
||||
// MARKED FOR UPDATE
|
||||
FutureTask<List<T>> ft = new FutureTask<List<T>>(callable);
|
||||
FThreads.invokeInEDTAndWait(ft);
|
||||
try {
|
||||
return ft.get();
|
||||
} catch (Exception e) { // we have waited enough
|
||||
e.printStackTrace();
|
||||
}
|
||||
dialog.setVisible(true);
|
||||
|
||||
List<T> objects = dual.getOrderedList();
|
||||
|
||||
dialog.dispose();
|
||||
GuiUtils.clearPanelSelections();
|
||||
return objects;
|
||||
return null;
|
||||
}
|
||||
|
||||
// If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine
|
||||
|
||||
Reference in New Issue
Block a user