summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/StructurePanel.java
blob: d8c4661464ce3ab724043ab88dbe8cb0efdbddd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cuchaz.enigma.gui.panels;

import cuchaz.enigma.analysis.StructureTreeNode;
import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.util.GuiUtil;
import cuchaz.enigma.gui.util.SingleTreeSelectionModel;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import cuchaz.enigma.translation.representation.entry.ParentedEntry;
import cuchaz.enigma.utils.I18n;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class StructurePanel extends JPanel {
    private JPanel sortingPanel;
    private JCheckBox hideDeobfuscated;

    private JTree structureTree;

    public StructurePanel(Gui gui) {
        this.sortingPanel = new JPanel();
        this.hideDeobfuscated = new JCheckBox(I18n.translate("info_panel.tree.structure.hide_deobfuscated"));
        this.hideDeobfuscated.addActionListener(event -> gui.showStructure(gui.getActiveEditor()));
        this.sortingPanel.add(this.hideDeobfuscated);
        this.sortingPanel.setVisible(false);

        this.structureTree = new JTree();
        this.structureTree.setModel(null);
        this.structureTree.setCellRenderer(new StructureTreeCellRenderer(gui));
        this.structureTree.setSelectionModel(new SingleTreeSelectionModel());
        this.structureTree.setShowsRootHandles(true);
        this.structureTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                if (event.getClickCount() >= 2 && event.getButton() == MouseEvent.BUTTON1) {
                    // get the selected node
                    TreePath path = structureTree.getSelectionPath();
                    if (path == null) {
                        return;
                    }

                    Object node = path.getLastPathComponent();
                    if (node instanceof StructureTreeNode) {
                        gui.getController().navigateTo(((StructureTreeNode) node).getEntry());
                    }
                }
            }
        });

        this.setLayout(new BorderLayout());
        this.add(this.sortingPanel, BorderLayout.NORTH);
        this.add(new JScrollPane(this.structureTree));
    }

    public JPanel getSortingPanel() {
        return this.sortingPanel;
    }

    /**
     * Returns whether the "Hide Deobfuscated" option of this structure panel is selected.
     */
    public boolean shouldHideDeobfuscated() {
        return this.hideDeobfuscated.isSelected();
    }

    public JTree getStructureTree() {
        return this.structureTree;
    }

    public void retranslateUi() {
        this.hideDeobfuscated.setText(I18n.translate("info_panel.tree.structure.hide_deobfuscated"));
    }

    class StructureTreeCellRenderer extends DefaultTreeCellRenderer {
        private final Gui gui;

        StructureTreeCellRenderer(Gui gui) {
            this.gui = gui;
        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            ParentedEntry<?> entry = ((StructureTreeNode) value).getEntry();

            if (entry instanceof ClassEntry) {
                this.setIcon(GuiUtil.getClassIcon(gui, (ClassEntry) entry));
            } else if (entry instanceof MethodEntry) {
                this.setIcon(GuiUtil.getMethodIcon((MethodEntry) entry));
            } else if (entry instanceof FieldEntry) {
                this.setIcon(GuiUtil.FIELD_ICON);
            }

            this.setText("<html>" + ((StructureTreeNode) value).toHtml());

            return c;
        }
    }
}