Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicating editor windows bug #92

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package betterquesting.client.gui2.editors;

import betterquesting.TextEditorFrame;
import betterquesting.api.client.gui.misc.INeedsRefresh;
import betterquesting.api.client.gui.misc.IVolatileScreen;
import betterquesting.api.enums.EnumLogic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package betterquesting;
package betterquesting.client.gui2.editors;

import betterquesting.api.properties.NativeProps;
import betterquesting.api.questing.IQuest;
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