* Fixed possible null pointer exception in client.

This commit is contained in:
LevelX2 2015-07-12 09:19:43 +02:00
parent 0e3d4b335a
commit 6cf0a59644

View file

@ -1,5 +1,28 @@
package mage.client.plugins.adapters; package mage.client.plugins.adapters;
import java.awt.Component;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import mage.cards.MageCard; import mage.cards.MageCard;
import mage.cards.action.ActionCallback; import mage.cards.action.ActionCallback;
import mage.cards.action.TransferData; import mage.cards.action.TransferData;
@ -24,21 +47,10 @@ import org.jdesktop.swingx.JXPanel;
import org.mage.card.arcane.CardPanel; import org.mage.card.arcane.CardPanel;
import org.mage.plugins.card.images.ImageCache; import org.mage.plugins.card.images.ImageCache;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/** /**
* Class that handles the callbacks from the card panels to mage to display big card * Class that handles the callbacks from the card panels to mage to display big
* images from the cards the mouse hovers on. Also handles tooltip text window. * card images from the cards the mouse hovers on. Also handles tooltip text
* window.
* *
* @author Nantuko, noxx * @author Nantuko, noxx
*/ */
@ -62,7 +74,9 @@ public class MageActionCallback implements ActionCallback {
private TransferData popupData; private TransferData popupData;
private JComponent cardInfoPane; private JComponent cardInfoPane;
private volatile boolean popupTextWindowOpen = false; private volatile boolean popupTextWindowOpen = false;
enum EnlargedWindowState { enum EnlargedWindowState {
CLOSED, NORMAL, ROTATED CLOSED, NORMAL, ROTATED
} }
private volatile EnlargedWindowState enlargedWindowState = EnlargedWindowState.CLOSED; private volatile EnlargedWindowState enlargedWindowState = EnlargedWindowState.CLOSED;
@ -212,7 +226,7 @@ public class MageActionCallback implements ActionCallback {
cardPanels.clear(); cardPanels.clear();
Point mouse = new Point(e.getX(), e.getY()); Point mouse = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(mouse, data.component); SwingUtilities.convertPointToScreen(mouse, data.component);
initialMousePos = new Point((int)mouse.getX(), (int)mouse.getY()); initialMousePos = new Point((int) mouse.getX(), (int) mouse.getY());
initialCardPos = data.component.getLocation(); initialCardPos = data.component.getLocation();
// Closes popup & enlarged view if a card/Permanent is selected // Closes popup & enlarged view if a card/Permanent is selected
hidePopup(); hidePopup();
@ -220,7 +234,7 @@ public class MageActionCallback implements ActionCallback {
@Override @Override
public void mouseReleased(MouseEvent e, TransferData transferData) { public void mouseReleased(MouseEvent e, TransferData transferData) {
CardPanel card = ((CardPanel)transferData.component); CardPanel card = ((CardPanel) transferData.component);
if (card.getZone() != null && card.getZone().equalsIgnoreCase("hand")) { if (card.getZone() != null && card.getZone().equalsIgnoreCase("hand")) {
int maxXOffset = 0; int maxXOffset = 0;
if (isDragging) { if (isDragging) {
@ -269,7 +283,7 @@ public class MageActionCallback implements ActionCallback {
@Override @Override
public void mouseDragged(MouseEvent e, TransferData transferData) { public void mouseDragged(MouseEvent e, TransferData transferData) {
CardPanel card = ((CardPanel)transferData.component); CardPanel card = ((CardPanel) transferData.component);
if (card.getZone() == null || !card.getZone().equalsIgnoreCase("hand")) { if (card.getZone() == null || !card.getZone().equalsIgnoreCase("hand")) {
// drag'n'drop is allowed for HAND zone only // drag'n'drop is allowed for HAND zone only
return; return;
@ -280,7 +294,7 @@ public class MageActionCallback implements ActionCallback {
Point mouse = new Point(e.getX(), e.getY()); Point mouse = new Point(e.getX(), e.getY());
SwingUtilities.convertPointToScreen(mouse, transferData.component); SwingUtilities.convertPointToScreen(mouse, transferData.component);
int xOffset = card.getXOffset(card.getCardWidth()); int xOffset = card.getXOffset(card.getCardWidth());
int newX = Math.max(initialCardPos.x + (int)(mouse.getX() - initialMousePos.x) - xOffset, 0); int newX = Math.max(initialCardPos.x + (int) (mouse.getX() - initialMousePos.x) - xOffset, 0);
card.setCardBounds( card.setCardBounds(
newX, newX,
p.y, p.y,
@ -312,13 +326,13 @@ public class MageActionCallback implements ActionCallback {
if (!cardPanels.contains(component)) { if (!cardPanels.contains(component)) {
component.setLocation(component.getLocation().x, component.getLocation().y + GO_DOWN_ON_DRAG_Y_OFFSET); component.setLocation(component.getLocation().x, component.getLocation().y + GO_DOWN_ON_DRAG_Y_OFFSET);
} }
cardPanels.add((CardPanel)component); cardPanels.add((CardPanel) component);
} else { } else {
if (!startedDragging) { if (!startedDragging) {
component.setLocation(component.getLocation().x, component.getLocation().y - GO_UP_ON_DRAG_Y_OFFSET); component.setLocation(component.getLocation().x, component.getLocation().y - GO_UP_ON_DRAG_Y_OFFSET);
} }
} }
cards.add((CardPanel)component); cards.add((CardPanel) component);
} }
} }
sortLayout(cards, card, sortSource); sortLayout(cards, card, sortSource);
@ -458,7 +472,8 @@ public class MageActionCallback implements ActionCallback {
/** /**
* Show the big card image on mouse position while hovering over a card * Show the big card image on mouse position while hovering over a card
* *
* @param showAlternative defines if the original image (if it's a copied card) or the opposite side of a transformable card will be shown * @param showAlternative defines if the original image (if it's a copied
* card) or the opposite side of a transformable card will be shown
*/ */
public void enlargeCard(EnlargeMode showAlternative) { public void enlargeCard(EnlargeMode showAlternative) {
if (enlargedWindowState.equals(EnlargedWindowState.CLOSED)) { if (enlargedWindowState.equals(EnlargedWindowState.CLOSED)) {
@ -496,8 +511,6 @@ public class MageActionCallback implements ActionCallback {
} }
} }
private void displayEnlargedCard(final CardView cardView, final TransferData transferData) { private void displayEnlargedCard(final CardView cardView, final TransferData transferData) {
ThreadUtils.threadPool2.submit(new Runnable() { ThreadUtils.threadPool2.submit(new Runnable() {
@Override @Override
@ -509,20 +522,18 @@ public class MageActionCallback implements ActionCallback {
if (enlargedWindowState.equals(EnlargedWindowState.CLOSED)) { if (enlargedWindowState.equals(EnlargedWindowState.CLOSED)) {
return; return;
} }
Component parentComponent = SwingUtilities.getRoot(transferData.component);
Point parentPoint = parentComponent.getLocationOnScreen();
MageComponents mageComponentCardPreviewContainer; MageComponents mageComponentCardPreviewContainer;
MageComponents mageComponentCardPreviewPane; MageComponents mageComponentCardPreviewPane;
if (cardView.isToRotate()) { if (cardView.isToRotate()) {
if(enlargedWindowState.equals(EnlargedWindowState.NORMAL)) { if (enlargedWindowState.equals(EnlargedWindowState.NORMAL)) {
hideEnlargedCard(); hideEnlargedCard();
enlargedWindowState = EnlargedWindowState.ROTATED; enlargedWindowState = EnlargedWindowState.ROTATED;
} }
mageComponentCardPreviewContainer = MageComponents.CARD_PREVIEW_CONTAINER_ROTATED; mageComponentCardPreviewContainer = MageComponents.CARD_PREVIEW_CONTAINER_ROTATED;
mageComponentCardPreviewPane = MageComponents.CARD_PREVIEW_PANE_ROTATED; mageComponentCardPreviewPane = MageComponents.CARD_PREVIEW_PANE_ROTATED;
} else { } else {
if(enlargedWindowState.equals(EnlargedWindowState.ROTATED)) { if (enlargedWindowState.equals(EnlargedWindowState.ROTATED)) {
hideEnlargedCard(); hideEnlargedCard();
enlargedWindowState = EnlargedWindowState.NORMAL; enlargedWindowState = EnlargedWindowState.NORMAL;
} }
@ -531,7 +542,9 @@ public class MageActionCallback implements ActionCallback {
} }
final Component popupContainer = MageFrame.getUI().getComponent(mageComponentCardPreviewContainer); final Component popupContainer = MageFrame.getUI().getComponent(mageComponentCardPreviewContainer);
Component cardPreviewPane = MageFrame.getUI().getComponent(mageComponentCardPreviewPane); Component cardPreviewPane = MageFrame.getUI().getComponent(mageComponentCardPreviewPane);
if (cardPreviewPane != null) { Component parentComponent = SwingUtilities.getRoot(transferData.component);
if (cardPreviewPane != null && parentComponent != null) {
Point parentPoint = parentComponent.getLocationOnScreen();
transferData.locationOnScreen = transferData.component.getLocationOnScreen(); transferData.locationOnScreen = transferData.component.getLocationOnScreen();
Point location = new Point((int) transferData.locationOnScreen.getX() + transferData.popupOffsetX - 40, (int) transferData.locationOnScreen.getY() + transferData.popupOffsetY - 40); Point location = new Point((int) transferData.locationOnScreen.getX() + transferData.popupOffsetX - 40, (int) transferData.locationOnScreen.getY() + transferData.popupOffsetY - 40);
location = GuiDisplayUtil.keepComponentInsideParent(location, parentPoint, cardPreviewPane, parentComponent); location = GuiDisplayUtil.keepComponentInsideParent(location, parentPoint, cardPreviewPane, parentComponent);
@ -561,10 +574,9 @@ public class MageActionCallback implements ActionCallback {
image = mageCard.getImage(); image = mageCard.getImage();
} }
// shows the card in the popup Container // shows the card in the popup Container
BigCard bigCard = (BigCard)cardPreviewPane; BigCard bigCard = (BigCard) cardPreviewPane;
displayCardInfo(mageCard, image, bigCard); displayCardInfo(mageCard, image, bigCard);
} else { } else {
logger.warn("No Card preview Pane in Mage Frame defined. Card: " + cardView.getName()); logger.warn("No Card preview Pane in Mage Frame defined. Card: " + cardView.getName());
} }