biskuteri-cafe-JKomasto2/PostWindow.java
2021-07-16 05:46:17 -04:00

210 lines
4.6 KiB
Java

import javax.swing.JFrame;
import javax.swing.JPanel;
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;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;
import java.util.ArrayList;
class
PostWindow extends JFrame {
private Post
post;
// - -%- -
private PostComponent
postDisplay;
private RepliesComponent
repliesDisplay;
// ---%-@-%---
PostWindow()
{
getContentPane().setPreferredSize(new Dimension(360, 270));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationByPlatform(true);
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();
JMenu postMenu = new JMenu("Post");
postMenu.add("Favourite");
postMenu.add("Reply");
JMenu displayMenu = new JMenu("Display");
displayMenu.add("Post");
displayMenu.add("Replies");
JMenuBar menuBar = new JMenuBar();
menuBar.add(postMenu);
menuBar.add(displayMenu);
setJMenuBar(menuBar);
setContentPane(postDisplay);
}
}
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)
{
int lineHeight = 20;
g.drawString(author, 0, lineHeight);
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()
{
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);
bottom.add(pageLabel);
bottom.add(nextPage);
JPanel centre = new JPanel();
centre.setLayout(new GridLayout(0, 1, 0, 2));
for (int n = 8; n > 0; --n) {
String label = "Eggplant -%- A short reply..";
JButton reply = new JButton(label);
// We likely need a custom class..
centre.add(reply);
}
setLayout(new BorderLayout(4, 4));
add(centre, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
}
}