Label editing now done via combo-box
package me.coley.recaf.ui.component.panel;
import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
-import javax.swing.BoxLayout;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
-import javax.swing.JRadioButton;
-import javax.swing.JScrollPane;
-
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
@@ -27,9 +24,9 @@ import org.objectweb.asm.tree.MethodNode;
@SuppressWarnings("serial")
public class LabelSwitcherPanel extends JPanel implements Opcodes {
/**
- * Map of radio buttons to possible opcodes.
+ * Map of strings to labels.
*/
- private final Map<JRadioButton, LabelNode> labels = new LinkedHashMap<>();
+ private final Map<String, LabelNode> labels = new LinkedHashMap<>();
/**
* Initial label value.
*/
@@ -43,61 +40,43 @@ public class LabelSwitcherPanel extends JPanel implements Opcodes {
*/
private final JList<AbstractInsnNode> list;
/**
- * Content wrapper.
+ * Combobox containing label node indices.
*/
- private final JPanel content = new JPanel();
+ private final JComboBox<String> combo = new JComboBox<>();
public LabelSwitcherPanel(JList<AbstractInsnNode> list, MethodNode method, LabelNode initial, Consumer<LabelNode> updater) {
this.initial = initial;
this.updater = updater;
this.list = list;
setLayout(new BorderLayout());
- JScrollPane scroll = new JScrollPane(content);
populate(method.instructions);
- int height = Math.min(200, labels.size() * 20);
- scroll.setPreferredSize(new Dimension(300, height));
- add(scroll, BorderLayout.CENTER);
+ add(combo, BorderLayout.CENTER);
}
private void populate(InsnList opcodes) {
- content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
+ int selected = -1, labelCount = 0;;
+ DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
for (int i = 0; i < opcodes.size(); i++) {
AbstractInsnNode ain = opcodes.get(i);
if (ain.getType() == AbstractInsnNode.LABEL) {
LabelNode label = (LabelNode) ain;
- JRadioButton btn = new JRadioButton(i + ".");
- labels.put(btn, label);
+ String s = i + ".";
+ labels.put(s, label);
+ model.addElement(s);
if (label.equals(initial)) {
- btn.setSelected(true);
- }
- btn.addActionListener(new RadioListener(btn));
- content.add(btn);
- }
+ selected = labelCount;
}
+ labelCount++;
}
-
- /**
- * Listener for disabling other radio buttons.
- *
- * @author Matt
- */
- private class RadioListener implements ActionListener {
- private final JRadioButton btn;
-
- public RadioListener(JRadioButton btn) {
- this.btn = btn;
}
-
+ combo.setModel(model);
+ combo.setSelectedIndex(selected);
+ combo.addItemListener(new ItemListener() {
@Override
- public void actionPerformed(ActionEvent e) {
- for (JRadioButton comp : labels.keySet()) {
- if (comp != btn && comp.isSelected()) {
- comp.setSelected(false);
- } else if (comp == btn) {
- updater.accept(labels.get(comp));
+ public void itemStateChanged(ItemEvent e) {
+ updater.accept(labels.get(e.getItem()));
list.repaint();
}
- }
- }
+ });
}
}
