biskuteri-cafe-JKomasto2/ComposeWindow.java

256 lines
5.6 KiB
Java
Raw Normal View History

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
2021-07-28 22:23:42 +02:00
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.Dimension;
2021-07-28 22:23:42 +02:00
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Cursor;
class
ComposeWindow extends JFrame {
2021-07-17 11:46:27 +02:00
private Composition
composition;
private ComposeComponent
display;
2021-07-17 11:46:27 +02:00
// ---%-@-%---
public void
2021-07-28 22:23:42 +02:00
startNewPost()
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = null;
syncDisplayToComposition();
2021-07-28 22:23:42 +02:00
}
public void
startReply(String postId)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.PUBLIC;
composition.replyToPostId = postId;
syncDisplayToComposition();
2021-07-28 22:23:42 +02:00
}
/*
* 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.
*/
2021-07-28 22:23:42 +02:00
public void
submit()
2021-07-17 11:46:27 +02:00
{
syncCompositionToDisplay();
2021-07-28 22:23:42 +02:00
display.setSubmitting(true);
2021-07-28 22:23:42 +02:00
// Perform fancy submission here..
display.setSubmitting(false);
// (悪) Are we going to block the EDT..?
2021-07-28 22:23:42 +02:00
startNewPost();
}
// - -%- -
2021-07-28 22:23:42 +02:00
private void
syncDisplayToComposition()
{
display.setText(composition.text);
display.setVisibility(stringFor(composition.visibility));
}
private void
syncCompositionToDisplay()
{
composition.text = display.getText();
composition.visibility =
visibilityFrom(display.getVisibility());
2021-07-17 11:46:27 +02:00
}
// ---%-@-%---
ComposeWindow()
{
2021-07-16 11:46:17 +02:00
getContentPane().setPreferredSize(new Dimension(360, 270));
pack();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2021-07-28 22:23:42 +02:00
display = new ComposeComponent(this);
2021-07-17 12:03:59 +02:00
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
2021-07-28 22:23:42 +02:00
ComposeComponent extends JPanel
implements ActionListener {
private ComposeWindow
primaire;
// - -%- -
private JTextArea
text;
2021-07-28 22:23:42 +02:00
private JComboBox<String>
visibility;
private JButton
submit;
// - -%- -
private static final Cursor
WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
// ---%-@-%---
2021-07-28 22:23:42 +02:00
public void
setText(String text)
{
this.text.setText(text);
}
2021-07-28 22:23:42 +02:00
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);
2021-07-28 22:23:42 +02:00
}
public String
getText()
{
return text.getText();
}
2021-07-28 22:23:42 +02:00
public String
getVisibility()
2021-07-28 22:23:42 +02:00
{
return (String)visibility.getSelectedItem();
}
public void
setSubmitting(boolean submitting)
2021-07-28 22:23:42 +02:00
{
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);
}
2021-07-28 22:23:42 +02:00
}
// - -%- -
2021-07-28 22:23:42 +02:00
public void
actionPerformed(ActionEvent eA) { primaire.submit(); }
// ---%-@-%---
ComposeComponent(ComposeWindow primaire)
{
this.primaire = primaire;
text = new JTextArea();
2021-07-28 22:23:42 +02:00
visibility = new JComboBox<>(new String[] {
"Public",
"Local",
"Followers",
"Mentioned"
// Where should we be saving strings..
2021-07-28 22:23:42 +02:00
});
visibility.setPreferredSize(new Dimension(64, 24));
submit = new JButton("Submit");
2021-07-28 22:23:42 +02:00
submit.addActionListener(this);
Box bottom = Box.createHorizontalBox();
bottom.add(Box.createGlue());
2021-07-28 22:23:42 +02:00
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));
}
}