Skip to content

Commit

Permalink
Merge pull request #5068 from spjspj/master
Browse files Browse the repository at this point in the history
Copy Paste image source
  • Loading branch information
spjspj authored Jun 24, 2018
2 parents 8f27adc + 56526d5 commit 2ec3ebd
Show file tree
Hide file tree
Showing 4 changed files with 518 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package org.mage.plugins.card.dl.sources;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import mage.cards.Sets;
import org.mage.plugins.card.images.CardDownloadData;

/**
*
* @author spjspj
*/
public enum CopyPasteImageSource implements CardImageSource {

instance;

private Set<String> supportedSets = new LinkedHashSet<String>();
private Set<String> missingCards = new LinkedHashSet<String>();
HashMap<String, String> singleLinks = null;
boolean loadedFromDialog = false;
boolean viewMissingCards = true;
HashMap<String, Integer> singleLinksDone = null;
private static int maxTimes = 2;

@Override
public String getSourceName() {
return "";
}

@Override
public float getAverageSize() {
return 260.7f;
}

@Override
public String getNextHttpImageUrl() {
if (singleLinks == null) {
setupLinks();
}

for (String key : singleLinksDone.keySet()) {
if (singleLinksDone.get(key) < maxTimes) {
singleLinksDone.put(key, maxTimes);
return key;
}
}
if (maxTimes < 2) {
maxTimes++;
}
for (String key : singleLinksDone.keySet()) {
if (singleLinksDone.get(key) < maxTimes) {
singleLinksDone.put(key, maxTimes);
return key;
}
}
return null;
}

@Override
public String getFileForHttpImage(String httpImageUrl) {
String copy = httpImageUrl;
if (copy != null) {
return singleLinks.get(copy);
}
return null;
}

@Override
public CardImageUrls generateURL(CardDownloadData card) throws Exception {
if (singleLinks == null) {
setupLinks();
}
String url = singleLinks.get(card.getSet() + "/" + card.getName());
if (url != null && url.length() > 0) {
return new CardImageUrls(url);
}
url = singleLinks.get(card.getSet() + "/" + card.getName() + "." + card.getCollectorId());
if (url != null && url.length() > 0) {
return new CardImageUrls(url);
}
return null;
}

int ls_size_mc = 0;
int ls_size_ss = 0;
int ls_size_sl = 0;
int num_nos = 0;

private boolean isDifferent() {
boolean isdiff = false;
if (ls_size_mc != missingCards.size()) {
ls_size_mc = missingCards.size();
isdiff = true;
}
if (ls_size_ss != supportedSets.size()) {
ls_size_ss = supportedSets.size();
isdiff = true;
}
if (ls_size_sl != singleLinks.size()) {
ls_size_sl = singleLinks.size();
isdiff = true;
}
num_nos++;
if (num_nos > 2) {
num_nos = 0;
isdiff = true;
}
return isdiff;
}

private void setupLinks() {
if (singleLinks != null && loadedFromDialog) {
if (!viewMissingCards) {
if (isDifferent() && JOptionPane.showConfirmDialog(null,
"View your missing cards and reset the list of card images to download again?",
"View missing cards (found " + missingCards.size() + ") / Reset URLs to download ", JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION) {
viewMissingCards = true;
singleLinks.clear();
loadedFromDialog = false;
supportedSets.clear();
} else {
return;
}
}
if (!(viewMissingCards && missingCards.size() > 0)) {
return;
}
}
singleLinks = new HashMap<>();
loadedFromDialog = false;

final CopyPasteImageSourceDialog dialog = new CopyPasteImageSourceDialog();
dialog.pack();
int count = 0;
if (viewMissingCards && missingCards.size() > 0 && singleLinks.size() == 0) {
viewMissingCards = false;
String displayMissingCardsStr = "Up to the first 20 cards are:\n";
String missingCardsStr = "";
if (this.missingCards != null) {
for (String card : this.missingCards) {
if (count < 20) {
displayMissingCardsStr = displayMissingCardsStr + card + "\n";
}
missingCardsStr = missingCardsStr + card + "\n";

count++;
}
}
StringSelection stringSelection = new StringSelection(missingCardsStr);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);

if (isDifferent() && JOptionPane.showConfirmDialog(null,
displayMissingCardsStr + "\n\nReset the list again?\n(NB: The full list has been copied to the clipboard)",
"Your missing cards (found " + missingCards.size() + "): ", JOptionPane.YES_NO_OPTION)
== JOptionPane.YES_OPTION) {
viewMissingCards = true;
singleLinks.clear();
loadedFromDialog = false;
supportedSets.clear();
} else {
return;
}
}
dialog.setVisible(true);
String[] lines = dialog.getPastedData().split(System.getProperty("line.separator"));

for (String line : lines) {
// Break into >> "\1", "\2"
Pattern regex = Pattern.compile("\\s*\"(.*?)/(.*?)\"\\s*,\\s*\"(.*?)\"");
Matcher regexMatcher = regex.matcher(line);
while (regexMatcher.find()) {
String setCode = regexMatcher.group(1);
String cardName = regexMatcher.group(2);
String imageURL = regexMatcher.group(3);
supportedSets.add(setCode);
singleLinks.put(setCode + "/" + cardName, imageURL);
isDifferent();
}
}

loadedFromDialog = true;
if (lines.length == 0) {
loadedFromDialog = false;
viewMissingCards = true;
}
}

@Override
public CardImageUrls generateTokenUrl(CardDownloadData card) throws IOException {
try {
return generateURL(card);
} catch (Exception ex) {
}
return null;
}

@Override
public int getTotalImages() {
if (singleLinks == null) {
setupLinks();
}
if (singleLinks != null) {
return singleLinks.size();
}
return -1;
}

@Override
public boolean isTokenSource() {
return false;
}

@Override
public ArrayList<String> getSupportedSets() {
setupLinks();
ArrayList<String> supportedSetsCopy = new ArrayList<>();
if (supportedSets.size() == 0) {
for (String setCode : Sets.getInstance().keySet()) {
supportedSets.add(setCode);
}
}

supportedSetsCopy.addAll(supportedSets);
return supportedSetsCopy;
}

@Override
public void doPause(String httpImageUrl) {
}

@Override
public boolean isImageProvided(String setCode, String cardName) {
missingCards.add(setCode + "/" + cardName);

if (singleLinks != null) {
return singleLinks.containsKey(setCode + "/" + cardName) || singleLinks.containsKey(setCode + "/" + cardName + "-a");
}
return false;
}

@Override
public boolean isSetSupportedComplete(String setCode) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.mage.plugins.card.dl.sources.CopyPasteImageSourceDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="540" height="500"/>
</constraints>
<properties>
<minimumSize width="540" height="450"/>
</properties>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Import"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="FormLayout">
<rowspec value="center:d:grow"/>
<colspec value="fill:d:noGrow"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f8bac" class="javax.swing.JEditorPane" binding="txtDeckList">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
<forms defaultalign-horz="false" defaultalign-vert="false"/>
</constraints>
<properties>
<minimumSize width="250" height="400"/>
<preferredSize width="550" height="400"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
Loading

0 comments on commit 2ec3ebd

Please sign in to comment.