biskuteri-cafe-JKomasto2/TimelineWindow.java

932 lines
23 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.JComponent;
import javax.swing.JButton;
2021-07-16 00:37:03 +02:00
import javax.swing.JLabel;
2021-07-17 12:03:59 +02:00
import javax.swing.JMenu;
2021-07-31 06:01:20 +02:00
import javax.swing.JMenuItem;
2021-07-17 12:03:59 +02:00
import javax.swing.JMenuBar;
import javax.swing.JSeparator;
import javax.swing.JRadioButton;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
2021-07-16 00:37:03 +02:00
import javax.swing.Box;
import javax.swing.BorderFactory;
2021-07-16 00:37:03 +02:00
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
2021-07-16 00:37:03 +02:00
import java.awt.Font;
2021-07-16 11:46:17 +02:00
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;
2021-07-31 06:01:20 +02:00
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;
2021-07-16 00:37:03 +02:00
class
2021-07-31 06:01:20 +02:00
TimelineWindow extends JFrame
implements ActionListener {
private JKomasto
primaire;
private MastodonApi
api;
private TimelinePage
page;
2021-07-16 11:46:17 +02:00
// - -%- -
private TimelineComponent
display;
2021-07-31 06:01:20 +02:00
private JMenuItem
openHome,
openMessages,
openLocal,
openFederated,
2022-04-16 16:54:17 +02:00
openNotifications,
openOwnProfile,
openProfile,
2021-07-31 06:01:20 +02:00
createPost,
openAutoPostView,
quit;
private JMenuItem
flipToNewestPost;
// - -%- -
private static final int
PREVIEW_COUNT = TimelineComponent.PREVIEW_COUNT;
2021-07-17 11:46:27 +02:00
// ---%-@-%---
public void
setTimelineType(TimelineType type)
2021-07-17 11:46:27 +02:00
{
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(
2022-04-16 16:54:17 +02:00
page.type,
PREVIEW_COUNT, null, null,
page.accountNumId, page.listId,
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)
{
page.posts = json;
display.displayEntities(page.posts);
boolean full = json.size() >= PREVIEW_COUNT;
display.setNextPageAvailable(full);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
2021-07-17 11:46:27 +02:00
}
2021-07-31 13:28:46 +02:00
public void
nextPage()
{
assert page.posts != null;
assert page.posts.size() != 0;
Tree<String> last = page.posts.get(page.posts.size() - 1);
String lastId = last.get("id").value;
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
api.getTimelinePage(
2022-04-16 16:54:17 +02:00
page.type,
PREVIEW_COUNT, lastId, null,
2022-04-16 16:54:17 +02:00
page.accountNumId, page.listId,
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)
{
if (json.size() == 0) {
// We should probably say something
// to the user here? For now, we
// quietly cancel.
return;
}
page.posts = json;
display.displayEntities(page.posts);
boolean full = json.size() >= PREVIEW_COUNT;
display.setNextPageAvailable(full);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
2021-07-31 13:28:46 +02:00
}
public void
previousPage()
{
assert page.posts != null;
assert page.posts.size() != 0;
Tree<String> first = page.posts.get(0);
String firstId = first.get("id").value;
display.setCursor(new Cursor(Cursor.WAIT_CURSOR));
api.getTimelinePage(
2022-04-16 16:54:17 +02:00
page.type,
PREVIEW_COUNT, null, firstId,
2022-04-16 16:54:17 +02:00
page.accountNumId, page.listId,
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)
{
if (json.size() < PREVIEW_COUNT) {
showLatestPage();
return;
}
page.posts = json;
display.displayEntities(page.posts);
display.setNextPageAvailable(true);
display.setPreviousPageAvailable(true);
display.resetFocus();
}
}
);
display.setCursor(null);
2021-07-31 13:28:46 +02:00
}
public void
openOwnProfile()
{
Tree<String> accountDetails = api.getAccountDetails();
String id = accountDetails.get("id").value;
assert id != null;
TimelineWindow w = new TimelineWindow(primaire);
w.showAuthorPosts(id);
w.showLatestPage();
w.setLocationRelativeTo(this);
w.setVisible(true);
}
public void
openProfile()
{
String query = JOptionPane.showInputDialog(
this,
"Whose account do you want to see?\n"
+ "Type an account name with the instance,\n"
+ "or a display name if you can't remember.\n",
"Profile search",
JOptionPane.PLAIN_MESSAGE
);
if (query == null) return;
class Handler implements RequestListener {
public Tree<String>
json;
// -=%=-
public void
connectionFailed(IOException eIo)
{
JOptionPane.showMessageDialog(
TimelineWindow.this,
"Tried to fetch accounts, but it failed.."
+ "\n" + eIo.getMessage()
);
}
public void
requestFailed(int httpCode, Tree<String> json)
{
JOptionPane.showMessageDialog(
TimelineWindow.this,
"Tried to fetch accounts, but it failed.."
+ "\n" + json.get("error").value
+ "\n(HTTP code: " + httpCode + ")"
);
}
public void
requestSucceeded(Tree<String> json) { this.json = json; }
}
// (知) Have to create a named class because
// it has to hold the variable.
Handler handler = new Handler();
api.getAccounts(query, handler);
if (handler.json == null) return;
if (handler.json.size() == 0) {
JOptionPane.showMessageDialog(
this,
"There were no results from the query.. ☹️"
);
return;
}
String id = null;
if (query.startsWith("@")) query = query.substring(1);
List<Object> message = new ArrayList<>();
message.add("Maybe one of these?");
ButtonGroup selGroup = new ButtonGroup();
for (Tree<String> account: handler.json)
{
String dname = account.get("display_name").value;
String acct = account.get("acct").value;
if (query.equals(acct)) {
id = account.get("id").value;
break;
}
JRadioButton b = new JRadioButton();
b.setText(dname + " (" + acct + ")");
selGroup.add(b);
message.add(b);
}
if (id == null)
{
int response = JOptionPane.showConfirmDialog(
this,
message.toArray(),
"Search results",
JOptionPane.OK_CANCEL_OPTION
);
if (response == JOptionPane.CANCEL_OPTION) return;
for (int o = 1; o < message.size(); ++o)
{
JRadioButton b = (JRadioButton)message.get(o);
if (selGroup.isSelected(b.getModel()))
{
id = handler.json.get(o - 1).get("id").value;
break;
}
}
}
TimelineWindow w = new TimelineWindow(primaire);
w.showAuthorPosts(id);
w.showLatestPage();
w.setLocationRelativeTo(this);
w.setVisible(true);
}
2021-07-31 06:01:20 +02:00
// - -%- -
public void
postSelected(Tree<String> post)
{
primaire.getAutoViewWindow().displayEntity(post);
}
public void
postOpened(Tree<String> post)
{
PostWindow w = new PostWindow(primaire);
w.displayEntity(post);
w.setLocationRelativeTo(this);
w.setVisible(true);
}
2021-07-31 06:01:20 +02:00
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 == openOwnProfile)
{
openOwnProfile();
}
if (src == openProfile)
{
openProfile();
}
if (src == createPost)
{
primaire.getComposeWindow().setVisible(true);
}
if (src == openAutoPostView)
{
PostWindow w = primaire.getAutoViewWindow();
w.setLocation(getX() + 10 + getWidth(), getY());
w.setVisible(true);
2022-04-16 16:54:17 +02:00
}
if (src == openNotifications)
{
NotificationsWindow w = primaire.getNotificationsWindow();
w.setLocationByPlatform(true);
w.setVisible(true);
}
if (src == flipToNewestPost)
{
showLatestPage();
}
2021-07-31 06:01:20 +02:00
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();
2021-07-31 06:01:20 +02:00
}
}
// - -%- -
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));
2021-07-16 11:46:17 +02:00
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2021-07-31 06:01:20 +02:00
openHome = new JMenuItem("Open home timeline");
openFederated = new JMenuItem("Open federated timeline");
2022-04-16 16:54:17 +02:00
openNotifications = new JMenuItem("Open notifications");
openOwnProfile = new JMenuItem("Open own profile");
openProfile = new JMenuItem("Open profile..");
2021-07-31 06:01:20 +02:00
createPost = new JMenuItem("Create a post");
openAutoPostView = new JMenuItem("Open auto post view");
quit = new JMenuItem("Quit");
openHome.addActionListener(this);
openFederated.addActionListener(this);
2022-04-16 16:54:17 +02:00
openNotifications.addActionListener(this);
openOwnProfile.addActionListener(this);
openProfile.addActionListener(this);
2021-07-31 06:01:20 +02:00
createPost.addActionListener(this);
openAutoPostView.addActionListener(this);
quit.addActionListener(this);
flipToNewestPost = new JMenuItem("Flip to newest post");
flipToNewestPost.addActionListener(this);
2021-07-17 12:03:59 +02:00
JMenu programMenu = new JMenu("Program");
programMenu.setMnemonic(KeyEvent.VK_P);
2021-07-31 06:01:20 +02:00
programMenu.add(openHome);
programMenu.add(openFederated);
2022-04-16 16:54:17 +02:00
programMenu.add(openNotifications);
programMenu.add(openOwnProfile);
programMenu.add(openProfile);
2021-07-17 12:03:59 +02:00
programMenu.add(new JSeparator());
2021-07-31 06:01:20 +02:00
programMenu.add(createPost);
programMenu.add(openAutoPostView);
2021-07-17 12:03:59 +02:00
programMenu.add(new JSeparator());
2021-07-31 06:01:20 +02:00
programMenu.add(quit);
2021-07-17 12:03:59 +02:00
JMenu timelineMenu = new JMenu("Timeline");
timelineMenu.setMnemonic(KeyEvent.VK_T);
2021-07-31 06:01:20 +02:00
timelineMenu.add(flipToNewestPost);
2021-07-17 12:03:59 +02:00
JMenuBar menuBar = new JMenuBar();
menuBar.add(programMenu);
menuBar.add(timelineMenu);
setJMenuBar(menuBar);
page = new TimelinePage();
page.posts = new Tree<String>();
2021-07-31 06:01:20 +02:00
display = new TimelineComponent(this);
display.setNextPageAvailable(false);
display.setPreviousPageAvailable(false);
2021-07-17 12:03:59 +02:00
setContentPane(display);
setTimelineType(TimelineType.HOME);
}
}
class
2021-07-31 06:01:20 +02:00
TimelineComponent extends JPanel
implements
ActionListener, KeyListener,
MouseListener, FocusListener {
2021-07-31 06:01:20 +02:00
private TimelineWindow
primaire;
private Tree<String>
posts;
2021-07-31 06:01:20 +02:00
// - -%- -
private JButton
next, prev;
private JLabel
pageLabel;
2021-07-17 11:46:27 +02:00
private final List<PostPreviewComponent>
postPreviews;
private boolean
hoverSelect;
private Image
backgroundImage;
// - -%- -
static final int
PREVIEW_COUNT = 10;
// ---%-@-%---
public void
displayEntities(Tree<String> postArray)
{
assert postArray.size() <= postPreviews.size();
this.posts = postArray;
for (int o = 0; o < postArray.size(); ++o)
{
PostPreviewComponent c = postPreviews.get(o);
Tree<String> p = postArray.get(o);
Tree<String> a = p.get("account");
String an = a.get("display_name").value;
if (an.isEmpty()) an = a.get("username").value;
c.setTopLeft(an);
Tree<String> boosted = p.get("reblog");
if (boosted.size() > 0) {
c.setTopLeft("boosted by " + an);
p = boosted;
a = p.get("account");
}
String f = "";
if (p.get("media_attachments").size() > 0) f += "a";
String t = "";
try {
String jv = p.get("created_at").value;
ZonedDateTime pv = ZonedDateTime.parse(jv);
ZoneId z = ZoneId.systemDefault();
pv = pv.withZoneSameInstant(z);
ZonedDateTime now = ZonedDateTime.now();
long d = ChronoUnit.SECONDS.between(pv, 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";
}
catch (DateTimeParseException eDt) {
assert false;
}
c.setTopRight(f + " " + t);
String html = p.get("content").value;
String cw = p.get("spoiler_text").value;
if (!cw.isEmpty()) {
c.setBottom("(" + cw + ")");
}
else {
c.setBottom(RudimentaryHTMLParser.stripTags(html) + " ");
}
}
for (int o = posts.size(); o < postPreviews.size(); ++o)
{
postPreviews.get(o).reset();
}
}
2021-07-31 06:01:20 +02:00
public void
setPageLabel(String label)
{
assert label != null;
pageLabel.setText("" + label);
}
public void
setNextPageAvailable(boolean n) { next.setEnabled(n); }
2021-07-31 06:01:20 +02:00
public void
setPreviousPageAvailable(boolean n) { prev.setEnabled(n); }
2021-07-31 06:01:20 +02:00
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);
}
2021-07-31 06:01:20 +02:00
private void
select(Object c)
{
assert c instanceof PostPreviewComponent;
for (int o = 0; o < postPreviews.size(); ++o)
{
PostPreviewComponent p = postPreviews.get(o);
if (c == p) {
primaire.postSelected(posts.get(o));
p.setSelected(true);
p.repaint();
}
else deselect(p);
}
}
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 o = postPreviews.indexOf(p);
assert o != -1;
if (o >= posts.size()) return;
primaire.postOpened(posts.get(o));
}
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());
2021-07-31 06:01:20 +02:00
}
// (知) 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);
}
2021-07-31 06:01:20 +02:00
public void
actionPerformed(ActionEvent eA)
{
Object src = eA.getSource();
2021-07-31 13:28:46 +02:00
if (src == next) primaire.nextPage();
else if (src == prev) primaire.previousPage();
/*
2021-07-31 06:01:20 +02:00
* 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) { }
// ---%-@-%---
2021-07-31 06:01:20 +02:00
TimelineComponent(TimelineWindow primaire)
{
2021-07-31 06:01:20 +02:00
this.primaire = primaire;
postPreviews = new ArrayList<>(PREVIEW_COUNT);
hoverSelect = true;
prev = new JButton("<");
next = new JButton(">");
2021-07-31 06:01:20 +02:00
prev.setEnabled(false);
next.setEnabled(false);
prev.setMnemonic(KeyEvent.VK_PAGE_UP);
next.setMnemonic(KeyEvent.VK_PAGE_DOWN);
prev.addActionListener(this);
next.addActionListener(this);
2021-07-31 06:01:20 +02:00
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));
}
}
2021-07-16 00:37:03 +02:00
class
2021-07-17 11:46:27 +02:00
PostPreviewComponent extends JComponent {
2021-07-16 00:37:03 +02:00
private JLabel
topLeft, topRight, bottom;
private boolean
selected;
2021-07-16 00:37:03 +02:00
// ---%-@-%---
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());
}
}
2021-07-16 00:37:03 +02:00
// ---%-@-%---
public
2021-07-17 11:46:27 +02:00
PostPreviewComponent()
2021-07-16 00:37:03 +02:00
{
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);
2021-07-16 00:37:03 +02:00
2021-07-31 06:01:20 +02:00
topLeft = new JLabel();
topLeft.setFont(f1);
topLeft.setOpaque(false);
2021-07-16 00:37:03 +02:00
2021-07-31 06:01:20 +02:00
topRight = new JLabel();
2021-07-16 00:37:03 +02:00
topRight.setHorizontalAlignment(JLabel.RIGHT);
topRight.setFont(f2);
topRight.setOpaque(false);
2021-07-16 00:37:03 +02:00
Box top = Box.createHorizontalBox();
top.setOpaque(false);
2021-07-16 00:37:03 +02:00
top.add(topLeft);
top.add(Box.createGlue());
top.add(topRight);
bottom = new JLabel();
bottom.setFont(f3);
bottom.setOpaque(false);
JPanel left = new JPanel();
left.setOpaque(false);
left.setLayout(new BorderLayout());
left.add(top, BorderLayout.NORTH);
left.add(bottom);
setFocusable(true);
setOpaque(false);
setLayout(new BorderLayout());
add(left);
2021-07-16 00:37:03 +02:00
}
}