From 324af846e90ae38886c2298559510aa935b054ee Mon Sep 17 00:00:00 2001 From: Gegy Date: Sun, 12 May 2019 11:04:33 +0200 Subject: Rename in place functionality (#131) * Rename in place functionality * Don't allow rename if renaming is not allowed for the token * Remove redundant 'R' hotkey --- src/main/java/cuchaz/enigma/gui/Gui.java | 38 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/main/java/cuchaz/enigma/gui/Gui.java') diff --git a/src/main/java/cuchaz/enigma/gui/Gui.java b/src/main/java/cuchaz/enigma/gui/Gui.java index 3e85920..304b779 100644 --- a/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/src/main/java/cuchaz/enigma/gui/Gui.java @@ -79,6 +79,8 @@ public class Gui { private JList tokens; private JTabbedPane tabs; + public JTextField renameTextField; + public void setEditorTheme(Config.LookAndFeel feel) { if (editor != null && (editorFeel == null || editorFeel != feel)) { editor.updateUI(); @@ -570,22 +572,22 @@ public class Gui { public void startRename() { // init the text box - final JTextField text = new JTextField(); + renameTextField = new JTextField(); EntryReference, Entry> translatedReference = controller.getDeobfuscator().deobfuscate(cursorReference); - text.setText(translatedReference.getNameableName()); + renameTextField.setText(translatedReference.getNameableName()); - text.setPreferredSize(new Dimension(360, text.getPreferredSize().height)); - text.addKeyListener(new KeyAdapter() { + renameTextField.setPreferredSize(new Dimension(360, renameTextField.getPreferredSize().height)); + renameTextField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_ENTER: - finishRename(text, true); + finishRename(true); break; case KeyEvent.VK_ESCAPE: - finishRename(text, false); + finishRename(false); break; default: break; @@ -596,28 +598,28 @@ public class Gui { // find the label with the name and replace it with the text box JPanel panel = (JPanel) infoPanel.getComponent(0); panel.remove(panel.getComponentCount() - 1); - panel.add(text); - text.grabFocus(); + panel.add(renameTextField); + renameTextField.grabFocus(); - int offset = text.getText().lastIndexOf('/') + 1; + int offset = renameTextField.getText().lastIndexOf('/') + 1; // If it's a class and isn't in the default package, assume that it's deobfuscated. - if (translatedReference.getNameableEntry() instanceof ClassEntry && text.getText().contains("/") && offset != 0) - text.select(offset, text.getText().length()); + if (translatedReference.getNameableEntry() instanceof ClassEntry && renameTextField.getText().contains("/") && offset != 0) + renameTextField.select(offset, renameTextField.getText().length()); else - text.selectAll(); + renameTextField.selectAll(); redraw(); } - private void finishRename(JTextField text, boolean saveName) { - String newName = text.getText(); + private void finishRename(boolean saveName) { + String newName = renameTextField.getText(); if (saveName && newName != null && !newName.isEmpty()) { try { this.controller.rename(cursorReference, newName, true); } catch (IllegalNameException ex) { - text.setBorder(BorderFactory.createLineBorder(Color.red, 1)); - text.setToolTipText(ex.getReason()); - Utils.showToolTipNow(text); + renameTextField.setBorder(BorderFactory.createLineBorder(Color.red, 1)); + renameTextField.setToolTipText(ex.getReason()); + Utils.showToolTipNow(renameTextField); } return; } @@ -627,6 +629,8 @@ public class Gui { panel.remove(panel.getComponentCount() - 1); panel.add(Utils.unboldLabel(new JLabel(cursorReference.getNameableName(), JLabel.LEFT))); + renameTextField = null; + this.editor.grabFocus(); redraw(); -- cgit v1.2.3