Skip to content

Commit

Permalink
enhance tree click on the pined dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nicol committed Oct 25, 2024
1 parent 49d427f commit 002a937
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public void setSelected(ButtonModel model, boolean selected) {
btnPin.addActionListener(al->{

var tree = new DeckTree();
tree.enableThumbnail();
tree.setDeck(deck);

MTGUIComponent.createJDialog(MTGUIComponent.build(tree, "Deck", MTGConstants.ICON_DECK),true,false).setVisible(true);
Expand Down
77 changes: 71 additions & 6 deletions src/main/java/org/magic/gui/components/deck/DeckTree.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package org.magic.gui.components.deck;

import java.awt.BorderLayout;
import java.awt.Color;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.magic.api.beans.MTGCard;
import org.magic.api.beans.MTGDeck;
import org.magic.api.interfaces.MTGDao;
import org.magic.gui.abstracts.MTGUIComponent;
import org.magic.gui.components.card.MagicCardMainDetailPanel;
import org.magic.gui.renderer.DeckTreeCellRenderer;
import org.magic.services.MTGConstants;
import org.magic.services.MTGControler;
import org.magic.services.tools.MTG;
import org.magic.services.tools.UITools;

public class DeckTree extends JTree{

Expand Down Expand Up @@ -71,19 +86,19 @@ private void initTree() {
if (selectedDeck != null) {
for (var mc : selectedDeck.getMain().entrySet()) {
if (mc.getKey().isCreature() && !mc.getKey().isArtifact())
creatureNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
creatureNode.add(new DefaultMutableTreeNode(mc));
else if (mc.getKey().isArtifact())
artifactsNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
artifactsNode.add(new DefaultMutableTreeNode(mc));
else if (mc.getKey().isLand())
landsNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
landsNode.add(new DefaultMutableTreeNode(mc));
else if (mc.getKey().isPlaneswalker())
planeswalkerNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
planeswalkerNode.add(new DefaultMutableTreeNode(mc));
else
spellsNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
spellsNode.add(new DefaultMutableTreeNode(mc));
}

for (var mc : selectedDeck.getSideBoard().entrySet())
sideNode.add(new DefaultMutableTreeNode(mc.getValue() + " " + mc.getKey()));
sideNode.add(new DefaultMutableTreeNode(mc));

model.reload();

Expand All @@ -92,5 +107,55 @@ else if (mc.getKey().isPlaneswalker())
}


public void enableThumbnail()
{
final var popUp = new JPopupMenu();
addTreeSelectionListener(tsl->{

var p = ((DefaultMutableTreeNode)tsl.getPath().getLastPathComponent()).getUserObject();


if(p instanceof Map.Entry e)
{
var mc = (MTGCard) e.getKey();

var pane = new MagicCardMainDetailPanel();
pane.enableThumbnail(true);
pane.init(mc);

popUp.setBorder(new LineBorder(Color.black));
popUp.setVisible(false);
popUp.removeAll();
popUp.setLayout(new BorderLayout());
popUp.add(pane, BorderLayout.CENTER);
popUp.show(this,getParent().getWidth(),0);
popUp.setVisible(true);

}


});
}


public static void main(String[] args) throws SQLException {

MTGControler.getInstance().init();


var d = MTG.getEnabledPlugin(MTGDao.class).getDeckById(163);

var tree = new DeckTree();
tree.setDeck(d);
tree.enableThumbnail();


MTGUIComponent.createJDialog(MTGUIComponent.build(tree, "Deck", MTGConstants.ICON_DECK),true,false).setVisible(true);

}





}
29 changes: 23 additions & 6 deletions src/main/java/org/magic/gui/renderer/DeckTreeCellRenderer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.magic.gui.renderer;

import java.awt.Component;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import org.magic.services.MTGConstants;
Expand All @@ -16,16 +18,31 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
var lab = new JLabel();
lab.setBackground(tree.getBackground());
lab.setForeground(tree.getForeground());
lab.setText(value.toString());
try {
var ic = MTGConstants.getManaSymbol(value.toString());
lab.setIcon(ImageTools.resize(ic, 18, 18));

var node = (DefaultMutableTreeNode)value;

if(node.getUserObject() instanceof String s)
{

lab.setText(s);
try {
var ic = MTGConstants.getManaSymbol(s);
lab.setIcon(ImageTools.resize(ic, 18, 18));
}
catch(Exception e)
{
//do nothing
}

}
catch(Exception e)

if(node.getUserObject() instanceof Map.Entry entry)
{
//do nothing
lab.setText(entry.getValue() + " " + entry.getKey());
}



return lab;
}

Expand Down

0 comments on commit 002a937

Please sign in to comment.