forked from External/mage
Merge pull request #2861 from kubikrubikvkube/master
Bugfix - after 12820b54d4 refactoring
This commit is contained in:
commit
c2191227cd
21 changed files with 103 additions and 105 deletions
|
|
@ -1,7 +1,5 @@
|
|||
package mage.client.util.gui;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
|
@ -14,31 +12,37 @@ import java.util.List;
|
|||
*/
|
||||
public class ArrowBuilder {
|
||||
|
||||
private static final ArrowBuilder instance;
|
||||
private static ArrowBuilder instance;
|
||||
|
||||
static {
|
||||
instance = new ArrowBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores arrow panels per game
|
||||
*/
|
||||
private final Map<UUID, JPanel> arrowPanels = new HashMap<>();
|
||||
private final Map<UUID, Map<Type, List<Arrow>>> map = new MapMaker().weakKeys().weakValues().makeMap();
|
||||
/**
|
||||
* The top panel where arrow panels are added to.
|
||||
*/
|
||||
private JPanel arrowsManagerPanel;
|
||||
private int currentWidth;
|
||||
private int currentHeight;
|
||||
|
||||
public static ArrowBuilder getBuilder() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* The top panel where arrow panels are added to.
|
||||
*/
|
||||
private JPanel arrowsManagerPanel;
|
||||
|
||||
/**
|
||||
* Stores arrow panels per game
|
||||
*/
|
||||
private final Map<UUID, JPanel> arrowPanels = new HashMap<UUID, JPanel>();
|
||||
|
||||
private final Map<UUID, Map<Type, List<Arrow>>> map = new HashMap<UUID, Map<Type, java.util.List<Arrow>>>();
|
||||
|
||||
private int currentWidth;
|
||||
private int currentHeight;
|
||||
|
||||
public enum Type {
|
||||
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the panel where all arrows are being drawn.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JPanel getArrowsManagerPanel() {
|
||||
|
|
@ -69,6 +73,17 @@ public class ArrowBuilder {
|
|||
return arrowPanels.get(gameId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Not synchronized method for arrows panel.
|
||||
* Doesn't create JPanel in case the panel doesn't exist.
|
||||
* Works faster.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/*public JPanel getPanelRef() {
|
||||
return arrowsManagerPanel;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Adds new arrow.
|
||||
*
|
||||
|
|
@ -85,25 +100,26 @@ public class ArrowBuilder {
|
|||
arrow.setColor(color);
|
||||
arrow.setArrowLocation(startX, startY, endX, endY);
|
||||
arrow.setBounds(0, 0, Math.max(startX, endX) + 40, Math.max(startY, endY) + 30); // 30 is offset for arrow heads (being cut otherwise)
|
||||
p.add(arrow);
|
||||
Map<Type, List<Arrow>> innerMap = map.computeIfAbsent(gameId, k -> new HashMap<>());
|
||||
List<Arrow> arrows = innerMap.computeIfAbsent(type, k -> new ArrayList<>());
|
||||
arrows.add(arrow);
|
||||
|
||||
synchronized (map) {
|
||||
p.add(arrow);
|
||||
Map<Type, java.util.List<Arrow>> innerMap = map.get(gameId);
|
||||
if (innerMap == null) {
|
||||
innerMap = new HashMap<Type, List<Arrow>>();
|
||||
map.put(gameId, innerMap);
|
||||
}
|
||||
java.util.List<Arrow> arrows = innerMap.get(type);
|
||||
if (arrows == null) {
|
||||
arrows = new ArrayList<Arrow>();
|
||||
innerMap.put(type, arrows);
|
||||
}
|
||||
arrows.add(arrow);
|
||||
}
|
||||
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Not synchronized method for arrows panel.
|
||||
* Doesn't create JPanel in case the panel doesn't exist.
|
||||
* Works faster.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/*public JPanel getPanelRef() {
|
||||
return arrowsManagerPanel;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Removes all arrows from the screen.
|
||||
*/
|
||||
|
|
@ -111,13 +127,15 @@ public class ArrowBuilder {
|
|||
if (map.containsKey(gameId)) {
|
||||
Map<Type, List<Arrow>> innerMap = map.get(gameId);
|
||||
JPanel p = getArrowsPanel(gameId);
|
||||
if (p != null && p.getComponentCount() > 0) {
|
||||
p.removeAll();
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
synchronized (map) {
|
||||
if (p != null && p.getComponentCount() > 0) {
|
||||
p.removeAll();
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
innerMap.clear();
|
||||
map.remove(gameId);
|
||||
}
|
||||
innerMap.clear();
|
||||
map.remove(gameId);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,12 +143,14 @@ public class ArrowBuilder {
|
|||
if (map.containsKey(gameId)) {
|
||||
Map<Type, List<Arrow>> innerMap = map.get(gameId);
|
||||
java.util.List<Arrow> arrows = innerMap.get(type);
|
||||
if (arrows != null && !arrows.isEmpty()) {
|
||||
if (arrows != null && arrows.size() > 0) {
|
||||
JPanel p = getArrowsPanel(gameId);
|
||||
for (Arrow arrow : arrows) {
|
||||
p.remove(arrow);
|
||||
synchronized (map) {
|
||||
for (Arrow arrow : arrows) {
|
||||
p.remove(arrow);
|
||||
}
|
||||
innerMap.put(type, new ArrayList<Arrow>());
|
||||
}
|
||||
innerMap.put(type, new ArrayList<>());
|
||||
p.revalidate();
|
||||
p.repaint();
|
||||
}
|
||||
|
|
@ -161,8 +181,4 @@ public class ArrowBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
PAIRED, SOURCE, TARGET, COMBAT, ENCHANT_PLAYERS
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -240,12 +240,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
|
||||
private static List<CardDownloadData> getNeededCards(List<CardInfo> allCards) {
|
||||
|
||||
List<CardDownloadData> cardsToDownload = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
/**
|
||||
* read all card names and urls
|
||||
*/
|
||||
List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
|
||||
HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls();
|
||||
|
||||
/**
|
||||
|
|
@ -261,6 +258,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
|
||||
int numberCardImages = allCards.size();
|
||||
int numberWithoutTokens = 0;
|
||||
List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
|
||||
try {
|
||||
offlineMode = true;
|
||||
allCards.parallelStream().forEach(card -> {
|
||||
|
|
@ -290,11 +288,11 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
if (card.getFlipCardName() == null || card.getFlipCardName().trim().isEmpty()) {
|
||||
throw new IllegalStateException("Flipped card can't have empty name.");
|
||||
}
|
||||
url = new CardDownloadData(card.getFlipCardName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, "", "", false, card.isDoubleFaced(), card.isNightCard());
|
||||
url.setFlipCard(true);
|
||||
url.setFlippedSide(true);
|
||||
url.setType2(isType2);
|
||||
allCardsUrls.add(url);
|
||||
CardDownloadData cardDownloadData = new CardDownloadData(card.getFlipCardName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, "", "", false, card.isDoubleFaced(), card.isNightCard());
|
||||
cardDownloadData.setFlipCard(true);
|
||||
cardDownloadData.setFlippedSide(true);
|
||||
cardDownloadData.setType2(isType2);
|
||||
allCardsUrls.add(cardDownloadData);
|
||||
}
|
||||
} else if (card.getCardNumber().isEmpty() || "0".equals(card.getCardNumber())) {
|
||||
System.err.println("There was a critical error!");
|
||||
|
|
@ -315,6 +313,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
/**
|
||||
* check to see which cards we already have
|
||||
*/
|
||||
List<CardDownloadData> cardsToDownload = Collections.synchronizedList(new ArrayList<>());
|
||||
allCardsUrls.parallelStream().forEach(card -> {
|
||||
TFile file = new TFile(CardImageUtils.generateImagePath(card));
|
||||
if (!file.exists()) {
|
||||
|
|
@ -348,19 +347,18 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
|
||||
try(InputStreamReader input = new InputStreamReader(in);
|
||||
BufferedReader reader = new BufferedReader(input)) {
|
||||
String line;
|
||||
|
||||
line = reader.readLine();
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
line = line.trim();
|
||||
if (line.startsWith("|")) { // new format
|
||||
String[] params = line.split("\\|", -1);
|
||||
if (params.length >= 5) {
|
||||
int type = 0;
|
||||
String fileName = "";
|
||||
if (params[4] != null && !params[4].isEmpty()) {
|
||||
type = Integer.parseInt(params[4].trim());
|
||||
}
|
||||
String fileName = "";
|
||||
if (params.length > 5 && params[5] != null && !params[5].isEmpty()) {
|
||||
fileName = params[5].trim();
|
||||
}
|
||||
|
|
@ -516,7 +514,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
private final String actualFilename;
|
||||
private final boolean useSpecifiedPaths;
|
||||
|
||||
public DownloadTask(CardDownloadData card, URL url, int count) {
|
||||
DownloadTask(CardDownloadData card, URL url, int count) {
|
||||
this.card = card;
|
||||
this.url = url;
|
||||
this.count = count;
|
||||
|
|
@ -524,7 +522,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
useSpecifiedPaths = false;
|
||||
}
|
||||
|
||||
public DownloadTask(CardDownloadData card, URL url, String actualFilename, int count) {
|
||||
DownloadTask(CardDownloadData card, URL url, String actualFilename, int count) {
|
||||
this.card = card;
|
||||
this.url = url;
|
||||
this.count = count;
|
||||
|
|
@ -583,7 +581,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
}
|
||||
return;
|
||||
}
|
||||
BufferedOutputStream out;
|
||||
|
||||
// Logger.getLogger(this.getClass()).info(url.toString());
|
||||
boolean useTempFile = false;
|
||||
|
|
@ -602,6 +599,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
|
||||
if (responseCode == 200 || useTempFile) {
|
||||
if (!useTempFile) {
|
||||
BufferedOutputStream out;
|
||||
try (BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream())) {
|
||||
//try (BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream())) {
|
||||
out = new BufferedOutputStream(new TFileOutputStream(temporaryFile));
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ import org.mage.plugins.card.utils.CardImageUtils;
|
|||
* look</li>
|
||||
* </ul>
|
||||
*/
|
||||
public class ImageCache {
|
||||
public final class ImageCache {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(ImageCache.class);
|
||||
|
||||
|
|
@ -157,6 +157,9 @@ public class ImageCache {
|
|||
});
|
||||
}
|
||||
|
||||
private ImageCache() {
|
||||
}
|
||||
|
||||
public static BufferedImage getMorphImage() {
|
||||
CardDownloadData info = new CardDownloadData("Morph", "KTK", "0", false, 0, "KTK", "");
|
||||
info.setToken(true);
|
||||
|
|
@ -222,8 +225,7 @@ public class ImageCache {
|
|||
*/
|
||||
private static BufferedImage getImage(String key) {
|
||||
try {
|
||||
BufferedImage image = IMAGE_CACHE.get(key);
|
||||
return image;
|
||||
return IMAGE_CACHE.get(key);
|
||||
} catch (NullPointerException ex) {
|
||||
// unfortunately NullOutputException, thrown when apply() returns
|
||||
// null, is not public
|
||||
|
|
@ -244,11 +246,7 @@ public class ImageCache {
|
|||
* the cache.
|
||||
*/
|
||||
private static BufferedImage tryGetImage(String key) {
|
||||
if (IMAGE_CACHE.containsKey(key)) {
|
||||
return IMAGE_CACHE.get(key);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return IMAGE_CACHE.containsKey(key) ? IMAGE_CACHE.get(key) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -279,11 +277,11 @@ public class ImageCache {
|
|||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
BufferedImage image = null;
|
||||
if (!file.exists()) {
|
||||
LOGGER.debug("File does not exist: " + file.toString());
|
||||
return null;
|
||||
}
|
||||
BufferedImage image = null;
|
||||
try {
|
||||
try (TFileInputStream inputStream = new TFileInputStream(file)) {
|
||||
image = ImageIO.read(inputStream);
|
||||
|
|
@ -395,8 +393,7 @@ public class ImageCache {
|
|||
|
||||
public static TFile getTFile(String path) {
|
||||
try {
|
||||
TFile file = new TFile(path);
|
||||
return file;
|
||||
return new TFile(path);
|
||||
} catch (NullPointerException ex) {
|
||||
LOGGER.warn("Imagefile does not exist: " + path);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue