package de.kopis.jusenet.ui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import de.kopis.jusenet.Application;
import de.kopis.jusenet.nntp.Group;
import de.kopis.jusenet.utils.GuiUtils;
import de.kopis.utils.FileUtils;
public class ComposeMessageWindow extends JDialog implements WindowListener, ActionListener {
private static final String SEND_COMMAND = "Send";
private JTextArea taContent;
private JToolBar _toolbar;
private JTextField tfGroup;
private JTextField tfSubject;
private boolean saved = false;
public ComposeMessageWindow() {
super();
init();
}
public ComposeMessageWindow(Frame f) {
super(f);
init();
}
public ComposeMessageWindow(Group g) {
this();
tfGroup.setText(g.getName());
}
private void init() {
createGUI();
addWindowListener(this);
setSize(400, 300);
}
private void createGUI() {
setLayout(new BorderLayout());
_toolbar = new JToolBar();
JButton sendAction = new JButton(SEND_COMMAND);
sendAction.setActionCommand(SEND_COMMAND);
sendAction.addActionListener(this);
_toolbar.add(sendAction);
add(_toolbar, BorderLayout.NORTH);
tfGroup = new JTextField();
tfGroup.setToolTipText("Separate multiple groups with ','");
tfSubject = new JTextField();
tfSubject.setToolTipText("Please provide a meaningful subject line.");
taContent = new JTextArea();
//TODO insert signature when opening ComposeMessageWindow
FormLayout layout = new FormLayout(
"right:pref, 3dlu, pref:grow",
"pref, 3dlu, pref, 3dlu, pref, 3dlu, pref, 3dlu, fill:pref:grow");
PanelBuilder builder = new PanelBuilder(layout);
CellConstraints c = new CellConstraints();
builder.addSeparator("Message Header", c.xyw(1, 1, 3));
builder.addLabel("Group:", c.xy (1, 3));
builder.add(tfGroup, c.xy (3, 3));
builder.addLabel("Subject:", c.xy (1, 5));
builder.add(tfSubject, c.xy (3, 5));
builder.addSeparator("Message Content", c.xyw(1, 7, 3));
builder.add(new JScrollPane(taContent), c.xyw(1, 9, 3));
builder.setDefaultDialogBorder();
add(builder.getPanel(), BorderLayout.CENTER);
}
private void send() {
StringBuffer articleContent = new StringBuffer();
String signature = null;
try {
signature = FileUtils.getFileContents(Application.getInstance().getProperty("signature"));
} catch (IOException e) {
GuiUtils.showError(e);
}
//TODO check correct article format
//TODO insert custom headers
articleContent.append("From: " + "carsten.ringe@web.de\n");
articleContent.append("Subject:" + tfSubject.getText() + "\n");
articleContent.append("Date: " + SimpleDateFormat.getDateTimeInstance().format(new Date()) + "\n");
articleContent.append("Newsgroups: " + tfGroup.getText() + "\n");
articleContent.append("\n");
articleContent.append(taContent.getText());
if(signature != null) {
articleContent.append("\n\n-- \n"); // signature divider
articleContent.append(signature); // signature
}
//TODO make new Article()
Application.getInstance().sendArticle(articleContent.toString());
saved = true;
dispose();
}
public void windowClosing(WindowEvent e) {
if(!saved) {
int result = JOptionPane.showConfirmDialog(this, "Do you want to discard this article?", "Discard article?", JOptionPane.YES_NO_OPTION);
if(result != JOptionPane.YES_OPTION) {
//TODO don't close window now
}
}
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void actionPerformed(ActionEvent e) {
if(SEND_COMMAND.equals(e.getActionCommand())) {
send();
}
}
}