001: package com.xoetrope.swing;
002:
003: import com.xoetrope.swing.date.XDateChooserDialog;
004: import java.text.ParsePosition;
005: import java.text.SimpleDateFormat;
006: import java.util.Date;
007:
008: import java.awt.BorderLayout;
009: import java.awt.Color;
010: import java.awt.Dimension;
011: import java.awt.Font;
012: import java.awt.Insets;
013: import java.awt.Point;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.awt.event.FocusListener;
017: import java.text.DateFormat;
018: import java.text.ParseException;
019: import javax.swing.JComponent;
020: import javax.swing.JFormattedTextField;
021: import javax.swing.text.DateFormatter;
022: import javax.swing.text.DefaultFormatterFactory;
023:
024: import net.xoetrope.swing.XButton;
025: import net.xoetrope.xui.XAttributedComponent;
026: import net.xoetrope.xui.XProjectManager;
027: import net.xoetrope.xui.XTextHolder;
028: import net.xoetrope.xui.style.XStyle;
029: import net.xoetrope.xui.style.XStyleManager;
030:
031: /**
032: * An edit control with a button that pops up a DateChooser. When the DateChooser
033: * is displayed it will show the date entered in the edit control. The date will
034: * also be updated when the chooser is closed. The component uses the SimpleDateFormat
035: * to specify how teh date is displayed
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.19 $</p>
042: */
043: public class XDateEdit extends JComponent implements
044: XAttributedComponent, ActionListener, XTextHolder {
045: protected JFormattedTextField dateEdit;
046: protected SimpleDateFormat dateFormat;
047: protected SimpleDateFormat parseFormat;
048: protected XButton popupBtn;
049: protected String[] styles;
050: protected boolean showDayNames;
051: protected XStyleManager styleManager;
052:
053: /**
054: * Create a new date edit
055: */
056: public XDateEdit() {
057: styleManager = XProjectManager.getCurrentProject()
058: .getStyleManager();
059:
060: dateFormat = new SimpleDateFormat("d-MMM-yyyy");
061: styles = new String[6];
062: showDayNames = true;
063:
064: dateEdit = new JFormattedTextField(dateFormat);
065: popupBtn = new XButton();
066: popupBtn.setText("...");
067: popupBtn.setMargin(new Insets(1, 2, 1, 2));
068:
069: setLayout(new BorderLayout());
070: add(dateEdit, BorderLayout.CENTER);
071: add(popupBtn, BorderLayout.EAST);
072:
073: popupBtn.addActionListener(this );
074: }
075:
076: /**
077: * Gets the content of the date field
078: * @return the date edit value
079: */
080: public String getText() {
081: return dateEdit.getText();
082: }
083:
084: /**
085: * Gets the content of the date field as a date
086: * @return the date value
087: */
088: public Date getDate() {
089: try {
090: return dateFormat.parse(dateEdit.getText());
091: } catch (ParseException pe) {
092: }
093: return null;
094: }
095:
096: /**
097: * Sets the content of the date field as a date
098: */
099: public void setDate(Date d) {
100: dateEdit.setText(dateFormat.format(d));
101: }
102:
103: /**
104: * Sets the content of the edit field
105: * @param s the new date string
106: */
107: public void setText(String s) {
108: if ((s != null) && (s.length() > 0)) {
109: Date d;
110: try {
111: if (parseFormat != null)
112: d = parseFormat.parse(s);
113: else
114: d = new Date(s);
115: } catch (ParseException ex) {
116: ex.printStackTrace();
117: d = new Date(s);
118: }
119: dateEdit.setText(dateFormat.format(d));
120: } else
121: dateEdit.setText("");
122: }
123:
124: public void setEditStyle(String styleName) {
125: XStyle style = styleManager.getStyle(styleName);
126: Color foreColor = style.getStyleAsColor(XStyle.COLOR_FORE);
127: Color backColor = style.getStyleAsColor(XStyle.COLOR_BACK);
128: Font font = styleManager.getFont(style);
129: dateEdit.setForeground(foreColor);
130: dateEdit.setBackground(backColor);
131: dateEdit.setFont(font);
132: }
133:
134: /**
135: * Set one or more attributes of the component. Currently this handles the
136: * attributes
137: * <OL>
138: * <LI>format, value=SimpleDateFormat the presentation format of the date</LI>
139: * <LI>parseformat, value=SimpleDateFormat the format of the incoming date</LI>
140: * <LI>style, value=the default style</LI>
141: * <LI>selectedStyle, value=the selected date's style</LI>
142: * <LI>weekendStyle, value=the weekend date style</LI>
143: * <LI>highlightStyle, value=the rollover/highlighted date style</LI>
144: * <LI>headerStyle, value=the header style</LI>
145: * <LI>threeDStyle, value=the 3d element style</LI>
146: * <LI>daynames, value=true to show the day names</LI>
147: * </OL>
148: * @param attribName the attribute name
149: * @param attribValue the attribute value
150: * @return 0 for success, non zero otherwise
151: */
152: public int setAttribute(String attribName, Object attribValue) {
153: String attribNameLwr = attribName.toLowerCase();
154: String attribValueStr = (String) attribValue;
155: String attribValueLwr = attribValueStr.toLowerCase();
156: if (attribNameLwr.equals("format"))
157: setFormat(attribValueStr);
158: else if (attribNameLwr.equals("parseformat"))
159: setParseFormat(attribValueStr);
160: else if (attribNameLwr.equals("style"))
161: styles[0] = attribValueStr;
162: else if (attribNameLwr.equals("selectedstyle"))
163: styles[1] = attribValueStr;
164: else if (attribNameLwr.equals("weekendstyle"))
165: styles[2] = attribValueStr;
166: else if (attribNameLwr.equals("highlightstyle"))
167: styles[3] = attribValueStr;
168: else if (attribNameLwr.equals("headerstyle"))
169: styles[4] = attribValueStr;
170: else if (attribNameLwr.equals("threedstyle"))
171: styles[5] = attribValueStr;
172: else if (attribNameLwr.equals("editstyle"))
173: setEditStyle(attribValueStr);
174: else if (attribNameLwr.equals("daynames"))
175: showDayNames = (attribValue.equals("true") || attribValue
176: .equals("1"));
177:
178: resize();
179:
180: return 0;
181: }
182:
183: /**
184: * Respond to the button click by showing the date chooser control.
185: * @param e the action event
186: */
187: public void actionPerformed(ActionEvent e) {
188: if (e.getSource() == popupBtn) {
189: Point pt = popupBtn.getLocationOnScreen();
190: XDateChooserDialog dpd = new XDateChooserDialog(pt);
191: try {
192: ParsePosition pp = new ParsePosition(0);
193: dateFormat.setLenient(false);
194: dpd.setDate(dateFormat.parse(dateEdit.getText(), pp));
195: } catch (Exception ex) {
196: }
197: dpd.setStyles(styles);
198: dpd.setShowDayNames(showDayNames);
199: dpd.setSaveOnClose(false);
200: dpd.showDialog(this , null);
201: dateEdit.setText(dateFormat.format(dpd.getDate()));
202: repaint();
203: }
204: }
205:
206: /**
207: * Add a focus listener for the child edit controls
208: * @param fl the focus listener
209: */
210: public void addFocusListener(FocusListener fl) {
211: if (dateEdit != null)
212: dateEdit.addFocusListener(fl);
213: }
214:
215: /**
216: * Remove a focus listener for the child edit controls
217: * @param fl the focus listener
218: */
219: public void removeFocusListener(FocusListener fl) {
220: if (dateEdit != null)
221: dateEdit.removeFocusListener(fl);
222: }
223:
224: private void resize() {
225: Dimension size = getSize();
226: if (dateEdit.getHeight() == 0) {
227: dateEdit.setSize(size.width - 20, size.height);
228: popupBtn.setSize(20, size.height);
229:
230: doLayout();
231: }
232: }
233:
234: /**
235: * Set the style of the weekend elements. The weekend elements are the dates
236: * that fall on weekends (Saturday and Sunday). This style allowes those
237: * dates to be shown in an alternative styles, typically with a grayed out
238: * or less saturated/right background color.
239: * @param styleName the style name for the weekend dates
240: */
241: public void setWeekendStyle(String styleName) {
242: styles[2] = styleName;
243: repaint();
244: }
245:
246: /**
247: * Set the style of the selected date. The selected date is chosen by clicking
248: * on a date. It is shown in the specified style even if the curosr is moved.
249: * @param styleName the style name for the selected date
250: */
251: public void setSelectedStyle(String styleName) {
252: styles[1] = styleName;
253: repaint();
254: }
255:
256: /**
257: * Set the style of the highlighted date. The highlighted date is is the date
258: * shown below the curose. Setting this style provides user feedback when the
259: * mouse moves.
260: * @param styleName the style name for the highlighted date
261: */
262: public void setHighlightStyle(String styleName) {
263: styles[3] = styleName;
264: repaint();
265: }
266:
267: /**
268: * Set the style of the header. The header shows the month, year and the
269: * navigation buttons. It is painted with a gradient
270: * @param styleName the style name for the highlighted date
271: */
272: public void setHeaderStyle(String styleName) {
273: styles[4] = styleName;
274: repaint();
275: }
276:
277: /**
278: * Set the style of the 3D elements including the navigation buttons
279: * @param styleName the style name for the highlighted date
280: */
281: public void setThreeDStyle(String styleName) {
282: styles[5] = styleName;
283: repaint();
284: }
285:
286: /**
287: * Set the format of the edit field. The format is used in the construction of
288: * a java.text.SimpleDateFormat instance.
289: * @param format the new date format
290: */
291: public void setFormat(String format) {
292: dateFormat = new SimpleDateFormat(format);
293: dateEdit.setFormatterFactory(new DefaultFormatterFactory(
294: new DateFormatter(dateFormat)));
295: }
296:
297: /**
298: * Set the format of the edit field data as it is parsed from the data model
299: * or when the setText method is invoked. The format is used in the construction of
300: * a java.text.SimpleDateFormat instance.
301: * @param format the incoming date
302: */
303: public void setParseFormat(String format) {
304: parseFormat = new SimpleDateFormat(format);
305: }
306:
307: /**
308: * Set the style of the 3D elements including the navigation buttons
309: * @param styleName the style name for the highlighted date
310: */
311: public void setDayNames(boolean show) {
312: showDayNames = show;
313: }
314:
315: /**
316: * Get the style of the weekend elements. The weekend elements are the dates
317: * that fall on weekends (Saturday and Sunday). This style allowes those
318: * dates to be shown in an alternative styles, typically with a grayed out
319: * or less saturated/right background color.
320: * @return the style name for the weekend dates
321: */
322: public String getWeekendStyle() {
323: return styles[2];
324: }
325:
326: /**
327: * Get the style of the selected date. The selected date is chosen by clicking
328: * on a date. It is shown in the specified style even if the curosr is moved.
329: * @return the style name for the selected date
330: */
331: public String getSelectedStyle() {
332: return styles[1];
333: }
334:
335: /**
336: * Get the style of the highlighted date. The highlighted date is is the date
337: * shown below the curose. Setting this style provides user feedback when the
338: * mouse moves.
339: * @return the style name for the highlighted date
340: */
341: public String getHighlightStyle() {
342: return styles[3];
343: }
344:
345: /**
346: * Get the style of the header. The header shows the month, year and the
347: * navigation buttons. It is painted with a gradient
348: * @return the style name for the highlighted date
349: */
350: public String getHeaderStyle() {
351: return styles[4];
352: }
353:
354: /**
355: * Get the style of the 3D elements including the navigation buttons
356: * @return the style name for the highlighted date
357: */
358: public String getThreeDStyle() {
359: return styles[5];
360: }
361:
362: /**
363: * Get the format of the edit field. The format is used in the construction of
364: * a java.text.SimpleDateFormat instance.
365: * @return the new date format
366: */
367: public String getFormat() {
368: return ((SimpleDateFormat) dateFormat).toPattern();
369: }
370:
371: /**
372: * Get the style of the 3D elements including the navigation buttons
373: * @return the style name for the highlighted date
374: */
375: public boolean getDayNames() {
376: return showDayNames;
377: }
378:
379: }
|