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

Add "Edit/Copy comment" into Game menu #880

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/main/java/featurecat/lizzie/gui/CommentPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public CommentPane(LizzieMain owner) {
public void mouseClicked(MouseEvent e) {
Lizzie.frame.getFocus();
}

@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
Lizzie.frame.editComment();
}
}
});
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ public void keyPressed(KeyEvent e) {
break;

case VK_C:
if (controlIsPressed(e)) {
if (controlIsPressed(e) && e.isShiftDown()) {
Lizzie.frame.copyCommentToClipboard();
} else if (controlIsPressed(e)) {
Lizzie.frame.copySgf();
} else {
Lizzie.config.toggleCoordinates();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ protected void paintComponent(Graphics g) {
createBufferStrategy(2);
bs = getBufferStrategy();

// avoid IME issue
// https://github.com/featurecat/lizzie/pull/880#issuecomment-800804632
// https://github.com/yzyray/lizzie_adv/commit/e5e8be01b4e072615250e4c4cc8462938b7c0332
mainPanel.enableInputMethods(false);

Input input = new Input();

mainPanel.addMouseListener(input);
Expand Down Expand Up @@ -1186,6 +1191,8 @@ public void onDoubleClicked(int x, int y) {
Lizzie.board.goToMoveNumberBeyondBranch(moveNumber);
}
}
} else if (Lizzie.config.showComment && commentRect.contains(x, y)) {
editComment();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/featurecat/lizzie/gui/LizzieMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ protected void paintComponent(Graphics g) {

setVisible(true);

// avoid IME issue
// https://github.com/featurecat/lizzie/pull/880#issuecomment-800804632
// https://github.com/yzyray/lizzie_adv/commit/e5e8be01b4e072615250e4c4cc8462938b7c0332
enableInputMethods(false);

input = new Input();
// addMouseListener(input);
addKeyListener(input);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/featurecat/lizzie/gui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import java.awt.FontFormatException;
import java.awt.HeadlessException;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.File;
Expand All @@ -23,6 +27,8 @@
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.json.JSONObject;

Expand Down Expand Up @@ -155,6 +161,37 @@ public void addSuggestionAsBranch() {}

public abstract void pasteSgf();

public void editComment() {
String oldComment = Lizzie.board.getHistory().getData().comment;
// https://stackoverflow.com/questions/7765478/how-to-add-text-area-on-joptionpane
// https://stackoverflow.com/a/55678093
JTextArea textArea = new JTextArea(oldComment);
textArea.setColumns(40);
textArea.setRows(20);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setSize(textArea.getPreferredSize().width, textArea.getPreferredSize().height);
int ret =
JOptionPane.showConfirmDialog(
null, new JScrollPane(textArea), "Comment", JOptionPane.OK_CANCEL_OPTION);
if (ret == JOptionPane.OK_OPTION) {
Lizzie.board.getHistory().getData().comment = textArea.getText();
refresh();
}
}

public void copyCommentToClipboard() {
String comment = Lizzie.board.getHistory().getData().comment;
if (comment.isEmpty()) return;
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferableString = new StringSelection(comment);
clipboard.setContents(transferableString, null);
} catch (Exception e) {
e.printStackTrace();
}
}

public void setPlayers(String whitePlayer, String blackPlayer) {
playerTitle = String.format("(%s [W] vs %s [B])", whitePlayer, blackPlayer);
updateTitle();
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/featurecat/lizzie/gui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,29 @@ public void actionPerformed(ActionEvent e) {
}
});
gameMenu.add(pass);

gameMenu.addSeparator();

final JMenuItem editComment = new JMenuItem("Edit comment");
editComment.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Lizzie.frame.editComment();
}
});
gameMenu.add(editComment);

final JMenuItem copyComment = new JMenuItem("Copy comment(Ctrl+Shift+C)");
copyComment.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Lizzie.frame.copyCommentToClipboard();
}
});
gameMenu.add(copyComment);

gameMenu.addSeparator();

final JMenuItem clearBoard = new JMenuItem(resourceBundle.getString("Menu.game.clearBoard"));
Expand Down