001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui;
029:
030: import java.text.ParseException;
031: import java.text.SimpleDateFormat;
032:
033: import thinwire.ui.event.ActionEvent;
034: import thinwire.ui.event.ActionListener;
035:
036: /**
037: * A <code>DropDownDateBox</code> is a <code>DropDown</code> that contains
038: * a <code>DateBox</code>.
039: * <p>
040: * <b>Example:</b> <br>
041: * <img src="doc-files/DropDownDateBox-1.png"> <br>
042: *
043: * <pre>
044: * final Dialog dlg = new Dialog("DateBox Test");
045: * dlg.setBounds(10, 10, 320, 240);
046: * DropDownDateBox dd = new DropDownDateBox();
047: * dd.setBounds(10, 10, 200, 20);
048: *
049: * dlg.getChildren().add(dd);
050: * dlg.setVisible(true);
051: * </pre>
052: *
053: * </p>
054: * <p>
055: * <b>Keyboard Navigation:</b><br>
056: * <table border="1">
057: * <tr>
058: * <td>KEY</td>
059: * <td>RESPONSE</td>
060: * <td>NOTE</td>
061: * </tr>
062: * <tr>
063: * <td>Down Arrow</td>
064: * <td>Drops the DateBox down.</td>
065: * <td>Only if the component has focus.</td>
066: * </tr>
067: * <tr>
068: * <td>Esc</td>
069: * <td>Closes the DateBox</td>
070: * <td>Only if the component has focus.</td>
071: * </tr>
072: * </table> See DateBox for additional keyboard support.
073: * </p>
074: *
075: * @author Ted C. Howard
076: */
077: public class DropDownDateBox extends DropDown<DateBox> {
078:
079: private static class DefaultView extends
080: DropDown.AbstractView<DateBox> {
081:
082: private static final float HEIGHT_MULTIPLIER = 17 / 20;
083: private static final int MIN_WIDTH = 150;
084: private static final int MIN_HEIGHT = 170;
085:
086: private SimpleDateFormat dateFormat;
087:
088: DefaultView() {
089:
090: }
091:
092: @Override
093: protected void init(DropDown<DateBox> ddDb, DateBox db) {
094: super .init(ddDb, db);
095: addCloseComponent(ddc);
096: ddc.addActionListener(DateBox.ACTION_CLICK,
097: new ActionListener() {
098: public void actionPerformed(ActionEvent ev) {
099: dd.setText(getValue().toString());
100: }
101: });
102: }
103:
104: public DropDownDateBox getDropDown() {
105: return (DropDownDateBox) dd;
106: }
107:
108: public int getOptimalHeight() {
109: if (ddc.getWidth() < MIN_WIDTH)
110: ddc.setWidth(getOptimalWidth());
111: int dbHeight = Math.round(ddc.getWidth()
112: * HEIGHT_MULTIPLIER);
113: if (dbHeight < MIN_HEIGHT)
114: dbHeight = MIN_HEIGHT;
115: return dbHeight;
116: }
117:
118: public int getOptimalWidth() {
119: int ddWidth = dd.getWidth();
120: int dbWidth = ddc.getWidth();
121: return dbWidth >= ddWidth ? dbWidth >= MIN_WIDTH ? dbWidth
122: : MIN_WIDTH : ddWidth >= MIN_WIDTH ? ddWidth
123: : MIN_WIDTH;
124: }
125:
126: public Object getValue() {
127: return dateFormat.format(ddc.getSelectedDate());
128: }
129:
130: public void setValue(Object value) {
131: String s;
132: if (value == null) {
133: s = "";
134: } else if (value instanceof String) {
135: s = (String) value;
136: } else {
137: s = value.toString();
138: }
139: try {
140: ddc.setSelectedDate(dateFormat.parse(s));
141: } catch (ParseException e) {
142:
143: }
144: }
145: }
146:
147: public DropDownDateBox() {
148: this (null);
149: }
150:
151: /**
152: * Creates a DropDownDateBox initialized a specific date
153: * @param text The formatted date to initilize the DateBox to (MM/dd/yyyy)
154: */
155: public DropDownDateBox(String text) {
156: super (new DefaultView(), new DateBox());
157: ((DefaultView) getView()).init(this , this .getComponent());
158: setEditMask("MM/dd/yyyy");
159: if (text != null)
160: setText(text);
161: }
162:
163: /**
164: * This method accepts an edit mask as a String and applies it to the
165: * <code>TextField</code>. The edit mask also defines the Format that the
166: * Date is displayed.
167: */
168: public void setEditMask(String editMask) {
169: if (editMask.matches(".*?[9#AaXxhmp]+.*?"))
170: throw new IllegalArgumentException(
171: "Only date mask characters are valid");
172: ((DefaultView) getView()).dateFormat = new SimpleDateFormat(
173: editMask);
174: super.setEditMask(editMask);
175: }
176:
177: }
|