001: package com.xoetrope.awt.date;
002:
003: import java.util.Calendar;
004: import java.util.Date;
005:
006: import java.awt.Color;
007: import java.awt.Dimension;
008: import java.awt.FontMetrics;
009: import java.awt.Graphics;
010: import java.awt.event.MouseEvent;
011: import java.awt.event.MouseListener;
012: import java.awt.event.MouseMotionListener;
013: import java.awt.Panel;
014:
015: import com.xoetrope.event.XDateListener;
016: import net.xoetrope.xui.style.XStyle;
017: import net.xoetrope.xui.style.XStyleManager;
018: import net.xoetrope.xui.XProjectManager;
019:
020: /**
021: * A date chooser class. This class is used by the XDateChooser class to display
022: * a month of the calendar. A date can then be selected visually.
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p>$Revision: 1.2 $</p>
028: */
029: class XDateChooserPanel extends Panel implements MouseListener,
030: MouseMotionListener {
031: protected XStyleManager styleManager;
032: private Calendar calendar;
033:
034: protected int selectedDay;
035: protected int highlightedDay;
036:
037: private boolean mouseEventInvoked = false;
038: private int cellHeight = 10;
039: private int cellWidth = 10;
040: private int startDay = 1;
041: private int oldX = 0;
042: private int oldY = 0;
043:
044: private Color foregroundColor, backgroundColor, highlightedColor,
045: selectedColor, titleColor, threeDColor, weekendColor;
046: private Color highlightedBkColor, selectedBkColor, titleBkColor,
047: threeDBkColor, weekendBkColor;
048: private String styleWeekend, styleSelected, styleHighlighted;
049:
050: private XDateListener listener;
051:
052: public XDateChooserPanel() {
053: calendar = Calendar.getInstance();
054: selectedDay = calendar.get(Calendar.DATE);
055:
056: addMouseListener(this );
057: addMouseMotionListener(this );
058:
059: styleWeekend = "base";
060: styleSelected = "base";
061: styleHighlighted = "base";
062:
063: styleManager = XProjectManager.getStyleManager();
064:
065: XStyle weekendStyle = styleManager.getStyle(styleWeekend);
066: XStyle selectedStyle = styleManager.getStyle(styleSelected);
067: XStyle highlightedStyle = styleManager
068: .getStyle(styleHighlighted);
069: selectedColor = selectedStyle
070: .getStyleAsColor(XStyle.COLOR_FORE);
071: selectedBkColor = selectedStyle
072: .getStyleAsColor(XStyle.COLOR_BACK);
073: highlightedColor = highlightedStyle
074: .getStyleAsColor(XStyle.COLOR_FORE);
075: highlightedBkColor = highlightedStyle
076: .getStyleAsColor(XStyle.COLOR_BACK);
077: weekendColor = weekendStyle.getStyleAsColor(XStyle.COLOR_FORE);
078: weekendBkColor = weekendStyle
079: .getStyleAsColor(XStyle.COLOR_BACK);
080: }
081:
082: /**
083: * Gets the current date.
084: * @return
085: */
086: public Date getDate() {
087: return calendar.getTime();
088: }
089:
090: /**
091: * Sets the current date
092: * @param newDate the new date
093: */
094: public void setDate(Date newDate) {
095: calendar.setTime(newDate);
096: selectedDay = calendar.get(Calendar.DATE);
097: repaint();
098: }
099:
100: /**
101: * Move to the previous month
102: */
103: public void prev() {
104: if (calendar.get(Calendar.MONTH) == 0)
105: calendar.roll(Calendar.YEAR, false);
106: calendar.roll(Calendar.MONTH, false);
107: selectedDay = calendar.get(Calendar.DAY_OF_MONTH);
108: highlightedDay = 0;
109: repaint();
110: }
111:
112: /**
113: * Move to the next month
114: */
115: public void next() {
116: if (calendar.get(Calendar.MONTH) == 11)
117: calendar.roll(Calendar.YEAR, true);
118: calendar.roll(Calendar.MONTH, true);
119: if (calendar.get(Calendar.MONTH) == 12)
120: selectedDay = 1;
121: highlightedDay = 0;
122: repaint();
123: }
124:
125: /**
126: * Renders the current month
127: * @param g
128: */
129: public void paint(Graphics g) {
130: backgroundColor = getBackground();
131: foregroundColor = getForeground();
132:
133: Color weekendShadow = new Color(240, 240, 240);
134: Dimension d = getSize();
135: int width = Math.max(d.width, 2);
136: int height = Math.max(d.height, 2);
137:
138: cellHeight = Math.max(1, height / 6);
139: cellWidth = Math.max(1, width / 7);
140:
141: g.setColor(backgroundColor);
142: g.fillRect(0, 0, width, height);
143: int month = calendar.get(Calendar.MONTH);
144: int year = calendar.get(Calendar.YEAR);
145: int day = calendar.get(Calendar.DATE);
146: calendar.set(year, month, 1);
147: int cellPos = startDay = calendar.get(calendar.DAY_OF_WEEK) - 1;
148:
149: FontMetrics fm = g.getFontMetrics();
150: int fontOffset = (cellHeight + fm.getAscent()) / 2;
151: int pad = (cellWidth - fm.stringWidth("28")) / 2;
152: for (int i = 1; i < 32; i++) {
153: // Calculate the position of the day in the months grid as the first day of
154: // the month may not be the first day of the week.
155: int cellX = cellPos % 7;
156: int cellY = cellPos / 7;
157:
158: // Choose the highlight colour
159: if (i == highlightedDay) {
160: if (i == selectedDay)
161: g.setColor(selectedBkColor);
162: else
163: g.setColor(highlightedBkColor);
164: g.fillRect(cellX * cellWidth, cellY * cellHeight,
165: cellWidth, cellHeight);
166: if (i == selectedDay) {
167: g.setColor(selectedColor);
168: g.drawRect(cellX * cellWidth + 1, cellY
169: * cellHeight + 1, cellWidth - 2,
170: cellHeight - 2);
171: } else
172: g.setColor(highlightedColor);
173: } else if (i == selectedDay) {
174: g.setColor(selectedBkColor);
175: g.fillRect(cellX * cellWidth, cellY * cellHeight,
176: cellWidth, cellHeight);
177: g.setColor(selectedColor);
178: } else {
179: // Shade weekend days
180: if ((cellX == 0) || (cellX == 6)) {
181: g.setColor(weekendBkColor);
182: g.fillRect(cellX * cellWidth, cellY * cellHeight,
183: cellWidth, cellHeight);
184: g.setColor(weekendColor);
185: } else
186: g.setColor(foregroundColor);
187: }
188: g.drawString(new Integer(i).toString(), pad + cellX
189: * cellWidth, cellY * cellHeight + fontOffset);
190:
191: calendar.roll(Calendar.DATE, 1);
192: cellPos++;
193: if (calendar.get(Calendar.DATE) == 1)
194: break;
195: }
196: g.setColor(backgroundColor);
197: g.drawRect(0, 0, width - 1, height - 1);
198:
199: calendar.set(year, month, day);
200: }
201:
202: /**
203: * Selects the highlighted date
204: */
205: private void setSelection() {
206: selectedDay = highlightedDay;
207: if ((selectedDay <= 0)
208: || (selectedDay > calendar
209: .getActualMaximum(calendar.DAY_OF_MONTH)))
210: return;
211:
212: int month = calendar.get(Calendar.MONTH);
213: int year = calendar.get(Calendar.YEAR);
214: int day = calendar.get(Calendar.DATE);
215: calendar.set(year, month, selectedDay);
216:
217: repaint();
218:
219: if (listener != null)
220: listener.setDate(calendar.getTime());
221: }
222:
223: /**
224: * Highlights the date under the cursor.
225: * @param e
226: */
227: private void showSelection(MouseEvent e) {
228: int x = e.getX();
229: int y = e.getY();
230: highlightedDay = (x / cellWidth + (e.getY() / cellHeight) * 7)
231: - startDay + 1;
232: repaint(0, oldX, oldY, cellWidth, cellHeight);
233: oldX = x - x % cellWidth;
234: oldY = y - y % cellHeight;
235: repaint(0, oldX, oldY, cellWidth, cellHeight);
236: }
237:
238: public void mouseClicked(MouseEvent e) {
239: if (!mouseEventInvoked)
240: setSelection();
241: mouseEventInvoked = true;
242: }
243:
244: public void mouseEntered(MouseEvent e) {
245: showSelection(e);
246: }
247:
248: public void mouseExited(MouseEvent e) {
249: showSelection(e);
250: }
251:
252: public void mousePressed(MouseEvent e) {
253: mouseEventInvoked = false;
254: }
255:
256: public void mouseReleased(MouseEvent e) {
257: if (!mouseEventInvoked)
258: setSelection();
259: mouseEventInvoked = true;
260: }
261:
262: public void mouseMoved(MouseEvent e) {
263: showSelection(e);
264: }
265:
266: public void mouseDragged(MouseEvent e) {
267: showSelection(e);
268: }
269:
270: void setDateListener(XDateListener dl) {
271: listener = dl;
272: }
273:
274: /**
275: * Get the style asociated with the selected elements
276: * @return the style name
277: */
278: public String getStyleSelected() {
279: return styleSelected;
280: }
281:
282: /**
283: * Get the style asociated with the selected elements
284: * @param newStyle the style name
285: */
286: public void setStyleSelected(String newStyle) {
287: styleSelected = newStyle;
288: XStyle selectedStyle = styleManager.getStyle(styleSelected);
289: selectedColor = selectedStyle
290: .getStyleAsColor(XStyle.COLOR_FORE);
291: selectedBkColor = selectedStyle
292: .getStyleAsColor(XStyle.COLOR_BACK);
293: }
294:
295: /**
296: * Get the style asociated with the weekend element
297: * @return the style name
298: */
299: public String getStyleWeekend() {
300: return styleWeekend;
301: }
302:
303: /**
304: * set the style asociated with weekend element
305: * @param newStyle the style name
306: */
307: public void setStyleWeekend(String newStyle) {
308: styleWeekend = newStyle;
309: XStyle weekendStyle = styleManager.getStyle(styleWeekend);
310: weekendColor = weekendStyle.getStyleAsColor(XStyle.COLOR_FORE);
311: weekendBkColor = weekendStyle
312: .getStyleAsColor(XStyle.COLOR_BACK);
313: repaint();
314: }
315:
316: /**
317: * Get the style asociated with the highlighted element
318: * @return the style name
319: */
320: public String getStyleHighlighted() {
321: return styleHighlighted;
322: }
323:
324: /**
325: * set the style asociated with highlighted element
326: * @param newStyle the style name
327: */
328: public void setStyleHighlighted(String newStyle) {
329: styleHighlighted = newStyle;
330: XStyle highlightedStyle = styleManager
331: .getStyle(styleHighlighted);
332: highlightedColor = highlightedStyle
333: .getStyleAsColor(XStyle.COLOR_FORE);
334: highlightedBkColor = highlightedStyle
335: .getStyleAsColor(XStyle.COLOR_BACK);
336: }
337: }
|