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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
package cuchaz.enigma.gui.panels;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.gui.EditableType;
import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.elements.ConvertingTextField;
import cuchaz.enigma.gui.events.ConvertingTextFieldListener;
import cuchaz.enigma.gui.util.GridBagConstraintsBuilder;
import cuchaz.enigma.gui.util.GuiUtil;
import cuchaz.enigma.gui.util.ScaleUtil;
import cuchaz.enigma.translation.mapping.AccessModifier;
import cuchaz.enigma.translation.mapping.EntryChange;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.representation.entry.*;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.utils.validation.ValidationContext;
public class IdentifierPanel {
private final Gui gui;
private final JPanel ui = new JPanel();
private Entry<?> entry;
private Entry<?> deobfEntry;
private ConvertingTextField nameField;
private final ValidationContext vc = new ValidationContext();
public IdentifierPanel(Gui gui) {
this.gui = gui;
this.ui.setLayout(new GridBagLayout());
this.ui.setPreferredSize(ScaleUtil.getDimension(0, 120));
this.ui.setBorder(BorderFactory.createTitledBorder(I18n.translate("info_panel.identifier")));
this.ui.setEnabled(false);
}
public void setReference(Entry<?> entry) {
this.entry = entry;
refreshReference();
}
public boolean startRenaming() {
if (this.nameField == null) return false;
this.nameField.startEditing();
return true;
}
public boolean startRenaming(String text) {
if (this.nameField == null) return false;
this.nameField.startEditing();
this.nameField.setEditText(text);
return true;
}
private void onModifierChanged(AccessModifier modifier) {
gui.validateImmediateAction(vc -> this.gui.getController().applyChange(vc, EntryChange.modify(entry).withAccess(modifier)));
}
public void refreshReference() {
this.deobfEntry = entry == null ? null : gui.getController().project.getMapper().deobfuscate(this.entry);
this.nameField = null;
TableHelper th = new TableHelper(this.ui, this.entry, this.gui);
th.begin();
if (this.entry == null) {
this.ui.setEnabled(false);
} else {
this.ui.setEnabled(true);
if (deobfEntry instanceof ClassEntry) {
ClassEntry ce = (ClassEntry) deobfEntry;
String name = ce.isInnerClass() ? ce.getName() : ce.getFullName();
this.nameField = th.addRenameTextField(EditableType.CLASS, name);
th.addModifierRow(I18n.translate("info_panel.identifier.modifier"), EditableType.CLASS, this::onModifierChanged);
} else if (deobfEntry instanceof FieldEntry) {
FieldEntry fe = (FieldEntry) deobfEntry;
this.nameField = th.addRenameTextField(EditableType.FIELD, fe.getName());
th.addStringRow(I18n.translate("info_panel.identifier.class"), fe.getParent().getFullName());
th.addCopiableStringRow(I18n.translate("info_panel.identifier.type_descriptor"), fe.getDesc().toString());
th.addModifierRow(I18n.translate("info_panel.identifier.modifier"), EditableType.FIELD, this::onModifierChanged);
} else if (deobfEntry instanceof MethodEntry) {
MethodEntry me = (MethodEntry) deobfEntry;
if (me.isConstructor()) {
ClassEntry ce = me.getParent();
if (ce != null) {
String name = ce.isInnerClass() ? ce.getName() : ce.getFullName();
this.nameField = th.addRenameTextField(EditableType.CLASS, name);
}
} else {
this.nameField = th.addRenameTextField(EditableType.METHOD, me.getName());
th.addStringRow(I18n.translate("info_panel.identifier.class"), me.getParent().getFullName());
}
th.addCopiableStringRow(I18n.translate("info_panel.identifier.method_descriptor"), me.getDesc().toString());
th.addModifierRow(I18n.translate("info_panel.identifier.modifier"), EditableType.METHOD, this::onModifierChanged);
} else if (deobfEntry instanceof LocalVariableEntry) {
LocalVariableEntry lve = (LocalVariableEntry) deobfEntry;
EditableType type;
if (lve.isArgument()) {
type = EditableType.PARAMETER;
} else {
type = EditableType.LOCAL_VARIABLE;
}
this.nameField = th.addRenameTextField(type, lve.getName());
th.addStringRow(I18n.translate("info_panel.identifier.class"), lve.getContainingClass().getFullName());
th.addStringRow(I18n.translate("info_panel.identifier.method"), lve.getParent().getName());
th.addStringRow(I18n.translate("info_panel.identifier.index"), Integer.toString(lve.getIndex()));
} else {
throw new IllegalStateException("unreachable");
}
}
th.end();
if (this.nameField != null) {
this.nameField.addListener(new ConvertingTextFieldListener() {
@Override
public void onStartEditing(ConvertingTextField field) {
int i = field.getText().lastIndexOf('/');
if (i != -1) {
field.selectSubstring(i + 1);
}
}
@Override
public boolean tryStopEditing(ConvertingTextField field, boolean abort) {
if (abort) return true;
vc.reset();
vc.setActiveElement(field);
validateRename(field.getText());
return vc.canProceed();
}
@Override
public void onStopEditing(ConvertingTextField field, boolean abort) {
if (!abort) {
vc.reset();
vc.setActiveElement(field);
doRename(field.getText());
}
EditorPanel e = gui.getActiveEditor();
if (e != null) {
e.getEditor().requestFocusInWindow();
}
}
});
}
this.ui.validate();
this.ui.repaint();
}
private void validateRename(String newName) {
gui.getController().validateChange(vc, EntryChange.modify(entry).withDeobfName(newName));
}
private void doRename(String newName) {
EntryChange<? extends Entry<?>> change = EntryChange.modify(entry).withDeobfName(newName);
gui.getController().applyChange(vc, change);
}
public void retranslateUi() {
this.ui.setBorder(BorderFactory.createTitledBorder(I18n.translate("info_panel.identifier")));
this.refreshReference();
}
public JPanel getUi() {
return ui;
}
private static final class TableHelper {
private final Container c;
private final Entry<?> e;
private final Gui gui;
private int row;
public TableHelper(Container c, Entry<?> e, Gui gui) {
this.c = c;
this.e = e;
this.gui = gui;
}
public void begin() {
c.removeAll();
c.setLayout(new GridBagLayout());
}
public void addRow(Component c1, Component c2) {
GridBagConstraintsBuilder cb = GridBagConstraintsBuilder.create()
.insets(2)
.anchor(GridBagConstraints.WEST);
c.add(c1, cb.pos(0, this.row).build());
c.add(c2, cb.pos(1, this.row).weightX(1.0).fill(GridBagConstraints.HORIZONTAL).build());
this.row += 1;
}
public void addCopiableRow(JLabel c1, JLabel c2) {
c2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
GuiUtil.copyToClipboard(c2.getText());
GuiUtil.showPopup(c2, I18n.translate("popup.copied"), e.getXOnScreen(), e.getYOnScreen());
}
}
});
addRow(c1, c2);
}
public ConvertingTextField addConvertingTextField(String c1, String c2) {
ConvertingTextField textField = new ConvertingTextField(c2);
addRow(new JLabel(c1), textField.getUi());
return textField;
}
public ConvertingTextField addRenameTextField(EditableType type, String c2) {
String description = switch(type) {
case CLASS -> I18n.translate("info_panel.identifier.class");
case METHOD -> I18n.translate("info_panel.identifier.method");
case FIELD -> I18n.translate("info_panel.identifier.field");
case PARAMETER, LOCAL_VARIABLE -> I18n.translate("info_panel.identifier.variable");
default -> throw new IllegalStateException("Unexpected value: " + type);
};
if (this.gui.getController().project.isRenamable(e)) {
ConvertingTextField field = addConvertingTextField(description, c2);
field.setEditable(this.gui.isEditable(type));
return field;
} else {
addStringRow(description, c2);
return null;
}
}
public void addStringRow(String c1, String c2) {
addRow(new JLabel(c1), GuiUtil.unboldLabel(new JLabel(c2)));
}
public void addCopiableStringRow(String c1, String c2) {
addCopiableRow(new JLabel(c1), GuiUtil.unboldLabel(new JLabel(c2)));
}
public JComboBox<AccessModifier> addModifierRow(String c1, EditableType type, Consumer<AccessModifier> changeListener) {
EnigmaProject project = this.gui.getController().project;
if (!project.isRenamable(e)) {
return null;
}
JComboBox<AccessModifier> combo = new JComboBox<>(AccessModifier.values());
EntryMapping mapping = project.getMapper().getDeobfMapping(e);
combo.setSelectedIndex(mapping.accessModifier().ordinal());
if (this.gui.isEditable(type)) {
combo.addItemListener(event -> {
if (event.getStateChange() == ItemEvent.SELECTED) {
AccessModifier modifier = (AccessModifier) event.getItem();
changeListener.accept(modifier);
}
});
} else {
combo.setEnabled(false);
}
addRow(new JLabel(c1), combo);
return combo;
}
public void end() {
// Add an empty panel with y-weight=1 so that all the other elements get placed at the top edge
c.add(new JPanel(), GridBagConstraintsBuilder.create().pos(0, row).weight(0.0, 1.0).build());
}
}
}
|