biskuteri-cafe-JKomasto2/ComposeWindow.java
2021-07-28 17:55:30 -04:00

256 lines
5.6 KiB
Java

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 {
private Composition
composition;
private ComposeComponent
display;
// ---%-@-%---
public void
startNewPost()
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = null;
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();
}
// - -%- -
private void
syncDisplayToComposition()
{
display.setText(composition.text);
display.setVisibility(stringFor(composition.visibility));
}
private void
syncCompositionToDisplay()
{
composition.text = display.getText();
composition.visibility =
visibilityFrom(display.getVisibility());
}
// ---%-@-%---
ComposeWindow()
{
getContentPane().setPreferredSize(new Dimension(360, 270));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
display = new ComposeComponent(this);
startNewPost();
setContentPane(display);
}
// - -%- -
private static final String
stringFor(PostVisibility visibility)
{
switch (visibility)
{
case PUBLIC: return "Public";
case LOCAL: return "Local";
case FOLLOWERS: return "Followers";
case MENTIONED: return "Mentioned";
}
assert false;
return null;
}
private static final PostVisibility
visibilityFrom(String string)
{
if (string.equals("Public"))
return PostVisibility.PUBLIC;
if (string.equals("Local"))
return PostVisibility.LOCAL;
if (string.equals("Followers"))
return PostVisibility.FOLLOWERS;
if (string.equals("Mentioned"))
return PostVisibility.MENTIONED;
assert false;
return null;
}
}
class
ComposeComponent extends JPanel
implements ActionListener {
private ComposeWindow
primaire;
// - -%- -
private JTextArea
text;
private JComboBox<String>
visibility;
private JButton
submit;
// - -%- -
private static final Cursor
WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
// ---%-@-%---
public void
setText(String text)
{
this.text.setText(text);
}
public void
setVisibility(String visibility)
{
if (visibility.equals("Public"))
this.visibility.setSelectedIndex(0);
else if (visibility.equals("Local"))
this.visibility.setSelectedIndex(1);
else if (visibility.equals("Followers"))
this.visibility.setSelectedIndex(2);
else if (visibility.equals("Mentioned"))
this.visibility.setSelectedIndex(3);
}
public String
getText()
{
return text.getText();
}
public String
getVisibility()
{
return (String)visibility.getSelectedItem();
}
public void
setSubmitting(boolean submitting)
{
if (submitting)
{
text.setEnabled(false);
visibility.setEnabled(false);
submit.setEnabled(false);
setCursor(WAIT_CURSOR);
}
else
{
text.setEnabled(true);
visibility.setEnabled(true);
submit.setEnabled(true);
setCursor(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"
// Where should we be saving strings..
});
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));
add(text, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
}
}