Source Code Cross Referenced for DateChooserPanel.java in  » Graphic-Library » jcommon-components » org » jfree » ui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Graphic Library » jcommon components » org.jfree.ui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ========================================================================
002:         * JCommon : a free general purpose class library for the Java(tm) platform
003:         * ========================================================================
004:         *
005:         * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006:         * 
007:         * Project Info:  http://www.jfree.org/jcommon/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         * 
027:         * ---------------------
028:         * DateChooserPanel.java
029:         * ---------------------
030:         * (C) Copyright 2000-2004, by Object Refinery Limited.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   -;
034:         *
035:         * $Id: DateChooserPanel.java,v 1.10 2005/11/16 15:58:41 taqua Exp $
036:         *
037:         * Changes (from 26-Oct-2001)
038:         * --------------------------
039:         * 26-Oct-2001 : Changed package to com.jrefinery.ui.* (DG);
040:         * 08-Dec-2001 : Dropped the getMonths() method (DG);
041:         * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042:         * 02-Nov-2005 : Fixed a bug where the current day-of-the-month is past
043:         *               the end of the newly selected month when the month or year
044:         *               combo boxes are changed - see bug id 1344319 (DG);
045:         *
046:         */
047:
048:        package org.jfree.ui;
049:
050:        import java.awt.BorderLayout;
051:        import java.awt.Color;
052:        import java.awt.Font;
053:        import java.awt.GridLayout;
054:        import java.awt.Insets;
055:        import java.awt.event.ActionEvent;
056:        import java.awt.event.ActionListener;
057:        import java.text.DateFormatSymbols;
058:        import java.util.Calendar;
059:        import java.util.Date;
060:
061:        import javax.swing.BorderFactory;
062:        import javax.swing.JButton;
063:        import javax.swing.JComboBox;
064:        import javax.swing.JLabel;
065:        import javax.swing.JPanel;
066:        import javax.swing.SwingConstants;
067:        import javax.swing.UIManager;
068:
069:        import org.jfree.date.SerialDate;
070:
071:        /**
072:         * A panel that allows the user to select a date.
073:         *
074:         * @author David Gilbert
075:         */
076:        public class DateChooserPanel extends JPanel implements  ActionListener {
077:
078:            /**
079:             * The date selected in the panel.
080:             */
081:            private Calendar chosenDate;
082:
083:            /**
084:             * The color for the selected date.
085:             */
086:            private Color chosenDateButtonColor;
087:
088:            /**
089:             * The color for dates in the current month.
090:             */
091:            private Color chosenMonthButtonColor;
092:
093:            /**
094:             * The color for dates that are visible, but not in the current month.
095:             */
096:            private Color chosenOtherButtonColor;
097:
098:            /**
099:             * The first day-of-the-week.
100:             */
101:            private int firstDayOfWeek;
102:
103:            /**
104:             * The range used for selecting years.
105:             */
106:            private int yearSelectionRange = 20;
107:
108:            /**
109:             * The font used to display the date.
110:             */
111:            private Font dateFont = new Font("SansSerif", Font.PLAIN, 10);
112:
113:            /**
114:             * A combo for selecting the month.
115:             */
116:            private JComboBox monthSelector;
117:
118:            /**
119:             * A combo for selecting the year.
120:             */
121:            private JComboBox yearSelector;
122:
123:            /**
124:             * A button for selecting today's date.
125:             */
126:            private JButton todayButton;
127:
128:            /**
129:             * An array of buttons used to display the days-of-the-month.
130:             */
131:            private JButton[] buttons;
132:
133:            /**
134:             * A flag that indicates whether or not we are currently refreshing the 
135:             * buttons.
136:             */
137:            private boolean refreshing = false;
138:
139:            /**
140:             * The ordered set of all seven days of a week,
141:             * beginning with the 'firstDayOfWeek'.
142:             */
143:            private int[] WEEK_DAYS;
144:
145:            /**
146:             * Constructs a new date chooser panel, using today's date as the initial 
147:             * selection.
148:             */
149:            public DateChooserPanel() {
150:                this (Calendar.getInstance(), false);
151:            }
152:
153:            /**
154:             * Constructs a new date chooser panel.
155:             *
156:             * @param calendar     the calendar controlling the date.
157:             * @param controlPanel a flag that indicates whether or not the 'today' 
158:             *                     button should appear on the panel.
159:             */
160:            public DateChooserPanel(final Calendar calendar,
161:                    final boolean controlPanel) {
162:
163:                super (new BorderLayout());
164:
165:                this .chosenDateButtonColor = UIManager
166:                        .getColor("textHighlight");
167:                this .chosenMonthButtonColor = UIManager.getColor("control");
168:                this .chosenOtherButtonColor = UIManager
169:                        .getColor("controlShadow");
170:
171:                // the default date is today...
172:                this .chosenDate = calendar;
173:                this .firstDayOfWeek = calendar.getFirstDayOfWeek();
174:                this .WEEK_DAYS = new int[7];
175:                for (int i = 0; i < 7; i++) {
176:                    this .WEEK_DAYS[i] = ((this .firstDayOfWeek + i - 1) % 7) + 1;
177:                }
178:
179:                add(constructSelectionPanel(), BorderLayout.NORTH);
180:                add(getCalendarPanel(), BorderLayout.CENTER);
181:                if (controlPanel) {
182:                    add(constructControlPanel(), BorderLayout.SOUTH);
183:                }
184:                setDate(calendar.getTime());
185:            }
186:
187:            /**
188:             * Sets the date chosen in the panel.
189:             *
190:             * @param theDate the new date.
191:             */
192:            public void setDate(final Date theDate) {
193:
194:                this .chosenDate.setTime(theDate);
195:                this .monthSelector.setSelectedIndex(this .chosenDate
196:                        .get(Calendar.MONTH));
197:                refreshYearSelector();
198:                refreshButtons();
199:
200:            }
201:
202:            /**
203:             * Returns the date selected in the panel.
204:             *
205:             * @return the selected date.
206:             */
207:            public Date getDate() {
208:                return this .chosenDate.getTime();
209:            }
210:
211:            /**
212:             * Handles action-events from the date panel.
213:             *
214:             * @param e information about the event that occurred.
215:             */
216:            public void actionPerformed(final ActionEvent e) {
217:
218:                if (e.getActionCommand().equals("monthSelectionChanged")) {
219:                    final JComboBox c = (JComboBox) e.getSource();
220:
221:                    // In most cases, changing the month will not change the selected
222:                    // day.  But if the selected day is 29, 30 or 31 and the newly
223:                    // selected month doesn't have that many days, we revert to the 
224:                    // last day of the newly selected month...
225:                    int dayOfMonth = this .chosenDate.get(Calendar.DAY_OF_MONTH);
226:                    this .chosenDate.set(Calendar.DAY_OF_MONTH, 1);
227:                    this .chosenDate.set(Calendar.MONTH, c.getSelectedIndex());
228:                    int maxDayOfMonth = this .chosenDate
229:                            .getActualMaximum(Calendar.DAY_OF_MONTH);
230:                    this .chosenDate.set(Calendar.DAY_OF_MONTH, Math.min(
231:                            dayOfMonth, maxDayOfMonth));
232:                    refreshButtons();
233:                } else if (e.getActionCommand().equals("yearSelectionChanged")) {
234:                    if (!this .refreshing) {
235:                        final JComboBox c = (JComboBox) e.getSource();
236:                        final Integer y = (Integer) c.getSelectedItem();
237:
238:                        // in most cases, changing the year will not change the 
239:                        // selected day.  But if the selected day is Feb 29, and the
240:                        // newly selected year is not a leap year, we revert to 
241:                        // Feb 28...
242:                        int dayOfMonth = this .chosenDate
243:                                .get(Calendar.DAY_OF_MONTH);
244:                        this .chosenDate.set(Calendar.DAY_OF_MONTH, 1);
245:                        this .chosenDate.set(Calendar.YEAR, y.intValue());
246:                        int maxDayOfMonth = this .chosenDate
247:                                .getActualMaximum(Calendar.DAY_OF_MONTH);
248:                        this .chosenDate.set(Calendar.DAY_OF_MONTH, Math.min(
249:                                dayOfMonth, maxDayOfMonth));
250:                        refreshYearSelector();
251:                        refreshButtons();
252:                    }
253:                } else if (e.getActionCommand().equals("todayButtonClicked")) {
254:                    setDate(new Date());
255:                } else if (e.getActionCommand().equals("dateButtonClicked")) {
256:                    final JButton b = (JButton) e.getSource();
257:                    final int i = Integer.parseInt(b.getName());
258:                    final Calendar cal = getFirstVisibleDate();
259:                    cal.add(Calendar.DATE, i);
260:                    setDate(cal.getTime());
261:                }
262:            }
263:
264:            /**
265:             * Returns a panel of buttons, each button representing a day in the month.
266:             * This is a sub-component of the DatePanel.
267:             *
268:             * @return the panel.
269:             */
270:            private JPanel getCalendarPanel() {
271:
272:                final JPanel p = new JPanel(new GridLayout(7, 7));
273:                final DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
274:                final String[] weekDays = dateFormatSymbols.getShortWeekdays();
275:
276:                for (int i = 0; i < this .WEEK_DAYS.length; i++) {
277:                    p.add(new JLabel(weekDays[this .WEEK_DAYS[i]],
278:                            SwingConstants.CENTER));
279:                }
280:
281:                this .buttons = new JButton[42];
282:                for (int i = 0; i < 42; i++) {
283:                    final JButton b = new JButton("");
284:                    b.setMargin(new Insets(1, 1, 1, 1));
285:                    b.setName(Integer.toString(i));
286:                    b.setFont(this .dateFont);
287:                    b.setFocusPainted(false);
288:                    b.setActionCommand("dateButtonClicked");
289:                    b.addActionListener(this );
290:                    this .buttons[i] = b;
291:                    p.add(b);
292:                }
293:                return p;
294:
295:            }
296:
297:            /**
298:             * Returns the button color according to the specified date.
299:             *
300:             * @param theDate the date.
301:             * @return the color.
302:             */
303:            private Color getButtonColor(final Calendar theDate) {
304:                if (equalDates(theDate, this .chosenDate)) {
305:                    return this .chosenDateButtonColor;
306:                } else if (theDate.get(Calendar.MONTH) == this .chosenDate
307:                        .get(Calendar.MONTH)) {
308:                    return this .chosenMonthButtonColor;
309:                } else {
310:                    return this .chosenOtherButtonColor;
311:                }
312:            }
313:
314:            /**
315:             * Returns true if the two dates are equal (time of day is ignored).
316:             *
317:             * @param c1 the first date.
318:             * @param c2 the second date.
319:             * @return boolean.
320:             */
321:            private boolean equalDates(final Calendar c1, final Calendar c2) {
322:                if ((c1.get(Calendar.DATE) == c2.get(Calendar.DATE))
323:                        && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
324:                        && (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))) {
325:                    return true;
326:                } else {
327:                    return false;
328:                }
329:            }
330:
331:            /**
332:             * Returns the first date that is visible in the grid.  This should always 
333:             * be in the month preceding the month of the selected date.
334:             *
335:             * @return the date.
336:             */
337:            private Calendar getFirstVisibleDate() {
338:                final Calendar c = Calendar.getInstance();
339:                c.set(this .chosenDate.get(Calendar.YEAR), this .chosenDate
340:                        .get(Calendar.MONTH), 1);
341:                c.add(Calendar.DATE, -1);
342:                while (c.get(Calendar.DAY_OF_WEEK) != getFirstDayOfWeek()) {
343:                    c.add(Calendar.DATE, -1);
344:                }
345:                return c;
346:            }
347:
348:            /**
349:             * Returns the first day of the week (controls the labels in the date 
350:             * panel).
351:             *
352:             * @return the first day of the week.
353:             */
354:            private int getFirstDayOfWeek() {
355:                return this .firstDayOfWeek;
356:            }
357:
358:            /**
359:             * Update the button labels and colors to reflect date selection.
360:             */
361:            private void refreshButtons() {
362:                final Calendar c = getFirstVisibleDate();
363:                for (int i = 0; i < 42; i++) {
364:                    final JButton b = this .buttons[i];
365:                    b.setText(Integer.toString(c.get(Calendar.DATE)));
366:                    b.setBackground(getButtonColor(c));
367:                    c.add(Calendar.DATE, 1);
368:                }
369:            }
370:
371:            /**
372:             * Changes the contents of the year selection JComboBox to reflect the 
373:             * chosen date and the year range.
374:             */
375:            private void refreshYearSelector() {
376:                if (!this .refreshing) {
377:                    this .refreshing = true;
378:                    this .yearSelector.removeAllItems();
379:                    final Integer[] years = getYears(this .chosenDate
380:                            .get(Calendar.YEAR));
381:                    for (int i = 0; i < years.length; i++) {
382:                        this .yearSelector.addItem(years[i]);
383:                    }
384:                    this .yearSelector.setSelectedItem(new Integer(
385:                            this .chosenDate.get(Calendar.YEAR)));
386:                    this .refreshing = false;
387:                }
388:            }
389:
390:            /**
391:             * Returns a vector of years preceding and following the specified year.  
392:             * The number of years preceding and following is determined by the 
393:             * yearSelectionRange attribute.
394:             *
395:             * @param chosenYear the selected year.
396:             * @return a vector of years.
397:             */
398:            private Integer[] getYears(final int chosenYear) {
399:                final int size = this .yearSelectionRange * 2 + 1;
400:                final int start = chosenYear - this .yearSelectionRange;
401:
402:                final Integer[] years = new Integer[size];
403:                for (int i = 0; i < size; i++) {
404:                    years[i] = new Integer(i + start);
405:                }
406:                return years;
407:            }
408:
409:            /**
410:             * Constructs a panel containing two JComboBoxes (for the month and year) 
411:             * and a button (to reset the date to TODAY).
412:             *
413:             * @return the panel.
414:             */
415:            private JPanel constructSelectionPanel() {
416:                final JPanel p = new JPanel();
417:
418:                final int minMonth = this .chosenDate.getMinimum(Calendar.MONTH);
419:                final int maxMonth = this .chosenDate.getMaximum(Calendar.MONTH);
420:                final String[] months = new String[maxMonth - minMonth + 1];
421:                System.arraycopy(SerialDate.getMonths(), minMonth, months, 0,
422:                        months.length);
423:
424:                this .monthSelector = new JComboBox(months);
425:                this .monthSelector.addActionListener(this );
426:                this .monthSelector.setActionCommand("monthSelectionChanged");
427:                p.add(this .monthSelector);
428:
429:                this .yearSelector = new JComboBox(getYears(0));
430:                this .yearSelector.addActionListener(this );
431:                this .yearSelector.setActionCommand("yearSelectionChanged");
432:                p.add(this .yearSelector);
433:
434:                return p;
435:            }
436:
437:            /**
438:             * Returns a panel that appears at the bottom of the calendar panel - 
439:             * contains a button for selecting today's date.
440:             *
441:             * @return the panel.
442:             */
443:            private JPanel constructControlPanel() {
444:
445:                final JPanel p = new JPanel();
446:                p.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
447:                this .todayButton = new JButton("Today");
448:                this .todayButton.addActionListener(this );
449:                this .todayButton.setActionCommand("todayButtonClicked");
450:                p.add(this .todayButton);
451:                return p;
452:
453:            }
454:
455:            /**
456:             * Returns the color for the currently selected date.
457:             *
458:             * @return a color.
459:             */
460:            public Color getChosenDateButtonColor() {
461:                return this .chosenDateButtonColor;
462:            }
463:
464:            /**
465:             * Redefines the color for the currently selected date.
466:             *
467:             * @param chosenDateButtonColor the new color
468:             */
469:            public void setChosenDateButtonColor(
470:                    final Color chosenDateButtonColor) {
471:                if (chosenDateButtonColor == null) {
472:                    throw new NullPointerException("UIColor must not be null.");
473:                }
474:                final Color oldValue = this .chosenDateButtonColor;
475:                this .chosenDateButtonColor = chosenDateButtonColor;
476:                refreshButtons();
477:                firePropertyChange("chosenDateButtonColor", oldValue,
478:                        chosenDateButtonColor);
479:            }
480:
481:            /**
482:             * Returns the color for the buttons representing the current month.
483:             *
484:             * @return the color for the current month.
485:             */
486:            public Color getChosenMonthButtonColor() {
487:                return this .chosenMonthButtonColor;
488:            }
489:
490:            /**
491:             * Defines the color for the buttons representing the current month.
492:             *
493:             * @param chosenMonthButtonColor the color for the current month.
494:             */
495:            public void setChosenMonthButtonColor(
496:                    final Color chosenMonthButtonColor) {
497:                if (chosenMonthButtonColor == null) {
498:                    throw new NullPointerException("UIColor must not be null.");
499:                }
500:                final Color oldValue = this .chosenMonthButtonColor;
501:                this .chosenMonthButtonColor = chosenMonthButtonColor;
502:                refreshButtons();
503:                firePropertyChange("chosenMonthButtonColor", oldValue,
504:                        chosenMonthButtonColor);
505:            }
506:
507:            /**
508:             * Returns the color for the buttons representing the other months.
509:             *
510:             * @return a color.
511:             */
512:            public Color getChosenOtherButtonColor() {
513:                return this .chosenOtherButtonColor;
514:            }
515:
516:            /**
517:             * Redefines the color for the buttons representing the other months.
518:             *
519:             * @param chosenOtherButtonColor a color.
520:             */
521:            public void setChosenOtherButtonColor(
522:                    final Color chosenOtherButtonColor) {
523:                if (chosenOtherButtonColor == null) {
524:                    throw new NullPointerException("UIColor must not be null.");
525:                }
526:                final Color oldValue = this .chosenOtherButtonColor;
527:                this .chosenOtherButtonColor = chosenOtherButtonColor;
528:                refreshButtons();
529:                firePropertyChange("chosenOtherButtonColor", oldValue,
530:                        chosenOtherButtonColor);
531:            }
532:
533:            /**
534:             * Returns the range of years available for selection (defaults to 20).
535:             * 
536:             * @return The range.
537:             */
538:            public int getYearSelectionRange() {
539:                return this .yearSelectionRange;
540:            }
541:
542:            /**
543:             * Sets the range of years available for selection.
544:             * 
545:             * @param yearSelectionRange  the range.
546:             */
547:            public void setYearSelectionRange(final int yearSelectionRange) {
548:                final int oldYearSelectionRange = this .yearSelectionRange;
549:                this .yearSelectionRange = yearSelectionRange;
550:                refreshYearSelector();
551:                firePropertyChange("yearSelectionRange", oldYearSelectionRange,
552:                        yearSelectionRange);
553:            }
554:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.