biskuteri-cafe-JKomasto2/PostWindow.java

268 lines
5.6 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.JButton;
import javax.swing.JLabel;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.Graphics;
2021-07-16 00:37:03 +02:00
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;
import java.util.ArrayList;
2021-07-16 00:37:03 +02:00
class
PostWindow extends JFrame {
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
// ---%-@-%---
PostWindow()
{
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-16 11:46:17 +02:00
postDisplay = new PostComponent();
postDisplay.setAuthor("杉原宇砂鷲");
postDisplay.setTime("いつ");
//postDisplay.setText("これはふさわしいですね。");
postDisplay.setText(
"While it is true that a formal science process is "
+ "not involved (recall that not just scientists, "
+ "but also psychologists, etc. follow it), "
+ "individuals' solutions can be trusted to be "
+ "the most focused on their unique needs. "
+ "If successful, the individual's solution can "
+ "be a formidable alternative to employing "
+ "assistance by professionals. Purportedly."
);
// (Don't take that seriously, I just wrote down
// random topical text)
repliesDisplay = new RepliesComponent();
2021-07-16 00:37:03 +02:00
JMenu postMenu = new JMenu("Post");
postMenu.add("Favourite");
postMenu.add("Reply");
2021-07-16 11:46:17 +02:00
JMenu displayMenu = new JMenu("Display");
displayMenu.add("Post");
displayMenu.add("Replies");
2021-07-16 00:37:03 +02:00
JMenuBar menuBar = new JMenuBar();
menuBar.add(postMenu);
2021-07-16 11:46:17 +02:00
menuBar.add(displayMenu);
2021-07-16 00:37:03 +02:00
setJMenuBar(menuBar);
2021-07-17 11:37:00 +02:00
//setContentPane(postDisplay);
setContentPane(repliesDisplay);
2021-07-16 00:37:03 +02:00
}
}
class
PostComponent extends JPanel {
private String
author, time, text;
// ---%-@-%---
public void
setAuthor(String author)
{
assert author != null;
this.author = author;
}
public void
setTime(String time)
{
assert time != null;
this.time = time;
}
public void
setText(String text)
{
assert text != null;
this.text = text;
}
// - -%- -
protected void
paintComponent(Graphics g)
{
2021-07-16 11:46:17 +02:00
int lineHeight = 20;
g.drawString(author, 0, lineHeight);
2021-07-16 11:46:17 +02:00
int y = lineHeight;
for (String line: split(text, 48)) {
y += lineHeight;
g.drawString(line, 0, y);
}
}
private List<String>
split(String string, int lineLength)
{
List<String> returnee = new ArrayList<>();
int start, max = string.length();
for (start = 0; start < max; start += lineLength) {
returnee.add(string.substring(
start,
Math.min(max, start + lineLength)
));
}
return returnee;
}
// ---%-@-%---
PostComponent()
{
2021-07-16 11:46:17 +02:00
author = time = text = "";
setFont(getFont().deriveFont(14f));
}
}
class
RepliesComponent extends JPanel {
private List<String>
authors, texts;
private JButton
prevPage, nextPage;
private JLabel
pageLabel;
// ---%-@-%---
public void
setReplies(List<String> authors, List<String> texts)
{
this.authors.clear();
this.authors.addAll(authors);
this.texts.clear();
this.texts.addAll(texts);
}
// ---%-@-%---
RepliesComponent()
{
authors = new ArrayList<>();
texts = new ArrayList<>();
prevPage = new JButton("<");
nextPage = new JButton(">");
prevPage.setEnabled(false);
nextPage.setEnabled(false);
pageLabel = new JLabel("0 / 0");
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));
for (int n = 8; n > 0; --n) {
2021-07-17 11:37:00 +02:00
ReplyPreviewComponent reply = new ReplyPreviewComponent();
reply.setAuthor("Eggplant");
reply.setText("bobofish..");
centre.add(reply);
}
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-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
setAuthor(String author)
{
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);
}
// ---%-@-%---
ReplyPreviewComponent() { }
}