Skip to content

Commit

Permalink
Fix duplicating editor windows bug (#92)
Browse files Browse the repository at this point in the history
* Fix duplicating editor windows
* Allow opening other editors for other quests
  • Loading branch information
KatatsumuriPan authored Feb 22, 2024
1 parent 7e60499 commit 9ea6c21
Showing 1 changed file with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -23,18 +28,31 @@
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;

public class TextEditorFrame extends JFrame {
private static final int initialRowCount = 30;
private static final int defaultColumns = 60;
private static final IntObjectMap<TextEditorFrame> 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;
Expand Down Expand Up @@ -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")));
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand Down

0 comments on commit 9ea6c21

Please sign in to comment.