2021-07-16 00:37:03 +02:00
|
|
|
|
|
|
|
import javax.swing.JFrame;
|
2021-07-16 01:07:23 +02:00
|
|
|
import javax.swing.JPanel;
|
2021-07-16 00:37:03 +02:00
|
|
|
import javax.swing.JMenuBar;
|
|
|
|
import javax.swing.JMenu;
|
2021-07-29 10:03:32 +02:00
|
|
|
import javax.swing.JMenuItem;
|
2021-07-16 01:07:23 +02:00
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
import javax.swing.Box;
|
2022-04-12 08:37:39 +02:00
|
|
|
import javax.swing.BoxLayout;
|
2021-07-16 01:07:23 +02:00
|
|
|
import javax.swing.BorderFactory;
|
2022-04-12 08:37:39 +02:00
|
|
|
import javax.swing.border.Border;
|
|
|
|
import javax.swing.JOptionPane;
|
2021-07-16 01:07:23 +02:00
|
|
|
import java.awt.Graphics;
|
2022-04-12 08:37:39 +02:00
|
|
|
import java.awt.Font;
|
2021-07-29 10:03:32 +02:00
|
|
|
import java.awt.FontMetrics;
|
2022-04-12 08:37:39 +02:00
|
|
|
import java.awt.Shape;
|
2021-07-16 00:37:03 +02:00
|
|
|
import java.awt.Dimension;
|
2021-07-16 01:07:23 +02:00
|
|
|
import java.awt.BorderLayout;
|
|
|
|
import java.awt.GridLayout;
|
2022-04-12 08:37:39 +02:00
|
|
|
import java.awt.Cursor;
|
2021-07-29 10:03:32 +02:00
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.ActionEvent;
|
2021-07-16 01:07:23 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
2022-04-12 08:37:39 +02:00
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import cafe.biskuteri.hinoki.Tree;
|
|
|
|
|
2021-07-16 00:37:03 +02:00
|
|
|
|
|
|
|
class
|
2021-07-29 10:03:32 +02:00
|
|
|
PostWindow extends JFrame
|
|
|
|
implements ActionListener {
|
2021-07-16 00:37:03 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
private JKomasto
|
|
|
|
primaire;
|
|
|
|
|
|
|
|
private MastodonApi
|
|
|
|
api;
|
|
|
|
|
2021-07-16 11:46:17 +02:00
|
|
|
private Post
|
|
|
|
post;
|
|
|
|
|
|
|
|
// - -%- -
|
|
|
|
|
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
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
// - -%- -
|
|
|
|
|
|
|
|
private static final DateTimeFormatter
|
|
|
|
DATE_FORMAT = DateTimeFormatter.ofPattern("d LLLL ''uu"),
|
|
|
|
TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
|
|
|
|
|
2021-07-29 10:03:32 +02:00
|
|
|
// ---%-@-%---
|
2021-07-17 13:17:14 +02:00
|
|
|
|
2021-07-29 10:03:32 +02:00
|
|
|
public void
|
2022-04-12 08:37:39 +02:00
|
|
|
showPost(Post post)
|
2021-07-17 13:17:14 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
assert post != null;
|
2021-07-29 10:03:32 +02:00
|
|
|
this.post = post;
|
|
|
|
|
2021-07-29 11:26:16 +02:00
|
|
|
List<RepliesComponent.Reply> replies = null;
|
|
|
|
{
|
|
|
|
List<Post> posts = null;
|
|
|
|
// We should make a request to JKomasto here.
|
|
|
|
}
|
|
|
|
if (replies == null)
|
|
|
|
{
|
|
|
|
RepliesComponent.Reply reply1, reply2, reply3;
|
|
|
|
reply1 = new RepliesComponent.Reply();
|
|
|
|
reply1.author = "Black tea";
|
|
|
|
reply1.text = "Rich..";
|
|
|
|
reply2 = new RepliesComponent.Reply();
|
|
|
|
reply2.author = "Green tea";
|
|
|
|
reply2.text = "Clean!";
|
|
|
|
reply3 = new RepliesComponent.Reply();
|
|
|
|
reply3.author = "Coffee";
|
|
|
|
reply3.text = "sleepy..";
|
|
|
|
|
|
|
|
replies = new ArrayList<>();
|
|
|
|
replies.add(reply1);
|
|
|
|
replies.add(reply2);
|
|
|
|
replies.add(reply3);
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
postDisplay.setAuthorName(post.authorName);
|
|
|
|
postDisplay.setAuthorId(post.authorId);
|
|
|
|
postDisplay.setDate(DATE_FORMAT.format(post.date));
|
|
|
|
postDisplay.setTime(TIME_FORMAT.format(post.date));
|
2021-07-29 10:03:32 +02:00
|
|
|
postDisplay.setText(post.text);
|
2022-04-12 08:37:39 +02:00
|
|
|
postDisplay.setFavourited(post.favourited);
|
|
|
|
postDisplay.setBoosted(post.boosted);
|
2021-07-29 11:26:16 +02:00
|
|
|
repliesDisplay.setReplies(replies);
|
2022-04-12 08:37:39 +02:00
|
|
|
repaint();
|
2021-07-17 13:17:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-31 13:36:56 +02:00
|
|
|
public void
|
|
|
|
openAuthorProfile()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
2022-04-12 08:37:39 +02:00
|
|
|
favourite(boolean favourited)
|
2021-07-31 13:36:56 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +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)
|
|
|
|
{
|
|
|
|
post.favourited = favourited;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
api.setPostFavourited(post.postId, favourited, handler);
|
|
|
|
postDisplay.setFavouriteBoostEnabled(true);
|
|
|
|
postDisplay.setCursor(null);
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
2022-04-12 08:37:39 +02:00
|
|
|
boost(boolean boosted)
|
2021-07-31 13:36:56 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +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)
|
|
|
|
{
|
|
|
|
post.boosted = boosted;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
api.setPostBoosted(post.postId, boosted, handler);
|
|
|
|
postDisplay.setFavouriteBoostEnabled(true);
|
|
|
|
postDisplay.setCursor(null);
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
reply()
|
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
ComposeWindow w = primaire.getComposeWindow();
|
|
|
|
w.setLocation(getX(), getY() + 100);
|
|
|
|
w.setVisible(true);
|
|
|
|
Composition c = new Composition();
|
|
|
|
c.text = "@" + post.authorId + " ";
|
|
|
|
c.visibility = PostVisibility.PUBLIC;
|
|
|
|
c.replyToPostId = post.postId;
|
|
|
|
w.setComposition(c);
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
openMedia()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-29 10:03:32 +02:00
|
|
|
// - -%- -
|
2021-07-17 11:46:27 +02:00
|
|
|
|
|
|
|
public void
|
2021-07-29 10:03:32 +02:00
|
|
|
actionPerformed(ActionEvent eA)
|
2021-07-17 11:46:27 +02:00
|
|
|
{
|
2021-07-29 10:03:32 +02:00
|
|
|
Object src = 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
|
|
|
// ---%-@-%---
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
PostWindow(JKomasto primaire)
|
2021-07-16 00:37:03 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +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);
|
|
|
|
|
2021-07-31 13:36:56 +02:00
|
|
|
postDisplay = new PostComponent(this);
|
2021-07-16 11:46:17 +02:00
|
|
|
|
|
|
|
repliesDisplay = new RepliesComponent();
|
2021-07-29 10:03:32 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
Post samplePost = new Post();
|
|
|
|
samplePost.text = "This is a sample post.";
|
|
|
|
samplePost.authorId = "snowyfox@biskuteri.cafe";
|
|
|
|
samplePost.authorName = "snowyfox";
|
|
|
|
samplePost.date = ZonedDateTime.now();
|
|
|
|
samplePost.visibility = PostVisibility.MENTIONED;
|
|
|
|
samplePost.postId = "000000000";
|
|
|
|
samplePost.boosted = false;
|
|
|
|
samplePost.favourited = true;
|
|
|
|
showPost(samplePost);
|
2021-07-16 00:37:03 +02:00
|
|
|
|
2021-07-17 11:46:27 +02:00
|
|
|
setContentPane(postDisplay);
|
2021-07-29 10:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 00:37:03 +02:00
|
|
|
}
|
2021-07-16 01:07:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class
|
2021-07-31 13:36:56 +02:00
|
|
|
PostComponent extends JPanel
|
|
|
|
implements ActionListener {
|
|
|
|
|
|
|
|
private PostWindow
|
|
|
|
primaire;
|
2021-07-16 01:07:23 +02:00
|
|
|
|
|
|
|
private String
|
2022-04-12 08:37:39 +02:00
|
|
|
authorName, authorId, date, time, text;
|
2021-07-16 01:07:23 +02:00
|
|
|
|
2021-07-31 13:28:46 +02:00
|
|
|
// - -%- -
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
private TwoToggleButton
|
2021-07-31 13:28:46 +02:00
|
|
|
favouriteBoost,
|
|
|
|
replyMisc,
|
2022-04-12 08:37:39 +02:00
|
|
|
nextPrev;
|
|
|
|
|
|
|
|
private JButton
|
|
|
|
profile,
|
2021-07-31 13:28:46 +02:00
|
|
|
media;
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
// ---%-@-%---
|
|
|
|
|
|
|
|
public void
|
2022-04-12 08:37:39 +02:00
|
|
|
setAuthorName(String authorName)
|
2021-07-16 01:07:23 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
assert authorName != null;
|
|
|
|
this.authorName = authorName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
setAuthorId(String authorId)
|
|
|
|
{
|
|
|
|
assert authorId != null;
|
|
|
|
this.authorId = authorId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
setDate(String date)
|
|
|
|
{
|
|
|
|
assert date != null;
|
|
|
|
this.date = date;
|
2021-07-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
setTime(String time)
|
|
|
|
{
|
|
|
|
assert time != null;
|
|
|
|
this.time = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void
|
|
|
|
setText(String text)
|
|
|
|
{
|
|
|
|
assert text != null;
|
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
// - -%- -
|
|
|
|
|
2021-07-31 13:36:56 +02:00
|
|
|
public void
|
|
|
|
actionPerformed(ActionEvent eA)
|
|
|
|
{
|
|
|
|
Object src = eA.getSource();
|
2022-04-12 08:37:39 +02:00
|
|
|
String command = eA.getActionCommand();
|
2021-07-31 13:36:56 +02:00
|
|
|
|
|
|
|
if (src == profile)
|
|
|
|
{
|
|
|
|
primaire.openAuthorProfile();
|
2022-04-12 08:37:39 +02:00
|
|
|
return;
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
2022-04-12 08:37:39 +02:00
|
|
|
|
|
|
|
if (src == favouriteBoost)
|
2021-07-31 13:36:56 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
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;
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
if (src == replyMisc)
|
|
|
|
{
|
|
|
|
if (command.startsWith("reply")) primaire.reply();
|
|
|
|
return;
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
2022-04-12 08:37:39 +02:00
|
|
|
|
|
|
|
if (src == nextPrev)
|
2021-07-31 13:36:56 +02:00
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
if (command.equals("next"))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-31 13:36:56 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
}
|
|
|
|
return;
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
2022-04-12 08:37:39 +02:00
|
|
|
|
|
|
|
if (src == media)
|
2021-07-31 13:36:56 +02:00
|
|
|
{
|
|
|
|
primaire.openMedia();
|
2022-04-12 08:37:39 +02:00
|
|
|
return;
|
2021-07-31 13:36:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
protected void
|
|
|
|
paintComponent(Graphics g)
|
|
|
|
{
|
2022-04-12 08:37:39 +02:00
|
|
|
g.clearRect(0, 0, getWidth(), getHeight());
|
|
|
|
|
|
|
|
Font f1 = new Font("IPAGothic", Font.PLAIN, 16);
|
|
|
|
Font f2 = new Font("IPAGothic", Font.PLAIN, 14);
|
|
|
|
FontMetrics fm1 = g.getFontMetrics(f1);
|
|
|
|
FontMetrics fm2 = g.getFontMetrics(f2);
|
|
|
|
|
|
|
|
int x1 = 60;
|
|
|
|
int x4 = getWidth() - 10;
|
|
|
|
int x2 = x4 - fm2.stringWidth(date);
|
|
|
|
int x3 = x4 - fm1.stringWidth(time);
|
|
|
|
int y1 = 10;
|
|
|
|
int y2 = y1 + fm2.getHeight();
|
|
|
|
int y3 = y2 + fm1.getHeight();
|
|
|
|
int y4 = y3 + 8;
|
|
|
|
|
|
|
|
Shape defaultClip = g.getClip();
|
|
|
|
g.setClip(x1, y1, Math.min(x2, x3) - 8 - x1, y4 - y1);
|
|
|
|
// First time I've used this method..
|
|
|
|
// Cause, clearRect is not working.
|
|
|
|
g.setFont(f2);
|
|
|
|
g.drawString(authorId, x1, y2);
|
|
|
|
g.setFont(f1);
|
|
|
|
g.drawString(authorName, x1, y3);
|
|
|
|
g.setClip(defaultClip);
|
|
|
|
|
|
|
|
g.setFont(f2);
|
|
|
|
g.drawString(date, x2, y2);
|
|
|
|
g.setFont(f1);
|
|
|
|
g.drawString(time, x3, y3);
|
|
|
|
|
|
|
|
int y = y4;
|
|
|
|
for (String line: split(text, 40)) {
|
|
|
|
y += fm1.getHeight();
|
2021-07-31 13:28:46 +02:00
|
|
|
g.drawString(line, x1, y);
|
2021-07-16 11:46:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
// - -%- -
|
|
|
|
|
|
|
|
private static List<String>
|
2021-07-16 11:46:17 +02:00
|
|
|
split(String string, int lineLength)
|
|
|
|
{
|
|
|
|
List<String> returnee = new ArrayList<>();
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
StringBuilder line = new StringBuilder();
|
|
|
|
for (String word: string.split(" "))
|
|
|
|
{
|
|
|
|
if (word.length() >= lineLength) {
|
|
|
|
word = word.substring(0, lineLength - 4) + "...";
|
|
|
|
}
|
|
|
|
if (word.equals("\n")) {
|
|
|
|
returnee.add(empty(line));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.length() + word.length() > lineLength) {
|
|
|
|
returnee.add(empty(line));
|
|
|
|
}
|
|
|
|
line.append(word);
|
|
|
|
line.append(" ");
|
2021-07-16 11:46:17 +02:00
|
|
|
}
|
2022-04-12 08:37:39 +02:00
|
|
|
returnee.add(empty(line));
|
2021-07-16 11:46:17 +02:00
|
|
|
|
|
|
|
return returnee;
|
2021-07-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
private static String
|
|
|
|
empty(StringBuilder b)
|
|
|
|
{
|
|
|
|
String s = b.toString();
|
|
|
|
b.delete(0, b.length());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
// ---%-@-%---
|
|
|
|
|
2021-07-31 13:36:56 +02:00
|
|
|
PostComponent(PostWindow primaire)
|
2021-07-16 01:07:23 +02:00
|
|
|
{
|
2021-07-31 13:36:56 +02:00
|
|
|
this.primaire = primaire;
|
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
authorName = authorId = time = text = "";
|
|
|
|
|
|
|
|
Dimension buttonSize = new Dimension(20, 40);
|
2021-07-16 11:46:17 +02:00
|
|
|
|
2021-07-31 13:28:46 +02:00
|
|
|
profile = new JButton("P");
|
2022-04-12 08:37:39 +02:00
|
|
|
profile.setPreferredSize(buttonSize);
|
|
|
|
profile.setMargin(null);
|
2021-07-31 13:36:56 +02:00
|
|
|
profile.addActionListener(this);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
favouriteBoost = new TwoToggleButton("favourite", "boost");
|
2021-07-31 13:36:56 +02:00
|
|
|
favouriteBoost.addActionListener(this);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
replyMisc = new TwoToggleButton("reply", "misc");
|
2021-07-31 13:36:56 +02:00
|
|
|
replyMisc.addActionListener(this);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
nextPrev = new TwoToggleButton("next", "prev");
|
2021-07-31 13:36:56 +02:00
|
|
|
nextPrev.addActionListener(this);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
media = new JButton("media");
|
|
|
|
media.setPreferredSize(buttonSize);
|
|
|
|
media.setMargin(null);
|
2021-07-31 13:36:56 +02:00
|
|
|
media.addActionListener(this);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2022-04-12 08:37:39 +02:00
|
|
|
Box ibuttons = Box.createVerticalBox();
|
|
|
|
ibuttons.setOpaque(false);
|
|
|
|
ibuttons.add(profile);
|
|
|
|
ibuttons.add(Box.createVerticalStrut(8));
|
|
|
|
ibuttons.add(favouriteBoost);
|
|
|
|
ibuttons.add(Box.createVerticalStrut(8));
|
|
|
|
ibuttons.add(replyMisc);
|
|
|
|
ibuttons.add(Box.createVerticalStrut(8));
|
|
|
|
ibuttons.add(nextPrev);
|
|
|
|
ibuttons.add(Box.createVerticalStrut(8));
|
|
|
|
ibuttons.add(media);
|
|
|
|
ibuttons.setMaximumSize(ibuttons.getPreferredSize());
|
|
|
|
Box buttons = Box.createVerticalBox();
|
|
|
|
buttons.setOpaque(false);
|
|
|
|
buttons.add(ibuttons);
|
|
|
|
buttons.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
|
|
|
|
|
|
setLayout(new BorderLayout());
|
|
|
|
add(buttons, BorderLayout.WEST);
|
2021-07-31 13:28:46 +02:00
|
|
|
|
2021-07-16 11:46:17 +02:00
|
|
|
setFont(getFont().deriveFont(14f));
|
2021-07-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class
|
|
|
|
RepliesComponent extends JPanel {
|
|
|
|
|
2021-07-29 11:26:16 +02:00
|
|
|
private List<RepliesComponent.Reply>
|
|
|
|
replies;
|
|
|
|
|
|
|
|
// - -%- -
|
2021-07-16 01:07:23 +02:00
|
|
|
|
|
|
|
private JButton
|
|
|
|
prevPage, nextPage;
|
|
|
|
|
|
|
|
private JLabel
|
|
|
|
pageLabel;
|
|
|
|
|
2021-07-29 10:03:32 +02:00
|
|
|
private ReplyPreviewComponent[]
|
|
|
|
previews;
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
// ---%-@-%---
|
|
|
|
|
|
|
|
public void
|
2021-07-29 11:26:16 +02:00
|
|
|
setReplies(List<RepliesComponent.Reply> replies)
|
2021-07-16 01:07:23 +02:00
|
|
|
{
|
2021-07-29 11:26:16 +02:00
|
|
|
assert replies != null;
|
|
|
|
this.replies = replies;
|
2021-07-29 10:03:32 +02:00
|
|
|
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 10:03:32 +02:00
|
|
|
{
|
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;
|
|
|
|
|
2021-07-29 10:03:32 +02:00
|
|
|
ReplyPreviewComponent preview = previews[o];
|
2021-07-29 11:26:16 +02:00
|
|
|
Reply reply = replies.get(o);
|
2022-04-12 08:37:39 +02:00
|
|
|
preview.setAuthorName(reply.author);
|
2021-07-29 11:26:16 +02:00
|
|
|
preview.setText(reply.text);
|
|
|
|
preview.setVisible(true);
|
|
|
|
}
|
2021-07-29 10:03:32 +02:00
|
|
|
|
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 10:03:32 +02:00
|
|
|
}
|
2021-07-29 11:26:16 +02:00
|
|
|
|
|
|
|
int pages = 1 + ((replies.size() - 1) / 8);
|
|
|
|
pageLabel.setText(pageNumber + "/" + pages);
|
2021-07-29 11:40:52 +02:00
|
|
|
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;
|
|
|
|
|
2021-07-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---%-@-%---
|
|
|
|
|
|
|
|
RepliesComponent()
|
|
|
|
{
|
|
|
|
prevPage = new JButton("<");
|
|
|
|
nextPage = new JButton(">");
|
|
|
|
prevPage.setEnabled(false);
|
|
|
|
nextPage.setEnabled(false);
|
|
|
|
|
2021-07-29 11:26:16 +02:00
|
|
|
pageLabel = new JLabel();
|
2021-07-16 01:07:23 +02:00
|
|
|
|
|
|
|
Box bottom = Box.createHorizontalBox();
|
|
|
|
bottom.add(Box.createGlue());
|
|
|
|
bottom.add(prevPage);
|
2021-07-17 11:37:00 +02:00
|
|
|
bottom.add(Box.createHorizontalStrut(8));
|
2021-07-16 01:07:23 +02:00
|
|
|
bottom.add(pageLabel);
|
2021-07-17 11:37:00 +02:00
|
|
|
bottom.add(Box.createHorizontalStrut(8));
|
2021-07-16 01:07:23 +02:00
|
|
|
bottom.add(nextPage);
|
|
|
|
|
|
|
|
JPanel centre = new JPanel();
|
2021-07-17 11:37:00 +02:00
|
|
|
centre.setOpaque(false);
|
2021-07-16 01:07:23 +02:00
|
|
|
centre.setLayout(new GridLayout(0, 1, 0, 2));
|
2021-07-29 10:03:32 +02:00
|
|
|
|
|
|
|
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-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 11:37:00 +02:00
|
|
|
setLayout(new BorderLayout(0, 8));
|
2021-07-16 01:07:23 +02:00
|
|
|
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-16 01:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
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
|
2022-04-12 08:37:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|