001: package com.xoetrope.awt.date;
002:
003: import com.xoetrope.awt.*;
004: import java.text.SimpleDateFormat;
005: import java.util.Date;
006:
007: import java.awt.BorderLayout;
008: import java.awt.Color;
009: import java.awt.Dimension;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.awt.Button;
013: import java.awt.Component;
014: import java.awt.Label;
015: import java.awt.Panel;
016:
017: import com.xoetrope.event.XDateListener;
018: import net.xoetrope.xui.XAttributedComponent;
019: import net.xoetrope.xui.style.XStyle;
020: import net.xoetrope.xui.style.XStyleManager;
021: import net.xoetrope.xui.XProjectManager;
022:
023: /**
024: * A panel containing a representation of a month so that the user can select
025: * a date visually. Navigation buttons can also be added to allow the previous
026: * or following months to be shown.
027: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
028: * the GNU Public License (GPL), please see license.txt for more details. If
029: * you make commercial use of this software you must purchase a commercial
030: * license from Xoetrope.</p>
031: * <p>$Revision: 1.4 $</p>
032: */
033: public class XDateChooser extends Panel implements
034: XAttributedComponent, ActionListener, XDateListener {
035: protected XStyleManager styleManager;
036:
037: protected Label title;
038: protected Panel titlePanel;
039: protected XDateChooserPanel datePanel;
040: protected Button prevBtn, nextBtn;
041:
042: private Color foregroundColor, backgroundColor;
043: private Color highlightedColor, selectedColor, titleColor,
044: threeDColor;
045: private Color highlightedBkColor, selectedBkColor, titleBkColor,
046: threeDBkColor;
047:
048: private String style3D, styleTitle;
049:
050: /**
051: * Sets up a new panel to display the current date. A title showing the month
052: * and year is also added, but the navigation buttons are omitted
053: */
054: public XDateChooser() {
055: styleManager = XProjectManager.getStyleManager();
056:
057: setLayout(new BorderLayout());
058: add(datePanel = new XDateChooserPanel(), BorderLayout.CENTER);
059:
060: titlePanel = new Panel();
061: titlePanel.setLayout(new BorderLayout());
062: titlePanel.add(title = new Label(), BorderLayout.CENTER);
063: title.setText(new SimpleDateFormat("MMMM yyyy")
064: .format(datePanel.getDate()));
065: add(titlePanel, BorderLayout.NORTH);
066:
067: style3D = "base";
068: styleTitle = "base";
069:
070: if (backgroundColor == null) {
071: XStyle threeDStyle = styleManager.getStyle(style3D);
072: XStyle titleStyle = styleManager.getStyle(styleTitle);
073: backgroundColor = getBackground();
074: foregroundColor = getForeground();
075: threeDColor = threeDStyle
076: .getStyleAsColor(XStyle.COLOR_FORE);
077: threeDBkColor = threeDStyle
078: .getStyleAsColor(XStyle.COLOR_BACK);
079: titleColor = titleStyle.getStyleAsColor(XStyle.COLOR_FORE);
080: titleBkColor = titleStyle
081: .getStyleAsColor(XStyle.COLOR_BACK);
082: }
083: setBackground(backgroundColor);
084: title.setForeground(titleColor);
085: title.setBackground(titleBkColor);
086: titlePanel.setBackground(titleBkColor);
087: }
088:
089: /**
090: * Adds the navigation buttons to the panel. The navigation buttons allow the
091: * previous or next month to be displayed
092: */
093: public void addNavigation(boolean bShow) {
094: if (bShow) {
095: if (prevBtn == null) {
096: prevBtn = new Button("<");
097: prevBtn.setBackground(threeDBkColor);
098: prevBtn.setForeground(threeDColor);
099: prevBtn.addActionListener(this );
100: titlePanel.add(prevBtn, BorderLayout.WEST);
101:
102: nextBtn = new Button(">");
103: nextBtn.setBackground(threeDBkColor);
104: nextBtn.setForeground(threeDColor);
105: nextBtn.addActionListener(this );
106: titlePanel.add(nextBtn, BorderLayout.EAST);
107: } else {
108: prevBtn.setVisible(true);
109: nextBtn.setVisible(true);
110: }
111: } else if (prevBtn != null) {
112: prevBtn.setVisible(false);
113: nextBtn.setVisible(false);
114: }
115:
116: resize();
117: repaint();
118: }
119:
120: /**
121: * Set one or more attributes of the component. Currently this handles the
122: * attributes
123: * <OL>
124: * <LI>nav, value=true adds navigation controls</LI>
125: * </OL>
126: * @param attribName the attribute name
127: * @param attribValue the attribute value
128: * @return 0 for success, non zero otherwise
129: */
130: public int setAttribute(String attribName, Object attribValue) {
131: if (attribName.equalsIgnoreCase("nav")) {
132: addNavigation(((String) attribValue)
133: .equalsIgnoreCase("true"));
134: resize();
135: }
136: return 0;
137: }
138:
139: private void resize() {
140: Dimension size = getSize();
141: if (titlePanel.getHeight() == 0) {
142: titlePanel.setSize(size.width, 20);
143: size = titlePanel.getSize();
144: if (prevBtn != null) {
145: prevBtn.setSize(size.width / 10, size.height);
146: nextBtn.setSize(size.width / 10, size.height);
147: title.setSize(size.width - size.width / 5, size.height);
148: }
149:
150: doLayout();
151: titlePanel.doLayout();
152: }
153: }
154:
155: /**
156: * Reacts to the previous and next buttons by modifying the display. The title
157: * is also updated to reflect the content
158: * @param e the action event
159: */
160: public void actionPerformed(ActionEvent e) {
161: if (e.getSource() == prevBtn)
162: datePanel.prev();
163: else if (e.getSource() == nextBtn)
164: datePanel.next();
165:
166: title.setText(new SimpleDateFormat("MMMM yyyy")
167: .format(datePanel.getDate()));
168: }
169:
170: /**
171: * Gets the current date.
172: * @return the date
173: */
174: public Date getDate() {
175: return datePanel.getDate();
176: }
177:
178: /**
179: * Sets the current date.
180: * @param newDate the new date
181: */
182: public void setDate(Date newDate) {
183: datePanel.setDate(newDate);
184: title
185: .setText(new SimpleDateFormat("MMMM yyyy")
186: .format(newDate));
187: }
188:
189: /**
190: * Get the style asociated with the 3D elements
191: * @return the style name
192: */
193: public String getStyle3D() {
194: return style3D;
195: }
196:
197: /**
198: * Get the style asociated with three dimensional objects/elements
199: * @param newStyle the style name
200: */
201: public void setStyle3D(String newStyle) {
202: style3D = newStyle;
203: XStyle threeDStyle = styleManager.getStyle(style3D);
204: threeDColor = threeDStyle.getStyleAsColor(XStyle.COLOR_FORE);
205: threeDBkColor = threeDStyle.getStyleAsColor(XStyle.COLOR_BACK);
206: if (nextBtn != null) {
207: nextBtn.setBackground(threeDBkColor);
208: nextBtn.setForeground(threeDColor);
209: }
210: if (prevBtn != null) {
211: prevBtn.setBackground(threeDBkColor);
212: prevBtn.setForeground(threeDColor);
213: }
214: }
215:
216: /**
217: * Get the style asociated with the title
218: * @return the style name
219: */
220: public String getStyleTitle() {
221: return styleTitle;
222: }
223:
224: /**
225: * set the style asociated with title element
226: * @param newStyle the style name
227: */
228: public void setStyleTitle(String newStyle) {
229: styleTitle = newStyle;
230: XStyle titleStyle = styleManager.getStyle(styleTitle);
231: titleColor = titleStyle.getStyleAsColor(XStyle.COLOR_FORE);
232: titleBkColor = titleStyle.getStyleAsColor(XStyle.COLOR_BACK);
233: title.setForeground(titleColor);
234: title.setBackground(titleBkColor);
235: titlePanel.setBackground(titleBkColor);
236: }
237:
238: /**
239: * Get the style asociated with the selected elements
240: * @return the style name
241: */
242: public String getStyleSelected() {
243: return datePanel.getStyleSelected();
244: }
245:
246: /**
247: * Get the style asociated with the selected elements
248: * @param newStyle the style name
249: */
250: public void setStyleSelected(String newStyle) {
251: datePanel.setStyleSelected(newStyle);
252: }
253:
254: /**
255: * Get the style asociated with the weekend element
256: * @return the style name
257: */
258: public String getStyleWeekend() {
259: return datePanel.getStyleWeekend();
260: }
261:
262: /**
263: * set the style asociated with weekend element
264: * @param newStyle the style name
265: */
266: public void setStyleWeekend(String newStyle) {
267: datePanel.setStyleWeekend(newStyle);
268: }
269:
270: /**
271: * Get the style asociated with the highlighted element
272: * @return the style name
273: */
274: public String getStyleHighlighted() {
275: return datePanel.getStyleHighlighted();
276: }
277:
278: /**
279: * Set the style asociated with highlighted element
280: * @param newStyle the style name
281: */
282: public void setStyleHighlighted(String newStyle) {
283: datePanel.setStyleHighlighted(newStyle);
284: }
285:
286: /**
287: * Check if the months are navigable by clicking next and previous buttons
288: * @return true if the month can be changed
289: */
290: public boolean isNavigable() {
291: return prevBtn != null;
292: }
293:
294: /**
295: * Set the months as navigable by clicking next and previous buttons
296: * @param bShow true to show the navigation buttons, false to hide them
297: */
298: public void setNavigable(boolean bShow) {
299: addNavigation(bShow);
300: }
301: }
|