From d82f83e6a698f29ee14897ce7666d6f095349dc4 Mon Sep 17 00:00:00 2001 From: Yanis48 Date: Mon, 25 May 2020 16:48:12 +0200 Subject: Add tags to javadoc dialog (#248) * Add tags to javadoc dialog * improvements when you have a text selected--- build.gradle | 2 +- .../cuchaz/enigma/gui/dialog/JavadocDialog.java | 69 ++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 0d744a3..ba2d86c 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'cuchaz' -version = '0.16.0' +version = '0.16.1' def generatedSourcesDir = "$buildDir/generated-src" diff --git a/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java index b2c159d..7e41441 100644 --- a/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java +++ b/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java @@ -16,6 +16,8 @@ import cuchaz.enigma.gui.util.ScaleUtil; import cuchaz.enigma.utils.Utils; import javax.swing.*; +import javax.swing.text.html.HTML; + import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; @@ -72,6 +74,50 @@ public class JavadocDialog { buttonsPanel.add(saveButton); pane.add(buttonsPanel, BorderLayout.SOUTH); + // tags panel + JMenuBar tagsMenu = new JMenuBar(); + + // add javadoc tags + for (JavadocTag tag : JavadocTag.values()) { + JButton tagButton = new JButton(tag.getText()); + tagButton.addActionListener(action -> { + boolean textSelected = text.getSelectedText() != null; + String tagText = tag.isInline() ? "{" + tag.getText() + " }" : tag.getText() + " "; + + if (textSelected) { + if (tag.isInline()) { + tagText = "{" + tag.getText() + " " + text.getSelectedText() + "}"; + } else { + tagText = tag.getText() + " " + text.getSelectedText(); + } + text.replaceSelection(tagText); + } else { + text.insert(tagText, text.getCaretPosition()); + } + + if (tag.isInline()) { + text.setCaretPosition(text.getCaretPosition() - 1); + } + text.grabFocus(); + }); + tagsMenu.add(tagButton); + } + + // add html tags + JComboBox htmlList = new JComboBox(); + htmlList.setPreferredSize(new Dimension()); + for (HTML.Tag htmlTag : HTML.getAllTags()) { + htmlList.addItem(htmlTag.toString()); + } + htmlList.addActionListener(action -> { + String tagText = "<" + htmlList.getSelectedItem().toString() + ">"; + text.insert(tagText, text.getCaretPosition()); + text.grabFocus(); + }); + tagsMenu.add(htmlList); + + pane.add(tagsMenu, BorderLayout.NORTH); + // show the frame frame.setSize(ScaleUtil.getDimension(600, 400)); frame.setLocationRelativeTo(parent); @@ -87,4 +133,27 @@ public class JavadocDialog { public interface Callback { void closeUi(JFrame frame, boolean save); } + + private enum JavadocTag { + CODE(true), + LINK(true), + LINKPLAIN(true), + RETURN(false), + SEE(false), + THROWS(false); + + private boolean inline; + + private JavadocTag(boolean inline) { + this.inline = inline; + } + + public String getText() { + return "@" + this.name().toLowerCase(); + } + + public boolean isInline() { + return this.inline; + } + } } -- cgit v1.2.3