From d2658197fb7c8ce08396a12cd1dd018d0a8bdd3a Mon Sep 17 00:00:00 2001 From: Marco Rebhan Date: Tue, 22 Jun 2021 12:13:04 +0200 Subject: Highlight tokens as 'proposed' when they're not editable --- .../main/java/cuchaz/enigma/gui/EditableType.java | 33 ++++++++++++++++++++++ .../enigma/gui/elements/EditorPopupMenu.java | 28 ++++-------------- .../java/cuchaz/enigma/gui/panels/EditorPanel.java | 32 +++++++++++++++------ 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/EditableType.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/EditableType.java index 3dfa35ce..1e8d6cf0 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/EditableType.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/EditableType.java @@ -1,5 +1,9 @@ package cuchaz.enigma.gui; +import javax.annotation.Nullable; + +import cuchaz.enigma.translation.representation.entry.*; + public enum EditableType { CLASS, METHOD, @@ -7,4 +11,33 @@ public enum EditableType { PARAMETER, LOCAL_VARIABLE, JAVADOC, + ; + + @Nullable + public static EditableType fromEntry(Entry entry) { + // TODO get rid of this with Entry rework + EditableType type = null; + + if (entry instanceof ClassEntry) { + type = EditableType.CLASS; + } else if (entry instanceof MethodEntry me) { + if (me.isConstructor()) { + // treat constructors as classes because renaming one renames + // the class + type = EditableType.CLASS; + } else { + type = EditableType.METHOD; + } + } else if (entry instanceof FieldEntry) { + type = EditableType.FIELD; + } else if (entry instanceof LocalVariableEntry lve) { + if (lve.isArgument()) { + type = EditableType.PARAMETER; + } else { + type = EditableType.LOCAL_VARIABLE; + } + } + + return type; + } } diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java index 637f6a48..2ce6ed93 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/EditorPopupMenu.java @@ -12,7 +12,10 @@ import cuchaz.enigma.gui.EditableType; import cuchaz.enigma.gui.Gui; import cuchaz.enigma.gui.GuiController; import cuchaz.enigma.gui.panels.EditorPanel; -import cuchaz.enigma.translation.representation.entry.*; +import cuchaz.enigma.translation.representation.entry.ClassEntry; +import cuchaz.enigma.translation.representation.entry.Entry; +import cuchaz.enigma.translation.representation.entry.FieldEntry; +import cuchaz.enigma.translation.representation.entry.MethodEntry; import cuchaz.enigma.utils.I18n; public class EditorPopupMenu { @@ -149,28 +152,7 @@ public class EditorPopupMenu { boolean isConstructorEntry = referenceEntry instanceof MethodEntry me && me.isConstructor(); boolean isRenamable = ref != null && controller.project.isRenamable(ref); - // TODO get rid of this with Entry rework - EditableType type = null; - - if (referenceEntry instanceof ClassEntry) { - type = EditableType.CLASS; - } else if (referenceEntry instanceof MethodEntry me) { - if (me.isConstructor()) { - // treat constructors as classes because renaming one renames - // the class - type = EditableType.CLASS; - } else { - type = EditableType.METHOD; - } - } else if (referenceEntry instanceof FieldEntry) { - type = EditableType.FIELD; - } else if (referenceEntry instanceof LocalVariableEntry lve) { - if (lve.isArgument()) { - type = EditableType.PARAMETER; - } else { - type = EditableType.LOCAL_VARIABLE; - } - } + EditableType type = EditableType.fromEntry(referenceEntry); this.renameItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type))); this.editJavadocItem.setEnabled(isRenamable && this.gui.isEditable(EditableType.JAVADOC)); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java index f6564f59..2055309c 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java @@ -11,7 +11,6 @@ import javax.annotation.Nullable; import javax.swing.*; import javax.swing.text.BadLocationException; import javax.swing.text.Document; -import javax.swing.text.Highlighter; import javax.swing.text.Highlighter.HighlightPainter; import de.sciss.syntaxpane.DefaultSyntaxKit; @@ -22,6 +21,7 @@ import cuchaz.enigma.classhandle.ClassHandle; import cuchaz.enigma.classhandle.ClassHandleError; import cuchaz.enigma.events.ClassHandleListener; import cuchaz.enigma.gui.BrowserCaret; +import cuchaz.enigma.gui.EditableType; import cuchaz.enigma.gui.Gui; import cuchaz.enigma.gui.GuiController; import cuchaz.enigma.gui.config.LookAndFeel; @@ -461,10 +461,26 @@ public class EditorPanel { this.editor.getHighlighter().removeAllHighlights(); if (this.boxHighlightPainters != null) { + BoxHighlightPainter proposedPainter = this.boxHighlightPainters.get(RenamableTokenType.PROPOSED); + for (RenamableTokenType type : tokens.keySet()) { BoxHighlightPainter painter = this.boxHighlightPainters.get(type); + if (painter != null) { - setHighlightedTokens(tokens.get(type), painter); + for (Token token : tokens.get(type)) { + EntryReference, Entry> reference = this.getReference(token); + BoxHighlightPainter tokenPainter; + + if (reference != null) { + EditableType t = EditableType.fromEntry(reference.entry); + boolean editable = t == null || this.gui.isEditable(t); + tokenPainter = editable ? painter : proposedPainter; + } else { + tokenPainter = painter; + } + + this.addHighlightedToken(token, tokenPainter); + } } } } @@ -473,13 +489,11 @@ public class EditorPanel { this.editor.repaint(); } - private void setHighlightedTokens(Iterable tokens, Highlighter.HighlightPainter painter) { - for (Token token : tokens) { - try { - this.editor.getHighlighter().addHighlight(token.start, token.end, painter); - } catch (BadLocationException ex) { - throw new IllegalArgumentException(ex); - } + private void addHighlightedToken(Token token, HighlightPainter tokenPainter) { + try { + this.editor.getHighlighter().addHighlight(token.start, token.end, tokenPainter); + } catch (BadLocationException ex) { + throw new IllegalArgumentException(ex); } } -- cgit v1.2.3