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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package cuchaz.enigma.gui.panels;
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import cuchaz.enigma.analysis.StructureTreeNode;
import cuchaz.enigma.analysis.StructureTreeOptions;
import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.renderer.StructureOptionListCellRenderer;
import cuchaz.enigma.gui.util.GridBagConstraintsBuilder;
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;
public class StructurePanel {
private final Gui gui;
private final JPanel panel = new JPanel(new BorderLayout());
private final JPanel optionsPanel;
private final JLabel obfuscationVisibilityLabel = new JLabel();
private final JLabel documentationVisibilityLabel = new JLabel();
private final JLabel sortingOrderLabel = new JLabel();
private final JComboBox<StructureTreeOptions.ObfuscationVisibility> obfuscationVisibility;
private final JComboBox<StructureTreeOptions.DocumentationVisibility> documentationVisibility;
private final JComboBox<StructureTreeOptions.SortingOrder> sortingOrder;
private final JTree structureTree;
public StructurePanel(Gui gui) {
this.gui = gui;
this.optionsPanel = new JPanel(new GridBagLayout());
this.optionsPanel.setVisible(false);
GridBagConstraintsBuilder cb = GridBagConstraintsBuilder.create().insets(5).fill(GridBagConstraints.HORIZONTAL);
this.optionsPanel.add(this.obfuscationVisibilityLabel, cb.pos(0, 0).build());
this.obfuscationVisibility = new JComboBox<>(StructureTreeOptions.ObfuscationVisibility.values());
this.obfuscationVisibility.setRenderer(new StructureOptionListCellRenderer());
this.obfuscationVisibility.addActionListener(event -> this.showStructure(gui.getActiveEditor()));
this.optionsPanel.add(this.obfuscationVisibility, cb.pos(1, 0).build());
this.optionsPanel.add(this.documentationVisibilityLabel, cb.pos(0, 1).build());
this.documentationVisibility = new JComboBox<>(StructureTreeOptions.DocumentationVisibility.values());
this.documentationVisibility.setRenderer(new StructureOptionListCellRenderer());
this.documentationVisibility.addActionListener(event -> this.showStructure(gui.getActiveEditor()));
this.optionsPanel.add(this.documentationVisibility, cb.pos(1, 1).build());
this.optionsPanel.add(this.sortingOrderLabel, cb.pos(0, 2).build());
this.sortingOrder = new JComboBox<>(StructureTreeOptions.SortingOrder.values());
this.sortingOrder.setRenderer(new StructureOptionListCellRenderer());
this.sortingOrder.addActionListener(event -> this.showStructure(gui.getActiveEditor()));
this.optionsPanel.add(this.sortingOrder, cb.pos(1, 2).build());
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(GuiUtil.onMouseClick(this::onClick));
this.retranslateUi();
this.panel.add(this.optionsPanel, BorderLayout.NORTH);
this.panel.add(new JScrollPane(this.structureTree));
}
public void showStructure(EditorPanel editor) {
structureTree.setModel(null);
if (editor == null) {
this.optionsPanel.setVisible(false);
return;
}
ClassEntry classEntry = editor.getClassHandle().getRef();
if (classEntry == null) return;
this.optionsPanel.setVisible(true);
// get the class structure
StructureTreeNode node = this.gui.getController().getClassStructure(classEntry, this.getOptions());
// show the tree at the root
TreePath path = GuiUtil.getPathToRoot(node);
structureTree.setModel(new DefaultTreeModel((TreeNode) path.getPathComponent(0)));
structureTree.expandPath(path);
structureTree.setSelectionRow(structureTree.getRowForPath(path));
}
private void onClick(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) {
this.gui.getController().navigateTo(((StructureTreeNode) node).getEntry());
}
}
}
/**
* Creates and returns the options of this structure panel.
*/
private StructureTreeOptions getOptions() {
return new StructureTreeOptions(
(StructureTreeOptions.ObfuscationVisibility) this.obfuscationVisibility.getSelectedItem(),
(StructureTreeOptions.DocumentationVisibility) this.documentationVisibility.getSelectedItem(),
(StructureTreeOptions.SortingOrder) this.sortingOrder.getSelectedItem()
);
}
public void retranslateUi() {
this.obfuscationVisibilityLabel.setText(I18n.translate("structure.options.obfuscation"));
this.documentationVisibilityLabel.setText(I18n.translate("structure.options.documentation"));
this.sortingOrderLabel.setText(I18n.translate("structure.options.sorting"));
}
public JPanel getPanel() {
return this.panel;
}
private static 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 classEntry) {
this.setIcon(GuiUtil.getClassIcon(gui, classEntry));
} else if (entry instanceof MethodEntry methodEntry) {
this.setIcon(GuiUtil.getMethodIcon(methodEntry));
} else if (entry instanceof FieldEntry) {
this.setIcon(GuiUtil.FIELD_ICON);
}
this.setText("<html>" + ((StructureTreeNode) value).toHtml());
return c;
}
}
}
|