Advertisement
Gaudenz

Untitled

Feb 20th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.28 KB | None | 0 0
  1. package kotlin.gomis.go.mis.GUI;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.event.ComponentAdapter;
  9. import java.awt.event.ComponentEvent;
  10. import java.sql.Connection;
  11. import java.time.LocalDate;
  12. import java.time.LocalDateTime;
  13. import java.time.YearMonth;
  14. import java.time.format.DateTimeFormatter;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Locale;
  18. import java.util.Map;
  19.  
  20. import javax.swing.BorderFactory;
  21. import javax.swing.JButton;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.SwingConstants;
  26.  
  27. import com.formdev.flatlaf.FlatClientProperties;
  28. import com.formdev.flatlaf.FlatLightLaf;
  29.  
  30. import kotlin.gomis.go.mis.Database.DAO.AppointmentCRUD;
  31. import kotlin.gomis.go.mis.Database.entity.Appointment;
  32. import kotlin.gomis.go.mis.components.FormManager.Form;
  33. import kotlin.gomis.go.mis.main.Main;
  34. import net.miginfocom.swing.MigLayout;
  35.  
  36. public class AppointmentMangement extends Form {
  37. private LocalDate currentDate;
  38. private JPanel calendarPanel;
  39. private JLabel monthYearLabel;
  40. private AppointmentCRUD appointmentDAO;
  41. private Connection connection;
  42.  
  43.  
  44. public AppointmentMangement(Connection connection) {
  45. this.connection = connection;
  46. this.appointmentDAO = new AppointmentCRUD();
  47. currentDate = LocalDate.now();
  48.  
  49. FlatLightLaf.setup();
  50.  
  51. // Main panel layout - fill the entire space
  52. setLayout(new MigLayout("fill, insets 0", "[grow, fill]", "[][][grow, fill]"));
  53.  
  54. // Header panel
  55. JPanel headerPanel = new JPanel(new MigLayout("fill", "[100px][grow][100px]", "[]"));
  56. headerPanel.putClientProperty(FlatClientProperties.STYLE, "arc: 8");
  57. add(headerPanel, "cell 0 0, growx");
  58.  
  59. JButton addAppointBtn = new JButton("Add");
  60. addAppointBtn.putClientProperty(FlatClientProperties.STYLE, "arc: 8");
  61. addAppointBtn.addActionListener(e -> createAppointment());
  62. headerPanel.add(addAppointBtn, "cell 0 0");
  63.  
  64. JLabel titleLabel = new JLabel("Appointments");
  65. headerPanel.add(titleLabel, "cell 1 0, alignx center");
  66.  
  67. // Navigation panel
  68. add(createNavigationPanel(), "cell 0 1, growx");
  69.  
  70. // Calendar panel with proper constraints
  71. calendarPanel = createCalendarPanel();
  72. add(calendarPanel, "cell 0 2, grow");
  73. }
  74.  
  75. private JPanel createNavigationPanel() {
  76. JPanel navigationPanel = new JPanel(new MigLayout("", "[grow][grow][grow]"));
  77. JButton prevButton = new JButton("<");
  78. prevButton.addActionListener(e -> changeMonth(-1));
  79. JButton nextButton = new JButton(">");
  80. nextButton.addActionListener(e -> changeMonth(1));
  81. monthYearLabel = new JLabel();
  82. updateMonthYearLabel();
  83. navigationPanel.add(prevButton, "cell 0 0, align left");
  84. navigationPanel.add(monthYearLabel, "cell 1 0, align center");
  85. navigationPanel.add(nextButton, "cell 2 0, align right");
  86. return navigationPanel;
  87. }
  88.  
  89. private JPanel createCalendarPanel() {
  90. // Create panel with equal spacing for all cells that grow proportionally
  91. JPanel panel = new JPanel(new MigLayout("wrap 7, fill, insets 5",
  92. "[grow, fill][grow, fill][grow, fill][grow, fill][grow, fill][grow, fill][grow, fill]",
  93. "[]5[grow, fill]5[grow, fill]5[grow, fill]5[grow, fill]5[grow, fill]"));
  94.  
  95. // Add day headers
  96. String[] dayNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  97. for (String day : dayNames) {
  98. JLabel dayLabel = new JLabel(day, SwingConstants.CENTER);
  99. panel.add(dayLabel, "alignx center");
  100. }
  101.  
  102. populateCalendarDays(panel);
  103. return panel;
  104. }
  105.  
  106. private void populateCalendarDays(JPanel panel) {
  107. YearMonth yearMonth = YearMonth.from(currentDate);
  108. LocalDate firstOfMonth = yearMonth.atDay(1);
  109. int daysInMonth = yearMonth.lengthOfMonth();
  110. int startDayOfWeek = firstOfMonth.getDayOfWeek().getValue() % 7;
  111.  
  112. // Add empty cells for days before the first of the month
  113. for (int i = 0; i < startDayOfWeek; i++) {
  114. panel.add(new JPanel(), "grow"); // Empty panel that will maintain square shape
  115. }
  116.  
  117. // Add day panels
  118. for (int day = 1; day <= daysInMonth; day++) {
  119. LocalDate currentDay = firstOfMonth.plusDays(day - 1);
  120. JPanel dayPanel = createDayPanel(currentDay);
  121. panel.add(dayPanel, "grow");
  122. }
  123. }
  124.  
  125. private JPanel createDayPanel(LocalDate date) {
  126. JPanel dayPanel = new JPanel(new MigLayout("wrap 1, insets 2", "[grow,fill]", "[][grow]")) {
  127. @Override
  128. public Dimension getPreferredSize() {
  129. Container parent = getParent();
  130. if (parent != null) {
  131. int parentWidth = parent.getWidth();
  132. int baseSize = (parentWidth - 30) / 7;
  133. return new Dimension(parentWidth / 7, baseSize);
  134. }
  135. return new Dimension(100, 100);
  136. }
  137. };
  138.  
  139. dayPanel.putClientProperty(FlatClientProperties.STYLE, "arc: 8");
  140. dayPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
  141. dayPanel.setBackground(new Color(230, 240, 250));
  142.  
  143. // Add date number
  144. JLabel dayLabel = new JLabel(String.valueOf(date.getDayOfMonth()));
  145. dayLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  146. dayPanel.add(dayLabel, "align right");
  147.  
  148. // Add appointments if any
  149. List<Appointment> appointments = appointmentDAO.getAppointmentsForDate(connection, date);
  150. if (!appointments.isEmpty()) {
  151. JPanel appointmentsContainer = new JPanel(new MigLayout("wrap 1, insets 0, gap 2",
  152. "[grow,fill]", // Full width
  153. appointments.size() == 1 ? "[30!]" : "[grow]")); // Adjust gap between appointments
  154. appointmentsContainer.setOpaque(false);
  155. dayPanel.add(appointmentsContainer, "grow");
  156.  
  157. Map<String, Color> appointmentColors = new HashMap<>();
  158. appointmentColors.put("Career", new Color(46, 204, 113));
  159. appointmentColors.put("Counseling", new Color(255, 118, 117));
  160. appointmentColors.put("Personal Counseling", new Color(241, 196, 15));
  161. appointmentColors.put("Group Counseling", new Color(181, 255, 140));
  162.  
  163. for (Appointment appt : appointments) {
  164. JPanel appointmentPanel = new JPanel(new MigLayout("fill, insets 2", "[grow]"));
  165. appointmentPanel.setBackground(
  166. appointmentColors.getOrDefault(appt.getAppointmentType(), Color.GRAY));
  167.  
  168. String timeStr = formatAppointmentTime(appt.getAppointmentDateTime());
  169.  
  170. JLabel appLabel = new JLabel() {
  171. @Override
  172. public void setText(String text) {
  173. Container parent = getParent();
  174. if (parent != null) {
  175. Container calendarCell = parent.getParent().getParent();
  176. if (appointments.size() == 1) {
  177. setFont(new Font("Arial", Font.BOLD, 14));
  178. setHorizontalAlignment(SwingConstants.CENTER);
  179. super.setText(timeStr);
  180. } else {
  181. // Larger font for multiple appointments
  182. setFont(new Font("Arial", Font.BOLD, 12));
  183. setHorizontalAlignment(SwingConstants.LEFT);
  184. if (calendarCell.getWidth() > 120) {
  185. super.setText(timeStr + " - " + appt.getAppointmentType());
  186. } else {
  187. super.setText(timeStr);
  188. }
  189. }
  190. }
  191. }
  192. };
  193. appLabel.setForeground(Color.BLACK);
  194.  
  195. appointmentPanel.add(appLabel, "grow");
  196.  
  197. // Adjust height based on number of appointments
  198. String constraints;
  199. if (appointments.size() == 1) {
  200. constraints = "grow, height 30!";
  201. } else if (appointments.size() == 2) {
  202. constraints = "grow, height 25!";
  203. } else {
  204. constraints = "grow, height 20!"; // Slightly smaller for 3+ appointments
  205. }
  206. appointmentsContainer.add(appointmentPanel, constraints);
  207. }
  208.  
  209. // Add resize listener to the day panel
  210. dayPanel.addComponentListener(new ComponentAdapter() {
  211. @Override
  212. public void componentResized(ComponentEvent e) {
  213. for (Component c : appointmentsContainer.getComponents()) {
  214. if (c instanceof JPanel) {
  215. for (Component label : ((JPanel) c).getComponents()) {
  216. if (label instanceof JLabel) {
  217. ((JLabel) label).setText(((JLabel) label).getText());
  218. }
  219. }
  220. }
  221. }
  222. }
  223. });
  224. }
  225.  
  226. // Add hover effects
  227. dayPanel.addMouseListener(new java.awt.event.MouseAdapter() {
  228. public void mouseEntered(java.awt.event.MouseEvent evt) {
  229. if (dayPanel.getBackground().equals(new Color(230, 240, 250))) {
  230. dayPanel.setBackground(new Color(220, 230, 240));
  231. }
  232. }
  233. public void mouseExited(java.awt.event.MouseEvent evt) {
  234. if (dayPanel.getBackground().equals(new Color(220, 230, 240))) {
  235. dayPanel.setBackground(new Color(230, 240, 250));
  236. }
  237. }
  238. public void mouseClicked(java.awt.event.MouseEvent evt) {
  239. showDayAppointments(date);
  240. }
  241. });
  242.  
  243. return dayPanel;
  244. }
  245.  
  246. private String formatAppointmentTime(LocalDateTime dateTime) {
  247. return dateTime.toLocalTime().format(DateTimeFormatter.ofPattern("h:mm a"));
  248. }
  249.  
  250. private void showDayAppointments(LocalDate date) {
  251. List<Appointment> dayAppointments = appointmentDAO.getAppointmentsForDate(connection, date);
  252. if (dayAppointments.isEmpty()) {
  253. JOptionPane.showMessageDialog(this, "No appointments on " + date, "Appointments", JOptionPane.INFORMATION_MESSAGE);
  254. return;
  255. }
  256. StringBuilder message = new StringBuilder("Appointments on " + date + "\n");
  257. for (Appointment appt : dayAppointments) {
  258. message.append(appt.getAppointmentDateTime()).append(" - ")
  259. .append(appt.getStudentUid()).append(" (")
  260. .append(appt.getAppointmentType()).append(")\n");
  261. }
  262. JOptionPane.showMessageDialog(this, message.toString(), "Appointments", JOptionPane.PLAIN_MESSAGE);
  263. }
  264.  
  265. private void changeMonth(int delta) {
  266. currentDate = currentDate.plusMonths(delta);
  267. updateMonthYearLabel();
  268.  
  269. // Remove old calendar and add new one
  270. remove(calendarPanel);
  271. calendarPanel = createCalendarPanel();
  272. add(calendarPanel, "cell 0 2, grow");
  273.  
  274. revalidate();
  275. repaint();
  276. }
  277.  
  278. private void updateMonthYearLabel() {
  279. String monthName = currentDate.getMonth().getDisplayName(java.time.format.TextStyle.FULL, Locale.getDefault());
  280. monthYearLabel.setText(monthName + " " + currentDate.getYear());
  281. }
  282.  
  283. private void createAppointment() {
  284. AppointmentDialog dialog = new AppointmentDialog(Main.jFrame);
  285. dialog.setVisible(true);
  286.  
  287. if (dialog.isConfirmed()) {
  288. try {
  289. Integer studentUid = dialog.getStudentUid();
  290. // Temporarily using counselor ID 1 for testing - you should implement proper user authentication
  291. int counselorId = 1; // TODO: Replace with actual getCurrentLoggedInCounselorId()
  292. String appointmentType = dialog.getAppointmentType();
  293. LocalDateTime appointmentDateTime = dialog.getAppointmentDateTime();
  294. String appointmentStatus = "Scheduled";
  295.  
  296. AppointmentCRUD appointmentCRUD = new AppointmentCRUD();
  297. appointmentCRUD.addAppointment(
  298. connection,
  299. studentUid,
  300. counselorId,
  301. appointmentType,
  302. java.sql.Timestamp.valueOf(appointmentDateTime),
  303. appointmentStatus
  304. );
  305.  
  306. JOptionPane.showMessageDialog(this,
  307. "Appointment scheduled successfully!",
  308. "Success",
  309. JOptionPane.INFORMATION_MESSAGE);
  310.  
  311. // Refresh calendar
  312. remove(calendarPanel);
  313. calendarPanel = createCalendarPanel();
  314. add(calendarPanel, "cell 0 2, grow");
  315.  
  316. revalidate();
  317. repaint();
  318. } catch (Exception e) {
  319. JOptionPane.showMessageDialog(this,
  320. "Error creating appointment: " + e.getMessage(),
  321. "Error",
  322. JOptionPane.ERROR_MESSAGE);
  323. }
  324. }
  325. }
  326.  
  327. // private static JFrame frame = new JFrame();
  328. // public static void main(String[] args) throws SQLException {
  329. // FlatLightLaf.setup();
  330. // Connection connection = DBConnection.getConnection();
  331. // frame.getContentPane().add(new AppointmentMangement(connection));
  332. // frame.setSize(800, 600);
  333. // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  334. // frame.setVisible(true);
  335. // }
  336. }
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement