001: package com.xoetrope.awt;
002:
003: import com.xoetrope.awt.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.Dimension;
010: import java.awt.Panel;
011: import java.awt.Point;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014:
015: import net.xoetrope.awt.XButton;
016: import net.xoetrope.awt.XEdit;
017: import net.xoetrope.xui.XAttributedComponent;
018: import net.xoetrope.xui.XTextHolder;
019: import java.awt.event.FocusListener;
020: import java.awt.event.FocusEvent;
021: import java.text.DateFormat;
022: import java.text.ParseException;
023: import net.xoetrope.xui.XEventHandler;
024: import net.xoetrope.xui.XPage;
025:
026: /**
027: * An edit control with a button that pops up a DateChooser. When the DateChooser
028: * is displayed it will show the date entered in the edit control. The date will
029: * also be updated when the chooser is closed.
030: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
031: * the GNU Public License (GPL), please see license.txt for more details. If
032: * you make commercial use of this software you must purchase a commercial
033: * license from Xoetrope.</p>
034: * <p>$Revision: 1.7 $</p>
035: */
036: public class XDateEdit extends Panel implements XAttributedComponent,
037: ActionListener, XTextHolder, FocusListener {
038: XEdit dateEdit;
039: XButton popupBtn;
040: String dateFormat;
041:
042: public XDateEdit() {
043: dateFormat = "dd/MM/yyyy";
044:
045: dateEdit = new XEdit();
046: popupBtn = new XButton();
047: popupBtn.setText("...");
048:
049: setLayout(new BorderLayout());
050: add(dateEdit, BorderLayout.CENTER);
051: add(popupBtn, BorderLayout.EAST);
052:
053: popupBtn.addActionListener(this );
054: }
055:
056: /**
057: * Gets the content of the date field
058: * @return the date edit value
059: */
060: public String getText() {
061: return dateEdit.getText();
062: }
063:
064: /**
065: * Gets the content of the date field as a date
066: * @return the date value
067: */
068: public Date getDate() {
069: try {
070: return DateFormat.getDateInstance().parse(
071: dateEdit.getText());
072: } catch (ParseException pe) {
073: }
074: return null;
075: }
076:
077: /**
078: * Sets the content of the edit field
079: * @param s the new date string
080: */
081: public void setText(String s) {
082: dateEdit.setText(s);
083: }
084:
085: /**
086: * Set one or more attributes of the component. Currently this handles the
087: * attributes
088: * <OL>
089: * <LI>format, value=the date format</LI>
090: * </OL>
091: * @param attribName the attribute name
092: * @param attribValue the attribute value
093: * @return 0 for success, non zero otherwise
094: */
095: public int setAttribute(String attribName, Object attribValue) {
096: if (attribName.equalsIgnoreCase("format"))
097: dateFormat = (String) attribValue;
098:
099: resize();
100: return 0;
101: }
102:
103: /**
104: * Respond to the button click by showing the date chooser control.
105: * @param e
106: */
107: public void actionPerformed(ActionEvent e) {
108: if (e.getSource() == popupBtn) {
109: Point pt = popupBtn.getLocationOnScreen();
110: XDateChooserDialog dpd = new XDateChooserDialog(pt);
111: try {
112: ParsePosition pp = new ParsePosition(0);
113: SimpleDateFormat formatter = new SimpleDateFormat(
114: dateFormat);
115: formatter.setLenient(false);
116: dpd.setDate(formatter.parse(dateEdit.getText(), pp));
117: } catch (Exception ex) {
118: }
119: dpd.showDialog(this , null);
120: dateEdit.setText(new SimpleDateFormat(dateFormat)
121: .format(dpd.getDate()));
122: repaint();
123: }
124: }
125:
126: /**
127: * Add a focus listener for the child edit controls
128: * @param fl
129: */
130: public void addFocusListener(FocusListener fl) {
131: dateEdit.addFocusListener(this );
132: }
133:
134: private void resize() {
135: Dimension size = getSize();
136: if (dateEdit.getHeight() == 0) {
137: dateEdit.setSize(size.width - 20, size.height);
138: popupBtn.setSize(20, size.height);
139:
140: doLayout();
141: }
142: }
143:
144: /**
145: * Trap the focus events and forward to the parent listner
146: * @param e
147: */
148: public void focusLost(FocusEvent e) {
149: // FocusEvent e2 = new FocusEvent( this, e.getID(), e.isTemporary(), e.getOppositeComponent());
150: FocusEvent e2 = new FocusEvent(this , e.getID(), e.isTemporary());
151: ((XEventHandler) ((XPage) getParent()).getEventHandler())
152: .focusLost(e2);
153: }
154:
155: /**
156: * Trap the focus events and forward to the parent listner
157: * @param e
158: */
159: public void focusGained(FocusEvent e) {
160: // FocusEvent e2 = new FocusEvent( this, e.getID(), e.isTemporary(), e.getOppositeComponent());
161: FocusEvent e2 = new FocusEvent(this , e.getID(), e.isTemporary());
162: ((XEventHandler) ((XPage) getParent()).getEventHandler())
163: .focusGained(e2);
164: }
165: }
|