biskuteri-cafe-JKomasto2/ComposeWindow.java

231 lines
5.0 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
setComposition(Composition composition)
{
if (composition == null)
{
composition = new Composition();
composition.text = "";
composition.visibility = PostVisibility.MENTIONED;
composition.replyToPostId = null;
}
this.composition = composition;
syncDisplayToComposition();
}
public void
submit()
{
syncCompositionToDisplay();
display.setSubmitting(true);
// Perform fancy submission here..
display.setSubmitting(false);
// (悪) Are we going to block the EDT..?
}
// - -%- -
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);
this.setComposition(null);
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));
}
}