import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
public class MailFrame2 extends JFrame implements ActionListener{
private Vector<String>mymails;
private JPanel p_receiver, p_text, p_attachment, p_text_manipulation;
private JScrollPane sp_receiver, sp_text;
private JLabel l_receiver, l_attachment;
private JList li_receiver, li_attachment;
private JTextPane tp_text;
private JComboBox cb_size, cb_fonts;
private JButton b_add_mail, b_del_mail, b_add_attachment, b_del_attachment;
private JToggleButton tb_bold, tb_italic, tb_underline;
private Color user_yellow, user_red, user_gray;
private AbstractDocument doc;
private SimpleAttributeSet attr;
public MailFrame2(Vector<String>mails){
this.mymails = mails;
user_yellow = new Color(255,255,215);
user_red = new Color(250, 210, 210);
user_gray = new Color(212,208,200);
this.setSize(600, 550);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setTitle("E-Mail versenden");
this.getContentPane().setBackground(user_gray);
this.setLayout(new BorderLayout());
Dimension dim = new Dimension(35,25);
// Empfänger-Panel
GridBagLayout gbl_receiver = new GridBagLayout();
GridBagConstraints gbc_sp_receiver = new GridBagConstraints();
gbc_sp_receiver.fill = GridBagConstraints.BOTH;
gbc_sp_receiver.gridx = 1;
gbc_sp_receiver.gridy = 0;
gbc_sp_receiver.gridheight = GridBagConstraints.REMAINDER; // 2
gbc_sp_receiver.weightx = 1.0;
gbc_sp_receiver.weighty = 1.0;
gbc_sp_receiver.anchor = GridBagConstraints.NORTHWEST;
gbc_sp_receiver.insets = new Insets(10, 5, 0, 10);
GridBagConstraints gbc_l_receiver = new GridBagConstraints();
gbc_l_receiver.insets = new Insets(10, 10, 0, 5);
gbc_l_receiver.gridy = 0;
gbc_l_receiver.gridx = 0;
gbc_l_receiver.anchor = GridBagConstraints.NORTHWEST;
GridBagConstraints gbc_add_mail = new GridBagConstraints();
gbc_add_mail.gridx = 6;
gbc_add_mail.gridy = 0;
gbc_add_mail.gridheight = 1;
gbc_add_mail.gridwidth = 1;
gbc_add_mail.insets = new Insets(10,10,2,10);
GridBagConstraints gbc_del_mail = new GridBagConstraints();
gbc_del_mail.gridx = 6;
gbc_del_mail.gridy = 1;
gbc_del_mail.gridheight = 1;
gbc_del_mail.gridwidth = 1;
gbc_del_mail.anchor = GridBagConstraints.SOUTH;
gbc_del_mail.insets = new Insets(2,10,0,10);
p_receiver = new JPanel(gbl_receiver);
p_receiver.setPreferredSize(new Dimension(0,80));
p_receiver.setBackground(user_gray);
l_receiver = new JLabel("Empfänger");
li_receiver = new JList(mails);
li_receiver.setSelectedIndex(0);
li_receiver.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
li_receiver.setBackground(user_yellow);
sp_receiver = new JScrollPane(li_receiver);
sp_receiver.setPreferredSize(new Dimension(200,60));
sp_receiver.setBackground(Color.blue);
b_add_mail = new JButton("+");
b_del_mail = new JButton("-");
b_del_mail.setBackground(user_red);
b_add_mail.setSize(dim);
b_del_mail.setSize(dim);
b_add_mail.setPreferredSize(dim);
b_del_mail.setPreferredSize(dim);
p_receiver.add(l_receiver, gbc_l_receiver);
p_receiver.add(sp_receiver, gbc_sp_receiver);
p_receiver.add(b_add_mail, gbc_add_mail);
p_receiver.add(b_del_mail, gbc_del_mail);
// Text-Panel
p_text = new JPanel(new BorderLayout());
p_text.setBackground(user_gray);
p_text.setLayout(new GridBagLayout());
p_text.setPreferredSize(new Dimension(30, 300));
p_text.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
// Panel für die Textmanipulation + Layout
GridBagConstraints gbc_p_manipulation = new GridBagConstraints();
gbc_p_manipulation.gridx = 0;
gbc_p_manipulation.anchor = GridBagConstraints.WEST;
gbc_p_manipulation.insets = new Insets(5, 10, 5, 0);
gbc_p_manipulation.fill = GridBagConstraints.NONE;
gbc_p_manipulation.ipadx = 100;
gbc_p_manipulation.gridwidth = 1;
gbc_p_manipulation.gridheight = 1;
gbc_p_manipulation.gridy = 1;
p_text_manipulation = new JPanel();
p_text_manipulation.setBackground(user_gray);
// ToggleButtons
tb_bold = new JToggleButton(new StyledEditorKit.BoldAction());
tb_bold.setText("<html><b>F</b></html>");
tb_bold.setPreferredSize(dim);
tb_bold.setFocusable(false);
tb_italic = new JToggleButton(new StyledEditorKit.ItalicAction());
tb_italic.setText("<html><i>K</i></html>");
tb_italic.setPreferredSize(dim);
tb_italic.setFocusable(false);
tb_underline = new JToggleButton(new StyledEditorKit.UnderlineAction());
tb_underline.setText("<html><u>U</u></html>");
tb_underline.setPreferredSize(dim);
tb_underline.setFocusable(false);
// Alle verfügbaren Schriftarten auslesen und in der JComboBox anzeigen
String[] font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
cb_fonts = new JComboBox(font);
cb_fonts.setEditable(false);
cb_fonts.setFocusable(false);
cb_fonts.addActionListener(this);
cb_fonts.setActionCommand("font");
// ComboBox für die Schriftgrösse
Vector<Integer>sizes = new Vector<Integer>();
for (int i=8;i<33;i++) {
sizes.add(i);
}
cb_size = new JComboBox(sizes);
cb_size.setPreferredSize(new Dimension(50, 25));
cb_size.addActionListener(this);
cb_size.setActionCommand("size");
cb_size.setEditable(false);
cb_size.setFocusable(false);
// Elemente dem Panel für die Text-Manipulation hinzufügen
p_text_manipulation.add(cb_fonts);
p_text_manipulation.add(cb_size);
p_text_manipulation.add(tb_bold);
p_text_manipulation.add(tb_italic);
p_text_manipulation.add(tb_underline);
// Textfeld für den Text
tp_text = new JTextPane();
tp_text.addCaretListener(new MyCaretListener());
tp_text.getDocument().addDocumentListener(new MyDocumentListener());
// Voreinstellung für Textfeld
cb_fonts.setSelectedItem(tp_text.getFont().getFamily());
cb_size.setSelectedIndex((tp_text.getFont().getSize())-8);
// Layout des Scrollpanes für das TextFeld
GridBagConstraints gbc_sp_text = new GridBagConstraints();
gbc_sp_text.fill = GridBagConstraints.BOTH;
gbc_sp_text.gridy = 2;
gbc_sp_text.weightx = 1.0;
gbc_sp_text.weighty = 1.0;
gbc_sp_text.insets = new Insets(5, 10, 10, 10);
gbc_sp_text.gridx = 0;
sp_text = new JScrollPane(tp_text);
// TextManipulationsPanel und ScrollPane dem TextPanel hinzufügen
p_text.add(sp_text, gbc_sp_text);
p_text.add(p_text_manipulation, gbc_p_manipulation);
// Anhang-Panel
p_attachment = new JPanel();
p_attachment.setPreferredSize(new Dimension(0,100));
p_attachment.setBackground(user_gray);
this.add("North",p_receiver);
this.add("Center",p_text);
this.add("South",p_attachment);
}
// innere Klasse um aktuellen Caret zu bekommen
private class MyCaretListener implements CaretListener{
@Override
public void caretUpdate(CaretEvent e) {
StyledDocument doc = tp_text.getStyledDocument();
int pos0= e.getDot();
if(pos0>0)pos0--;
Element element = doc.getCharacterElement(pos0);
if(StyleConstants.isItalic(element.getAttributes())){
tb_italic.setSelected(true);
}else tb_italic.setSelected(false);
if(StyleConstants.isBold(element.getAttributes())){
tb_bold.setSelected(true);
}else tb_bold.setSelected(false);
if(StyleConstants.isUnderline(element.getAttributes())){
tb_underline.setSelected(true);
}else tb_underline.setSelected(false);
cb_size.setSelectedIndex(StyleConstants.getFontSize(element.getAttributes())-8);
cb_fonts.setSelectedItem(StyleConstants.getFontFamily(element.getAttributes()));
System.out.println(cb_fonts.getSelectedItem().toString());
}
}
// innere Klasse um Attribute aus dem TextFeld zu bekommen
private class MyDocumentListener implements DocumentListener{
@Override
public void changedUpdate(DocumentEvent e) {
//System.out.println("change");
}
@Override
public void insertUpdate(DocumentEvent e) {
//System.out.println("insert");
}
@Override
public void removeUpdate(DocumentEvent e) {
//System.out.println("remove");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("size")){
JComboBox cb = (JComboBox)e.getSource();
Integer size = (Integer)cb.getSelectedItem();
Action act = new StyledEditorKit.FontSizeAction(size.toString(),size);
act.actionPerformed(new ActionEvent(act,ActionEvent.ACTION_PERFORMED,
(String)act.getValue(Action.ACTION_COMMAND_KEY)));
}
if(e.getActionCommand().equals("font")){
JComboBox cb = (JComboBox)e.getSource();
String font = (String)cb.getSelectedItem();
Action act = new StyledEditorKit.FontFamilyAction(font,font);
act.actionPerformed(new ActionEvent(act,ActionEvent.ACTION_PERFORMED,
(String)act.getValue(Action.ACTION_COMMAND_KEY)));
}
}
}