Added rudimentary connections for PostWindow

This commit is contained in:
Snowyfox 2021-07-29 04:03:32 -04:00
parent a93dc7b488
commit 467e168557
3 changed files with 155 additions and 86 deletions

View File

@ -24,51 +24,28 @@ ComposeWindow extends JFrame {
// ---%-@-%---
public void
startNewPost()
setComposition(Composition composition)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = null;
if (composition == null)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.MENTIONED;
composition.replyToPostId = null;
}
this.composition = composition;
syncDisplayToComposition();
}
public void
startReply(String postId)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = postId;
syncDisplayToComposition();
}
/*
* Are we supposed to be reusable? Or should we be more like
* a dialog box, constructed as a new post or reply, and
* locked once submitted.
*
* We seem to accept new compositions after we're constructed,
* but does this make sense for our usage scenarios..?
*
* For example, if not, then we don't need to sync the
* display to a composition first, or even maintain a
* composition, instead setting anew in #submit.
*/
public void
submit()
{
syncCompositionToDisplay();
display.setSubmitting(true);
// Perform fancy submission here..
display.setSubmitting(false);
// () Are we going to block the EDT..?
startNewPost();
}
// - -%- -
@ -97,8 +74,8 @@ ComposeWindow extends JFrame {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
display = new ComposeComponent(this);
this.setComposition(null);
startNewPost();
setContentPane(display);
}
@ -115,8 +92,7 @@ ComposeWindow extends JFrame {
case MENTIONED: return "Mentioned";
}
assert false;
return null;
assert false; return null;
}
private static final PostVisibility
@ -131,8 +107,7 @@ ComposeWindow extends JFrame {
if (string.equals("Mentioned"))
return PostVisibility.MENTIONED;
assert false;
return null;
assert false; return null;
}
}

View File

@ -16,8 +16,8 @@ JKomasto {
public
JKomasto()
{
new TimelineWindow().setVisible(true);
new ComposeWindow().setVisible(true);
//new TimelineWindow().setVisible(true);
//new ComposeWindow().setVisible(true);
new PostWindow().setVisible(true);
}

View File

@ -3,19 +3,24 @@ import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;
class
PostWindow extends JFrame {
PostWindow extends JFrame
implements ActionListener {
private Post
post;
@ -28,28 +33,58 @@ PostWindow extends JFrame {
private RepliesComponent
repliesDisplay;
// - -%- -
private static final Post
NULL_POST = new Post();
{
NULL_POST.text = "This is a sample post.";
NULL_POST.authorId = "snowyfox@biskuteri.cafe";
NULL_POST.authorName = "snowyfox";
NULL_POST.date = "0712hrs, 17 July";
NULL_POST.visibility = PostVisibility.MENTIONED;
NULL_POST.postId = "000000000";
NULL_POST.boosted = false;
NULL_POST.favourited = true;
}
// ---%-@-%---
public void
setPost(Post post)
{
assert post != null;
if (post == null)
{
post = new Post();
post.text = "This is a sample post.";
post.authorId = "snowyfox@biskuteri.cafe";
post.authorName = "snowyfox";
post.date = "0712hrs, 17 July";
post.visibility = PostVisibility.MENTIONED;
post.postId = "000000000";
post.boosted = false;
post.favourited = true;
}
this.post = post;
postDisplay.setAuthor(post.authorName);
postDisplay.setTime(post.date);
postDisplay.setText(post.text);
// And, reply display too, later.
}
// - -%- -
public void
actionPerformed(ActionEvent eA)
{
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();
}
}
// ---%-@-%---
@ -62,37 +97,42 @@ PostWindow extends JFrame {
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();
{
List<String> authors = new ArrayList<>();
List<String> texts = new ArrayList<>();
authors.add("Black tea");
authors.add("Green tea");
authors.add("White tea");
texts.add("Rich..");
texts.add("Clean!");
texts.add("Myu..");
repliesDisplay.setReplies(authors, texts);
}
setPost(null);
JMenu postMenu = new JMenu("Post");
postMenu.add("Favourite");
postMenu.add("Reply");
addToMenu(postMenu, "Favourite");
addToMenu(postMenu, "Reply");
JMenu displayMenu = new JMenu("Display");
displayMenu.add("Post");
displayMenu.add("Replies");
addToMenu(displayMenu, "Post");
addToMenu(displayMenu, "Replies");
JMenuBar menuBar = new JMenuBar();
menuBar.add(postMenu);
menuBar.add(displayMenu);
setJMenuBar(menuBar);
setContentPane(postDisplay);
//setContentPane(repliesDisplay);
}
private void
addToMenu(JMenu menu, String text)
{
JMenuItem item = new JMenuItem(text);
item.addActionListener(this);
menu.add(item);
}
}
@ -134,12 +174,17 @@ PostComponent extends JPanel {
paintComponent(Graphics g)
{
int lineHeight = 20;
g.drawString(author, 0, lineHeight);
int y = lineHeight;
FontMetrics met = g.getFontMetrics();
g.drawString(author, 0, lineHeight);
int t1 = getWidth() - met.stringWidth(time);
g.drawString(time, t1, lineHeight);
int t2 = lineHeight;
for (String line: split(text, 48)) {
y += lineHeight;
g.drawString(line, 0, y);
t2 += lineHeight;
g.drawString(line, 0, t2);
}
}
@ -184,15 +229,62 @@ RepliesComponent extends JPanel {
private JLabel
pageLabel;
private ReplyPreviewComponent[]
previews;
// ---%-@-%---
public void
setReplies(List<String> authors, List<String> texts)
{
assert authors != null;
assert texts != null;
assert authors.size() == texts.size();
this.authors.clear();
this.authors.addAll(authors);
this.texts.clear();
this.texts.addAll(texts);
displayPage(1);
int currentPage = authors.isEmpty() ? 0 : 1;
int pages = (7 + authors.size()) / 8;
// (0 + 7) / 8 = 0
// (1 + 7) / 8 = 1
// (8 + 7) / 8 = 1
// (9 + 7) / 8 = 2
// (32 + 7) / 8 = 4
pageLabel.setText(currentPage + "/" + pages);
}
// - -%- -
private void
displayPage(int pageNumber)
{
int oS = (pageNumber - 1) * 8;
if (oS >= authors.size()) return;
// () Quietly fail?
int oE = Math.min(oS + 8, authors.size());
List<String> authors = this.authors.subList(oS, oE);
List<String> texts = this.texts.subList(oS, oE);
int l = oE - oS;
for (int o = 0; o < 8; ++o)
{
ReplyPreviewComponent preview = previews[o];
if (o >= l) // That doesn't seem right..
{
preview.setVisible(false);
}
else
{
preview.setAuthor(authors.get(oS + o));
preview.setText(texts.get(oS + o));
preview.setVisible(true);
}
}
}
// ---%-@-%---
@ -220,11 +312,13 @@ RepliesComponent extends JPanel {
JPanel centre = new JPanel();
centre.setOpaque(false);
centre.setLayout(new GridLayout(0, 1, 0, 2));
for (int n = 8; n > 0; --n) {
ReplyPreviewComponent reply = new ReplyPreviewComponent();
reply.setAuthor("Eggplant");
reply.setText("bobofish..");
centre.add(reply);
previews = new ReplyPreviewComponent[8];
for (int o = 0; o < previews.length; ++o)
{
previews[o] = new ReplyPreviewComponent();
previews[o].setVisible(false);
centre.add(previews[o]);
}
setLayout(new BorderLayout(0, 8));