biskuteri-cafe-JKomasto2/TimelineWindow.java
2021-07-31 07:28:46 -04:00

329 lines
7.5 KiB
Java

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JSeparator;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Insets;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class
TimelineWindow extends JFrame
implements ActionListener {
private Timeline
timeline;
// - -%- -
private TimelineComponent
display;
private JMenuItem
openHome,
// umm, what about the timeline that's like, notes that your
// post was favourited or replied to? those aren't messages..
openMessages,
openLocal,
openFederated,
createPost,
openAutoPostView,
quit;
private JMenuItem
flipToNewestPost;
// ---%-@-%---
public void
setTimeline(Timeline timeline)
{
if (timeline == null)
{
timeline = new Timeline();
timeline.type = TimelineType.LOCAL;
}
assert timeline != null;
this.timeline = timeline;
// Here, we should request for posts for the timeline.
// And pass data to TimelineComponent.
}
public void
nextPage()
{
}
public void
previousPage()
{
}
// - -%- -
public void
actionPerformed(ActionEvent eA)
{
Object src = eA.getSource();
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).
*/
}
}
// ---%-@-%---
TimelineWindow()
{
getContentPane().setPreferredSize(new Dimension(300, 400));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
openHome = new JMenuItem("Open home timeline");
openFederated = new JMenuItem("Open federated timeline");
createPost = new JMenuItem("Create a post");
openAutoPostView = new JMenuItem("Open auto post view");
quit = new JMenuItem("Quit");
openHome.addActionListener(this);
openFederated.addActionListener(this);
createPost.addActionListener(this);
openAutoPostView.addActionListener(this);
quit.addActionListener(this);
flipToNewestPost = new JMenuItem("Flip to newest post");
flipToNewestPost.addActionListener(this);
JMenu programMenu = new JMenu("Program");
programMenu.add(openHome);
programMenu.add(openFederated);
programMenu.add(new JSeparator());
programMenu.add(createPost);
programMenu.add(openAutoPostView);
programMenu.add(new JSeparator());
programMenu.add(quit);
JMenu timelineMenu = new JMenu("Timeline");
timelineMenu.add(flipToNewestPost);
JMenuBar menuBar = new JMenuBar();
menuBar.add(programMenu);
menuBar.add(timelineMenu);
setJMenuBar(menuBar);
display = new TimelineComponent(this);
setContentPane(display);
}
}
class
TimelineComponent extends JPanel
implements ActionListener {
private TimelineWindow
primaire;
private final List<Post>
posts = new ArrayList<>();
// - -%- -
private JButton
next, prev;
private JLabel
pageLabel;
private final List<PostPreviewComponent>
postPreviews = new ArrayList<>();
// ---%-@-%---
public void
setPosts(List<Post> posts)
{
if (posts == null)
{
posts = new ArrayList<>();
// Insert sample timeline posts here
}
this.posts.clear();
this.posts.addAll(posts);
syncPreviewsToPosts();
}
public void
setPageLabel(String label)
{
assert label != null;
pageLabel.setText("" + label);
}
public void
setNextPageAvailable(boolean available)
{
next.setEnabled(available);
}
public void
setPreviousPageAvailable(boolean available)
{
prev.setEnabled(available);
}
// - -%- -
private void
syncPreviewsToPosts()
{
for (PostPreviewComponent p: postPreviews)
{
p.setTopLeft("Top left");
p.setTopRight("Top right");
p.setBottom("Bottom");
// (In reality we are supposed to map with posts)
}
}
public void
actionPerformed(ActionEvent eA)
{
Object src = eA.getSource();
if (src == next) primaire.nextPage();
else if (src == prev) primaire.previousPage();
/*
* 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.
*/
}
// ---%-@-%---
TimelineComponent(TimelineWindow primaire)
{
this.primaire = primaire;
prev = new JButton("<");
next = new JButton(">");
prev.setEnabled(false);
next.setEnabled(false);
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 = 8; n > 0; --n)
{
PostPreviewComponent p = new PostPreviewComponent();
centre.add(p, constraints);
postPreviews.add(p);
}
setLayout(new BorderLayout());
add(centre, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
setPosts(null);
}
}
class
PostPreviewComponent extends JComponent {
private JLabel
topLeft, topRight, bottom;
// ---%-@-%---
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
PostPreviewComponent()
{
Font font;
topLeft = new JLabel();
font = topLeft.getFont();
topLeft.setFont(font.deriveFont(Font.PLAIN, 12f));
setOpaque(false);
topRight = new JLabel();
topRight.setHorizontalAlignment(JLabel.RIGHT);
font = topRight.getFont();
topRight.setFont(font.deriveFont(Font.ITALIC, 12f));
setOpaque(false);
bottom = new JLabel();
font = bottom.getFont();
bottom.setFont(font.deriveFont(Font.PLAIN, 16f));
bottom.setOpaque(false);
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(Box.createGlue());
top.add(topRight);
setLayout(new BorderLayout());
add(top, BorderLayout.NORTH);
add(bottom, BorderLayout.SOUTH);
}
}