diff options
| author | 2021-06-22 12:13:04 +0200 | |
|---|---|---|
| committer | 2021-06-22 12:13:04 +0200 | |
| commit | d2658197fb7c8ce08396a12cd1dd018d0a8bdd3a (patch) | |
| tree | 53ce13ebf38de5091e1ce8856b3414ff4006f9b8 | |
| parent | Bump version (diff) | |
| download | enigma-d2658197fb7c8ce08396a12cd1dd018d0a8bdd3a.tar.gz enigma-d2658197fb7c8ce08396a12cd1dd018d0a8bdd3a.tar.xz enigma-d2658197fb7c8ce08396a12cd1dd018d0a8bdd3a.zip | |
Highlight tokens as 'proposed' when they're not editable
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 @@ | |||
| 1 | package cuchaz.enigma.gui; | 1 | package cuchaz.enigma.gui; |
| 2 | 2 | ||
| 3 | import javax.annotation.Nullable; | ||
| 4 | |||
| 5 | import cuchaz.enigma.translation.representation.entry.*; | ||
| 6 | |||
| 3 | public enum EditableType { | 7 | public enum EditableType { |
| 4 | CLASS, | 8 | CLASS, |
| 5 | METHOD, | 9 | METHOD, |
| @@ -7,4 +11,33 @@ public enum EditableType { | |||
| 7 | PARAMETER, | 11 | PARAMETER, |
| 8 | LOCAL_VARIABLE, | 12 | LOCAL_VARIABLE, |
| 9 | JAVADOC, | 13 | JAVADOC, |
| 14 | ; | ||
| 15 | |||
| 16 | @Nullable | ||
| 17 | public static EditableType fromEntry(Entry<?> entry) { | ||
| 18 | // TODO get rid of this with Entry rework | ||
| 19 | EditableType type = null; | ||
| 20 | |||
| 21 | if (entry instanceof ClassEntry) { | ||
| 22 | type = EditableType.CLASS; | ||
| 23 | } else if (entry instanceof MethodEntry me) { | ||
| 24 | if (me.isConstructor()) { | ||
| 25 | // treat constructors as classes because renaming one renames | ||
| 26 | // the class | ||
| 27 | type = EditableType.CLASS; | ||
| 28 | } else { | ||
| 29 | type = EditableType.METHOD; | ||
| 30 | } | ||
| 31 | } else if (entry instanceof FieldEntry) { | ||
| 32 | type = EditableType.FIELD; | ||
| 33 | } else if (entry instanceof LocalVariableEntry lve) { | ||
| 34 | if (lve.isArgument()) { | ||
| 35 | type = EditableType.PARAMETER; | ||
| 36 | } else { | ||
| 37 | type = EditableType.LOCAL_VARIABLE; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return type; | ||
| 42 | } | ||
| 10 | } | 43 | } |
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; | |||
| 12 | import cuchaz.enigma.gui.Gui; | 12 | import cuchaz.enigma.gui.Gui; |
| 13 | import cuchaz.enigma.gui.GuiController; | 13 | import cuchaz.enigma.gui.GuiController; |
| 14 | import cuchaz.enigma.gui.panels.EditorPanel; | 14 | import cuchaz.enigma.gui.panels.EditorPanel; |
| 15 | import cuchaz.enigma.translation.representation.entry.*; | 15 | import cuchaz.enigma.translation.representation.entry.ClassEntry; |
| 16 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 17 | import cuchaz.enigma.translation.representation.entry.FieldEntry; | ||
| 18 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 16 | import cuchaz.enigma.utils.I18n; | 19 | import cuchaz.enigma.utils.I18n; |
| 17 | 20 | ||
| 18 | public class EditorPopupMenu { | 21 | public class EditorPopupMenu { |
| @@ -149,28 +152,7 @@ public class EditorPopupMenu { | |||
| 149 | boolean isConstructorEntry = referenceEntry instanceof MethodEntry me && me.isConstructor(); | 152 | boolean isConstructorEntry = referenceEntry instanceof MethodEntry me && me.isConstructor(); |
| 150 | boolean isRenamable = ref != null && controller.project.isRenamable(ref); | 153 | boolean isRenamable = ref != null && controller.project.isRenamable(ref); |
| 151 | 154 | ||
| 152 | // TODO get rid of this with Entry rework | 155 | EditableType type = EditableType.fromEntry(referenceEntry); |
| 153 | EditableType type = null; | ||
| 154 | |||
| 155 | if (referenceEntry instanceof ClassEntry) { | ||
| 156 | type = EditableType.CLASS; | ||
| 157 | } else if (referenceEntry instanceof MethodEntry me) { | ||
| 158 | if (me.isConstructor()) { | ||
| 159 | // treat constructors as classes because renaming one renames | ||
| 160 | // the class | ||
| 161 | type = EditableType.CLASS; | ||
| 162 | } else { | ||
| 163 | type = EditableType.METHOD; | ||
| 164 | } | ||
| 165 | } else if (referenceEntry instanceof FieldEntry) { | ||
| 166 | type = EditableType.FIELD; | ||
| 167 | } else if (referenceEntry instanceof LocalVariableEntry lve) { | ||
| 168 | if (lve.isArgument()) { | ||
| 169 | type = EditableType.PARAMETER; | ||
| 170 | } else { | ||
| 171 | type = EditableType.LOCAL_VARIABLE; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | 156 | ||
| 175 | this.renameItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type))); | 157 | this.renameItem.setEnabled(isRenamable && (type != null && this.gui.isEditable(type))); |
| 176 | this.editJavadocItem.setEnabled(isRenamable && this.gui.isEditable(EditableType.JAVADOC)); | 158 | 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; | |||
| 11 | import javax.swing.*; | 11 | import javax.swing.*; |
| 12 | import javax.swing.text.BadLocationException; | 12 | import javax.swing.text.BadLocationException; |
| 13 | import javax.swing.text.Document; | 13 | import javax.swing.text.Document; |
| 14 | import javax.swing.text.Highlighter; | ||
| 15 | import javax.swing.text.Highlighter.HighlightPainter; | 14 | import javax.swing.text.Highlighter.HighlightPainter; |
| 16 | 15 | ||
| 17 | import de.sciss.syntaxpane.DefaultSyntaxKit; | 16 | import de.sciss.syntaxpane.DefaultSyntaxKit; |
| @@ -22,6 +21,7 @@ import cuchaz.enigma.classhandle.ClassHandle; | |||
| 22 | import cuchaz.enigma.classhandle.ClassHandleError; | 21 | import cuchaz.enigma.classhandle.ClassHandleError; |
| 23 | import cuchaz.enigma.events.ClassHandleListener; | 22 | import cuchaz.enigma.events.ClassHandleListener; |
| 24 | import cuchaz.enigma.gui.BrowserCaret; | 23 | import cuchaz.enigma.gui.BrowserCaret; |
| 24 | import cuchaz.enigma.gui.EditableType; | ||
| 25 | import cuchaz.enigma.gui.Gui; | 25 | import cuchaz.enigma.gui.Gui; |
| 26 | import cuchaz.enigma.gui.GuiController; | 26 | import cuchaz.enigma.gui.GuiController; |
| 27 | import cuchaz.enigma.gui.config.LookAndFeel; | 27 | import cuchaz.enigma.gui.config.LookAndFeel; |
| @@ -461,10 +461,26 @@ public class EditorPanel { | |||
| 461 | this.editor.getHighlighter().removeAllHighlights(); | 461 | this.editor.getHighlighter().removeAllHighlights(); |
| 462 | 462 | ||
| 463 | if (this.boxHighlightPainters != null) { | 463 | if (this.boxHighlightPainters != null) { |
| 464 | BoxHighlightPainter proposedPainter = this.boxHighlightPainters.get(RenamableTokenType.PROPOSED); | ||
| 465 | |||
| 464 | for (RenamableTokenType type : tokens.keySet()) { | 466 | for (RenamableTokenType type : tokens.keySet()) { |
| 465 | BoxHighlightPainter painter = this.boxHighlightPainters.get(type); | 467 | BoxHighlightPainter painter = this.boxHighlightPainters.get(type); |
| 468 | |||
| 466 | if (painter != null) { | 469 | if (painter != null) { |
| 467 | setHighlightedTokens(tokens.get(type), painter); | 470 | for (Token token : tokens.get(type)) { |
| 471 | EntryReference<Entry<?>, Entry<?>> reference = this.getReference(token); | ||
| 472 | BoxHighlightPainter tokenPainter; | ||
| 473 | |||
| 474 | if (reference != null) { | ||
| 475 | EditableType t = EditableType.fromEntry(reference.entry); | ||
| 476 | boolean editable = t == null || this.gui.isEditable(t); | ||
| 477 | tokenPainter = editable ? painter : proposedPainter; | ||
| 478 | } else { | ||
| 479 | tokenPainter = painter; | ||
| 480 | } | ||
| 481 | |||
| 482 | this.addHighlightedToken(token, tokenPainter); | ||
| 483 | } | ||
| 468 | } | 484 | } |
| 469 | } | 485 | } |
| 470 | } | 486 | } |
| @@ -473,13 +489,11 @@ public class EditorPanel { | |||
| 473 | this.editor.repaint(); | 489 | this.editor.repaint(); |
| 474 | } | 490 | } |
| 475 | 491 | ||
| 476 | private void setHighlightedTokens(Iterable<Token> tokens, Highlighter.HighlightPainter painter) { | 492 | private void addHighlightedToken(Token token, HighlightPainter tokenPainter) { |
| 477 | for (Token token : tokens) { | 493 | try { |
| 478 | try { | 494 | this.editor.getHighlighter().addHighlight(token.start, token.end, tokenPainter); |
| 479 | this.editor.getHighlighter().addHighlight(token.start, token.end, painter); | 495 | } catch (BadLocationException ex) { |
| 480 | } catch (BadLocationException ex) { | 496 | throw new IllegalArgumentException(ex); |
| 481 | throw new IllegalArgumentException(ex); | ||
| 482 | } | ||
| 483 | } | 497 | } |
| 484 | } | 498 | } |
| 485 | 499 | ||