package a3_tagebuch;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.text.*;
import javax.swing.*;
public class DiaryPanel extends JPanel{
private JLabel datum,titel,pic,bb;
private JTextField datumFeld,titelFeld;
private JTextArea textFeld;
private JButton jetzt,speichern,neu,bla;
private Color back = new Color(213,232,255);
private Font text1 = new Font("Trebuchet MS",Font.PLAIN,14);
private JPanel datumPanel = new JPanel();
private JPanel titelPanel = new JPanel();
private JPanel textPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JPanel writePanel = new JPanel();
private JPanel mainPanel = new JPanel();
//private GregorianCalendar cal = new GregorianCalendar(); //geändert, mit DateFormat realisiert
private DateFormat formatter;
private int count = 0;
private int flag;
private Image logo;
private ImageIcon icon;
private ArrayList<DiaryEintrag> eintragListe = new ArrayList();
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbl1 = new GridBagLayout();
private GridBagConstraints gbc1 = new GridBagConstraints();
private GridLayout grid;
private JPanel entryPanel = new JPanel();
public DiaryPanel(int dFlag){
flag = dFlag;
if(flag == 0){
writeMethod();
}
if(flag == 1){
showPanel(count);
}
setFont(text1);
logo = getToolkit().getImage("images/title.jpg");
icon = new ImageIcon(logo);
pic = new JLabel(icon, JLabel.CENTER);
datum = new JLabel("Datum:");
datumFeld = new JTextField("Format: 3.8.2007, 12:00", 30);
jetzt = new JButton("Jetzt");
jetzt.addActionListener(new ButtonHandler());
titel = new JLabel("Titel:");
titelFeld = new JTextField(37);
textFeld = new JTextArea("Eintrag",22,41);
textFeld.setLineWrap(true);
speichern = new JButton("Speichern");
speichern.addActionListener(new ButtonHandler());
neu = new JButton("Neu");
neu.addActionListener(new ButtonHandler());
datumPanel.setLayout(new FlowLayout());
titelPanel.setLayout(new FlowLayout());
textPanel.setLayout(new FlowLayout());
buttonPanel.setLayout(new FlowLayout());
mainPanel.setLayout(gbl);
datumPanel.add(datum);
datumPanel.add(datumFeld);
datumPanel.add(jetzt);
titelPanel.add(titel);
titelPanel.add(titelFeld);
textPanel.add(new JScrollPane(textFeld));
buttonPanel.add(speichern);
buttonPanel.add(neu);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2,2,2,2);
gbc.gridx = 0; // x-Position im gedachten Gitter
gbc.gridy = 0; // y-Position im gedachten Gitter
gbc.gridheight = 1;
gbl.setConstraints(pic, gbc);
mainPanel.add(pic);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridheight = 1;
gbl.setConstraints(datumPanel, gbc);
mainPanel.add(datumPanel);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridheight = 1;
gbl.setConstraints(titelPanel, gbc);
mainPanel.add(titelPanel);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridheight = 1;
gbl.setConstraints(textPanel, gbc);
mainPanel.add(textPanel);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridheight = 1;
gbl.setConstraints(buttonPanel, gbc);
mainPanel.add(buttonPanel);
formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
}
public void writeMethod(){
this.add(mainPanel);
}
public String getDate(){
/*cal.setTimeZone(TimeZone.getTimeZone("CET"));
String tempDate = cal.get(Calendar.DATE) + "." + (cal.get(Calendar.MONTH)+1) + "." + cal.get(Calendar.YEAR) + ", " + cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE);
return tempDate;*/
return formatter.format(new Date());
}
/*public void paint(Graphics g) {
g.setColor(back);
g.fillRect(0,0,500,600);
title = getToolkit().getImage("images/title.jpg");
g.drawImage(title,129,10,this);
g.setColor(Color.black);
}*/
public void showPanel(int count){
writePanel.setLayout(new GridLayout(5,1));
if(eintragListe.size() > 0){
DiaryFrame fr = new DiaryFrame();
DiaryEintrag ein = eintragListe.get(count);
System.out.println("Datum: "+ein.getDatum()+"\nTitel: "+ein.getTitel()+"\nNummer: "+eintragListe.size()+"\nText: "+ein.getText()+"\n");
writePanel.add(new JButton("3"));
}
writePanel.add(new JButton("1"));
writePanel.add(new JButton("2"));
this.add(writePanel);
validate();
}
private class ButtonHandler implements ActionListener{
public void actionPerformed (ActionEvent evt){
if(evt.getSource() == jetzt){
datumFeld.setText(getDate());
}
if(evt.getSource() == neu){
int confirm = JOptionPane.showConfirmDialog(DiaryPanel.this, "Neuen Eintrag beginnen und Diesen verwerfen?", "Neuer Eintrag", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
datumFeld.setText("Format: 3.8.2007, 12:00");
titelFeld.setText("");
textFeld.setText("Eintrag:");
}
}
if(evt.getSource() == speichern){
JOptionPane.showMessageDialog(DiaryPanel.this,"Eintrag gespeichert");
DiaryEintrag dEntry = new DiaryEintrag(count,titelFeld.getText(),datumFeld.getText(),textFeld.getText());
eintragListe.add(dEntry);
showPanel(count);
count++;
}
}
}
}