import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import oracle.jdeveloper.layout.BoxLayout2;
public class ChooseDate extends JPanel
{
JToggleButton days_jToggleBtn[] = new JToggleButton[42];
GregorianCalendar gc=new GregorianCalendar();
int totdays;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel(new BorderLayout());
private JPanel jPanel2 = new JPanel(new BorderLayout());
private JPanel jPanel3 = new JPanel(new BoxLayout2());
private JPanel jPanel4 = new JPanel(new GridLayout(1,7));
private JPanel jPanel5 = new JPanel(new FlowLayout());
private JPanel jPanel6 = new JPanel(new FlowLayout());
private JPanel jPanel7 = new JPanel(new FlowLayout());
private JPanel jPanel8 = new JPanel(new FlowLayout());
private JPanel jPanel9 = new JPanel(new FlowLayout());
private JPanel jPanel10 = new JPanel(new FlowLayout());
private JPanel jPanel11 = new JPanel(new FlowLayout());
private JPanel jPanel12 = new JPanel(new FlowLayout());
private JPanel jPanel13 = new JPanel(new FlowLayout());
private JPanel Days_jPanel = new JPanel(new GridLayout(6,7));
//Calendar rightNow;
private int year;
private int month;
private int day;
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
private JLabel jLabel8 = new JLabel();
private JLabel jLabel9 = new JLabel();
private JSpinner Month_choice;
private JSpinner Year_choice;
private JPanel jPanel17 = new JPanel();
private JPanel jPanel18 = new JPanel();
public JTextField date_jTextField = new JTextField();
private BorderLayout borderLayout2 = new BorderLayout();
private JLabel jLabel10 = new JLabel();
private static int numOfMonths = 12;
private static int numOfYearsShown = 21;
private Integer[] monthValues = new Integer[numOfMonths];
private Integer[] yearValues = new Integer[numOfYearsShown];
public ChooseDate() {
//super("selected date",true,true);
//this.setFrameIcon(MainFrame.getImage("title2"));
//setVisible(false);
//setResizable(false);
//setLocation(200,140);
setSize(356,260);
for(int i=0;i<12;i++)
{
monthValues[i] = new Integer(i+1);
}
Calendar rightNow = Calendar.getInstance();
year=rightNow.get(Calendar.YEAR);
month=rightNow.get(Calendar.MONTH)+1;
Month_choice = new JSpinner(new SpinnerListModel(monthValues));
Month_choice.setPreferredSize(new Dimension(40,24));
day=rightNow.get(Calendar.DATE);
setYear(year,month,day);
Month_choice.addChangeListener(new myLis(this));
Year_choice.addChangeListener(new myLis(this));
//
ButtonGroup bgroup1=new ButtonGroup();
for(int i=0;i<42;i++)
{
days_jToggleBtn[i]=new JToggleButton("");
Days_jPanel.add(days_jToggleBtn[i]);
bgroup1.add(days_jToggleBtn[i]);
}
//Day_choice.addItemListener(new myLis(this));
int selMonth=((Integer)this.Month_choice.getValue()).intValue()-1;
int selYear1=((Integer)Year_choice.getValue()).intValue();
int selYear = selYear1- 1900;
Date d1 = new Date(selYear,selMonth,1);
int day = d1.getDay();
this.setVal(d1,day,selMonth,selYear);
days_jToggleBtn[rightNow.get(Calendar.DATE)+day-1].setSelected(true);
setLabelName();
try
{
rightNow = sys.getNow();
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
setVisible(true);
}
private void jbInit() throws Exception
{
//this.setTitle("ChooseDate");
this.setLayout(borderLayout1);
jPanel3.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
jPanel4.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
Days_jPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
jLabel1.setPreferredSize(new Dimension(35, 25));
jLabel2.setPreferredSize(new Dimension(30, 25));
Month_choice.setSize(new Dimension(70, 20));
Month_choice.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
Month_choice_itemStateChanged(e);
}
});
Year_choice.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
Year_choice_itemStateChanged(e);
}
});
jPanel17.setPreferredSize(new Dimension(142, 30));
jPanel18.setLayout(borderLayout2);
date_jTextField.setPreferredSize(new Dimension(87, 22));
date_jTextField.setFocusable(false);
jLabel10.setPreferredSize(new Dimension(30, 25));
jPanel3.add(jPanel6, null);
jPanel3.add(jPanel5, null);
jPanel18.add(date_jTextField, BorderLayout.NORTH);
jPanel17.add(jLabel10, null);
jPanel17.add(jPanel18, null);
jPanel3.add(jPanel17, null);
jPanel5.add(jLabel1, null);
jPanel5.add(Month_choice, null);
jPanel6.add(jLabel2, null);
jPanel6.add(Year_choice, null);
jPanel1.add(jPanel3, BorderLayout.NORTH);
jPanel7.add(jLabel3, null);
jPanel4.add(jPanel7, null);
jPanel8.add(jLabel4, null);
jPanel4.add(jPanel8, null);
jPanel9.add(jLabel5, null);
jPanel4.add(jPanel9, null);
jPanel10.add(jLabel6, null);
jPanel4.add(jPanel10, null);
jPanel11.add(jLabel7, null);
jPanel4.add(jPanel11, null);
jPanel12.add(jLabel8, null);
jPanel4.add(jPanel12, null);
jPanel13.add(jLabel9, null);
jPanel4.add(jPanel13, null);
jPanel1.add(jPanel4, BorderLayout.CENTER);
jPanel2.add(Days_jPanel, BorderLayout.CENTER);
this.add(jPanel1, BorderLayout.NORTH);
this.add(jPanel2, BorderLayout.CENTER);
}
class myLis implements ChangeListener
{
ChooseDate calLis;
public myLis(ChooseDate c)
{
calLis=c;
}
public void stateChanged(ChangeEvent i)
{
int selMonth=((Integer)calLis.Month_choice.getValue()).intValue()-1;
int selYear1=((Integer)calLis.Year_choice.getValue()).intValue(); /////////////
int selYear = selYear1- 1900;
Date d1 = new Date(selYear,selMonth,1);
int day = d1.getDay();
calLis.setVal(d1,day,selMonth,selYear);
days_jToggleBtn[day].setSelected(true);
}
}
void setVal(Date date,int iday,int iselMonth,int iselYear)
{
gc.setTime(date);
if(iselMonth==0 || iselMonth==2 || iselMonth==4 || iselMonth==6 || iselMonth== 7 ||iselMonth==9 || iselMonth==11)
{
totdays=31;
setButtons(iday,totdays);
}
if(iselMonth==3 || iselMonth==5 || iselMonth==8 || iselMonth==10)
{
totdays=30;
setButtons(iday,totdays);
}
if(gc.isLeapYear(iselYear) && iselMonth==1)
{
totdays=29;
setButtons(iday,totdays);
}
if( !gc.isLeapYear(iselYear) && iselMonth==1)
{
totdays=28;
setButtons(iday,totdays);
}
}
void setYear(int currentyear,int currentmonth,int currentday)
{
for(int i = 0, adder=currentyear-10; adder<=currentyear+10; i++,adder++)
{
yearValues[i] = new Integer(adder);
}
Year_choice = new JSpinner(new SpinnerListModel(yearValues));
Year_choice.setValue(new Integer(currentyear));
Month_choice.setValue(new Integer(currentmonth));
date_jTextField.setText(" "+currentyear+" / "+currentmonth+" / "+currentday);
}
void setButtons(int myday,int mytotdays)
{
int count=0;
if ( myday>0)
{
int blank= myday;
for( ;blank>0;blank--,count++)
{
days_jToggleBtn[count].setText("");
days_jToggleBtn[count].setEnabled(false);
days_jToggleBtn[count].setSelected(false);
}
}
//Day_choice.removeAll();
for(int i=1;i<=mytotdays; i++,count++)
{
days_jToggleBtn[count].setText(""+i);
days_jToggleBtn[count].setEnabled(true);
days_jToggleBtn[count].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Day_jToggleButton_actionPerformed(e);
}
});
days_jToggleBtn[count].addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JToggleButton jtb = (JToggleButton)e.getSource();
jtb.setBackground(jtb.isSelected() ? new Color(51, 204, 244) : null);
}
});
}
for(int j = 1;count < 42; j++,count++)
{
days_jToggleBtn[count].setText("");
days_jToggleBtn[count].setEnabled(false);
days_jToggleBtn[count].setSelected(false);
}
//Day_choice.select(0);
// days_jToggleBtn[Day_choice.getSelectedIndex()+1].setSelected(true);
}
private void setLabelName()
{
jLabel1.setText(sys.getApplictionLabel("Calendar","month","Month"));
jLabel2.setText(sys.getApplictionLabel("Calendar","year","Year"));
jLabel10.setText(sys.getApplictionLabel("Calendar","date","Date"));
jLabel4.setText(sys.getApplictionLabel("Calendar","mon","MON"));
jLabel5.setText(sys.getApplictionLabel("Calendar","tue","TUE"));
jLabel6.setText(sys.getApplictionLabel("Calendar","wed","WED"));
jLabel7.setText(sys.getApplictionLabel("Calendar","thur","THUR"));
jLabel8.setText(sys.getApplictionLabel("Calendar","fri","FRI"));
jLabel9.setText(sys.getApplictionLabel("Calendar","sat","SAT"));
jLabel3.setText(sys.getApplictionLabel("Calendar","sun","SUN"));
}
public String getDate(){
if (year != 0 && month != 0 && day != 0)
return String.valueOf(year)+"/"+String.valueOf(month)+"/"+String.valueOf(day);
return "";
}
/*
if(table != null){
((TableDataModel)table.getModel()).setValueAt(date,curRow, curCol);
table.removeEditor();
}
if(textfield != null)
textfield.setText(date);
if(txtdatefield != null)
txtdatefield.setValue(txtdatefield.toDate(date));
*/
private void Day_jToggleButton_actionPerformed(ActionEvent e)
{
int count=0;
for(int j = 1;count < 42; j++,count++)
{
days_jToggleBtn[count].setBackground(null);
if (days_jToggleBtn[count].isSelected())
{
day=Integer.parseInt( days_jToggleBtn[count].getText());
date_jTextField.setText(" "+year+" / "+month+" / "+day);
}
}
}
private void Year_choice_itemStateChanged(ChangeEvent e)
{
year=((Integer)Year_choice.getValue()).intValue();
day=1;
date_jTextField.setText(" "+year+" / "+month+" / "+day);
}
private void Month_choice_itemStateChanged(ChangeEvent e)
{
month=((Integer)Month_choice.getValue()).intValue();
day=1;
date_jTextField.setText(" "+year+" / "+month+" / "+day);
}
}