GuiChoose.order is also always invoked by EDT.

Found a right wrapper class to pass Callables to EDT,
This commit is contained in:
Maxmtg
2013-04-02 20:21:11 +00:00
parent a7a6a199e8
commit 1f9e4b07e3
2 changed files with 59 additions and 43 deletions

View File

@@ -16,13 +16,6 @@ import forge.control.input.InputSynchronized;
*/ */
public class FThreads { 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 { static {
System.out.printf("(FThreads static ctor): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() ); System.out.printf("(FThreads static ctor): Running on a machine with %d cpu core(s)%n", Runtime.getRuntime().availableProcessors() );
} }

View File

@@ -8,6 +8,9 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; 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.JDialog;
import javax.swing.JFrame; 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 @Override
public void run() { public List<T> call() {
ListChooser<T> c = new ListChooser<T>(message, min, max, choices); ListChooser<T> c = new ListChooser<T>(message, min, max, choices);
final JList list = c.getJList(); final JList list = c.getJList();
list.addListSelectionListener(new ListSelectionListener() { list.addListSelectionListener(new ListSelectionListener() {
@@ -126,12 +128,18 @@ public class GuiChoose {
}); });
c.show(); c.show();
GuiUtils.clearPanelSelections(); GuiUtils.clearPanelSelections();
result = c.getSelectedValues(); return c.getSelectedValues();
} }
}; };
FThreads.invokeInEDTAndWait(showChoice); FutureTask<List<T>> future = new FutureTask<List<T>>(showChoice);
return showChoice.getResult(); 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. // Nothing to choose here. Code uses this to just show a card.
@@ -153,9 +161,13 @@ public class GuiChoose {
} }
public static <T> List<T> order(final String title, final String top, int remainingObjects, public static <T> List<T> order(final String title, final String top, final int remainingObjects,
final List<T> sourceChoices, List<T> destChoices, Card referenceCard, boolean sideboardingMode) { final List<T> sourceChoices, final List<T> destChoices, final Card referenceCard, final boolean sideboardingMode) {
// An input box for handling the order of choices. // An input box for handling the order of choices.
Callable<List<T>> callable = new Callable<List<T>>() {
@Override
public List<T> call() throws Exception {
final JFrame frame = new JFrame(); final JFrame frame = new JFrame();
DualListBox<T> dual = new DualListBox<T>(remainingObjects, sourceChoices, destChoices); DualListBox<T> dual = new DualListBox<T>(remainingObjects, sourceChoices, destChoices);
dual.setSecondColumnLabelText(top); dual.setSecondColumnLabelText(top);
@@ -187,6 +199,17 @@ public class GuiChoose {
GuiUtils.clearPanelSelections(); GuiUtils.clearPanelSelections();
return objects; return objects;
} }
};
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();
}
return null;
}
// If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine // If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine
public static <T> T sortedOneOrNone(final String message, final T[] choices, Comparator<T> comparer) { public static <T> T sortedOneOrNone(final String message, final T[] choices, Comparator<T> comparer) {