1 package snow.utils.gui; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.border.*; 7 import javax.swing.event.*; 8 import java.util.*; 9 import java.text.*; 10 11 public final class DateEditorPanel extends JPanel 12 { 13 private Calendar selectedDate = Calendar.getInstance(); 14 15 private final int fontSize = UIManager.getFont("Label.font").getSize(); 16 private final DaysPanel daysPanel; 17 private final MonthPanel monthPanel; 18 private final YearPanel yearPanel; 19 20 private final Font boldFont; 21 private final Font smallFont; 22 23 public DateEditorPanel(Date date) 24 { 25 super(new BorderLayout()); 26 27 smallFont = new Font("Dialog", Font.PLAIN, Math.max(fontSize-2, 9)); 28 boldFont = new Font("Dialog", Font.BOLD, Math.min(fontSize+2, 12)); 29 30 selectedDate.setTime(date); 31 selectedDate.setLenient(true); 33 monthPanel = new MonthPanel(); 34 daysPanel = new DaysPanel(); 35 yearPanel = new YearPanel(); 36 37 add(monthPanel, BorderLayout.NORTH); 38 add(daysPanel, BorderLayout.CENTER); 39 add(yearPanel, BorderLayout.SOUTH); 40 41 42 monthPanel.updateLabel(); 43 yearPanel.updateLabel(); 44 daysPanel.update(); 45 } 47 public Date getSelectedDate() 48 { 49 return this.selectedDate.getTime(); 50 } 51 52 class DaysPanel extends JPanel 53 { 54 String [] days = new String []{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; 55 public DaysPanel() 56 { 57 super(new GridLayout(7,7)); 58 } 60 61 private void setColor(int dayInWeek, JComponent c) 62 { 63 c.setOpaque(true); 64 if(dayInWeek==0 || dayInWeek==6) 65 { 66 c.setBackground(new Color(250,200,200,255)); 67 } 68 else 69 { 70 c.setBackground(new Color(186,250,181,255)); 71 } 72 } 73 74 public void update() 75 { 76 this.removeAll(); 77 for(int i=0; i<7; i++) 78 { 79 JLabel dayLabel = new JLabel(days[i]); 80 add(dayLabel); 81 dayLabel.setFont(smallFont); 82 dayLabel.setHorizontalAlignment(JLabel.CENTER); 83 dayLabel.setBorder(null); 84 } 85 86 int selectedDayOfWeek = selectedDate.get(Calendar.DAY_OF_WEEK)-1; int selectedDay = selectedDate.get(Calendar.DAY_OF_MONTH); 90 System.out.println("day="+selectedDay+" in week="+selectedDayOfWeek); 91 92 int weekDayOfMonthFirst = (7+selectedDayOfWeek - (selectedDay-1)%7) % 7; 94 int numberOfDaysInMonth = selectedDate.getActualMaximum(Calendar.DAY_OF_MONTH); 95 System.out.println(""+weekDayOfMonthFirst); 96 97 int dayInWeek = 0; 98 for(int i=0; i<weekDayOfMonthFirst; i++) 99 { 100 JLabel lab = new JLabel(""); 101 setColor(dayInWeek%7, lab); 102 dayInWeek++; 103 add(lab); 104 } 105 106 for(int i=1; i<=numberOfDaysInMonth; i++) 107 { 108 final int ii = i; 109 JButton jb = new JButton(""+(i)); 110 jb.addActionListener(new ActionListener() 111 { 112 public void actionPerformed(ActionEvent e) 113 { 114 selectedDate.set(Calendar.DAY_OF_MONTH, ii); 115 update(); 116 } 117 }); 118 jb.setBorder(null); 119 jb.setFocusPainted(false); 120 if(i==selectedDay) 121 { 122 jb.setFont(boldFont); 123 } 124 else 125 { 126 jb.setFont(smallFont); 127 } 128 add(jb); 129 setColor(dayInWeek%7, jb); 130 dayInWeek++; 131 } 132 133 int remains = 42-numberOfDaysInMonth-weekDayOfMonthFirst; 134 for(int i=0; i<remains; i++) 135 { 136 JLabel lab = new JLabel(""); 137 setColor(dayInWeek%7, lab); 138 dayInWeek++; 139 add(lab); 140 } 141 } 142 } 143 144 class MonthPanel extends JPanel 145 { 146 JButton shiftUpMonthBT = new JButton(" > "); 147 JButton shiftDownMonthBT = new JButton(" < "); 148 JLabel monthLabel = new JLabel(); 149 SimpleDateFormat monthFormat = new SimpleDateFormat("MMMMMMMMMM"); 150 151 public MonthPanel() 152 { 153 super(new BorderLayout()); 154 monthLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); 155 monthLabel.setHorizontalAlignment(JLabel.CENTER); 156 monthLabel.setFont(boldFont); 157 158 add(shiftDownMonthBT, BorderLayout.WEST); 159 add(monthLabel, BorderLayout.CENTER); 160 add(shiftUpMonthBT, BorderLayout.EAST); 161 162 shiftUpMonthBT.setBorder(null); 163 shiftDownMonthBT.setBorder(null); 164 165 shiftUpMonthBT.addActionListener(new ActionListener() 166 { 167 public void actionPerformed(ActionEvent e) 168 { 169 selectedDate.set(Calendar.MONTH, 170 selectedDate.get(Calendar.MONTH)+1 ); 171 updateLabel(); 172 yearPanel.updateLabel(); 173 daysPanel.update(); 174 } 175 }); 176 177 shiftDownMonthBT.addActionListener(new ActionListener() 178 { 179 public void actionPerformed(ActionEvent e) 180 { 181 selectedDate.set(Calendar.MONTH, 182 selectedDate.get(Calendar.MONTH)-1 ); 183 updateLabel(); 184 yearPanel.updateLabel(); 185 daysPanel.update(); 186 } 187 }); 188 189 } 191 192 public void updateLabel() 193 { 194 monthLabel.setText( monthFormat.format( selectedDate.getTime() )); 195 } 196 } 197 198 199 class YearPanel extends JPanel 200 { 201 JButton shiftUpMonthBT = new JButton(" > "); 202 JButton shiftDownMonthBT = new JButton(" < "); 203 JLabel monthLabel = new JLabel(); 204 SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); 205 206 public YearPanel() 207 { 208 super(new BorderLayout()); 209 monthLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); 210 monthLabel.setHorizontalAlignment(JLabel.CENTER); 211 monthLabel.setFont(boldFont); 212 213 add(shiftDownMonthBT, BorderLayout.WEST); 214 add(monthLabel, BorderLayout.CENTER); 215 add(shiftUpMonthBT, BorderLayout.EAST); 216 217 shiftUpMonthBT.setBorder(null); 218 shiftDownMonthBT.setBorder(null); 219 220 221 shiftUpMonthBT.addActionListener(new ActionListener() 222 { 223 public void actionPerformed(ActionEvent e) 224 { 225 selectedDate.set(Calendar.YEAR, 226 (selectedDate.get(Calendar.YEAR)+1) ); 227 updateLabel(); 228 daysPanel.update(); 229 } 230 }); 231 232 shiftDownMonthBT.addActionListener(new ActionListener() 233 { 234 public void actionPerformed(ActionEvent e) 235 { 236 selectedDate.set(Calendar.YEAR, 237 (selectedDate.get(Calendar.YEAR)-1) ); 238 updateLabel(); 239 daysPanel.update(); 240 } 241 }); 242 243 } 245 246 public void updateLabel() 247 { 248 monthLabel.setText( yearFormat.format( selectedDate.getTime() )); 249 } 250 } 251 252 253 public static void main(String [] a) 254 { 255 JFrame jf = new JFrame("test"); 256 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 257 DateEditorPanel dv = new DateEditorPanel(new Date()); 258 259 jf.getContentPane().add(dv); 260 jf.setSize(200, 250); 262 jf.setVisible(true); 263 264 } 266 267 268 269 } | Popular Tags |