biskuteri-cafe-JKomasto2/TimelineWindow.java
Snowyfox 7ede5e1290 Added buggy rich text pane.
Added rendering of emojis.
Added run script.
2022-04-15 08:54:31 -04:00

860 lines
22 KiB
Java
Executable File

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JSeparator;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Cursor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;
import java.util.ArrayList;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import cafe.biskuteri.hinoki.Tree;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeParseException;
class
TimelineWindow extends JFrame
implements ActionListener {
private JKomasto
primaire;
private MastodonApi
api;
private TimelinePage
page;
// - -%- -
private TimelineComponent
display;
private JMenuItem
openHome,
openMessages,
openLocal,
openFederated,
createPost,
openAutoPostView,
quit;
private JMenuItem
flipToNewestPost;
// - -%- -
private static final int
PREVIEW_COUNT = TimelineComponent.PREVIEW_COUNT;
// ---%-@-%---
public void
setTimelineType(TimelineType type)
{
page.type = type;
page.accountNumId = null;
String s1 = type.toString();
s1 = s1.charAt(0) + s1.substring(1).toLowerCase();
setTitle(s1 + " - JKomasto");
String s2 = type.toString().toLowerCase();
display.setBackgroundImage(ImageApi.local(s2));
}
public TimelineType
getTimelineType() { return page.type; }
public void
showAuthorPosts(String authorNumId)
{
assert authorNumId != null;
page.type = TimelineType.FEDERATED;
page.accountNumId = authorNumId;
setTitle(authorNumId + " - JKomasto");
display.setBackgroundImage(ImageApi.local("profile"));
}
public void
showLatestPage()
{
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
api.getTimelinePage(
page.type, page.accountNumId,
PREVIEW_COUNT, null, null,
new RequestListener() {
public void
connectionFailed(IOException eIo)
{
eIo.printStackTrace();
String s = eIo.getClass().getName();
setTitle(s + " - JKomasto");
}
public void
requestFailed(int httpCode, Tree<String> json)
{
System.err.println(json.get("error").value);
setTitle(httpCode + " - JKomasto");
// lol...
}
public void
requestSucceeded(Tree<String> json)
{
List<Post> posts = toPosts(json);
page.posts = posts;
display.setPosts(page.posts);
boolean full = posts.size() >= PREVIEW_COUNT;
display.setNextPageAvailable(full);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
}
public void
nextPage()
{
assert page.posts != null;
assert page.posts.size() != 0;
Post last = page.posts.get(page.posts.size() - 1);
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
api.getTimelinePage(
page.type, page.accountNumId,
PREVIEW_COUNT, last.postId, null,
new RequestListener() {
public void
connectionFailed(IOException eIo)
{
String s = eIo.getClass().getName();
setTitle(s + " - JKomasto");
}
public void
requestFailed(int httpCode, Tree<String> json)
{
setTitle(httpCode + " - JKomasto");
}
public void
requestSucceeded(Tree<String> json)
{
List<Post> posts = toPosts(json);
if (posts.size() == 0) {
// We should probably say something
// to the user here? For now, we
// quietly cancel.
return;
}
page.posts = posts;
display.setPosts(page.posts);
boolean full = posts.size() >= PREVIEW_COUNT;
display.setNextPageAvailable(full);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
}
public void
previousPage()
{
assert page.posts != null;
assert page.posts.size() != 0;
Post first = page.posts.get(0);
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
api.getTimelinePage(
page.type, page.accountNumId,
PREVIEW_COUNT, null, first.postId,
new RequestListener() {
public void
connectionFailed(IOException eIo)
{
String s = eIo.getClass().getName();
setTitle(s + " - JKomasto");
}
public void
requestFailed(int httpCode, Tree<String> json)
{
setTitle(httpCode + " - JKomasto");
}
public void
requestSucceeded(Tree<String> json)
{
List<Post> posts = toPosts(json);
if (posts.size() < PREVIEW_COUNT) {
showLatestPage();
return;
}
page.posts = posts;
display.setPosts(page.posts);
display.setNextPageAvailable(true);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
}
// - -%- -
public void
postSelected(Post post)
{
primaire.getAutoViewWindow().showPost(post);
}
public void
postOpened(Post post)
{
PostWindow w = new PostWindow(primaire);
w.showPost(post);
w.setLocationRelativeTo(this);
w.setVisible(true);
}
public void
actionPerformed(ActionEvent eA)
{
Object src = eA.getSource();
if (src == openHome)
{
setTimelineType(TimelineType.HOME);
showLatestPage();
}
if (src == openFederated)
{
setTimelineType(TimelineType.FEDERATED);
showLatestPage();
}
if (src == openLocal)
{
setTimelineType(TimelineType.LOCAL);
showLatestPage();
}
if (src == createPost)
{
primaire.getComposeWindow().setVisible(true);
}
if (src == openAutoPostView)
{
PostWindow w = primaire.getAutoViewWindow();
w.setLocation(getX() + 10 + getWidth(), getY());
w.setVisible(true);
}
if (src == flipToNewestPost)
{
showLatestPage();
}
if (src == quit)
{
/*
* Umm.. should we even have a quit option?
* Wouldn't closing every window work? By
* disposing of everyone. We won't be having any
* background threads IIRC (and they can check
* if the Swing thread is alive).
*/
dispose();
}
}
// - -%- -
private static List<Post>
toPosts(Tree<String> json)
{
List<Post> posts = new ArrayList<>();
for (Tree<String> post: json.children)
{
Post addee = new Post();
if (post.get("reblog").size() != 0) {
Tree<String> a = post.get("account");
String s = a.get("display_name").value;
addee.boosterName = s;
post = post.get("reblog");
}
addee.postId = post.get("id").value;
try {
String s = post.get("created_at").value;
addee.date = ZonedDateTime.parse(s);
ZoneId z = ZoneId.systemDefault();
addee.date = addee.date.withZoneSameInstant(z);
}
catch (DateTimeParseException eDt) {
assert false;
addee.date = ZonedDateTime.now();
}
String s2 = addee.html = post.get("content").value;
StringBuilder b = new StringBuilder();
Tree<String> nodes = RudimentaryHTMLParser.depthlessRead(s2);
for (Tree<String> node: nodes)
{
if (node.key.equals("tag"))
{
String tagName = node.get(0).key;
if (tagName.equals("br")) b.append(" \n ");
if (tagName.equals("/p")) b.append(" \n \n ");
}
if (node.key.equals("text")) b.append(node.value);
if (node.key.equals("emoji")) b.append(node.value);
}
addee.text = b.toString();
String s3 = post.get("spoiler_text").value;
if (!s3.isEmpty()) addee.contentWarning = s3;
else addee.contentWarning = null;
Tree<String> account = post.get("account");
addee.authorId = account.get("acct").value;
addee.authorName = account.get("username").value;
addee.authorNumId = account.get("id").value;
String s4 = account.get("display_name").value;
if (!s4.isEmpty()) addee.authorName = s4;
String s5 = account.get("avatar").value;
addee.authorAvatar = ImageApi.remote(s5);
if (addee.authorAvatar == null) {
s5 = "defaultAvatar";
addee.authorAvatar = ImageApi.local(s5);
}
String s6 = post.get("favourited").value;
String s7 = post.get("reblogged").value;
addee.favourited = s6.equals("true");
addee.boosted = s7.equals("true");
Tree<String> as1 = post.get("media_attachments");
Attachment[] as2 = new Attachment[as1.size()];
for (int o = 0; o < as2.length; ++o)
{
Tree<String> a1 = as1.get(o);
Attachment a2 = as2[o] = new Attachment();
a2.type = a1.get("type").value;
String u1 = a1.get("remote_url").value;
String u2 = a1.get("text_url").value;
a2.url = u1 == null ? u2 : u1;
a2.description = a1.get("description").value;
a2.image = null;
if (a2.type.equals("image"))
a2.image = ImageApi.remote(a2.url);
}
addee.attachments = as2;
Tree<String> es1 = post.get("emojis");
String[][] es2 = new String[es1.size()][];
for (int o = 0; o < es2.length; ++o)
{
Tree<String> e1 = es1.get(o);
String[] e2 = es2[o] = new String[2];
e2[0] = e1.get("shortcode").value;
e2[1] = e1.get("url").value;
}
addee.emojiUrls = es2;
posts.add(addee);
}
return posts;
}
private static String
plainify(String html)
{
// Delete all tags.
StringBuilder b = new StringBuilder();
boolean in = false;
for (char c: html.toCharArray()) switch (c) {
case '<': in = true; break;
case '>': in = false; break;
default: if (!in) b.append(c);
}
String s = b.toString();
s = s.replaceAll("&lt;", "<");
s = s.replaceAll("&gt;", ">");
s = s.replaceAll("&nbsp;", "");
return s;
}
// ---%-@-%---
TimelineWindow(JKomasto primaire)
{
this.primaire = primaire;
this.api = primaire.getMastodonApi();
getContentPane().setPreferredSize(new Dimension(320, 460));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
openHome = new JMenuItem("Open home timeline");
openFederated = new JMenuItem("Open federated timeline");
createPost = new JMenuItem("Create a post");
openAutoPostView = new JMenuItem("Open auto post view");
quit = new JMenuItem("Quit");
openHome.addActionListener(this);
openFederated.addActionListener(this);
createPost.addActionListener(this);
openAutoPostView.addActionListener(this);
quit.addActionListener(this);
flipToNewestPost = new JMenuItem("Flip to newest post");
flipToNewestPost.addActionListener(this);
JMenu programMenu = new JMenu("Program");
programMenu.setMnemonic(KeyEvent.VK_P);
programMenu.add(openHome);
programMenu.add(openFederated);
programMenu.add(new JSeparator());
programMenu.add(createPost);
programMenu.add(openAutoPostView);
programMenu.add(new JSeparator());
programMenu.add(quit);
JMenu timelineMenu = new JMenu("Timeline");
timelineMenu.setMnemonic(KeyEvent.VK_T);
timelineMenu.add(flipToNewestPost);
JMenuBar menuBar = new JMenuBar();
menuBar.add(programMenu);
menuBar.add(timelineMenu);
setJMenuBar(menuBar);
page = new TimelinePage();
page.posts = new ArrayList<>();
display = new TimelineComponent(this);
display.setNextPageAvailable(false);
display.setPreviousPageAvailable(false);
setContentPane(display);
setTimelineType(TimelineType.HOME);
}
}
class
TimelineComponent extends JPanel
implements
ActionListener, KeyListener,
MouseListener, FocusListener {
private TimelineWindow
primaire;
private final List<Post>
posts = new ArrayList<>();
// - -%- -
private JButton
next, prev;
private JLabel
pageLabel;
private final List<PostPreviewComponent>
postPreviews;
private boolean
hoverSelect;
private Image
backgroundImage;
// - -%- -
static final int
PREVIEW_COUNT = 10;
// ---%-@-%---
public void
setPosts(List<Post> posts)
{
this.posts.clear();
this.posts.addAll(posts);
assert posts.size() <= postPreviews.size();
for (int o = 0; o < posts.size(); ++o)
{
PostPreviewComponent c = postPreviews.get(o);
Post p = posts.get(o);
c.setTopLeft(p.authorName);
{
String f = "";
if (p.boosterName != null)
f += "b";
if (p.attachments.length > 0)
f += "a";
String t;
ZonedDateTime now = ZonedDateTime.now();
long d = ChronoUnit.SECONDS.between(p.date, now);
long s = Math.abs(d);
if (s < 30) t = "now";
else if (s < 60) t = d + "s";
else if (s < 3600) t = (d / 60) + "m";
else if (s < 86400) t = (d / 3600) + "h";
else t = (d / 86400) + "d";
c.setTopRight(f + " " + t);
}
if (p.contentWarning != null)
c.setBottom("(" + p.contentWarning + ")");
else
c.setBottom(p.text + " ");
}
for (int o = posts.size(); o < postPreviews.size(); ++o)
{
postPreviews.get(o).reset();
}
}
public void
setPageLabel(String label)
{
assert label != null;
pageLabel.setText("" + label);
}
public void
setNextPageAvailable(boolean n) { next.setEnabled(n); }
public void
setPreviousPageAvailable(boolean n) { prev.setEnabled(n); }
public void
setHoverSelect(boolean n) { hoverSelect = n; }
public void
setBackgroundImage(Image n) { backgroundImage = n; }
public void
resetFocus() { postPreviews.get(0).requestFocusInWindow(); }
// - -%- -
protected void
paintComponent(Graphics g)
{
((java.awt.Graphics2D)g).setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON
);
int w = getWidth(), h = getHeight();
g.clearRect(0, 0, w, h);
int h2 = h * 5 / 10, w2 = h2;
int x = w - w2, y = h - h2;
g.drawImage(backgroundImage, x, y, w2, h2, this);
}
private void
select(Object c)
{
assert c instanceof PostPreviewComponent;
PostPreviewComponent p = (PostPreviewComponent)c;
int offset = postPreviews.indexOf(p);
assert offset != -1;
if (offset < posts.size()) {
primaire.postSelected(posts.get(offset));
p.setSelected(true);
}
else {
p.setSelected(false);
}
p.repaint();
}
private void
deselect(Object c)
{
assert c instanceof PostPreviewComponent;
PostPreviewComponent p = (PostPreviewComponent)c;
p.setSelected(false);
p.repaint();
}
private void
open(Object c)
{
assert c instanceof PostPreviewComponent;
PostPreviewComponent p = (PostPreviewComponent)c;
int offset = postPreviews.indexOf(p);
assert offset != -1;
if (offset < posts.size()) {
primaire.postOpened(posts.get(offset));
}
}
public void
focusGained(FocusEvent eF) { select(eF.getSource()); }
public void
focusLost(FocusEvent eF) { deselect(eF.getSource()); }
public void
mouseClicked(MouseEvent eM)
{
if (eM.getClickCount() == 2) open(eM.getSource());
else select(eM.getSource());
}
public void
mouseEntered(MouseEvent eM)
{
if (!hoverSelect) return;
mouseClicked(eM);
}
public void
mouseExited(MouseEvent eM)
{
if (!hoverSelect) return;
deselect(eM.getSource());
}
// (知) First time I'm using these two..!
public void
keyPressed(KeyEvent eK)
{
if (eK.getKeyCode() != KeyEvent.VK_ENTER) return;
PostPreviewComponent selected = null;
for (PostPreviewComponent c: postPreviews)
if (c.getSelected()) selected = c;
if (selected == null) return;
open(selected);
}
public void
actionPerformed(ActionEvent eA)
{
Object src = eA.getSource();
if (src == next) primaire.nextPage();
else if (src == prev) primaire.previousPage();
/*
* I think the page previews will just forward to us.
* But I think they'll have to tell us where they are
* in the list somehow, because we need to show only
* one post as selected.
*/
}
public void
mousePressed(MouseEvent eM) { }
public void
mouseReleased(MouseEvent eM) { }
public void
keyTyped(KeyEvent eK) { }
public void
keyReleased(KeyEvent eK) { }
// ---%-@-%---
TimelineComponent(TimelineWindow primaire)
{
this.primaire = primaire;
postPreviews = new ArrayList<>(PREVIEW_COUNT);
hoverSelect = true;
prev = new JButton("<");
next = new JButton(">");
prev.setEnabled(false);
next.setEnabled(false);
prev.setMnemonic(KeyEvent.VK_PAGE_UP);
next.setMnemonic(KeyEvent.VK_PAGE_DOWN);
prev.addActionListener(this);
next.addActionListener(this);
pageLabel = new JLabel("0");
Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue());
bottom.add(prev);
bottom.add(Box.createHorizontalStrut(8));
bottom.add(next);
JPanel centre = new JPanel();
centre.setOpaque(false);
centre.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.weightx = 1;
constraints.insets = new Insets(4, 0, 4, 0);
for (int n = PREVIEW_COUNT; n > 0; --n)
{
PostPreviewComponent c = new PostPreviewComponent();
c.reset();
c.addMouseListener(this);
c.addFocusListener(this);
c.addKeyListener(this);
centre.add(c, constraints);
postPreviews.add(c);
}
setLayout(new BorderLayout());
add(centre, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
}
}
class
PostPreviewComponent extends JComponent {
private JLabel
topLeft, topRight, bottom;
private boolean
selected;
// ---%-@-%---
public void
setTopLeft(String text) { topLeft.setText(text); }
public void
setTopRight(String text) { topRight.setText(text); }
public void
setBottom(String text) { bottom.setText(text); }
public void
reset()
{
setTopLeft(" ");
setTopRight(" ");
setBottom(" ");
}
public void
setSelected(boolean selected)
{
this.selected = selected;
if (!selected) setBackground(null);
else setBackground(new Color(0, 0, 0, 25));
}
public boolean
getSelected() { return selected; }
// - -%- -
protected void
paintComponent(Graphics g)
{
if (selected) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
// ---%-@-%---
public
PostPreviewComponent()
{
Font f = new JLabel().getFont();
Font f1 = f.deriveFont(Font.PLAIN, 12f);
Font f2 = f.deriveFont(Font.ITALIC, 12f);
Font f3 = f.deriveFont(Font.PLAIN, 14f);
topLeft = new JLabel();
topLeft.setFont(f1);
topLeft.setOpaque(false);
topRight = new JLabel();
topRight.setHorizontalAlignment(JLabel.RIGHT);
topRight.setFont(f2);
topRight.setOpaque(false);
Box top = Box.createHorizontalBox();
top.setOpaque(false);
top.add(topLeft);
top.add(Box.createGlue());
top.add(topRight);
bottom = new JLabel();
bottom.setFont(f3);
bottom.setOpaque(false);
setFocusable(true);
setOpaque(false);
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(bottom);
}
}