/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Hijrah extends JTextField {
int ye;
int mo;
int da;
String[] moonNames = {"ٱلْمُحَرَّم", "صَفَر", "رَبِيع ٱلْأَوَّل", "رَبِيع ٱلثَّانِي", "جُمَادَىٰ ٱلْأُولَىٰ", "جُمَادَىٰ ٱلثَّانِيَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو ٱلْقَعْدَة", "ذُو ٱلْحِجَّة"};
//String[] moonNames = {"al-Muḥarram", "Ṣafar", "Rabīʿ al-ʾAwwal", "Rabīʿ ath-Thānī", "Jumadā al-ʾŪlā", "Jumādā ath-Thāniyah", "Rajab", "Shaʿbān", "Ramaḍān", "Shawwāl", "Ḏū al-Qaʿdah", "Ḏū al-Ḥijjah"};
String colname[] = {"سب", "اح", "ثن", "ثل", "ار", "خم", "جم"};
// String colname[] = {"Sa", "sun", "Mon", "Tu", "We", "Th", "Fr"};
JLabel cell;
JLabel monthNameLabel = new JLabel(); // get name from array
private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
private static final int DIALOG_WIDTH = 270;
private static final int DIALOG_HEIGHT = 210;
private SimpleDateFormat dateFormat;
private DatePanel datePanel = null;
private JDialog dateDialog = null;
public void hdate() {
HijrahDate hijrahDate = HijrahDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String formatted = formatter.format(hijrahDate); // 07/03/1439
ye = Integer.parseInt(formatted.substring(0, 4));
mo = Integer.parseInt(formatted.substring(5, 7));
da = Integer.parseInt(formatted.substring(8, 10));
}
// public Hijrah() {
// this();
// }
public Hijrah(String dateFormatPattern, Date date) {
this();
DEFAULT_DATE_FORMAT = dateFormatPattern;
}
public Hijrah() {
hdate();
setDate();
setEditable(false);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addListeners();
}
private void addListeners() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent paramMouseEvent) {
if (datePanel == null) {
datePanel = new DatePanel();
}
Point point = getLocationOnScreen();
point.y = point.y + 30;
showDateDialog(datePanel, point);
}
});
}
private void showDateDialog(DatePanel dateChooser, Point position) {
Frame owner = (Frame) SwingUtilities
.getWindowAncestor(Hijrah.this);
if (dateDialog == null || dateDialog.getOwner() != owner) {
dateDialog = createDateDialog(owner, dateChooser);
}
dateDialog.setLocation(getAppropriateLocation(owner, position));
dateDialog.setVisible(true);
}
private JDialog createDateDialog(Frame owner, JPanel contentPanel) {
JDialog dialog = new JDialog(owner, "Date Selected", false);// Non-modal
// JDialog dialog = new JDialog(owner, "Date Selected", true);
dialog.setUndecorated(true);
dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);
dialog.pack();
dialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
dialog.addWindowFocusListener(new WindowFocusListener() {
public void windowLostFocus(WindowEvent e) {
dialog.setVisible(false);
}
public void windowGainedFocus(WindowEvent e) {
}
});
return dialog;
}
private Point getAppropriateLocation(Frame owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + DIALOG_WIDTH) - (p.x + owner.getWidth());
int offsetY = (position.y + DIALOG_HEIGHT) - (p.y + owner.getHeight());
if (offsetX > 0) {
result.x -= offsetX;
}
if (offsetY > 0) {
result.y -= offsetY;
}
return result;
}
private SimpleDateFormat getDefaultDateFormat() {
if (dateFormat == null) {
dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
}
return dateFormat;
}
public void setText() {
setDate();
}
public void setDate() {
super.setText(testMethod()); //<<<<< Changed
}
public Date getDate() {
try {
return getDefaultDateFormat().parse(getText());
} catch (ParseException e) {
return new Date();
}
}
private class DatePanel extends JPanel implements ChangeListener {
int startYear = 1;
int lastYear = 99999;
Color backGroundColor = Color.gray;
Color palletTableColor = Color.white;
Color todayBackColor = Color.orange;
Color weekFontColor = Color.blue;
Color dateFontColor = Color.black;
Color weekendFontColor = Color.red;
Color controlLineColor = Color.GREEN;
Color controlTextColor = new java.awt.Color(153, 0, 153);
Color controlMoonnameTextColor = Color.BLUE;
JSpinner yearSpin;
JSpinner monthSpin;
JButton[][] daysButton = new JButton[8][7];
DatePanel() {
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
reflushWeekAndDay();
}
private JPanel createYearAndMonthPanal() {
Font font = new Font("Tahoma", Font.BOLD, 10);
Calendar cal = getCalendar();
int currentYear = cal.get(Calendar.YEAR);
int currentMonth = cal.get(Calendar.MONTH) + 1;
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(controlLineColor);
JLabel yearLabel = new JLabel("السنة");
yearLabel.setForeground(controlTextColor);
yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
yearLabel.setVerticalAlignment(SwingConstants.CENTER);
yearLabel.setFont(font);
panel.add(yearLabel);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(60, 30));
yearSpin.setName("Month");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
panel.add(yearSpin);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(45, 30));
monthSpin.setName("Year");
monthSpin.addChangeListener(this);
monthNameLabel.setPreferredSize(new Dimension(65, 20));
monthNameLabel.setForeground(controlTextColor);
monthNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
monthNameLabel.setVerticalAlignment(SwingConstants.CENTER);
monthNameLabel.setFont(font);
monthNameLabel.setText(moonNames[(Integer) monthSpin.getValue() - 1]); // get name from array
panel.add(monthNameLabel);
panel.add(monthSpin);
JLabel monthLabel = new JLabel("الشهر");
monthLabel.setForeground(controlTextColor);
monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
monthLabel.setVerticalAlignment(SwingConstants.CENTER);
monthLabel.setFont(font);
panel.add(monthLabel);
return panel;
}
private JPanel createWeekAndDayPanal() {
JPanel panel = new JPanel();
panel.setFont(new Font("Tahoma", Font.PLAIN, 10));
panel.setLayout(new GridLayout(7, 7));
panel.setBackground(Color.white);
for (int i = 0; i < 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.CENTER);
if (i == 6) {
cell.setForeground(weekendFontColor);
} else {
cell.setForeground(weekFontColor);
}
panel.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
JButton numBtn = new JButton();
numBtn.setBorder(null);
numBtn.setHorizontalAlignment(SwingConstants.CENTER);
numBtn.setActionCommand(String.valueOf(actionCommandId));
numBtn.setBackground(palletTableColor);
numBtn.setForeground(dateFontColor);
numBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JButton source = (JButton) event.getSource();
if (source.getText().length() == 0) {
numBtn.setForeground(dateFontColor);
return;
}
dayColorUpdate(true);
numBtn.setBackground(palletTableColor);
numBtn.setForeground(dateFontColor);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
da = newDay;
setDate();
dateDialog.setVisible(false);
}
});
daysButton[i][j] = numBtn;
panel.add(numBtn);
actionCommandId++;
if (j == 6) {
numBtn.setForeground(weekendFontColor);
} else {
numBtn.setForeground(dateFontColor);
}
}
}
return panel;
}
private Calendar getCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getDate());
return calendar;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
ye = (Integer) yearSpin.getValue();
mo = (Integer) monthSpin.getValue();
System.out.println("spinner ye " + ye);
System.out.println("spinner mo " + mo);
Locale ar = new Locale("ar");
DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
String MoondayNum = hijrahDate.format(dayOfMoonNum);
String WeekdayNum = hijrahDate.format(dayOfweekNum);
int WeekdayNumb = Integer.parseInt(WeekdayNum);
int moondayNumb = Integer.parseInt(MoondayNum);
int moondaysnumber = hijrahDate.lengthOfMonth();
System.out.println("hijrahDate= " + hijrahDate);
System.out.println("Number day-of-month " + MoondayNum);
System.out.println("Number day-of-Week " + WeekdayNum);
System.out.println("Number of days of the month " + moondaysnumber);
int actionCommandId = moondayNumb + 3 + WeekdayNumb;
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay) {
daysButton[i][j].setForeground(dateFontColor);
daysButton[i][6].setForeground(weekendFontColor);
} else {
daysButton[i][j].setForeground(dateFontColor);
daysButton[i][6].setForeground(weekendFontColor);
daysButton[i][j].setForeground(todayBackColor);
}
}
private void reflushWeekAndDay() {
ye = (Integer) yearSpin.getValue();
mo = (Integer) monthSpin.getValue();
System.out.println("spinner ye " + ye);
System.out.println("spinner mo " + mo);
Locale ar = new Locale("ar");
DateTimeFormatter dayOfMoonNum = DateTimeFormatter.ofPattern("d", ar);
DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, da);
HijrahChronology crono = hijrahDate.getChronology();
HijrahDate date = crono.date(ye, mo, da);
System.out.println("crono date= " + date);
String MoondayNum = hijrahDate.format(dayOfMoonNum);
String WeekdayNum = hijrahDate.format(dayOfweekNum);
int WeekdayNumb = Integer.parseInt(WeekdayNum);
int moondayNumb = Integer.parseInt(MoondayNum);
int moondaysnumber = hijrahDate.lengthOfMonth();
System.out.println("hijrahDate= " + hijrahDate);
System.out.println("Number day-of-month " + moondayNumb);
System.out.println("Number day-of-Week " + WeekdayNumb);
System.out.println("Number of days of the month " + moondaysnumber);
int maxDayNo = moondaysnumber;
System.out.println("Number of days of the month " + moondaysnumber);
int dayNo = -3 - WeekdayNumb;
// int dayNo = 0;
System.out.println("dayNo " + dayNo);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
String s = "";
if (dayNo >= 1 && dayNo <= maxDayNo) {
s = String.valueOf(dayNo);
}
daysButton[i][j].setText(s);
dayNo++;
if (daysButton[i][j].getText().equals("")) {
daysButton[i][j].setVisible(false);
} else {
daysButton[i][j].setVisible(true);
}
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
dayColorUpdate(true);
JSpinner source = (JSpinner) e.getSource();
Calendar cal = getCalendar();
if (source.getName().equals("Year")) {
cal.set(Calendar.YEAR, getSelectedYear());
} else {
cal.set(Calendar.MONTH, getSelectedMonth() - 1);
}
setDate();
reflushWeekAndDay();
mothnam();
}
}
public void mothnam() {
monthNameLabel.setText(moonNames[mo - 1]);
}
String testMethod() { // ADDED
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
String formatted = formatter.format(today); // 07/03/1439
// System.out.println("hijrahDate " + formatted);
return formatted.toString(); //<<<<<< ADDED
}
}