1 package sellwin.gui; 2 3 import sellwin.utils.*; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.util.*; 8 import java.text.*; 9 10 14 19 public class CalendarDialog extends javax.swing.JDialog implements GUIChars { 20 21 private static final int HEIGHT = 26; 22 private static final int WIDTH = 25; 23 24 private static final String [] HOURS = { 25 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 26 "12" }; 27 private static final String [] MINUTES = { 28 "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 29 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 30 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 31 "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 32 "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 33 "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 34 }; 35 private static final String [] AM_PM = { "AM", "PM" }; 36 37 private static java.awt.Dimension STANDARD_DIMENSION = new Dimension(HEIGHT, WIDTH); 38 39 private Date date; 40 private Calendar thisMonthCal = Calendar.getInstance(); 41 private Calendar currentCal = Calendar.getInstance(); 42 43 private JPanel buttonPanel; 44 private JButton previousButton, nextButton; 45 private JLabel monthYearLabel; 46 private static final int MAX_BUTTONS=35; 47 private ArrayList dayButtons = new ArrayList(); 48 private JPanel dayButtonPanel; 49 private JPanel timePanel; 50 private JLabel sundayLabel; 51 private JLabel mondayLabel; 52 private JLabel tuesdayLabel; 53 private JLabel wednesdayLabel; 54 private JLabel thursdayLabel; 55 private JLabel fridayLabel; 56 private JLabel saturdayLabel; 57 private JComboBox hoursCombo, minutesCombo, ampmCombo; 58 private Whiteboard wb=null; 59 60 private DateEditorDialogListener myEditor; 61 62 68 public CalendarDialog(java.awt.Frame parent, DateEditorDialogListener editor, boolean modal) { 69 super(parent, modal); 70 myEditor = editor; 71 wb = MainWindow.getWhiteboard(); 72 73 if (wb.getLang() == null) 74 LogWrite.write("lang is null at this point"); 75 setTitle(wb.getLang().getString("calendarSelection")); 76 77 initComponents(); 78 79 setSize(290, 190); 80 81 setMonthLabel(); 82 updateDayButtons(); 83 84 nextButton.addActionListener( 85 new ActionListener() { 86 public void actionPerformed(ActionEvent e) { 87 currentCal.add(Calendar.MONTH, 1); 88 setMonthLabel(); 89 updateDayButtons(); 90 } 91 } 92 ); 93 94 previousButton.addActionListener( 95 new ActionListener() { 96 public void actionPerformed(ActionEvent e) { 97 currentCal.add(Calendar.MONTH, -1); 98 setMonthLabel(); 99 updateDayButtons(); 100 } 101 } 102 ); 103 } 104 105 109 private final void initComponents() { 110 buttonPanel = new JPanel(); 111 previousButton = new JButton(); 112 monthYearLabel = new JLabel(); 113 nextButton = new JButton(); 114 dayButtonPanel = new JPanel(); 115 timePanel = new JPanel(); 116 117 hoursCombo = new JComboBox(HOURS); 118 minutesCombo = new JComboBox(MINUTES); 119 minutesCombo.setMaximumRowCount(10); 120 ampmCombo = new JComboBox(AM_PM); 121 122 sundayLabel = new JLabel("Sun", SwingConstants.CENTER); 123 sundayLabel.setPreferredSize(STANDARD_DIMENSION); 124 sundayLabel.setMinimumSize(STANDARD_DIMENSION); 125 sundayLabel.setMaximumSize(STANDARD_DIMENSION); 126 mondayLabel = new JLabel("Mon", SwingConstants.CENTER); 127 mondayLabel.setPreferredSize(STANDARD_DIMENSION); 128 mondayLabel.setMinimumSize(STANDARD_DIMENSION); 129 mondayLabel.setMaximumSize(STANDARD_DIMENSION); 130 tuesdayLabel = new JLabel("Tue", SwingConstants.CENTER); 131 tuesdayLabel.setPreferredSize(STANDARD_DIMENSION); 132 tuesdayLabel.setMinimumSize(STANDARD_DIMENSION); 133 tuesdayLabel.setMaximumSize(STANDARD_DIMENSION); 134 wednesdayLabel = new JLabel("Wed", SwingConstants.CENTER); 135 wednesdayLabel.setPreferredSize(STANDARD_DIMENSION); 136 wednesdayLabel.setMinimumSize(STANDARD_DIMENSION); 137 wednesdayLabel.setMaximumSize(STANDARD_DIMENSION); 138 thursdayLabel = new JLabel("Thu", SwingConstants.CENTER); 139 thursdayLabel.setPreferredSize(STANDARD_DIMENSION); 140 thursdayLabel.setMinimumSize(STANDARD_DIMENSION); 141 thursdayLabel.setMaximumSize(STANDARD_DIMENSION); 142 fridayLabel = new JLabel("Fri", SwingConstants.CENTER); 143 fridayLabel.setPreferredSize(STANDARD_DIMENSION); 144 fridayLabel.setMinimumSize(STANDARD_DIMENSION); 145 fridayLabel.setMaximumSize(STANDARD_DIMENSION); 146 saturdayLabel = new JLabel("Sat", SwingConstants.CENTER); 147 saturdayLabel.setPreferredSize(STANDARD_DIMENSION); 148 saturdayLabel.setMinimumSize(STANDARD_DIMENSION); 149 saturdayLabel.setMaximumSize(STANDARD_DIMENSION); 150 151 addWindowListener(new java.awt.event.WindowAdapter () { 152 public void windowClosing(java.awt.event.WindowEvent evt) { 153 closeDialog(evt); 154 } 155 }); 156 157 previousButton.setText("<"); 158 buttonPanel.add(previousButton); 159 160 monthYearLabel.setText("Jan 2002"); 161 buttonPanel.add(monthYearLabel); 162 163 nextButton.setText(">"); 164 buttonPanel.add(nextButton); 165 166 getContentPane().add(buttonPanel, java.awt.BorderLayout.NORTH); 167 168 dayButtonPanel.setLayout(new java.awt.GridLayout (6, 7, 0, 0)); 169 170 dayButtonPanel.add(sundayLabel); 171 dayButtonPanel.add(mondayLabel); 172 dayButtonPanel.add(tuesdayLabel); 173 dayButtonPanel.add(wednesdayLabel); 174 dayButtonPanel.add(thursdayLabel); 175 dayButtonPanel.add(fridayLabel); 176 dayButtonPanel.add(saturdayLabel); 177 178 timePanel.add(hoursCombo); 179 timePanel.add(minutesCombo); 180 timePanel.add(ampmCombo); 181 182 JButton butt; 183 ButtonPressAction buttonPressAction = new ButtonPressAction(); 184 for (int i=0;i<MAX_BUTTONS;i++) { 185 butt = new JButton(Integer.toString(i)); 186 butt.addActionListener(buttonPressAction); 187 dayButtons.add(butt); 188 butt.setMargin(new Insets(0, 0, 0, 0)); 189 butt.setPreferredSize(STANDARD_DIMENSION); 190 butt.setMaximumSize(STANDARD_DIMENSION); 191 butt.setMinimumSize(STANDARD_DIMENSION); 192 dayButtonPanel.add(butt); 193 } 194 195 196 getContentPane().add(dayButtonPanel, java.awt.BorderLayout.CENTER); 197 getContentPane().add(timePanel, java.awt.BorderLayout.SOUTH); 198 199 pack(); 200 } 201 202 206 private final void closeDialog(java.awt.event.WindowEvent evt) { 207 setVisible(false); 208 dispose(); 209 } 210 211 212 218 public final void setDate(Date d) { 219 this.date = d; 220 Calendar cal = new GregorianCalendar(); 221 cal.setTime(d); 222 int hour = cal.get(Calendar.HOUR); 223 hoursCombo.setSelectedItem(Integer.toString(hour)); 224 int minute = cal.get(Calendar.MINUTE); 225 minutesCombo.setSelectedItem(Integer.toString(minute)); 226 int ampm = cal.get(Calendar.AM_PM); 227 if (ampm == Calendar.PM) 228 ampmCombo.setSelectedItem("PM"); 229 else 230 ampmCombo.setSelectedItem("AM"); 231 } 232 233 237 public final Date getDate() { 238 return date; 239 } 240 241 244 private final void setMonthLabel() { 245 246 Date d = currentCal.getTime(); 247 SimpleDateFormat formatter = new SimpleDateFormat("MMM yyyy"); 248 monthYearLabel.setText(formatter.format(d)); 249 } 250 251 254 private final void updateDayButtons() { 255 boolean thisMonth=false; 256 257 if ((currentCal.get(Calendar.YEAR) == thisMonthCal.get(Calendar.YEAR)) && 258 (currentCal.get(Calendar.MONTH) == thisMonthCal.get(Calendar.MONTH))) 259 thisMonth=true; 260 261 currentCal.set(Calendar.DAY_OF_MONTH, 1); int day_of_week = currentCal.get(Calendar.DAY_OF_WEEK); 263 int startingAt=0; 264 265 JButton dayButton; 267 for (int i=0;i<dayButtons.size();i++) { 268 dayButton = (JButton)dayButtons.get(i); 269 dayButton.setText(""); 270 } 271 272 switch (day_of_week) { 274 case Calendar.MONDAY: 275 startingAt = 1; 276 break; 277 case Calendar.TUESDAY: 278 startingAt = 2; 279 break; 280 case Calendar.WEDNESDAY: 281 startingAt = 3; 282 break; 283 case Calendar.THURSDAY: 284 startingAt = 4; 285 break; 286 case Calendar.FRIDAY: 287 startingAt = 5; 288 break; 289 case Calendar.SATURDAY: 290 startingAt = 6; 291 break; 292 case Calendar.SUNDAY: 293 startingAt = 0; 294 break; 295 default: 296 Exception e = new Exception (); 297 e.printStackTrace(); 298 System.exit(1); 299 } 300 301 int last_day_in_month = currentCal.getActualMaximum(Calendar.DATE); 303 int dayNumber=1; 305 for (int j=startingAt;j<MAX_BUTTONS;j++) { 306 dayButton = (JButton)(dayButtons.get(j)); 307 dayButton.setText(Integer.toString(dayNumber)); 308 if (thisMonth) { 309 if (dayNumber == thisMonthCal.get(Calendar.DATE)) 310 dayButton.setForeground(Color.red); 311 } else 312 dayButton.setForeground(Color.black); 313 314 dayNumber++; 315 316 if (j > last_day_in_month) break; 317 } 318 319 } 320 321 class ButtonPressAction implements ActionListener { 322 public void actionPerformed(ActionEvent e) { 323 JButton button = (JButton)(e.getSource()); 324 325 currentCal.set(Calendar.DATE, Integer.parseInt(button.getText())); 326 currentCal.set(Calendar.HOUR, Integer.parseInt((String )hoursCombo.getSelectedItem())); 327 currentCal.set(Calendar.MINUTE, Integer.parseInt((String )minutesCombo.getSelectedItem())); 328 String ampm = (String )(ampmCombo.getSelectedItem()); 329 if (ampm.equals("PM")) 330 currentCal.set(Calendar.AM_PM, Calendar.PM); 331 else 332 currentCal.set(Calendar.AM_PM, Calendar.AM); 333 334 335 date = currentCal.getTime(); 337 myEditor.setDate(date); 338 hide(); 339 } 340 } 341 342 343 346 public final void setColors() { 347 } 348 349 352 public final void setFonts() { 353 } 354 355 358 public final void setLang() { 359 setTitle(wb.getLang().getString("calendarSelection")); 360 } 361 362 } 363 | Popular Tags |