biskuteri-cafe-JKomasto2/PostWindow.java

692 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;
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 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 void
openAuthorProfile()
{
TimelineWindow w = new TimelineWindow(primaire);
w.showAuthorPosts(post.author.numId);
w.showLatestPage();
w.setLocationRelativeTo(this);
w.setVisible(true);
}
public 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 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 void
reply()
{
String ownId = api.getAccountDetails().get("id").value;
Composition c = Composition.reply(this.post, ownId);
ComposeWindow w = primaire.getComposeWindow();
w.setComposition(c);
w.setLocation(getX(), getY() + 100);
w.setVisible(true);
}
public 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.showAttachments(post.attachments);
w.setLocationRelativeTo(null);
w.setVisible(true);
display.setCursor(null);
}
2022-04-28 03:56:25 +02:00
public void
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
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();
}
2022-04-29 19:44:38 +02:00
public void
copyPostId()
{
ClipboardApi.serve(post.id);
2022-04-29 19:44:38 +02:00
}
2022-04-29 19:44:38 +02:00
public void
copyPostLink()
{
ClipboardApi.serve(post.uri);
2022-04-29 19:44:38 +02:00
}
public void
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>
authorNameOr, bodyOr;
private RichTextPane
authorName, body;
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
setEmojiUrls(String[][] n) { emojiUrls = n; }
public void
setHtml(String n)
{
RichTextPane.Builder b = new RichTextPane.Builder();
Tree<String> nodes = RudimentaryHTMLParser.depthlessRead(n);
for (Tree<String> node: nodes)
{
if (node.key.equals("tag"))
{
String tagName = node.get(0).key;
if (tagName.equals("br"))
b = b.spacer("\n");
if (tagName.equals("/p"))
b = b.spacer("\n").spacer("\n");
if (tagName.equals("a"))
b = b.link(node.get("href").value, null).spacer(" ");
}
if (node.key.equals("text"))
{
BreakIterator it = BreakIterator.getWordInstance(Locale.ROOT);
String text = node.value;
it.setText(text);
int start = it.first(), end = it.next();
while (end != BreakIterator.DONE)
{
String word = text.substring(start, end);
char c = word.isEmpty() ? ' ' : word.charAt(0);
boolean w = Character.isWhitespace(c);
b = w ? b.spacer(word) : b.text(word);
start = end;
end = it.next();
}
}
if (node.key.equals("emoji"))
{
String shortcode = node.value;
String url = null;
for (String[] entry: emojiUrls)
if (entry[0].equals(shortcode)) url = entry[1];
try {
ImageIcon image = new ImageIcon(new URL(url));
b = b.image(image, node.value);
}
catch (MalformedURLException eMu) {
b = b.text(":" + shortcode + ":");
}
}
}
bodyOr = b.finish();
}
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() { 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();
int w2 = body.getWidth();
FontMetrics fm1 = getFontMetrics(authorName.getFont());
FontMetrics fm2 = getFontMetrics(body.getFont());
List<RichTextPane.Segment> lay1, lay2;
lay1 = RichTextPane.layout(authorNameOr, fm1, w1);
lay2 = RichTextPane.layout(bodyOr, fm2, w2);
authorName.setText(lay1);
body.setText(lay2);
int maxY = 0; for (RichTextPane.Segment s: lay2)
{
if (s.y > maxY) maxY = s.y;
}
body.setPreferredSize(new Dimension(1, maxY + 10));
}
// ---%-@-%---
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);
body = new RichTextPane();
body.setFont(f3);
JScrollPane scroll = new JScrollPane(
body,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
JScrollBar vsb = scroll.getVerticalScrollBar();
vsb.setPreferredSize(new Dimension(0, 0));
vsb.setUnitIncrement(16);
scroll.setBorder(null);
scroll.setFocusable(true);
JPanel centre = new JPanel();
centre.setOpaque(false);
centre.setLayout(new BorderLayout(0, 8));
centre.add(top, BorderLayout.NORTH);
centre.add(scroll);
setLayout(new BorderLayout(8, 0));
add(left, BorderLayout.WEST);
add(centre);
setBorder(b);
}
}