/*
* Many thanks to Norm from the www.javaprogrammingforums.com that knew more than me.put more time and energy to create this component
* Maysam Soleymani Qorabaee www.programsfuture.com *
*/
/**
*
* @author Admin
*/
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.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
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.io.IOException;
import java.text.DateFormat;
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.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
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;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.text.DefaultFormatter;
public class HijrahextendsJPanel extends JPanel {
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
Image img;
private static String DEFAULT_DATE_FORMAT = "yyyy/MM/dd";
private static final int DIALOG_WIDTH = 270;
private static final int DIALOG_HEIGHT = 230;
private SimpleDateFormat dateFormat;
private HijrahextendsJPanel.DatePanel datePanel = null;
private JDialog dateDialog = null;
private JButton choosdate = new JButton();
private JFormattedTextField datetext = new JFormattedTextField();
private JPanel datePanels = new JPanel();
String formatted;
// String alldate;
//Build a block to give a value to the year and month and day when the class loads
public void hdate() {
HijrahDate hijrahDate = HijrahDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
formatted = formatter.format(hijrahDate); // 1442/10/14
ye = Integer.parseInt(formatted.substring(0, 4));
mo = Integer.parseInt(formatted.substring(5, 7));
da = Integer.parseInt(formatted.substring(8, 10));
}
public HijrahextendsJPanel(String dateFormatPattern, Date date) {
this();
DEFAULT_DATE_FORMAT = dateFormatPattern;
}
public HijrahextendsJPanel() {
hdate();
setdateformatString();
javax.swing.GroupLayout datePanelsLayout = new javax.swing.GroupLayout(datePanels);
datePanels.setLayout(datePanelsLayout);
datePanelsLayout.setHorizontalGroup(
datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(datePanelsLayout.createSequentialGroup()
.addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
datePanelsLayout.setVerticalGroup(
datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(datePanelsLayout.createSequentialGroup()
.addGroup(datePanelsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(datetext, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(choosdate, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
);
add(datePanels);
try {
img = ImageIO.read(getClass().getResource("cleander.png"));
choosdate.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
setDate();
choosdate.setCursor(new Cursor(Cursor.HAND_CURSOR));
datePanels.add(datetext);
datePanels.add(choosdate);
choosdate.setVisible(true);
choosdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (datePanel == null) {
setdateformatString();
datePanel = new DatePanel();
} else {
setdateformatString();
getHDateString();
datetext.setText(getHDateString());
}
Point point = getLocationOnScreen();
point.y = point.y + 30;
showDateDialog(datePanel, point);
}
});
}
private void showDateDialog(DatePanel dateChooser, Point position) {
Frame owner = (Frame) SwingUtilities
.getWindowAncestor(HijrahextendsJPanel.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) {
// datetext.setText(getHDateString());
dialog.setVisible(false);
}
public void windowGainedFocus(WindowEvent e) {
// alldate=getHDateString();
}
});
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 HijrahDate getDate() {
HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
return today;
}
public void setText() {
setDate();
}
public void setDate() {
datetext.setText(getHDateString()); //<<<<< Changed
}
public void setText(String settext) {
setdateformatString();
// reflushWeekAndDay();
datetext.setText(settext);
}
public void setDate(Date date) {
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
datetext.setText(dateFormat.format(date));
}
public String getText() {
if (datetext.getText().equals(" / / ")) {
try {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter(null)));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
datetext.setText(null);
} else if (datetext.getText().equals(" - - ")) {
try {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter(null)));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
datetext.setText(null);
} else {
datetext.getText();
}
return datetext.getText();
}
public void setdateformatString() {
if (DEFAULT_DATE_FORMAT.equals("yy/MM/dd")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yy/MM/dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("yyyy/MM/dd")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy/MM/dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("dd/MM/yyyy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
} else if (DEFAULT_DATE_FORMAT.equals("dd/MM/yy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yy"))));
} else if (DEFAULT_DATE_FORMAT.equals("MM/dd/yy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("MM/dd/yy"))));
} else if (DEFAULT_DATE_FORMAT.equals("MM/dd/yyyy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("MM/dd/yyyy"))));
} else if (DEFAULT_DATE_FORMAT.equals(null)) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy/MM/dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy/MM/dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("yy-MM-dd")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yy-MM-dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("yyyy-MM-dd")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-MM-dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("dd-MM-yyyy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd-MM-yyyy"))));
} else if (DEFAULT_DATE_FORMAT.equals("dd-MM-yy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd-MM-yy"))));
} else if (DEFAULT_DATE_FORMAT.equals("MM-dd-yy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("MM-dd-yy"))));
} else if (DEFAULT_DATE_FORMAT.equals("MM-dd-yyyy")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("MM-dd-yyyydd"))));
} else if (DEFAULT_DATE_FORMAT.equals(null)) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-MM-dd"))));
} else if (DEFAULT_DATE_FORMAT.equals("")) {
datetext.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyyy-MM-dd"))));
} else {
if (datetext.getFormatter() instanceof DefaultFormatter) {
DefaultFormatter f = (DefaultFormatter) datetext.getFormatter();
f.setAllowsInvalid(true);
}
}
}
public void setDateFormatString(String dateformat) {
DEFAULT_DATE_FORMAT = dateformat;
setdateformatString();
// return string;
}
private class DatePanel extends JPanel implements ChangeListener {
int startYear = 1;
int lastYear = 9999;
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];
// Fill the days of the month in the buttons on the desired day
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();
}
//Build a panel for daylight months and sppiners and labels
private JPanel createYearAndMonthPanal() {
Font font = new Font("Tahoma", Font.BOLD, 10);
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(ye,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(60, 30));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
panel.add(yearSpin);
monthSpin = new JSpinner(new SpinnerNumberModel(mo, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(45, 30));
monthSpin.setName("Month");
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;
}
//Fill the days of the month in the buttons on the desired day
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;
}
numBtn.setBackground(palletTableColor);
numBtn.setForeground(dateFontColor);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
da = newDay;
setDate();
dayColorUpdate(true);
dateDialog.setVisible(false);
}
});
daysButton[i][j] = numBtn;
panel.add(numBtn);
actionCommandId++;
if (j == 6) {
numBtn.setForeground(weekendFontColor);
} else {
numBtn.setForeground(dateFontColor);
}
}
}
return panel;
}
private HijrahDate getCalendar() {
HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
return today;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
// Paint the days of the week and today
public void dayColorUpdate(boolean isOldDay) {
String s = String.valueOf(da);
for (int a = 0; a < 6; a++) {
for (int b = 0; b < 7; b++) {
daysButton[a][b].setForeground(dateFontColor);
daysButton[a][6].setForeground(weekendFontColor);
if (daysButton[a][b].getText().equals(s)) {
daysButton[a][b].setForeground(todayBackColor);
}
}
}
}
// Specify the number of days of a cat and start on what day and end on what day
private void reflushWeekAndDay() {
ye = (Integer) yearSpin.getValue();
mo = (Integer) monthSpin.getValue();
Locale ar = new Locale("ar");
DateTimeFormatter dayOfweekNum = DateTimeFormatter.ofPattern("c", ar);
HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(ye, mo, 1);
String WeekdayNum = hijrahDate.format(dayOfweekNum);
DateTimeFormatter dayOfMonth = DateTimeFormatter.ofPattern("d", ar);
String MonthdayNum = hijrahDate.format(dayOfMonth);
int WeekdayNumb = Integer.parseInt(WeekdayNum);
int moondaysnumber = hijrahDate.lengthOfMonth();
int maxDayNo = moondaysnumber;
int dayNo = 2 - WeekdayNumb;//set days in are in place
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);
}
// set year when click on jdialog button's and spinner and textfild
public void stateChanged(ChangeEvent e) {
dayColorUpdate(true);
JSpinner source = (JSpinner) e.getSource();
if (source.getName().equals("Year")) {
ye = (getSelectedYear());
} else {
mo = (getSelectedMonth());
}
getCalendar();
reflushWeekAndDay();
setDate();
mothnam();
}
}
// set month when click on jdialog button's and spinner and textfild
public void mothnam() {
monthNameLabel.setText(moonNames[mo - 1]);
}
//set date for jtext and jdialog....
String getHDateString() { // ADDED
// if(!datetext.getText().equals(null)||!datetext.getText().equals("")||!datetext.getText().equals(" / / ")||!datetext.getText().equals(" - - ")||!datetext.getText().equals(" - - ")||!datetext.getText().equals(" - - ")||!datetext.getText().equals(" - - ")){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
HijrahDate today = HijrahChronology.INSTANCE.date(ye, mo, da);
String formatted = formatter.format(today); // 1442/10/14
return formatted.toString(); //<<<<<< ADDED
}
}