001: package com.xoetrope.swing.date;
002:
003: import java.text.SimpleDateFormat;
004: import java.util.Date;
005:
006: import java.awt.BorderLayout;
007: import java.awt.Insets;
008: import java.text.DateFormat;
009: import java.text.ParseException;
010: import java.awt.Color;
011: import java.awt.Dimension;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014:
015: import javax.swing.JButton;
016: import javax.swing.JComponent;
017: import javax.swing.BorderFactory;
018: import javax.swing.SwingConstants;
019:
020: import com.xoetrope.event.XDateListener;
021: import java.awt.FlowLayout;
022: import java.awt.Font;
023: import javax.swing.JPanel;
024:
025: import net.xoetrope.swing.util.XGradientHeaderPanel;
026: import net.xoetrope.xui.XAttributedComponent;
027: import net.xoetrope.xui.XProjectManager;
028: import net.xoetrope.xui.XTextHolder;
029: import net.xoetrope.xui.style.XStyle;
030: import net.xoetrope.xui.style.XStyleManager;
031:
032: /**
033: * A panel containing a representation of a month so that the user can select
034: * a date visually. Navigation buttons can also be added to allow the previous
035: * or following months to be shown.
036: *
037: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
038: * the GNU Public License (GPL), please see license.txt for more details. If
039: * you make commercial use of this software you must purchase a commercial
040: * license from Xoetrope.</p>
041: * <p> $Revision: 1.10 $</p>
042: */
043: public class XDateChooser extends JComponent implements
044: XAttributedComponent, ActionListener, XDateListener,
045: XTextHolder {
046: protected XStyleManager styleManager;
047:
048: protected XGradientHeaderPanel titlePanel;
049: protected XDateChooserPanel datePanel;
050: protected JButton prevBtn, nextBtn, prevYearBtn, nextYearBtn;
051:
052: private Color foregroundColor, backgroundColor;
053: private Color highlightedColor, selectedColor, titleColor,
054: threeDColor;
055: private Color highlightedBkColor, selectedBkColor, titleBkColor,
056: threeDBkColor;
057:
058: private String style3D, styleTitle;
059:
060: /**
061: * Sets up a new panel to display the current date. A title showing the month
062: * and year is also added, but the navigation buttons are omitted
063: */
064: public XDateChooser() {
065: styleManager = XProjectManager.getStyleManager();
066:
067: setLayout(new BorderLayout());
068: add(datePanel = new XDateChooserPanel(), BorderLayout.CENTER);
069:
070: titlePanel = new XGradientHeaderPanel();
071: titlePanel.setPreferredSize(new Dimension(200, 27));
072: titlePanel.setLayout(new BorderLayout());
073: titlePanel.setBorder(BorderFactory
074: .createEmptyBorder(2, 2, 2, 2));
075: titlePanel.setText(new SimpleDateFormat("MMMM yyyy")
076: .format(datePanel.getDate()));
077: add(titlePanel, BorderLayout.NORTH);
078:
079: style3D = "base";
080: styleTitle = "base";
081:
082: if (backgroundColor == null) {
083: XStyle threeDStyle = styleManager.getStyle(style3D);
084: XStyle titleStyle = styleManager.getStyle(styleTitle);
085: backgroundColor = getBackground();
086: foregroundColor = getForeground();
087: threeDColor = threeDStyle
088: .getStyleAsColor(XStyle.COLOR_FORE);
089: threeDBkColor = threeDStyle
090: .getStyleAsColor(XStyle.COLOR_BACK);
091: titleColor = titleStyle.getStyleAsColor(XStyle.COLOR_FORE);
092: titleBkColor = titleStyle
093: .getStyleAsColor(XStyle.COLOR_BACK);
094: Font titleFont = styleManager.getFont(styleTitle);
095: titlePanel.setFont(titleFont);
096: }
097: setBackground(backgroundColor);
098: titlePanel.setForeground(titleColor);
099: titlePanel.setBackground(titleBkColor);
100: titlePanel.setHorizontalAlignment(SwingConstants.CENTER);
101: }
102:
103: /**
104: * Adds the navigation buttons to the panel. The navigation buttons allow the
105: * previous or next month to be displayed
106: */
107: public void addNavigation(boolean bShow) {
108: if (bShow) {
109: if (prevBtn == null) {
110: JPanel leftBtnPanel = new JPanel();
111: leftBtnPanel.setLayout(new FlowLayout(FlowLayout.LEFT,
112: 0, 0));
113: leftBtnPanel.setOpaque(false);
114: prevYearBtn = new JButton("<<");
115: prevYearBtn.setBackground(threeDBkColor);
116: prevYearBtn.setForeground(threeDColor);
117: prevYearBtn.setMargin(new Insets(0, 0, 0, 0));
118: prevYearBtn.addActionListener(this );
119: //prevYearBtn.setBorder( null );
120:
121: prevBtn = new JButton("<");
122: prevBtn.setBackground(threeDBkColor);
123: prevBtn.setForeground(threeDColor);
124: prevBtn.setMargin(new Insets(0, 1, 0, 1));
125: prevBtn.addActionListener(this );
126: //prevBtn.setBorder( null );
127: leftBtnPanel.add(prevYearBtn);
128: leftBtnPanel.add(prevBtn);
129: titlePanel.add(leftBtnPanel, BorderLayout.WEST);
130:
131: JPanel rightBtnPanel = new JPanel();
132: rightBtnPanel.setLayout(new FlowLayout(
133: FlowLayout.RIGHT, 0, 0));
134: rightBtnPanel.setOpaque(false);
135: nextBtn = new JButton(">");
136: nextBtn.setBackground(threeDBkColor);
137: nextBtn.setForeground(threeDColor);
138: nextBtn.setMargin(new Insets(0, 2, 0, 0));
139: nextBtn.addActionListener(this );
140: //nextBtn.setBorder( null );
141: nextBtn.setOpaque(true);
142:
143: nextYearBtn = new JButton(">>");
144: nextYearBtn.setBackground(threeDBkColor);
145: nextYearBtn.setForeground(threeDColor);
146: nextYearBtn.setMargin(new Insets(0, 0, 0, 0));
147: nextYearBtn.addActionListener(this );
148: //nextYearBtn.setBorder( null );
149: nextYearBtn.setOpaque(true);
150: rightBtnPanel.add(nextBtn);
151: rightBtnPanel.add(nextYearBtn);
152: titlePanel.add(rightBtnPanel, BorderLayout.EAST);
153: } else {
154: prevBtn.setVisible(true);
155: nextBtn.setVisible(true);
156: prevYearBtn.setVisible(true);
157: nextYearBtn.setVisible(true);
158: }
159: } else if (prevBtn != null) {
160: prevBtn.setVisible(false);
161: nextBtn.setVisible(false);
162: prevYearBtn.setVisible(false);
163: nextYearBtn.setVisible(false);
164: }
165:
166: repaint();
167: }
168:
169: /**
170: * Set one or more attributes of the component. Currently this handles the
171: * attributes
172: * <OL>
173: * <LI>nav, value=true adds navigation controls</LI>
174: * </OL>
175: * @param attribName the attribute name
176: * @param attribValue the attribute value
177: * @return 0 for success, non zero otherwise
178: */
179: public int setAttribute(String attribName, Object attribValue) {
180: if (attribName.equalsIgnoreCase("nav"))
181: addNavigation(((String) attribValue)
182: .equalsIgnoreCase("true"));
183:
184: return 0;
185: }
186:
187: /**
188: * Reacts to the previous and next buttons by modifying the display. The title
189: * is also updated to reflect the content
190: * @param e the action event
191: */
192: public void actionPerformed(ActionEvent e) {
193: if (e.getSource() == prevBtn)
194: datePanel.prev();
195: else if (e.getSource() == nextBtn)
196: datePanel.next();
197: else if (e.getSource() == prevYearBtn)
198: datePanel.prevYear();
199: else if (e.getSource() == nextYearBtn)
200: datePanel.nextYear();
201:
202: titlePanel.setText(new SimpleDateFormat("MMMM yyyy")
203: .format(datePanel.getDate()));
204: }
205:
206: /**
207: * Gets the current date.
208: * @return the date
209: */
210: public Date getDate() {
211: return datePanel.getDate();
212: }
213:
214: /**
215: * Sets the current date.
216: * @param newDate the new date
217: */
218: public void setDate(Date newDate) {
219: datePanel.setDate(newDate);
220: titlePanel.setText(new SimpleDateFormat("MMMM yyyy")
221: .format(newDate));
222: }
223:
224: /**
225: * Get the style asociated with the 3D elements
226: * @return the style name
227: */
228: public String getStyle3D() {
229: return style3D;
230: }
231:
232: /**
233: * Get the style asociated with three dimensional objects/elements
234: * @param newStyle the style name
235: */
236: public void setStyle3D(String newStyle) {
237: style3D = newStyle;
238: XStyle threeDStyle = styleManager.getStyle(style3D);
239: if (threeDStyle != null) {
240: threeDColor = threeDStyle
241: .getStyleAsColor(XStyle.COLOR_FORE);
242: threeDBkColor = threeDStyle
243: .getStyleAsColor(XStyle.COLOR_BACK);
244: if (nextBtn != null) {
245: nextBtn.setBackground(threeDBkColor);
246: nextBtn.setForeground(threeDColor);
247: }
248: if (prevBtn != null) {
249: prevBtn.setBackground(threeDBkColor);
250: prevBtn.setForeground(threeDColor);
251: }
252: if (nextYearBtn != null) {
253: nextYearBtn.setBackground(threeDBkColor);
254: nextYearBtn.setForeground(threeDColor);
255: }
256: if (prevYearBtn != null) {
257: prevYearBtn.setBackground(threeDBkColor);
258: prevYearBtn.setForeground(threeDColor);
259: }
260: }
261: }
262:
263: /**
264: * Get the style asociated with the title
265: * @return the style name
266: */
267: public String getStyleTitle() {
268: return styleTitle;
269: }
270:
271: /**
272: * set the style asociated with title element
273: * @param newStyle the style name
274: */
275: public void setStyleTitle(String newStyle) {
276: styleTitle = newStyle;
277: XStyle titleStyle = styleManager.getStyle(styleTitle);
278: if (titleStyle != null) {
279: titleColor = titleStyle.getStyleAsColor(XStyle.COLOR_FORE);
280: titleBkColor = titleStyle
281: .getStyleAsColor(XStyle.COLOR_BACK);
282: titlePanel.setForeground(titleColor);
283: titlePanel.setBackground(titleBkColor);
284: titlePanel.setFont(styleManager.getFont(styleTitle));
285: }
286: }
287:
288: /**
289: * Get the style asociated with the selected elements
290: * @return the style name
291: */
292: public String getStyleSelected() {
293: return datePanel.getStyleSelected();
294: }
295:
296: /**
297: * Get the style asociated with the selected elements
298: * @param newStyle the style name
299: */
300: public void setStyleSelected(String newStyle) {
301: datePanel.setStyleSelected(newStyle);
302: }
303:
304: /**
305: * Get the style asociated with the weekend element
306: * @return the style name
307: */
308: public String getStyleWeekend() {
309: return datePanel.getStyleWeekend();
310: }
311:
312: /**
313: * set the style asociated with weekend element
314: * @param newStyle the style name
315: */
316: public void setStyleWeekend(String newStyle) {
317: datePanel.setStyleWeekend(newStyle);
318: }
319:
320: /**
321: * Get the style asociated with the highlighted element
322: * @return the style name
323: */
324: public String getStyleHighlighted() {
325: return datePanel.getStyleHighlighted();
326: }
327:
328: /**
329: * Set the style asociated with highlighted element
330: * @param newStyle the style name
331: */
332: public void setStyleHighlighted(String newStyle) {
333: datePanel.setStyleHighlighted(newStyle);
334: }
335:
336: /**
337: * Check if the months are navigable by clicking next and previous buttons
338: * @return true if the month can be changed
339: */
340: public boolean isNavigable() {
341: return prevBtn != null;
342: }
343:
344: /**
345: * Set the months as navigable by clicking next and previous buttons
346: * @param bShow true to show the navigation buttons, false to hide them
347: */
348: public void setNavigable(boolean bShow) {
349: addNavigation(bShow);
350: }
351:
352: /**
353: * Set the text/label of a component
354: * @param text the new text
355: */
356: public void setText(String text) {
357: try {
358: datePanel.setDate(DateFormat.getInstance().parse(text));
359: } catch (ParseException pe) {
360: }
361: }
362:
363: /**
364: * Get the text/label of a component
365: * @return the component's text
366: */
367: public String getText() {
368: return datePanel.getDate().toString();
369: }
370:
371: /**
372: * Show or hide the day names
373: * @param state true to show the day names
374: */
375: public void setShowDayNames(boolean state) {
376: datePanel.setShowDayNames(state);
377: }
378:
379: /**
380: * Set the styles for the date panel
381: * @param styles the styles in the following order: style, selectedStyle, weekendStyle, highlightStyle, headerStyle, threeDStyle
382: */
383: public void setStyles(String[] styles) {
384: datePanel.setStyles(styles);
385: setStyleTitle(styles[4]);
386: setStyle3D(styles[5]);
387: }
388: }
|