biskuteri-cafe-JKomasto2/PostWindow.java

687 lines
17 KiB
Java
Raw Normal View History

2021-07-16 00:37:03 +02:00
import javax.swing.JFrame;
import javax.swing.JPanel;
2021-07-16 00:37:03 +02:00
import javax.swing.JMenuBar;
2022-04-28 03:56:25 +02:00
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Shape;
2021-07-16 00:37:03 +02:00
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import cafe.biskuteri.hinoki.Tree;
import java.text.BreakIterator;
import java.util.Locale;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
2022-05-16 18:52:00 +02:00
import java.util.Map;
import java.util.HashMap;
2021-07-16 00:37:03 +02:00
class
2022-04-29 19:44:38 +02:00
PostWindow extends JFrame {
2021-07-16 00:37:03 +02:00
private JKomasto
primaire;
private MastodonApi
api;
private Post
post,
wrapperPost;
2021-07-16 11:46:17 +02:00
// - -%- -
2021-07-16 00:37:03 +02:00
private PostComponent
2022-04-29 19:44:38 +02:00
display;
2021-07-16 00:37:03 +02:00
// - -%- -
private static final DateTimeFormatter
DATE_FORMAT = DateTimeFormatter.ofPattern("d LLLL ''uu"),
TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
// ---%-@-%---
2021-07-17 13:17:14 +02:00
public synchronized void
use(Post post)
{
assert post != null;
if (post.boostedPost != null)
{
wrapperPost = post;
this.post = post.boostedPost;
post = post.boostedPost;
}
else
{
wrapperPost = null;
this.post = post;
}
display.setAuthorName(post.author.name);
display.setAuthorId(post.author.id);
String oid = api.getAccountDetails().get("id").value;
String aid = post.author.numId;
display.setDeleteEnabled(aid.equals(oid));
post.author.resolveAvatar();
display.setAuthorAvatar(post.author.avatar);
display.setDate(post.date);
display.setTime(post.time);
display.setEmojiUrls(post.emojiUrls);
display.setHtml(post.text);
display.setFavourited(post.favourited);
display.setBoosted(post.boosted);
if (post.attachments.length > 0)
{
post.attachments[0].resolveImage();
display.setMediaPreview(post.attachments[0].image);
}
else display.setMediaPreview(null);
2022-04-28 03:56:25 +02:00
post.resolveApproximateText();
this.setTitle(post.approximateText);
2022-04-29 19:44:38 +02:00
display.resetFocus();
repaint();
2021-07-17 13:17:14 +02:00
}
public void
readEntity(Tree<String> post)
{
use(new Post(post));
}
public synchronized void
openAuthorProfile()
{
ProfileWindow w = new ProfileWindow(primaire);
w.use(post.author);
w.setLocationRelativeTo(this);
w.setVisible(true);
}
public synchronized void
favourite(boolean favourited)
{
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
2022-04-29 19:44:38 +02:00
display.setFavouriteBoostEnabled(false);
display.paintImmediately(display.getBounds());
RequestListener handler = new RequestListener() {
public void
connectionFailed(IOException eIo)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Tried to favourite post, failed.."
+ "\n" + eIo.getClass() + ": " + eIo.getMessage()
);
}
public void
requestFailed(int httpCode, Tree<String> json)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Tried to favourite post, failed.."
+ "\n" + json.get("error").value
+ "\n(HTTP error code: " + httpCode + ")"
);
}
public void
requestSucceeded(Tree<String> json)
{
PostWindow.this.post.favourited = favourited;
}
};
api.setPostFavourited(post.id, favourited, handler);
2022-04-29 19:44:38 +02:00
display.setCursor(null);
display.setFavouriteBoostEnabled(true);
display.repaint();
}
public synchronized void
boost(boolean boosted)
{
2022-04-29 19:44:38 +02:00
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
display.setFavouriteBoostEnabled(false);
display.paintImmediately(display.getBounds());
RequestListener handler = new RequestListener() {
public void
connectionFailed(IOException eIo)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Tried to boost post, failed.."
+ "\n" + eIo.getClass() + ": " + eIo.getMessage()
);
}
public void
requestFailed(int httpCode, Tree<String> json)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Tried to boost post, failed.."
+ "\n" + json.get("error").value
+ "\n(HTTP error code: " + httpCode + ")"
);
}
public void
requestSucceeded(Tree<String> json)
{
PostWindow.this.post.boosted = boosted;
}
};
api.setPostBoosted(post.id, boosted, handler);
2022-04-29 19:44:38 +02:00
display.setCursor(null);
display.setFavouriteBoostEnabled(true);
display.repaint();
}
public synchronized void
reply()
{
String ownId = api.getAccountDetails().get("id").value;
Composition c = Composition.reply(this.post, ownId);
ComposeWindow w = primaire.getComposeWindow();
w.setComposition(c);
2022-05-19 09:24:03 +02:00
if (!w.isVisible())
{
w.setLocation(getX(), getY() + 100);
w.setVisible(true);
}
}
public synchronized void
openMedia()
{
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
for (Attachment a: post.attachments) a.resolveImage();
ImageWindow w = new ImageWindow();
w.setTitle("Media - " + this.getTitle());
w.setIconImage(this.getIconImage());
w.showAttachments(post.attachments);
w.setLocationRelativeTo(null);
w.setVisible(true);
display.setCursor(null);
}
public synchronized void
2022-04-28 03:56:25 +02:00
deletePost(boolean redraft)
{
2022-04-29 19:44:38 +02:00
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
display.setDeleteEnabled(false);
display.paintImmediately(display.getBounds());
2022-04-28 03:56:25 +02:00
final String S1 =
"Are you sure you'd like to delete this post?\n";
final String S2 =
"Are you sure you'd like to delete this post?\n"
+ "You are redrafting, so a composition window\n"
+ "should open with its contents filled.";
JOptionPane dialog = new JOptionPane();
dialog.setMessageType(JOptionPane.QUESTION_MESSAGE);
dialog.setMessage(redraft ? S2 : S1);
dialog.setOptions(new String[] { "No", "Yes" });
String title = "Confirm delete";
dialog.createDialog(this, title).setVisible(true);
if (!dialog.getValue().equals("Yes"))
{
display.setCursor(null);
display.setDeleteEnabled(true);
display.paintImmediately(display.getBounds());
return;
}
api.deletePost(post.id, new RequestListener() {
2022-04-28 03:56:25 +02:00
public void
connectionFailed(IOException eIo)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Failed to delete post.."
+ "\n" + eIo.getMessage()
);
}
public void
requestFailed(int httpCode, Tree<String> json)
{
JOptionPane.showMessageDialog(
PostWindow.this,
"Failed to delete post.."
+ "\n" + json.get("error").value
+ "\n(HTTP code: " + httpCode + ")"
);
}
public void
requestSucceeded(Tree<String> json)
{
setVisible(false);
if (redraft)
{
Composition c = Composition.recover(json);
ComposeWindow w = new ComposeWindow(primaire);
w.setComposition(c);
w.setLocation(getX(), getY() + 100);
w.setVisible(true);
}
2022-04-28 03:56:25 +02:00
}
});
2022-04-29 19:44:38 +02:00
display.setCursor(null);
display.setDeleteEnabled(true);
display.paintImmediately(display.getBounds());
2022-04-28 03:56:25 +02:00
if (!isVisible()) dispose();
}
public synchronized void
2022-04-29 19:44:38 +02:00
copyPostId()
{
ClipboardApi.serve(post.id);
2022-04-29 19:44:38 +02:00
}
public synchronized void
2022-04-29 19:44:38 +02:00
copyPostLink()
{
ClipboardApi.serve(post.uri);
2022-04-29 19:44:38 +02:00
}
public synchronized void
2022-04-29 19:44:38 +02:00
openReplies()
{
RepliesWindow w = new RepliesWindow(primaire, this);
w.showFor(post.id);
2022-04-29 19:44:38 +02:00
w.setLocation(getX(), getY() + 100);
w.setVisible(true);
}
2021-07-17 11:46:27 +02:00
2021-07-16 00:37:03 +02:00
// ---%-@-%---
PostWindow(JKomasto primaire)
2021-07-16 00:37:03 +02:00
{
this.primaire = primaire;
this.api = primaire.getMastodonApi();
2021-07-16 11:46:17 +02:00
getContentPane().setPreferredSize(new Dimension(360, 270));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2021-07-16 00:37:03 +02:00
setLocationByPlatform(true);
2022-04-29 19:44:38 +02:00
display = new PostComponent(this);
2022-04-29 19:44:38 +02:00
setContentPane(display);
setIconImage(primaire.getProgramIcon());
}
2021-07-16 00:37:03 +02:00
}
class
PostComponent extends JPanel
implements ActionListener {
private PostWindow
primaire;
2021-07-31 13:28:46 +02:00
// - -%- -
private List<RichTextPane.Segment>
2022-05-19 09:24:03 +02:00
authorNameOr;
private RichTextPane
2022-05-19 09:24:03 +02:00
authorName;
private RichTextPane3
body;
private JScrollPane
bodyScrollPane;
private JLabel
authorId, time, date;
private String[][]
emojiUrls;
private TwoToggleButton
2021-07-31 13:28:46 +02:00
favouriteBoost,
replyMisc,
nextPrev;
private RoundButton
profile,
2021-07-31 13:28:46 +02:00
media;
2022-04-28 03:56:25 +02:00
private JPopupMenu
miscMenu;
private JMenuItem
2022-04-29 19:44:38 +02:00
openReplies,
copyPostId,
copyPostLink,
2022-04-28 03:56:25 +02:00
deletePost,
redraftPost;
// ---%-@-%---
public void
setAuthorName(String n)
{
authorNameOr = new RichTextPane.Builder().text(n).finish();
}
public void
setAuthorId(String n) { authorId.setText(n); }
public void
setAuthorAvatar(Image n) { profile.setImage(n); }
public void
setDate(String n) { date.setText(n); }
public void
setTime(String n) { time.setText(n); }
public void
2022-05-19 09:24:03 +02:00
setEmojiUrls(String[][] n)
{
emojiUrls = n;
Map<String, Image> emojis = new HashMap<>();
for (String[] entry: n)
{
emojis.put(entry[0], ImageApi.remote(entry[1]));
}
body.setEmojis(emojis);
}
public void
setHtml(String n)
{
2022-05-19 09:24:03 +02:00
body.setText(BasicHTMLParser.parse(n));
}
public void
setFavourited(boolean a)
{
favouriteBoost.removeActionListener(this);
favouriteBoost.setPrimaryToggled(a);
favouriteBoost.addActionListener(this);
}
public void
setBoosted(boolean a)
{
favouriteBoost.removeActionListener(this);
favouriteBoost.setSecondaryToggled(a);
favouriteBoost.addActionListener(this);
}
public void
setFavouriteBoostEnabled(boolean a)
{
favouriteBoost.setEnabled(a);
}
2022-04-28 03:56:25 +02:00
public void
setDeleteEnabled(boolean a)
{
deletePost.setEnabled(a);
redraftPost.setEnabled(a);
}
public void
setMediaPreview(Image n) { media.setImage(n); }
public void
resetFocus()
{
bodyScrollPane.getVerticalScrollBar().setValue(0);
media.requestFocusInWindow();
}
// - -%- -
public void
actionPerformed(ActionEvent eA)
{
Component src = (Component)eA.getSource();
String command = eA.getActionCommand();
if (src == profile)
{
primaire.openAuthorProfile();
return;
}
if (src == favouriteBoost)
{
if (command.equals("favouriteOn"))
primaire.favourite(true);
if (command.equals("favouriteOff"))
primaire.favourite(false);
if (command.equals("boostOn"))
primaire.boost(true);
if (command.equals("boostOff"))
primaire.boost(false);
return;
}
if (src == replyMisc)
{
2022-04-28 03:56:25 +02:00
if (command.startsWith("reply"))
primaire.reply();
if (command.startsWith("misc"))
{
int rx = replyMisc.getWidth() / 2;
int ry = replyMisc.getHeight() - miscMenu.getHeight();
miscMenu.show(replyMisc, rx, ry);
}
return;
}
if (src == nextPrev)
{
if (command.equals("next"))
{
}
else
{
}
return;
}
if (src == media)
{
primaire.openMedia();
return;
2022-04-28 03:56:25 +02:00
}
2022-04-29 19:44:38 +02:00
if (src == openReplies) primaire.openReplies();
if (src == copyPostId) primaire.copyPostId();
if (src == copyPostLink) primaire.copyPostLink();
if (src == deletePost) primaire.deletePost(false);
if (src == redraftPost) primaire.deletePost(true);
2022-04-28 03:56:25 +02:00
}
protected void
paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
int w1 = authorName.getWidth();
FontMetrics fm1 = getFontMetrics(authorName.getFont());
2022-05-19 09:24:03 +02:00
List<RichTextPane.Segment> lay1;
lay1 = RichTextPane.layout(authorNameOr, fm1, w1);
authorName.setText(lay1);
((java.awt.Graphics2D)g).setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON
);
}
// ---%-@-%---
PostComponent(PostWindow primaire)
{
this.primaire = primaire;
emojiUrls = new String[0][];
Border b = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Font f1 = new Font("MotoyaLMaru", Font.PLAIN, 18);
Font f2 = new Font("MotoyaLMaru", Font.PLAIN, 14);
Font f3 = new Font("MotoyaLMaru", Font.PLAIN, 18);
2021-07-16 11:46:17 +02:00
profile = new RoundButton();
favouriteBoost = new TwoToggleButton("favourite", "boost");
replyMisc = new TwoToggleButton("reply", "misc");
nextPrev = new TwoToggleButton("next", "prev");
media = new RoundButton();
profile.addActionListener(this);
favouriteBoost.addActionListener(this);
replyMisc.addActionListener(this);
nextPrev.addActionListener(this);
media.addActionListener(this);
2021-07-31 13:28:46 +02:00
2022-04-29 19:44:38 +02:00
openReplies = new JMenuItem("Browse thread");
copyPostId = new JMenuItem("Copy post ID");
copyPostLink = new JMenuItem("Copy post link");
2022-04-28 03:56:25 +02:00
deletePost = new JMenuItem("Delete post");
redraftPost = new JMenuItem("Delete and redraft post");
2022-04-29 19:44:38 +02:00
openReplies.addActionListener(this);
copyPostId.addActionListener(this);
copyPostLink.addActionListener(this);
2022-04-28 03:56:25 +02:00
deletePost.addActionListener(this);
redraftPost.addActionListener(this);
miscMenu = new JPopupMenu();
2022-04-29 19:44:38 +02:00
miscMenu.add(openReplies);
miscMenu.add(new JSeparator());
miscMenu.add(copyPostId);
miscMenu.add(copyPostLink);
miscMenu.add(new JSeparator());
2022-04-28 03:56:25 +02:00
miscMenu.add(deletePost);
2022-04-29 19:44:38 +02:00
miscMenu.add(new JSeparator());
2022-04-28 03:56:25 +02:00
miscMenu.add(redraftPost);
Box buttons = Box.createVerticalBox();
buttons.setOpaque(false);
buttons.add(profile);
buttons.add(Box.createVerticalStrut(8));
buttons.add(favouriteBoost);
buttons.add(Box.createVerticalStrut(8));
buttons.add(replyMisc);
buttons.add(Box.createVerticalStrut(8));
buttons.add(nextPrev);
buttons.add(Box.createVerticalStrut(8));
buttons.add(media);
buttons.setMaximumSize(buttons.getPreferredSize());
Box left = Box.createVerticalBox();
left.setOpaque(false);
left.add(buttons);
authorId = new JLabel();
authorName = new RichTextPane();
time = new JLabel();
date = new JLabel();
authorId.setFont(f2);
date.setFont(f2);
authorName.setFont(f1);
time.setFont(f1);
JPanel top1 = new JPanel();
top1.setLayout(new BorderLayout(8, 0));
top1.add(authorId);
top1.add(date, BorderLayout.EAST);
JPanel top2 = new JPanel();
top2.setLayout(new BorderLayout(8, 0));
top2.add(authorName);
top2.add(time, BorderLayout.EAST);
Box top = Box.createVerticalBox();
top.add(top1);
top.add(Box.createVerticalStrut(2));
top.add(top2);
2022-05-19 09:24:03 +02:00
body = new RichTextPane3();
body.setFont(f3);
bodyScrollPane = new JScrollPane(
body,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
JScrollBar vsb = bodyScrollPane.getVerticalScrollBar();
vsb.setPreferredSize(new Dimension(0, 0));
vsb.setUnitIncrement(16);
bodyScrollPane.setBorder(null);
bodyScrollPane.setFocusable(true);
JPanel centre = new JPanel();
centre.setOpaque(false);
centre.setLayout(new BorderLayout(0, 8));
centre.add(top, BorderLayout.NORTH);
centre.add(bodyScrollPane);
setLayout(new BorderLayout(8, 0));
add(left, BorderLayout.WEST);
add(centre);
setBorder(b);
}
}