Tried to setup composition code.

This commit is contained in:
Snowyfox 2021-07-28 16:23:42 -04:00
parent 2209236ead
commit 97f4463b43

View File

@ -2,11 +2,15 @@
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Cursor;
class
ComposeWindow extends JFrame {
@ -14,8 +18,6 @@ ComposeWindow extends JFrame {
private Composition
composition;
// - -%- -
private ComposeComponent
display;
@ -23,30 +25,74 @@ ComposeWindow extends JFrame {
private static final Composition
NULL_COMPOSITION = new Composition();
{
NULL_COMPOSITION.text = "This is a sample composition..!";
NULL_COMPOSITION.visibility = PostVisibility.PUBLIC;
NULL_COMPOSITION.replyToPostId = null;
// Static final does not like structs very much..
}
// ---%-@-%---
public void
setComposition(Composition composition)
startNewPost()
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = null;
}
public void
startReply(String postId)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = postId;
}
public void
submit()
{
assert composition != null;
this.composition = composition;
composition.text = display.getText();
String visibility = display.getSelectedVisibility();
if (visibility.equals("Public"))
composition.visibility = PostVisibility.PUBLIC;
else if (visibility.equals("Local"))
composition.visibility = PostVisibility.LOCAL;
else if (visibility.equals("Followers"))
composition.visibility = PostVisibility.FOLLOWERS;
else if (visibility.equals("Mentioned"))
composition.visibility = PostVisibility.MENTIONED;
else
assert false;
display.match(composition); // (For good measure)
display.setEnabled(false);
// Perform fancy submission
display.setEnabled(true);
// () Are we going to block the EDT?
}
// ---%-@-%---
ComposeWindow()
{
//startNewPost();
composition = NULL_COMPOSITION;
getContentPane().setPreferredSize(new Dimension(360, 270));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
display = new ComposeComponent();
display = new ComposeComponent(this);
display.match(composition);
setContentPane(display);
}
@ -56,24 +102,102 @@ ComposeWindow extends JFrame {
class
ComposeComponent extends JPanel {
ComposeComponent extends JPanel
implements ActionListener {
private ComposeWindow
primaire;
// - -%- -
private JTextArea
text;
private JComboBox<String>
visibility;
private JButton
submit;
// ---%-@-%---
ComposeComponent()
public void
match(Composition composition)
{
text.setText(composition.text);
switch (composition.visibility)
{
case PUBLIC:
visibility.setSelectedIndex(0);
break;
case LOCAL:
visibility.setSelectedIndex(1);
break;
case FOLLOWERS:
visibility.setSelectedIndex(2);
break;
case MENTIONED:
visibility.setSelectedIndex(3);
break;
}
// And we haven't implemented the reply indicator yet.
}
public String
getText() { return text.getText(); }
public String
getSelectedVisibility()
{
return (String)visibility.getSelectedItem();
}
public void
setEnabled(boolean enabled)
{
text.setEnabled(enabled);
visibility.setEnabled(enabled);
submit.setEnabled(enabled);
// If we're disabled, presumably it's because we are
// waiting for an existing submission to complete.
setCursor(
!enabled
? Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)
: null
);
}
public void
actionPerformed(ActionEvent eA) { primaire.submit(); }
// ---%-@-%---
ComposeComponent(ComposeWindow primaire)
{
this.primaire = primaire;
text = new JTextArea();
visibility = new JComboBox<>(new String[] {
"Public",
"Local",
"Followers",
"Mentioned"
// This might be hard to localise.. And, should we
// save the strings in the enum instead?
});
visibility.setPreferredSize(new Dimension(64, 24));
submit = new JButton("Submit");
submit.addActionListener(this);
Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue());
bottom.add(visibility);
bottom.add(Box.createHorizontalStrut(8));
bottom.add(submit);
setLayout(new BorderLayout(0, 8));