mirror of
https://gitlab.com/biskuteri-cafe/JKomasto2.git
synced 2024-11-20 06:24:50 +01:00
307 lines
7.0 KiB
Java
Executable File
307 lines
7.0 KiB
Java
Executable File
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JTextArea;
|
|
import javax.swing.JTextField;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JButton;
|
|
import javax.swing.Box;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.JOptionPane;
|
|
import java.awt.BorderLayout;
|
|
import java.awt.Dimension;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.Cursor;
|
|
|
|
import cafe.biskuteri.hinoki.Tree;
|
|
import java.io.IOException;
|
|
|
|
class
|
|
ComposeWindow extends JFrame {
|
|
|
|
private JKomasto
|
|
primaire;
|
|
|
|
private MastodonApi
|
|
api;
|
|
|
|
// - -%- -
|
|
|
|
private Composition
|
|
composition;
|
|
|
|
private ComposeComponent
|
|
display;
|
|
|
|
// ---%-@-%---
|
|
|
|
public void
|
|
setComposition(Composition composition)
|
|
{
|
|
assert composition != null;
|
|
this.composition = composition;
|
|
syncDisplayToComposition();
|
|
}
|
|
|
|
public void
|
|
newComposition()
|
|
{
|
|
composition = new Composition();
|
|
composition.text = "";
|
|
composition.visibility = PostVisibility.MENTIONED;
|
|
composition.replyToPostId = null;
|
|
syncDisplayToComposition();
|
|
}
|
|
|
|
public void
|
|
submit()
|
|
{
|
|
syncCompositionToDisplay();
|
|
display.setSubmitting(true);
|
|
api.submit(
|
|
composition.text, composition.visibility,
|
|
composition.replyToPostId,
|
|
new RequestListener() {
|
|
|
|
public void
|
|
connectionFailed(IOException eIo)
|
|
{
|
|
JOptionPane.showMessageDialog(
|
|
ComposeWindow.this,
|
|
"Tried to submit post, failed..."
|
|
+ "\n" + eIo.getMessage()
|
|
);
|
|
}
|
|
|
|
public void
|
|
requestFailed(int httpCode, Tree<String> json)
|
|
{
|
|
JOptionPane.showMessageDialog(
|
|
ComposeWindow.this,
|
|
"Tried to submit post, failed..."
|
|
+ "\n" + json.get("error").value
|
|
+ "(HTTP error code: " + httpCode + ")"
|
|
);
|
|
}
|
|
|
|
public void
|
|
requestSucceeded(Tree<String> json)
|
|
{
|
|
newComposition();
|
|
}
|
|
|
|
}
|
|
);
|
|
display.setSubmitting(false);
|
|
}
|
|
|
|
// - -%- -
|
|
|
|
private void
|
|
syncDisplayToComposition()
|
|
{
|
|
display.setText(composition.text);
|
|
display.setReplyToPostId(composition.replyToPostId);
|
|
display.setVisibility(stringFor(composition.visibility));
|
|
}
|
|
|
|
private void
|
|
syncCompositionToDisplay()
|
|
{
|
|
composition.text = display.getText();
|
|
composition.visibility =
|
|
visibilityFrom(display.getVisibility());
|
|
composition.replyToPostId = display.getReplyToPostId();
|
|
}
|
|
|
|
// ---%-@-%---
|
|
|
|
ComposeWindow(JKomasto primaire)
|
|
{
|
|
super("Submit a new post");
|
|
this.primaire = primaire;
|
|
this.api = primaire.getMastodonApi();
|
|
|
|
getContentPane().setPreferredSize(new Dimension(360, 270));
|
|
pack();
|
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
|
|
|
display = new ComposeComponent(this);
|
|
newComposition();
|
|
|
|
setContentPane(display);
|
|
}
|
|
|
|
// - -%- -
|
|
|
|
private static final String
|
|
stringFor(PostVisibility visibility)
|
|
{
|
|
switch (visibility)
|
|
{
|
|
case PUBLIC: return "Public";
|
|
case UNLISTED: return "Unlisted";
|
|
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("Unlisted"))
|
|
return PostVisibility.UNLISTED;
|
|
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 JTextField
|
|
reply;
|
|
|
|
private JComboBox<String>
|
|
visibility;
|
|
|
|
private JButton
|
|
submit;
|
|
|
|
// ---%-@-%---
|
|
|
|
public void
|
|
setText(String text)
|
|
{
|
|
this.text.setText(text);
|
|
}
|
|
|
|
public void
|
|
setReplyToPostId(String postId)
|
|
{
|
|
this.reply.setText(postId);
|
|
}
|
|
|
|
public void
|
|
setVisibility(String visibility)
|
|
{
|
|
if (visibility.equals("Public"))
|
|
this.visibility.setSelectedIndex(0);
|
|
else if (visibility.equals("Unlisted"))
|
|
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
|
|
getReplyToPostId()
|
|
{
|
|
return reply.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(new Cursor(Cursor.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();
|
|
text.setLineWrap(true);
|
|
text.setWrapStyleWord(true);
|
|
|
|
reply = new JTextField();
|
|
|
|
visibility = new JComboBox<>(new String[] {
|
|
"Public",
|
|
"Unlisted",
|
|
"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);
|
|
|
|
JPanel top = new JPanel();
|
|
top.setOpaque(false);
|
|
top.setLayout(new BorderLayout(8, 0));
|
|
top.add(new JLabel("In reply to: "), BorderLayout.WEST);
|
|
top.add(reply);
|
|
|
|
setLayout(new BorderLayout(0, 8));
|
|
add(top, BorderLayout.NORTH);
|
|
add(text, BorderLayout.CENTER);
|
|
add(bottom, BorderLayout.SOUTH);
|
|
|
|
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
|
|
}
|
|
|
|
}
|