package slutprojekt;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.DayOfWeek;
import java.time.LocalDate;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.util.ArrayList;
class CalenderSetup {
private static LocalDate today = LocalDate.now();
private static DayOfWeek dayOfWeek = DayOfWeek.from(today);
private static LocalDate date;
private static Color darkerBlue = new Color(0, 0, 250, 150);
private static Color blue = new Color(0, 0, 250, 75);
private static GridLayout componentLayout = new GridLayout(8, 0);
private static GridLayout panelLayout = new GridLayout();
private static String[] week = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
private static ArrayList<JPanel> listOfPanels = new ArrayList<JPanel>();
private static JFrame frame = new JFrame("Calender");
static void getWeek() {
// dateOrigin is the date that all other dates of the week are calculated from.
for (int i = 0; i < 7; i++) {
addPanel(i);
addLabelsToPanel(i);
addFourTextAreasToPanel(i);
addTextFieldToPanel(i);
addButtonToPanel(i);
highlightToday(i);
frameSettings();
eventAdder(button, textAreaList, textField, darkerBlue);
}
}
//button, textAreaList, textField, darkerBlue
//JButton button, ArrayList<JTextArea> textAreaList, JTextField textField, Color darkerBlue
static void eventAdder() {
ActionListener buttonListener = new ActionListener(JButton button, ArrayList<JTextArea> textAreaList, JTextField textField, Color darkerBlue) {
int counter = 0;
public void actionPerformed(ActionEvent e) {
textAreaList.get(counter).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY));
textAreaList.get(counter).setText(textField.getText());
textField.setText("");
if (counter == 3) {
counter -= 3;
} else {
counter++;
}
textAreaList.get(counter).setBorder(BorderFactory.createMatteBorder(0, 2, 0, 2, darkerBlue));
}
};
button.addActionListener(buttonListener);
}
static void frameSettings() {
frame.pack();
frame.setSize(770, 500);
frame.setLayout(panelLayout);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static void addPanel(int i) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, blue));
panel.setLayout(componentLayout);
listOfPanels.add(panel);
frame.add(listOfPanels.get(i));
}
static void addLabelsToPanel(int i) {
date = today.plusDays(i + 1 - dayOfWeek.getValue());
JLabel dayOfWeekLabel = new JLabel(week[i]);
JLabel dateLabel = new JLabel(date.getMonth() + " " + date.getDayOfMonth());
listOfPanels.get(i).add(dayOfWeekLabel);
listOfPanels.get(i).add(dateLabel);
}
static void addFourTextAreasToPanel(int i) {
ArrayList<JTextArea> textAreaList = new ArrayList<JTextArea>();
for (int j = 0; j < 4; j++) {
JTextArea textArea = new JTextArea();
textAreaList.add(textArea);
listOfPanels.get(i).add(textAreaList.get(j));
textAreaList.get(j).setBackground(Color.lightGray);
textAreaList.get(j).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY));
textAreaList.get(j).setEditable(false);
textAreaList.get(j).setLineWrap(true);
textAreaList.get(j).setWrapStyleWord(true);
}
}
static void addTextFieldToPanel(int i) {
JTextField textField = new JTextField();
listOfPanels.get(i).add(textField);
}
static void addButtonToPanel(int i) {
JButton button = new JButton("add event");
button.setFont(button.getFont().deriveFont(0, 11f));
listOfPanels.get(i).add(button);
}
static void highlightToday(int i) {
if (date == today) {
listOfPanels.get(i).setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, darkerBlue));
}
}
}