biskuteri-cafe-JKomasto2/PostWindow.java

825 lines
21 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;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
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
PostWindow extends JFrame
implements ActionListener {
2021-07-16 00:37:03 +02:00
private JKomasto
primaire;
private MastodonApi
api;
private Tree<String>
post;
2021-07-16 11:46:17 +02:00
// - -%- -
2021-07-16 00:37:03 +02:00
private PostComponent
2021-07-16 11:46:17 +02:00
postDisplay;
2021-07-16 00:37:03 +02:00
private RepliesComponent
2021-07-16 11:46:17 +02:00
repliesDisplay;
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
displayEntity(Tree<String> post)
{
this.post = post;
Tree<String> boosted = post.get("reblog");
if (boosted.size() > 0) post = boosted;
Tree<String> author = post.get("account");
Tree<String> emojis = post.get("emojis");
Tree<String> media = post.get("media_attachments");
String an = author.get("display_name").value;
if (an.isEmpty()) an = author.get("username").value;
postDisplay.setAuthorName(an);
postDisplay.setAuthorId(author.get("acct").value);
String avurl = author.get("avatar").value;
postDisplay.setAuthorAvatar(ImageApi.remote(avurl));
String sdate = post.get("created_at").value;
ZonedDateTime date = ZonedDateTime.parse(sdate);
date = date.withZoneSameInstant(ZoneId.systemDefault());
postDisplay.setDate(DATE_FORMAT.format(date));
postDisplay.setTime(TIME_FORMAT.format(date));
String[][] emojiUrls = new String[emojis.size()][];
for (int o = 0; o < emojiUrls.length; ++o) {
Tree<String> emoji = emojis.get(o);
emojiUrls[o] = new String[2];
emojiUrls[o][0] = emoji.get("shortcode").value;
emojiUrls[o][1] = emoji.get("url").value;
}
postDisplay.setEmojiUrls(emojiUrls);
postDisplay.setHtml(post.get("content").value);
boolean f = post.get("favourited").value.equals("true");
boolean b = post.get("reblogged").value.equals("true");
postDisplay.setFavourited(f);
postDisplay.setBoosted(b);
2022-04-27 10:17:02 +02:00
if (media.size() > 0)
{
Tree<String> first = media.get(0);
String u1 = first.get("remote_url").value;
String u2 = first.get("text_url").value;
String u3 = first.get("url").value;
String purl = u1 != null ? u1 : u2 != null ? u2 : u3;
postDisplay.setMediaPreview(ImageApi.remote(purl));
}
else postDisplay.setMediaPreview(null);
2021-07-29 11:26:16 +02:00
postDisplay.resetFocus();
repaint();
2021-07-17 13:17:14 +02:00
}
public void
openAuthorProfile()
{
Tree<String> post = this.post;
Tree<String> boosted = post.get("reblog");
if (boosted.size() > 0) post = boosted;
TimelineWindow w = new TimelineWindow(primaire);
w.showAuthorPosts(post.get("account").get("id").value);
w.showLatestPage();
w.setLocationRelativeTo(this);
w.setVisible(true);
}
public void
favourite(boolean favourited)
{
Tree<String> post = this.post;
Tree<String> boosted = post.get("reblog");
if (boosted.size() > 0) post = boosted;
2022-04-27 10:17:02 +02:00
postDisplay.setCursor(new Cursor(Cursor.WAIT_CURSOR));
postDisplay.setFavouriteBoostEnabled(false);
postDisplay.paintImmediately(postDisplay.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)
{
String n = Boolean.toString(favourited);
PostWindow.this.post.get("favourited").value = n;
}
};
String postId = post.get("id").value;
api.setPostFavourited(postId, favourited, handler);
postDisplay.setCursor(null);
postDisplay.setFavouriteBoostEnabled(true);
postDisplay.repaint();
}
public void
boost(boolean boosted)
{
Tree<String> post = this.post;
Tree<String> boosted2 = post.get("reblog");
if (boosted2.size() > 0) post = boosted2;
2022-04-27 10:17:02 +02:00
postDisplay.setCursor(new Cursor(Cursor.WAIT_CURSOR));
postDisplay.setFavouriteBoostEnabled(false);
postDisplay.paintImmediately(postDisplay.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)
{
String n = Boolean.toString(boosted);
PostWindow.this.post.get("reblogged").value = n;
}
};
String postId = post.get("id").value;
api.setPostBoosted(postId, boosted, handler);
postDisplay.setCursor(null);
postDisplay.setFavouriteBoostEnabled(true);
postDisplay.repaint();
}
public void
reply()
{
Tree<String> post = this.post;
Tree<String> boosted = post.get("reblog");
if (boosted.size() > 0) post = boosted;
String authorId = post.get("account").get("acct").value;
String postId = post.get("id").value;
String cw = post.get("spoiler_text").value;
2022-04-23 16:28:29 +02:00
String id1 = post.get("account").get("id").value;
String id2 = api.getAccountDetails().get("id").value;
2022-04-27 10:17:02 +02:00
String vs = post.get("visibility").value;
PostVisibility v = null;
if (vs.equals("public")) v = PostVisibility.PUBLIC;
if (vs.equals("unlisted")) v = PostVisibility.UNLISTED;
if (vs.equals("private")) v = PostVisibility.FOLLOWERS;
if (vs.equals("direct")) v = PostVisibility.MENTIONED;
Composition c = new Composition();
c.contentWarning = cw;
2022-04-23 16:28:29 +02:00
c.text = id1.equals(id2) ? "" : "@" + authorId + " ";
c.visibility = v;
c.replyToPostId = postId;
ComposeWindow w = primaire.getComposeWindow();
w.setLocation(getX(), getY() + 100);
w.setVisible(true);
w.setComposition(c);
}
public void
openMedia()
{
2022-04-27 10:17:02 +02:00
Tree<String> post = this.post;
Tree<String> boosted = post.get("reblog");
if (boosted.size() > 0) post = boosted;
Tree<String> media = post.get("media_attachments");
2022-04-27 10:17:02 +02:00
Attachment[] as = new Attachment[media.size()];
for (int o = 0; o < as.length; ++o)
{
Tree<String> medium = media.get(o);
String u1 = medium.get("remote_url").value;
String u2 = medium.get("text_url").value;
String u3 = medium.get("url").value;
2022-04-27 10:17:02 +02:00
Attachment a = as[o] = new Attachment();
a.url = u1 != null ? u1 : u2 != null ? u2 : u3;
a.type = medium.get("type").value;
a.description = medium.get("description").value;
a.image = ImageApi.remote(a.url);
}
ImageWindow w = primaire.getMediaWindow();
w.setTitle(post.get("id").value);
w.showAttachments(as);
if (!w.isVisible()) {
w.setLocationRelativeTo(null);
w.setVisible(true);
}
}
// - -%- -
2021-07-17 11:46:27 +02:00
public void
actionPerformed(ActionEvent eA)
2021-07-17 11:46:27 +02:00
{
Component src = (Component)eA.getSource();
if (!(src instanceof JMenuItem)) return;
String text = ((JMenuItem)src).getText();
if (text.equals("Post"))
{
setContentPane(postDisplay);
revalidate();
/*
* () Setting a content pane in itself doesn't
* do anything to the content pane. Validation
* of the validate root does. Which happens on
* window realisation, or by manual call.
*/
}
else if (text.equals("Replies"))
{
setContentPane(repliesDisplay);
revalidate();
}
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);
postDisplay = new PostComponent(this);
2021-07-16 11:46:17 +02:00
repliesDisplay = new RepliesComponent();
2021-07-17 11:46:27 +02:00
setContentPane(postDisplay);
}
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;
// ---%-@-%---
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);
}
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)
{
if (command.startsWith("reply")) primaire.reply();
return;
}
if (src == nextPrev)
{
if (command.equals("next"))
{
}
else
{
}
return;
}
if (src == media)
{
primaire.openMedia();
return;
}
}
protected void
paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
((java.awt.Graphics2D)g).setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON
);
int w1 = authorName.getWidth();
int w2 = body.getWidth();
FontMetrics fm1 = getFontMetrics(authorName.getFont());
FontMetrics fm2 = getFontMetrics(body.getFont());
authorName.setText(RichTextPane.layout(authorNameOr, fm1, w1));
body.setText(RichTextPane.layout(bodyOr, fm2, w2));
List<RichTextPane.Segment> lay;
lay = RichTextPane.layout(bodyOr, fm2, w2);
int height = 0; for (RichTextPane.Segment s: lay)
{
if ((s.y + 10) > height) height = s.y + 10;
}
body.setPreferredSize(new Dimension(1, height));
}
// ---%-@-%---
PostComponent(PostWindow primaire)
{
this.primaire = primaire;
emojiUrls = new String[0][];
Border b = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Font f1 = new Font("IPAGothic", Font.PLAIN, 16);
Font f2 = new Font("IPAGothic", Font.PLAIN, 13);
Font f3 = getFont().deriveFont(14f);
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
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);
}
}
class
RepliesComponent extends JPanel {
2021-07-29 11:26:16 +02:00
private List<RepliesComponent.Reply>
replies;
// - -%- -
private JButton
prevPage, nextPage;
private JLabel
pageLabel;
private ReplyPreviewComponent[]
previews;
// ---%-@-%---
public void
2021-07-29 11:26:16 +02:00
setReplies(List<RepliesComponent.Reply> replies)
{
2021-07-29 11:26:16 +02:00
assert replies != null;
this.replies = replies;
displayPage(1);
}
// - -%- -
private void
displayPage(int pageNumber)
{
2021-07-29 11:26:16 +02:00
assert pageNumber > 0;
assert this.replies != null;
List<RepliesComponent.Reply> page;
{
2021-07-29 11:26:16 +02:00
int oS = (pageNumber - 1) * 8;
int oE = Math.min(oS + 8, replies.size());
if (oS > oE) page = new ArrayList<>();
else page = this.replies.subList(oS, oE);
}
for (int o = 0; o < page.size(); ++o)
{
assert o < previews.length;
ReplyPreviewComponent preview = previews[o];
2021-07-29 11:26:16 +02:00
Reply reply = replies.get(o);
preview.setAuthorName(reply.author);
2021-07-29 11:26:16 +02:00
preview.setText(reply.text);
preview.setVisible(true);
}
2021-07-29 11:26:16 +02:00
for (int o = page.size(); o < previews.length; ++o)
{
ReplyPreviewComponent preview = previews[o];
preview.setVisible(false);
}
2021-07-29 11:26:16 +02:00
int pages = 1 + ((replies.size() - 1) / 8);
pageLabel.setText(pageNumber + "/" + pages);
prevPage.setEnabled(pageNumber > 1);
nextPage.setEnabled(pageNumber < pages);
2021-07-29 11:26:16 +02:00
}
// ---%-@-%---
public static class
Reply {
public String
author;
public String
text;
}
// ---%-@-%---
RepliesComponent()
{
prevPage = new JButton("<");
nextPage = new JButton(">");
prevPage.setEnabled(false);
nextPage.setEnabled(false);
2021-07-29 11:26:16 +02:00
pageLabel = new JLabel();
Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue());
bottom.add(prevPage);
2021-07-17 11:37:00 +02:00
bottom.add(Box.createHorizontalStrut(8));
bottom.add(pageLabel);
2021-07-17 11:37:00 +02:00
bottom.add(Box.createHorizontalStrut(8));
bottom.add(nextPage);
JPanel centre = new JPanel();
2021-07-17 11:37:00 +02:00
centre.setOpaque(false);
centre.setLayout(new GridLayout(0, 1, 0, 2));
previews = new ReplyPreviewComponent[8];
for (int o = 0; o < previews.length; ++o)
{
previews[o] = new ReplyPreviewComponent();
previews[o].setVisible(false);
centre.add(previews[o]);
}
2021-07-17 11:37:00 +02:00
setLayout(new BorderLayout(0, 8));
add(centre, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
2021-07-17 11:37:00 +02:00
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
2021-07-29 11:26:16 +02:00
setReplies(new ArrayList<>());
}
}
2021-07-17 11:37:00 +02:00
class
ReplyPreviewComponent extends JButton {
private String
author;
private String
text;
// ---%-@-%---
@Override
public void
setText(String text)
{
assert text != null;
this.text = text;
setText();
}
public void
setAuthorName(String author)
2021-07-17 11:37:00 +02:00
{
assert author != null;
this.author = author;
setText();
}
// - -%- -
private void
setText()
{
StringBuilder text = new StringBuilder();
text.append(this.author);
text.append(" @ ");
text.append(this.text);
super.setText(text.toString());
}
protected void
paintComponent(Graphics g)
{
g.drawString(getText(), 8, 2 * getHeight() / 3);
}
}