use of org.magic.api.beans.MagicFormat in project MtgDesktopCompanion by nicho92.
the class MagicCardDetailPanel method initDataBindings.
protected BindingGroup initDataBindings() {
BeanProperty<MagicCard, Integer> cmcProperty = BeanProperty.create("cmc");
BeanProperty<JTextField, String> textProperty = BeanProperty.create("text");
AutoBinding<MagicCard, Integer, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, cmcProperty, cmcJTextField, textProperty);
autoBinding.bind();
//
BeanProperty<MagicCard, String> costProperty = BeanProperty.create("cost");
BeanProperty<ManaPanel, String> textProperty1 = BeanProperty.create("manaCost");
AutoBinding<MagicCard, String, ManaPanel, String> autoBinding1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, costProperty, manaPanel, textProperty1);
autoBinding1.bind();
//
BeanProperty<MagicCard, String> fullTypeProperty = BeanProperty.create("fullType");
BeanProperty<JTextField, String> textProperty2 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, fullTypeProperty, fullTypeJTextField, textProperty2);
autoBinding2.bind();
//
BeanProperty<MagicCard, Integer> loyaltyProperty = BeanProperty.create("loyalty");
BeanProperty<JTextField, String> textProperty4 = BeanProperty.create("text");
AutoBinding<MagicCard, Integer, JTextField, String> autoBinding4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, loyaltyProperty, loyaltyJTextField, textProperty4);
autoBinding4.bind();
//
BeanProperty<MagicCard, String> nameProperty = BeanProperty.create("name");
BeanProperty<JTextField, String> textProperty5 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding5 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, nameProperty, nameJTextField, textProperty5);
autoBinding5.bind();
//
BeanProperty<MagicCard, String> powerProperty = BeanProperty.create("power");
BeanProperty<JTextField, String> textProperty6 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding6 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, powerProperty, powerJTextField, textProperty6);
autoBinding6.bind();
//
BeanProperty<MagicCard, String> textProperty8 = BeanProperty.create("text");
BeanProperty<MagicTextPane, String> textProperty9 = BeanProperty.create("text");
AutoBinding<MagicCard, String, MagicTextPane, String> autoBinding8 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, textProperty8, txtTextPane, textProperty9);
autoBinding8.bind();
//
BeanProperty<MagicCard, String> toughnessProperty = BeanProperty.create("toughness");
BeanProperty<JTextField, String> textProperty10 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding9 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, toughnessProperty, toughnessJTextField, textProperty10);
autoBinding9.bind();
BeanProperty<MagicCard, String> flavorProperty = BeanProperty.create("flavor");
BeanProperty<JTextPane, String> textProperty11 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextPane, String> autoBinding10 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, flavorProperty, txtFlavorArea, textProperty11);
autoBinding10.bind();
BeanProperty<MagicCard, String> artistProperty = BeanProperty.create("artist");
BeanProperty<JTextField, String> textProperty12 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding11 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, artistProperty, txtArtist, textProperty12);
autoBinding11.bind();
BeanProperty<MagicCard, String> layoutProperty = BeanProperty.create("layout");
BeanProperty<JTextField, String> textProperty13 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding12 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, layoutProperty, txtLayoutField, textProperty13);
autoBinding12.bind();
BeanProperty<MagicCard, String> waterProperty = BeanProperty.create("watermarks");
BeanProperty<JTextField, String> textProperty14 = BeanProperty.create("text");
AutoBinding<MagicCard, String, JTextField, String> autoBinding13 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, waterProperty, txtWatermark, textProperty14);
autoBinding13.bind();
BeanProperty<MagicCard, Boolean> reservedProperty = BeanProperty.create("reserved");
BeanProperty<JCheckBox, Boolean> chkProperty15 = BeanProperty.create("selected");
AutoBinding<MagicCard, Boolean, JCheckBox, Boolean> autoBinding15 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, magicCard, reservedProperty, chckbxReserved, chkProperty15);
autoBinding15.bind();
try {
if (magicCard != null)
rarityJTextField.setText(magicCard.getEditions().get(0).getRarity());
} catch (Exception e) {
rarityJTextField.setText("");
}
txtTextPane.updateTextWithIcons();
if (thumbnail && magicCard != null)
ThreadManager.getInstance().execute(() -> loadPics(), "load pic");
if (magicCard != null && !magicCard.getEditions().isEmpty()) {
ThreadManager.getInstance().execute(() -> {
setMagicLogo(magicCard.getEditions().get(0).getId(), magicCard.getEditions().get(0).getRarity());
lblnumberInSet.setText(magicCard.getEditions().get(0).getNumber() + "/" + magicCard.getEditions().get(0).getCardCount());
}, "loadLogo");
}
if (magicCard != null && enableCollectionLookup)
ThreadManager.getInstance().execute(() -> {
try {
((DefaultListModel) listCollection.getModel()).removeAllElements();
for (MagicCollection col : MTGControler.getInstance().getEnabledDAO().listCollectionFromCards(magicCard)) ((DefaultListModel) listCollection.getModel()).addElement(col);
} catch (Exception e) {
logger.error(e);
}
}, "loadCollections");
if (magicCard != null && enableCollectionLookup)
ThreadManager.getInstance().execute(() -> {
if (MTGControler.getInstance().getEnabledDAO().hasAlert(magicCard)) {
btnAlert.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("HAD_ALERT"));
btnAlert.setEnabled(false);
} else {
btnAlert.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("ADD_ALERT_FOR", magicCard.getName()));
btnAlert.setEnabled(true);
}
}, "Get alerts for " + magicCard);
((DefaultListModel) lstFormats.getModel()).removeAllElements();
if (magicCard != null)
for (MagicFormat mf : magicCard.getLegalities()) ((DefaultListModel) lstFormats.getModel()).addElement(mf);
//
BindingGroup bindingGroup = new BindingGroup();
//
bindingGroup.addBinding(autoBinding);
bindingGroup.addBinding(autoBinding1);
bindingGroup.addBinding(autoBinding2);
bindingGroup.addBinding(autoBinding4);
bindingGroup.addBinding(autoBinding5);
bindingGroup.addBinding(autoBinding6);
bindingGroup.addBinding(autoBinding8);
bindingGroup.addBinding(autoBinding9);
bindingGroup.addBinding(autoBinding10);
bindingGroup.addBinding(autoBinding11);
bindingGroup.addBinding(autoBinding12);
bindingGroup.addBinding(autoBinding13);
bindingGroup.addBinding(autoBinding15);
return bindingGroup;
}
use of org.magic.api.beans.MagicFormat in project MtgDesktopCompanion by nicho92.
the class ConstructPanel method initGUI.
private void initGUI() {
JPanel panneauHaut = new JPanel();
JButton btnSearch;
JButton btnUpdate;
HandPanel thumbnail;
JPanel panelBottom;
JTextField txtSearch;
JComboBox<String> cboAttributs;
JScrollPane scrollResult;
JTabbedPane tabbedPane;
ButtonGroup groupsFilterResult;
lblExport.setIcon(MTGConstants.ICON_LOADING);
lblExport.setVisible(false);
setLayout(new BorderLayout(0, 0));
deckmodel = new DeckModel(DeckModel.TYPE.DECK);
deckSidemodel = new DeckModel(DeckModel.TYPE.SIDE);
deckDetailsPanel = new DeckDetailsPanel();
panelBottom = new JPanel();
thumbnail = new HandPanel();
thumbnail.setThumbnailSize(new Dimension(223, 311));
thumbnail.enableDragging(false);
thumbnail.setMaxCardsRow(4);
FlowLayout flowLayout = (FlowLayout) panneauHaut.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
add(panneauHaut, BorderLayout.NORTH);
cboAttributs = new JComboBox<>(new DefaultComboBoxModel<String>(MTGControler.getInstance().getEnabledProviders().getQueryableAttributs()));
panneauHaut.add(cboAttributs);
txtSearch = new JTextField();
panneauHaut.add(txtSearch);
txtSearch.setColumns(25);
btnSearch = new JButton(MTGConstants.ICON_SEARCH);
panneauHaut.add(btnSearch);
final JLabel lblCards = new JLabel();
panneauHaut.add(lblCards);
JButton btnNewDeck = new JButton(MTGConstants.ICON_NEW);
btnNewDeck.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("CREATE_NEW_DECK"));
panneauHaut.add(btnNewDeck);
btnNewDeck.addActionListener(newDeckEvent -> {
MagicDeck newDeck = new MagicDeck();
setDeck(newDeck);
deckmodel.init(newDeck);
deckSidemodel.init(newDeck);
});
JButton btnOpen = new JButton(MTGConstants.ICON_OPEN);
btnOpen.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("OPEN_DECK"));
panneauHaut.add(btnOpen);
btnOpen.addActionListener(openEvent -> {
try {
JDeckChooserDialog choose = new JDeckChooserDialog();
choose.setVisible(true);
deck = choose.getSelectedDeck();
if (deck != null) {
deckDetailsPanel.setMagicDeck(deck);
deckmodel.init(deck);
deckSidemodel.init(deck);
setDeck(deck);
updatePanels();
}
} catch (Exception ex) {
logger.error(ex);
JOptionPane.showMessageDialog(null, ex, MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
}
});
btnUpdate = new JButton();
btnUpdate.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("UPDATE_DECK"));
btnUpdate.addActionListener(updateEvent -> ThreadManager.getInstance().execute(() -> {
Map<MagicCard, Integer> updateM = new HashMap<>();
Map<MagicCard, Integer> updateS = new HashMap<>();
btnUpdate.setEnabled(false);
lblExport.setVisible(true);
for (MagicCard mc : deck.getMap().keySet()) {
try {
updateM.put(MTGControler.getInstance().getEnabledProviders().getCardById(mc.getId()), deck.getMap().get(mc));
} catch (Exception e) {
logger.error(e);
btnUpdate.setEnabled(true);
lblExport.setVisible(false);
}
}
for (MagicCard mc : deck.getMapSideBoard().keySet()) {
try {
updateS.put(MTGControler.getInstance().getEnabledProviders().getCardById(mc.getId()), deck.getMapSideBoard().get(mc));
} catch (Exception e) {
btnUpdate.setEnabled(true);
lblExport.setVisible(false);
}
}
deck.getMap().clear();
deck.setMapDeck(updateM);
deck.getMapSideBoard().clear();
deck.setMapSideBoard(updateS);
updatePanels();
btnUpdate.setEnabled(true);
lblExport.setVisible(false);
JOptionPane.showMessageDialog(null, MTGControler.getInstance().getLangService().getCapitalize("UPDATED_DECK"), MTGControler.getInstance().getLangService().getCapitalize("FINISHED"), JOptionPane.INFORMATION_MESSAGE);
}, "Update Deck"));
btnUpdate.setIcon(MTGConstants.ICON_REFRESH);
panneauHaut.add(btnUpdate);
JButton btnSave = new JButton(MTGConstants.ICON_SAVE);
btnSave.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("SAVE_DECK"));
panneauHaut.add(btnSave);
btnSave.addActionListener(e -> {
try {
String name = JOptionPane.showInputDialog(MTGControler.getInstance().getLangService().getCapitalize("DECK_NAME") + " ?", deck.getName());
deck.setName(name);
MTGControler.getInstance().saveDeck(deck);
} catch (Exception ex) {
logger.error("error saving", ex);
JOptionPane.showMessageDialog(null, ex, MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
}
});
JButton btnImport = new JButton(MTGConstants.ICON_IMPORT);
btnImport.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("DECK_IMPORT_AS"));
btnImport.addActionListener(ae -> {
JPopupMenu menu = new JPopupMenu();
for (final MTGCardsExport exp : MTGControler.getInstance().getEnabledDeckExports()) {
if (exp.getMods() == MODS.BOTH || exp.getMods() == MODS.IMPORT) {
JMenuItem it = new JMenuItem();
it.setIcon(exp.getIcon());
it.setText(exp.getName());
it.addActionListener(itEvent -> {
JFileChooser jf = new JFileChooser(".");
jf.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return exp.getName();
}
@Override
public boolean accept(File f) {
return (f.isDirectory() || f.getName().endsWith(exp.getFileExtension()));
}
});
int res = -1;
f = new File("");
if (!exp.needDialogGUI()) {
res = jf.showOpenDialog(null);
f = jf.getSelectedFile();
} else {
res = JFileChooser.APPROVE_OPTION;
}
if (res == JFileChooser.APPROVE_OPTION)
ThreadManager.getInstance().execute(() -> {
try {
loading(true, MTGControler.getInstance().getLangService().get("LOADING_FILE", f.getName(), exp));
deck = exp.importDeck(f);
JOptionPane.showMessageDialog(null, MTGControler.getInstance().getLangService().getCapitalize("FINISHED"), exp.getName() + " " + MTGControler.getInstance().getLangService().get("FINISHED"), JOptionPane.INFORMATION_MESSAGE);
setDeck(deck);
loading(false, "");
deckmodel.init(deck);
deckSidemodel.init(deck);
setDeck(deck);
updatePanels();
} catch (Exception e) {
logger.error("error import", e);
MTGLogger.printStackTrace(e);
loading(false, "");
JOptionPane.showMessageDialog(null, e, MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
}
}, "import " + exp);
});
menu.add(it);
}
}
Component b = (Component) ae.getSource();
Point point = b.getLocationOnScreen();
menu.show(b, 0, 0);
menu.setLocation(point.x, point.y + b.getHeight());
});
panneauHaut.add(btnImport);
btnExports = new JButton();
btnExports.setEnabled(false);
btnExports.setToolTipText(MTGControler.getInstance().getLangService().getCapitalize("EXPORT_AS"));
btnExports.setIcon(MTGConstants.ICON_EXPORT);
btnExports.addActionListener(exportsAction -> {
JPopupMenu menu = new JPopupMenu();
for (final MTGCardsExport exp : MTGControler.getInstance().getEnabledDeckExports()) {
if (exp.getMods() == MODS.BOTH || exp.getMods() == MODS.EXPORT) {
JMenuItem it = new JMenuItem();
it.setIcon(exp.getIcon());
it.setText(exp.getName());
it.addActionListener(pluginExportEvent -> {
JFileChooser jf = new JFileChooser(".");
jf.setSelectedFile(new File(deck.getName() + exp.getFileExtension()));
jf.showSaveDialog(null);
exportedFile = jf.getSelectedFile();
ThreadManager.getInstance().execute(() -> {
try {
loading(true, MTGControler.getInstance().getLangService().get("EXPORT_TO", deck, exp));
exp.export(deck, exportedFile);
JOptionPane.showMessageDialog(null, MTGControler.getInstance().getLangService().combine("EXPORT", "FINISHED"), exp.getName() + " " + MTGControler.getInstance().getLangService().getCapitalize("FINISHED"), JOptionPane.INFORMATION_MESSAGE);
loading(false, "");
} catch (Exception e) {
logger.error(e);
loading(false, "");
JOptionPane.showMessageDialog(null, e, MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
}
}, "Export " + deck + " to " + exp.getName());
});
menu.add(it);
}
}
Component b = (Component) exportsAction.getSource();
Point point = b.getLocationOnScreen();
menu.show(b, 0, 0);
menu.setLocation(point.x, point.y + b.getHeight());
});
panneauHaut.add(btnExports);
panneauHaut.add(lblExport);
JPanel panneauBas = new JPanel();
add(panneauBas, BorderLayout.SOUTH);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
add(tabbedPane, BorderLayout.CENTER);
JSplitPane panneauDeck = new JSplitPane();
panneauDeck.setOrientation(JSplitPane.VERTICAL_SPLIT);
tabbedPane.addTab(MTGControler.getInstance().getLangService().getCapitalize("DECK"), null, panneauDeck, null);
DefaultRowSorter sorterCards = new TableRowSorter<DefaultTableModel>(deckmodel);
magicCardDetailPanel = new MagicCardDetailPanel();
magicCardDetailPanel.setPreferredSize(new Dimension(0, 0));
magicCardDetailPanel.enableThumbnail(true);
panelBottom.setLayout(new BorderLayout(0, 0));
panelBottom.add(magicCardDetailPanel);
panneauDeck.setRightComponent(panelBottom);
cardDrawProbaPanel = new DrawProbabilityPanel();
panelBottom.add(cardDrawProbaPanel, BorderLayout.EAST);
final JTabbedPane tabbedDeckSide = new JTabbedPane(JTabbedPane.BOTTOM);
panneauDeck.setLeftComponent(tabbedDeckSide);
JScrollPane scrollDeck = new JScrollPane();
tabbedDeckSide.addTab("Main", null, scrollDeck, null);
tableDeck = new JTable();
scrollDeck.setViewportView(tableDeck);
tableDeck.setModel(deckmodel);
tableDeck.getColumnModel().getColumn(2).setCellRenderer(new ManaCellRenderer());
tableDeck.setRowHeight(MTGConstants.TABLE_ROW_HEIGHT);
tableDeck.setRowSorter(sorterCards);
tableDeck.getColumnModel().getColumn(4).setCellEditor(new IntegerCellEditor());
tableDeck.getColumnModel().getColumn(3).setCellRenderer(new MagicEditionRenderer());
tableDeck.getColumnModel().getColumn(3).setCellEditor(new MagicEditionListEditor());
JScrollPane scrollSideboard = new JScrollPane();
tabbedDeckSide.addTab("SideBoard", null, scrollSideboard, null);
tableSide = new JTable();
tableSide.setModel(deckSidemodel);
tableSide.setRowHeight(MTGConstants.TABLE_ROW_HEIGHT);
tableSide.getColumnModel().getColumn(2).setCellRenderer(new ManaCellRenderer());
tableSide.getColumnModel().getColumn(4).setCellEditor(new IntegerCellEditor());
tableSide.getColumnModel().getColumn(3).setCellRenderer(new MagicEditionRenderer());
tableSide.getColumnModel().getColumn(3).setCellEditor(new MagicEditionListEditor());
tableSide.getColumnModel().getColumn(4).setCellEditor(new IntegerCellEditor());
scrollSideboard.setViewportView(tableSide);
tableDeck.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
MagicCard mc = (MagicCard) tableDeck.getValueAt(tableDeck.getSelectedRow(), 0);
magicCardDetailPanel.setMagicCard(mc);
cardDrawProbaPanel.init(deck, mc);
}
});
tableSide.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
MagicCard mc = (MagicCard) tableSide.getValueAt(tableSide.getSelectedRow(), 0);
magicCardDetailPanel.setMagicCard(mc);
}
});
tableDeck.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
MagicCard mc = (MagicCard) tableDeck.getValueAt(tableDeck.getSelectedRow(), 0);
if (e.getKeyCode() == 0) {
deck.getMap().remove(mc);
deckmodel.init(deck);
}
}
});
tableSide.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
MagicCard mc = (MagicCard) tableSide.getValueAt(tableSide.getSelectedRow(), 0);
if (e.getKeyCode() == 0) {
deck.getMapSideBoard().remove(mc);
deckmodel.init(deck);
}
}
});
tableDeck.getModel().addTableModelListener(e -> updatePanels());
tableSide.getModel().addTableModelListener(e -> updatePanels());
tableDeck.getDefaultEditor(String.class).addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
updatePanels();
}
@Override
public void editingCanceled(ChangeEvent e) {
updatePanels();
}
});
tableSide.getDefaultEditor(String.class).addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
updatePanels();
}
@Override
public void editingCanceled(ChangeEvent e) {
updatePanels();
}
});
JPanel panelInfoDeck = new JPanel();
tabbedPane.addTab(MTGControler.getInstance().getLangService().getCapitalize("INFORMATIONS"), null, panelInfoDeck, null);
panelInfoDeck.setLayout(new BorderLayout(0, 0));
panelInfoDeck.add(deckDetailsPanel, BorderLayout.NORTH);
deckDetailsPanel.setMagicDeck(deck);
cmcChartPanel = new CmcChartPanel();
manaRepartitionPanel = new ManaRepartitionPanel();
typeRepartitionPanel = new TypeRepartitionPanel();
rarityRepartitionPanel = new RarityRepartitionPanel();
drawProbabilityPanel = new DrawProbabilityPanel();
JPanel randomHandPanel = new JPanel();
JPanel statPanel = new JPanel();
randomHandPanel.setLayout(new BorderLayout(0, 0));
randomHandPanel.add(thumbnail, BorderLayout.CENTER);
statPanel.setLayout(new GridLayout(3, 2, 0, 0));
statPanel.add(manaRepartitionPanel);
statPanel.add(typeRepartitionPanel);
statPanel.add(rarityRepartitionPanel);
statPanel.add(cmcChartPanel);
statPanel.add(drawProbabilityPanel);
tabbedPane.addTab(MTGControler.getInstance().getLangService().getCapitalize("STATS"), null, statPanel, null);
deckPricePanel = new DeckPricePanel();
statPanel.add(deckPricePanel);
tabbedPane.addTab(MTGControler.getInstance().getLangService().getCapitalize("SAMPLE_HAND"), null, randomHandPanel, null);
JPanel panel = new JPanel();
randomHandPanel.add(panel, BorderLayout.NORTH);
JButton btnDrawAHand = new JButton(MTGControler.getInstance().getLangService().getCapitalize("DRAW_HAND"));
btnDrawAHand.addActionListener(ae -> {
thumbnail.removeAll();
p.mixHandAndLibrary();
p.shuffleLibrary();
p.drawCard(7);
thumbnail.initThumbnails(p.getHand(), false, false);
});
panel.add(btnDrawAHand);
JPanel panneauGauche = new JPanel();
add(panneauGauche, BorderLayout.WEST);
panneauGauche.setLayout(new BorderLayout(0, 0));
scrollResult = new JScrollPane();
panneauGauche.add(scrollResult);
listResult = new JList<>(new DefaultListModel<MagicCard>());
listResult.setCellRenderer(new MagicCardListRenderer());
listResult.setMinimumSize(new Dimension(100, 0));
listResult.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollResult.setViewportView(listResult);
JPanel panneauResultFilter = new JPanel();
panneauGauche.add(panneauResultFilter, BorderLayout.NORTH);
groupsFilterResult = new ButtonGroup() {
@Override
public void setSelected(ButtonModel model, boolean selected) {
if (selected) {
super.setSelected(model, selected);
} else {
clearSelection();
}
}
};
JToggleButton tglbtnStd = new JToggleButton("STD");
tglbtnStd.setActionCommand("Standard");
panneauResultFilter.add(tglbtnStd);
JToggleButton tglbtnMdn = new JToggleButton("MDN");
tglbtnMdn.setActionCommand("Modern");
panneauResultFilter.add(tglbtnMdn);
JToggleButton tglbtnLeg = new JToggleButton("LEG");
tglbtnLeg.setActionCommand("Legacy");
panneauResultFilter.add(tglbtnLeg);
JToggleButton tglbtnVin = new JToggleButton("VIN");
tglbtnVin.setActionCommand("Vintage");
panneauResultFilter.add(tglbtnVin);
groupsFilterResult.add(tglbtnStd);
groupsFilterResult.add(tglbtnMdn);
groupsFilterResult.add(tglbtnLeg);
groupsFilterResult.add(tglbtnVin);
listResult.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
if (ev.getClickCount() == 1 && !ev.isConsumed()) {
ev.consume();
MagicCard mc = listResult.getSelectedValue();
magicCardDetailPanel.setMagicCard(mc);
}
if (ev.getClickCount() == 2 && !ev.isConsumed()) {
ev.consume();
MagicCard mc = listResult.getSelectedValue();
if (getSelectedMap().get(mc) != null) {
getSelectedMap().put(mc, deck.getMap().get(mc) + 1);
} else {
getSelectedMap().put(mc, 1);
}
deckmodel.init(deck);
deckSidemodel.init(deck);
}
}
});
txtSearch.addActionListener(e -> btnSearch.doClick());
tabbedDeckSide.addChangeListener(e -> selectedIndex = tabbedDeckSide.getSelectedIndex());
btnSearch.addActionListener(aeSearch -> {
if (txtSearch.getText().equals(""))
return;
resultListModel.removeAllElements();
ThreadManager.getInstance().execute(() -> {
try {
String searchName = txtSearch.getText();
List<MagicCard> cards = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria(cboAttributs.getSelectedItem().toString(), searchName, null, false);
MagicFormat form = new MagicFormat();
for (MagicCard m : cards) {
if (groupsFilterResult.getSelection() != null) {
form.setFormat(groupsFilterResult.getSelection().getActionCommand());
if (m.getLegalities().contains(form))
resultListModel.addElement(m);
} else {
resultListModel.addElement(m);
}
}
lblCards.setText(resultListModel.size() + " " + MTGControler.getInstance().getLangService().get("RESULTS"));
listResult.setModel(resultListModel);
listResult.updateUI();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), MTGControler.getInstance().getLangService().getError(), JOptionPane.ERROR_MESSAGE);
}
}, "search deck");
});
}
use of org.magic.api.beans.MagicFormat in project MtgDesktopCompanion by nicho92.
the class MtgjsonProvider method search.
private List<MagicCard> search(String jsquery, String att, String crit) {
currentSet = new ArrayList<>();
List<MagicCard> listCards = new ArrayList<>();
logger.debug("searchCardByCriteria : " + jsquery);
List<Map<String, Object>> cardsElement = ctx.withListeners(fr -> {
if (fr.path().startsWith("$")) {
currentSet.add(fr.path().substring(fr.path().indexOf("$[") + 3, fr.path().indexOf("]") - 1));
}
return null;
}).read(jsquery, List.class);
int indexSet = 0;
for (Map<String, Object> map : cardsElement) {
MagicCard mc = new MagicCard();
if (map.get("name") != null)
mc.setName(map.get("name").toString());
mc.setFlippable(false);
mc.setTranformable(false);
if (map.get("multiverseid") != null)
mc.setMultiverseid((int) (double) map.get("multiverseid"));
mc.setId(String.valueOf(map.get("id")));
mc.setText(String.valueOf(map.get("text")));
if (map.get("cmc") != null)
mc.setCmc((int) Double.parseDouble(String.valueOf(map.get("cmc"))));
mc.setPower(String.valueOf(map.get("power")));
mc.setToughness(String.valueOf(map.get("toughness")));
mc.setFlavor(String.valueOf(map.get("flavor")));
mc.setArtist(String.valueOf(map.get("artist")));
mc.setLayout(String.valueOf(map.get("layout")));
if (map.get("mciNumber") != null)
mc.setMciNumber(String.valueOf(map.get("mciNumber")));
if (map.get("originalText") != null)
mc.setOriginalText(String.valueOf(map.get("originalText")));
if (map.get("originalType") != null)
mc.setOriginalType(String.valueOf(map.get("originalType")));
if (map.get("supertypes") != null)
mc.getSupertypes().addAll((List<String>) map.get("supertypes"));
if (map.get("types") != null)
mc.getTypes().addAll((List<String>) map.get("types"));
if (map.get("subtypes") != null)
mc.getSubtypes().addAll((List<String>) map.get("subtypes"));
if (map.get("variations") != null)
mc.getVariations().addAll((List<Integer>) map.get("variations"));
if (map.get("colors") != null)
mc.getColors().addAll((List<String>) map.get("colors"));
if (map.get("colorIdentity") != null)
mc.getColorIdentity().addAll((List<String>) map.get("colorIdentity"));
if (map.get("watermark") != null)
mc.setWatermarks(String.valueOf(map.get("watermark")));
if (map.get("number") != null)
mc.setNumber(String.valueOf(map.get("number")));
if (map.get("gathererCode") != null)
mc.setGathererCode(String.valueOf(map.get("gathererCode")));
if (map.get("reserved") != null)
mc.setReserved(Boolean.valueOf(String.valueOf(map.get("reserved"))));
if (map.get("loyalty") != null) {
try {
mc.setLoyalty((int) Double.parseDouble(map.get("loyalty").toString()));
} catch (Exception e) {
mc.setLoyalty(0);
}
}
if (map.get("manaCost") != null)
mc.setCost(String.valueOf(map.get("manaCost")));
else
mc.setCost("");
if (map.get("legalities") != null) {
for (Map<String, Object> mapFormats : (List<Map>) map.get("legalities")) {
MagicFormat mf = new MagicFormat();
mf.setFormat(String.valueOf(mapFormats.get("format")));
mf.setLegality(String.valueOf(mapFormats.get("legality")));
mc.getLegalities().add(mf);
}
}
if (map.get("rulings") != null) {
for (Map<String, Object> mapRules : (List<Map>) map.get("rulings")) {
MagicRuling mr = new MagicRuling();
mr.setDate(String.valueOf(mapRules.get("date")));
mr.setText(String.valueOf(mapRules.get("text")));
mc.getRulings().add(mr);
}
}
String codeEd;
if (currentSet.size() == 1)
codeEd = currentSet.get(0);
else
codeEd = currentSet.get(indexSet++);
MagicEdition me = getSetById(codeEd);
me.setRarity(String.valueOf(map.get("rarity")));
me.setNumber(mc.getNumber());
if (mc.getMultiverseid() != null)
me.setMultiverse_id(String.valueOf(mc.getMultiverseid()));
mc.getEditions().add(me);
if (// mtgjson give null text for basic Land. Modify it for adding mana color
me.getRarity().equals("Basic Land")) {
mc.setText(mc.getOriginalText());
switch(mc.getName()) {
case "Plains":
mc.setText("{T} : Add {W} to your mana pool");
break;
case "Island":
mc.setText("{T} : Add {U} to your mana pool");
break;
case "Swamp":
mc.setText("{T} : Add {B} to your mana pool");
break;
case "Mountain":
mc.setText("{T} : Add {R} to your mana pool");
break;
case "Forest":
mc.setText("{T} : Add {G} to your mana pool");
break;
default:
break;
}
}
/*get other sets*/
if (// too much elements, so, remove all re-printings information for basic lands
!me.getRarity().equals("Basic Land") && map.get("printings") != null) {
for (String print : (List<String>) map.get("printings")) {
if (!print.equalsIgnoreCase(codeEd)) {
MagicEdition meO = getSetById(print);
if (mc.getMultiverseid() == null)
meO.setMultiverse_id(String.valueOf(0));
initOtherEditionCardsVar(mc, meO);
mc.getEditions().add(meO);
}
}
}
MagicCardNames defnames = new MagicCardNames();
defnames.setLanguage("English");
defnames.setName(mc.getName());
if (mc.getMultiverseid() != null)
defnames.setGathererId(mc.getMultiverseid());
mc.getForeignNames().add(defnames);
if (map.get("foreignNames") != null) {
for (Map<String, Object> mapNames : (List<Map>) map.get("foreignNames")) {
MagicCardNames fnames = new MagicCardNames();
fnames.setLanguage(String.valueOf(mapNames.get("language")));
fnames.setName(String.valueOf(mapNames.get("name")));
if (mapNames.get("multiverseid") != null)
fnames.setGathererId((int) (double) mapNames.get("multiverseid"));
mc.getForeignNames().add(fnames);
}
}
if (map.get("names") != null) {
((List) map.get("names")).remove(mc.getName());
String rotateName = ((List) map.get("names")).get(((List) map.get("names")).size() - 1).toString();
mc.setRotatedCardName(rotateName);
if (mc.getLayout().equals("flip"))
mc.setFlippable(true);
}
if (mc.getLayout().equals("double-faced") || mc.getLayout().equals("meld"))
mc.setTranformable(true);
setChanged();
notifyObservers(mc);
listCards.add(mc);
}
currentSet.clear();
if (att.equalsIgnoreCase("set"))
cachedCardEds.put(crit, listCards);
return listCards;
}
use of org.magic.api.beans.MagicFormat in project MtgDesktopCompanion by nicho92.
the class DeckbrewProvider method searchCardByCriteria.
public List<MagicCard> searchCardByCriteria(String att, String crit, MagicEdition me, boolean exact) throws IOException {
crit = att + "=" + URLEncoder.encode(crit, encoding);
String url = urldeckbrewJSON + "/cards?" + crit;
Reader reader = new InputStreamReader(new URL(url).openStream(), encoding);
List<MagicCard> retour = new ArrayList<>();
JsonArray root = new JsonParser().parse(reader).getAsJsonArray();
int page = 1;
boolean gonextpage = true;
while (gonextpage) {
String pagination;
if (crit == null)
pagination = "?";
else
pagination = "&";
URL u = null;
if (root.size() == 100) {
u = new URL(url + pagination + "page=" + page++);
gonextpage = true;
} else {
u = new URL(url);
gonextpage = false;
}
logger.info("Connexion to " + u);
reader = new InputStreamReader(u.openStream(), encoding);
root = new JsonParser().parse(reader).getAsJsonArray();
for (int i = 0; i < root.size(); i++) {
JsonObject e = root.get(i).getAsJsonObject();
MagicCard mc = new MagicCard();
if (e.get("name") != null)
mc.setName(e.get("name").getAsString());
if (e.get("id") != null)
mc.setId(e.get("id").getAsString());
if (e.get("cmc") != null)
mc.setCmc(e.get("cmc").getAsInt());
if (e.get("cost") != null)
mc.setCost(e.get("cost").getAsString());
if (e.get("text") != null)
mc.setText(e.get("text").getAsString());
if (e.get("power") != null)
mc.setPower(e.get("power").getAsString());
if (e.get("toughness") != null)
mc.setToughness(e.get("toughness").getAsString());
if (e.get("types") != null) {
Iterator<JsonElement> it = e.get("types").getAsJsonArray().iterator();
while (it.hasNext()) {
mc.getTypes().add(it.next().getAsString());
}
}
if (e.get("subtypes") != null) {
Iterator<JsonElement> it2 = e.get("subtypes").getAsJsonArray().iterator();
while (it2.hasNext()) {
mc.getSubtypes().add(it2.next().getAsString());
}
}
if (e.get("colors") != null) {
Iterator<JsonElement> it3 = e.get("colors").getAsJsonArray().iterator();
while (it3.hasNext()) {
mc.getColors().add(it3.next().getAsString());
}
}
JsonArray editions = e.getAsJsonArray("editions");
for (int j = 0; j < editions.size(); j++) {
JsonObject obj = editions.get(j).getAsJsonObject();
MagicEdition ed = getSetById(obj.get("set_id").getAsString());
ed.setArtist(obj.get("artist").getAsString());
ed.setMultiverse_id(obj.get("multiverse_id").getAsString());
ed.setRarity(obj.get("rarity").getAsString());
ed.setNumber(obj.get("number").getAsString());
MagicCardNames name = new MagicCardNames();
name.setName(mc.getName());
name.setLanguage("English");
name.setGathererId(Integer.parseInt(ed.getMultiverse_id()));
mc.getForeignNames().add(name);
mc.setLayout(obj.get("layout").getAsString());
mc.getEditions().add(ed);
mc.setNumber(ed.getNumber());
}
if (e.get("formats") != null) {
JsonObject obj = e.get("formats").getAsJsonObject();
for (Entry<String, JsonElement> k : obj.entrySet()) {
MagicFormat format = new MagicFormat();
format.setFormat(k.getKey());
format.setLegality(k.getValue().getAsString());
mc.getLegalities().add(format);
}
}
setChanged();
notifyObservers(mc);
retour.add(mc);
}
}
return retour;
}
use of org.magic.api.beans.MagicFormat in project MtgDesktopCompanion by nicho92.
the class MagicTheGatheringIOProvider method generateCard.
private MagicCard generateCard(JsonObject obj) throws IOException {
MagicCard mc = new MagicCard();
if (obj.get("name") != null)
mc.setName(obj.get("name").getAsString());
if (obj.get("manaCost") != null)
mc.setCost(obj.get("manaCost").getAsString());
if (obj.get("text") != null)
mc.setText(obj.get("text").getAsString());
if (obj.get("originalText") != null)
mc.setOriginalText(obj.get("originalText").getAsString());
if (obj.get("id") != null)
mc.setId(obj.get("id").getAsString());
if (obj.get("artist") != null)
mc.setArtist(obj.get("artist").getAsString());
if (obj.get("cmc") != null)
mc.setCmc(obj.get("cmc").getAsInt());
if (obj.get("layout") != null)
mc.setLayout(obj.get("layout").getAsString());
if (obj.get("number") != null)
mc.setNumber(obj.get("number").getAsString());
if (obj.get("power") != null)
mc.setPower(obj.get("power").getAsString());
if (obj.get("toughness") != null)
mc.setToughness(obj.get("toughness").getAsString());
if (obj.get("loyalty") != null)
mc.setLoyalty(obj.get("loyalty").getAsInt());
if (obj.get("mciNumber") != null)
mc.setMciNumber(String.valueOf(obj.get("mciNumber")));
if (obj.get("colors") != null) {
Iterator<JsonElement> it = obj.get("colors").getAsJsonArray().iterator();
while (it.hasNext()) mc.getColors().add(it.next().getAsString());
}
if (obj.get("types") != null) {
Iterator<JsonElement> it = obj.get("types").getAsJsonArray().iterator();
while (it.hasNext()) mc.getTypes().add(it.next().getAsString());
}
if (obj.get("supertypes") != null) {
Iterator<JsonElement> it = obj.get("supertypes").getAsJsonArray().iterator();
while (it.hasNext()) mc.getSupertypes().add(it.next().getAsString());
}
if (obj.get("subtypes") != null) {
Iterator<JsonElement> it = obj.get("subtypes").getAsJsonArray().iterator();
while (it.hasNext()) mc.getSubtypes().add(it.next().getAsString());
}
if (obj.get("legalities") != null) {
JsonArray arr = obj.get("legalities").getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject k = arr.get(i).getAsJsonObject();
MagicFormat format = new MagicFormat();
format.setFormat(k.get("format").getAsString());
format.setLegality(k.get("legality").getAsString());
mc.getLegalities().add(format);
}
}
if (obj.get("rulings") != null) {
JsonArray arr = obj.get("rulings").getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject k = arr.get(i).getAsJsonObject();
MagicRuling rule = new MagicRuling();
rule.setDate(k.get("date").getAsString());
rule.setText(k.get("text").getAsString());
mc.getRulings().add(rule);
}
}
if (obj.get("names") != null) {
JsonArray arr = obj.get("names").getAsJsonArray();
List<String> list = new ArrayList<>();
for (int i = 0; i < arr.size(); list.add(arr.get(i++).getAsString())) {
// TODO complete this function
}
list.remove(mc.getName());
String rotateName = (list.get(list.size() - 1));
mc.setRotatedCardName(rotateName);
if (mc.getLayout().equals("flip"))
mc.setFlippable(true);
if (mc.getLayout().equals("double-faced") || mc.getLayout().equals("meld"))
mc.setTranformable(true);
}
String currentSet = obj.get("set").getAsString();
MagicEdition currentEd = getSetById(currentSet);
if (obj.get("multiverseid") != null)
currentEd.setMultiverse_id(obj.get("multiverseid").getAsString());
if (obj.get("rarity") != null)
currentEd.setRarity(obj.get("rarity").getAsString());
currentEd.setNumber(mc.getNumber());
mc.getEditions().add(0, currentEd);
if (obj.get("printings") != null) {
JsonArray arr = obj.get("printings").getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
String k = arr.get(i).getAsString();
if (!k.equals(currentSet)) {
MagicEdition ed = getSetById(k);
initOtherEdVariable(mc, ed);
mc.getEditions().add(ed);
}
}
}
MagicCardNames defaultMcn = new MagicCardNames();
defaultMcn.setName(mc.getName());
defaultMcn.setLanguage("English");
try {
defaultMcn.setGathererId(Integer.parseInt(currentEd.getMultiverse_id()));
} catch (Exception e) {
defaultMcn.setGathererId(0);
}
mc.getForeignNames().add(defaultMcn);
if (obj.get("foreignNames") != null) {
JsonArray arr = obj.get("foreignNames").getAsJsonArray();
for (int i = 0; i < arr.size(); i++) {
JsonObject lang = arr.get(i).getAsJsonObject();
MagicCardNames mcn = new MagicCardNames();
mcn.setName(lang.get("name").getAsString());
mcn.setLanguage(lang.get("language").getAsString());
if (lang.get("multiverseid") != null)
mcn.setGathererId(lang.get("multiverseid").getAsInt());
mc.getForeignNames().add(mcn);
}
}
setChanged();
notifyObservers(mc);
return mc;
}
Aggregations