From 9ea6c21f3b77827e21b54ad776be01b9f48c79ea Mon Sep 17 00:00:00 2001 From: "Katatsumuri.Pan" <79035439+KatatsumuriPan@users.noreply.github.com> Date: Fri, 23 Feb 2024 07:30:15 +0900 Subject: [PATCH] Fix duplicating editor windows bug (#92) * Fix duplicating editor windows * Allow opening other editors for other quests --- .../client/gui2/editors/TextEditorFrame.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/betterquesting/client/gui2/editors/TextEditorFrame.java b/src/main/java/betterquesting/client/gui2/editors/TextEditorFrame.java index 6110af71d..aad634aba 100644 --- a/src/main/java/betterquesting/client/gui2/editors/TextEditorFrame.java +++ b/src/main/java/betterquesting/client/gui2/editors/TextEditorFrame.java @@ -5,12 +5,17 @@ import betterquesting.core.BetterQuesting; import betterquesting.core.ModReference; import betterquesting.network.handlers.NetQuestEdit; +import io.netty.util.collection.IntObjectHashMap; +import io.netty.util.collection.IntObjectMap; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.I18n; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.IntHashMap; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.TextFormatting; +import org.jetbrains.annotations.Nullable; +import scala.collection.immutable.IntMap; import javax.imageio.ImageIO; import javax.swing.*; @@ -23,6 +28,8 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.InputStream; @@ -30,11 +37,22 @@ public class TextEditorFrame extends JFrame { private static final int initialRowCount = 30; private static final int defaultColumns = 60; + private static final IntObjectMap opened = new IntObjectHashMap<>();// questId -> TextEditorFrame + private static BufferedImage logoCache = null; + public static void openTextEditor(int questID, IQuest quest) { - new TextEditorFrame(questID, quest).requestFocus(); - } + if (opened.containsKey(questID)){ + TextEditorFrame frame = opened.get(questID); + frame.toFront(); + frame.requestFocus(); + Toolkit.getDefaultToolkit().beep(); + }else { + TextEditorFrame frame = new TextEditorFrame(questID, quest); + opened.put(questID, frame); + frame.requestFocus(); + } + } - private static BufferedImage logoCache = null; private final int questID; public final IQuest quest; @@ -89,6 +107,13 @@ private TextEditorFrame(int questID, IQuest q) { description.setLineWrap(true); UndoHelper.addUndoHelper(description); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + opened.remove(questID); + } + }); + JPanel footerPanel = add(wholePanel, new JPanel()); footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); JButton cancel = add(footerPanel, new JButton(I18n.format("gui.cancel"))); @@ -175,10 +200,10 @@ private UndoHelper(JTextComponent textComponent) { class UndoAction extends AbstractAction { UndoAction() { super("Undo(U)"); - putValue(MNEMONIC_KEY, new Integer('U')); + putValue(MNEMONIC_KEY, (int)'U'); putValue(SHORT_DESCRIPTION, "Undo"); putValue(LONG_DESCRIPTION, "Undo"); - putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Z', Event.CTRL_MASK)); + putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Z', InputEvent.CTRL_DOWN_MASK)); } public void actionPerformed(ActionEvent e) { if (undoManager.canUndo()) { @@ -190,10 +215,10 @@ public void actionPerformed(ActionEvent e) { class RedoAction extends AbstractAction { RedoAction() { super("Redo(R)"); - putValue(MNEMONIC_KEY, new Integer('R')); + putValue(MNEMONIC_KEY, (int) 'R'); putValue(SHORT_DESCRIPTION, "Redo"); putValue(LONG_DESCRIPTION, "Redo"); - putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Y', Event.CTRL_MASK)); + putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Y', InputEvent.CTRL_DOWN_MASK)); } public void actionPerformed(ActionEvent e) { if (undoManager.canRedo()) {