mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
FThreads - renamed methods that plan tasks for EDT.
all calls to CMatchUI.setCard have to be performed from EDT, otherwise an exception will raise. - this fixes problems with Goblin Guides and Inquisition of Kozilek (and all others whose effects displayed a card in UI to confirm or choose from list)
This commit is contained in:
@@ -56,10 +56,20 @@ public class FThreads {
|
|||||||
* TODO: Write javadoc for this method.
|
* TODO: Write javadoc for this method.
|
||||||
* @param runnable
|
* @param runnable
|
||||||
*/
|
*/
|
||||||
public static void invokeInEDT(Runnable runnable) {
|
public static void invokeInEdtLater(Runnable runnable) {
|
||||||
SwingUtilities.invokeLater(runnable);
|
SwingUtilities.invokeLater(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: Write javadoc for this method.
|
||||||
|
*/
|
||||||
|
public static void invokeInEdtNowOrLater(Runnable proc) {
|
||||||
|
if( isEDT() )
|
||||||
|
proc.run();
|
||||||
|
else
|
||||||
|
invokeInEdtLater(proc);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke the given Runnable in an Event Dispatch Thread and wait for it to
|
* Invoke the given Runnable in an Event Dispatch Thread and wait for it to
|
||||||
* finish; but <B>try to use SwingUtilities.invokeLater instead whenever
|
* finish; but <B>try to use SwingUtilities.invokeLater instead whenever
|
||||||
@@ -72,7 +82,7 @@ public class FThreads {
|
|||||||
* the Runnable to run
|
* the Runnable to run
|
||||||
* @see javax.swing.SwingUtilities#invokeLater(Runnable)
|
* @see javax.swing.SwingUtilities#invokeLater(Runnable)
|
||||||
*/
|
*/
|
||||||
public static void invokeInEDTAndWait(final Runnable proc) {
|
public static void invokeInEdtAndWait(final Runnable proc) {
|
||||||
if (SwingUtilities.isEventDispatchThread()) {
|
if (SwingUtilities.isEventDispatchThread()) {
|
||||||
// Just run in the current thread.
|
// Just run in the current thread.
|
||||||
proc.run();
|
proc.run();
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public class InputLockUI implements Input {
|
|||||||
public void run() {
|
public void run() {
|
||||||
if ( ixCall != iCall.get() || !isActive()) // cancel the message if it's not from latest call or input is gone already
|
if ( ixCall != iCall.get() || !isActive()) // cancel the message if it's not from latest call or input is gone already
|
||||||
return;
|
return;
|
||||||
FThreads.invokeInEDT(showMessageFromEdt);
|
FThreads.invokeInEdtLater(showMessageFromEdt);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -202,8 +202,6 @@ public class MatchController {
|
|||||||
CMessage.SINGLETON_INSTANCE.updateGameInfo(this);
|
CMessage.SINGLETON_INSTANCE.updateGameInfo(this);
|
||||||
// Update observers
|
// Update observers
|
||||||
currentGame.getGameLog().updateObservers();
|
currentGame.getGameLog().updateObservers();
|
||||||
|
|
||||||
CMatchUI.SINGLETON_INSTANCE.setCard(Singletons.getControl().getPlayer().getCardsIn(ZoneType.Hand).get(0));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
BugReporter.reportException(e);
|
BugReporter.reportException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,8 +58,6 @@ import forge.game.player.AIPlayer;
|
|||||||
import forge.game.player.HumanPlayer;
|
import forge.game.player.HumanPlayer;
|
||||||
import forge.game.player.Player;
|
import forge.game.player.Player;
|
||||||
import forge.gui.GuiChoose;
|
import forge.gui.GuiChoose;
|
||||||
import forge.gui.framework.EDocID;
|
|
||||||
import forge.gui.framework.SDisplayUtil;
|
|
||||||
import forge.util.MyObservable;
|
import forge.util.MyObservable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -607,7 +605,6 @@ public class MagicStack extends MyObservable {
|
|||||||
game.getPhaseHandler().setPriority(sp.getActivatingPlayer());
|
game.getPhaseHandler().setPriority(sp.getActivatingPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc());
|
|
||||||
this.updateObservers();
|
this.updateObservers();
|
||||||
|
|
||||||
if (sp.isSpell() && !sp.getSourceCard().isCopiedSpell()) {
|
if (sp.isSpell() && !sp.getSourceCard().isCopiedSpell()) {
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ public class GuiChoose {
|
|||||||
};
|
};
|
||||||
|
|
||||||
FutureTask<List<T>> future = new FutureTask<List<T>>(showChoice);
|
FutureTask<List<T>> future = new FutureTask<List<T>>(showChoice);
|
||||||
FThreads.invokeInEDTAndWait(future);
|
FThreads.invokeInEdtAndWait(future);
|
||||||
try {
|
try {
|
||||||
return future.get();
|
return future.get();
|
||||||
} catch (Exception e) { // should be no exception here
|
} catch (Exception e) { // should be no exception here
|
||||||
@@ -200,7 +200,7 @@ public class GuiChoose {
|
|||||||
};
|
};
|
||||||
|
|
||||||
FutureTask<List<T>> ft = new FutureTask<List<T>>(callable);
|
FutureTask<List<T>> ft = new FutureTask<List<T>>(callable);
|
||||||
FThreads.invokeInEDTAndWait(ft);
|
FThreads.invokeInEdtAndWait(ft);
|
||||||
try {
|
try {
|
||||||
return ft.get();
|
return ft.get();
|
||||||
} catch (Exception e) { // we have waited enough
|
} catch (Exception e) { // we have waited enough
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
package forge.gui;
|
package forge.gui;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.FutureTask;
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import forge.Card;
|
import forge.Card;
|
||||||
|
import forge.FThreads;
|
||||||
import forge.Singletons;
|
import forge.Singletons;
|
||||||
import forge.game.event.FlipCoinEvent;
|
import forge.game.event.FlipCoinEvent;
|
||||||
import forge.game.player.Player;
|
import forge.game.player.Player;
|
||||||
@@ -26,23 +32,28 @@ public class GuiDialog {
|
|||||||
return GuiDialog.confirm(c, question, true, options);
|
return GuiDialog.confirm(c, question, true, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean confirm(final Card c, String question, final boolean defaultIsYes, final String[] options) {
|
public static boolean confirm(final Card c, final String question, final boolean defaultIsYes, final String[] options) {
|
||||||
|
Callable<Boolean> confirmTask = new Callable<Boolean>() {
|
||||||
|
@Override
|
||||||
|
public Boolean call() throws Exception {
|
||||||
CMatchUI.SINGLETON_INSTANCE.setCard(c);
|
CMatchUI.SINGLETON_INSTANCE.setCard(c);
|
||||||
final StringBuilder title = new StringBuilder();
|
final String title = c == null ? "Question" : c.getName() + " - Ability";
|
||||||
if ( c != null)
|
String questionToUse = StringUtils.isBlank(question) ? "Activate card's ability?" : question;
|
||||||
title.append(c.getName()).append(" - Ability");
|
|
||||||
|
|
||||||
if (!(question.length() > 0)) {
|
|
||||||
question = "Activate card's ability?";
|
|
||||||
}
|
|
||||||
|
|
||||||
int answer;
|
|
||||||
|
|
||||||
String[] opts = options == null ? defaultConfirmOptions : options;
|
String[] opts = options == null ? defaultConfirmOptions : options;
|
||||||
answer = JOptionPane.showOptionDialog(null, question, title.toString(), JOptionPane.YES_NO_OPTION,
|
int answer = JOptionPane.showOptionDialog(null, questionToUse, title,
|
||||||
JOptionPane.QUESTION_MESSAGE, null, opts, opts[defaultIsYes ? 0 : 1]);
|
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
|
||||||
|
opts, opts[defaultIsYes ? 0 : 1]);
|
||||||
return answer == JOptionPane.YES_OPTION;
|
return answer == JOptionPane.YES_OPTION;
|
||||||
|
}};
|
||||||
|
|
||||||
|
FutureTask<Boolean> future = new FutureTask<Boolean>(confirmTask);
|
||||||
|
FThreads.invokeInEdtAndWait(future);
|
||||||
|
try {
|
||||||
|
return future.get().booleanValue();
|
||||||
|
} catch (Exception e) { // should be no exception here
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public class InputProxy implements Observer {
|
|||||||
// if( nextInput instanceof AiInput )
|
// if( nextInput instanceof AiInput )
|
||||||
// FThreads.invokeInNewThread(showMessage, true);
|
// FThreads.invokeInNewThread(showMessage, true);
|
||||||
// else
|
// else
|
||||||
FThreads.invokeInEDT(showMessage);
|
FThreads.invokeInEdtLater(showMessage);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import java.util.TimerTask;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
import forge.FThreads;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Experimental static factory for generic operations carried out
|
* Experimental static factory for generic operations carried out
|
||||||
* onto specific members of the framework. Doublestrike 11-04-12
|
* onto specific members of the framework. Doublestrike 11-04-12
|
||||||
@@ -86,6 +88,11 @@ public class SDisplayUtil {
|
|||||||
|
|
||||||
/** @param tab0   {@link java.gui.framework.IVDoc} */
|
/** @param tab0   {@link java.gui.framework.IVDoc} */
|
||||||
public static void showTab(final IVDoc<? extends ICDoc> tab0) {
|
public static void showTab(final IVDoc<? extends ICDoc> tab0) {
|
||||||
|
|
||||||
|
Runnable showTabRoutine = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
FThreads.assertExecutedByEdt(true);
|
||||||
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
|
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
|
||||||
tab0.getParentCell().setSelected(tab0);
|
tab0.getParentCell().setSelected(tab0);
|
||||||
// set focus back to previous owner, if any
|
// set focus back to previous owner, if any
|
||||||
@@ -93,4 +100,7 @@ public class SDisplayUtil {
|
|||||||
c.requestFocusInWindow();
|
c.requestFocusInWindow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
FThreads.invokeInEdtNowOrLater(showTabRoutine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ public enum CMatchUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Object[] result = { null }; // how else can I extract a value from EDT thread?
|
final Object[] result = { null }; // how else can I extract a value from EDT thread?
|
||||||
FThreads.invokeInEDTAndWait(new Runnable() {
|
FThreads.invokeInEdtAndWait(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
@@ -237,6 +237,7 @@ public enum CMatchUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setCard(final Card c) {
|
public void setCard(final Card c) {
|
||||||
|
FThreads.assertExecutedByEdt(true);
|
||||||
setCard(c, false);
|
setCard(c, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.Observable;
|
|||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
import forge.Command;
|
import forge.Command;
|
||||||
|
import forge.FThreads;
|
||||||
import forge.gui.framework.EDocID;
|
import forge.gui.framework.EDocID;
|
||||||
import forge.gui.framework.ICDoc;
|
import forge.gui.framework.ICDoc;
|
||||||
import forge.gui.framework.SDisplayUtil;
|
import forge.gui.framework.SDisplayUtil;
|
||||||
@@ -48,6 +49,8 @@ public enum CStack implements ICDoc, Observer {
|
|||||||
@Override
|
@Override
|
||||||
public void update(Observable arg0, Object arg1) {
|
public void update(Observable arg0, Object arg1) {
|
||||||
SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc());
|
SDisplayUtil.showTab(EDocID.REPORT_STACK.getDoc());
|
||||||
update();
|
FThreads.invokeInEdtNowOrLater(doUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final Runnable doUpdate = new Runnable() { @Override public void run() {update(); } };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ public enum VStack implements IVDoc<CStack> {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This updates the Card Picture/Detail when the spell is added to
|
* This updates the Card Picture/Detail when the spell is added to
|
||||||
* the stack. This funcaitonality was not present in v 1.1.8.
|
* the stack. This functionality was not present in v 1.1.8.
|
||||||
*
|
*
|
||||||
* Problem is described in TODO right above this.
|
* Problem is described in TODO right above this.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -503,7 +503,7 @@ public class PlayArea extends CardPanelContainer implements CardPanelMouseListen
|
|||||||
public void setupPlayZone() {
|
public void setupPlayZone() {
|
||||||
boolean wasSet = wantRedraw.getAndSet(true);
|
boolean wasSet = wantRedraw.getAndSet(true);
|
||||||
if(wasSet) return;
|
if(wasSet) return;
|
||||||
FThreads.invokeInEDT(new Runnable() {
|
FThreads.invokeInEdtLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try { // user won't notice, but the requests coming in that interval won't trigger re-draw
|
try { // user won't notice, but the requests coming in that interval won't trigger re-draw
|
||||||
|
|||||||
Reference in New Issue
Block a user