001: package com.csa.lib.swing;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Dimension;
005: import java.awt.Event;
006: import java.awt.FlowLayout;
007: import java.awt.Image;
008: import java.awt.MediaTracker;
009: import java.awt.Point;
010: import java.awt.Toolkit;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.awt.event.MouseAdapter;
014: import java.awt.event.MouseEvent;
015: import java.awt.event.WindowEvent;
016: import java.awt.event.WindowFocusListener;
017: import java.net.URL;
018: import java.text.ParseException;
019: import java.text.SimpleDateFormat;
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.Vector;
023:
024: import javax.swing.ImageIcon;
025: import javax.swing.JButton;
026: import javax.swing.JDialog;
027: import javax.swing.JFrame;
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JTextField;
031:
032: import com.toedter.calendar.JCalendar;
033:
034: public class DateField extends JPanel implements ActionListener {
035: private static final long serialVersionUID = 1L;
036: public static final String IMG_NAME = "datefield.gif";
037: SimpleDateFormat sdf;
038: Date currentDate;
039: JTextField textField;
040: JButton openButton;
041: JDialog helpWindow;
042: JCalendar jCalendar;
043: JButton bOk;
044: JButton bCancel;
045: JButton bClear;
046: Vector actionListeners = new Vector();
047:
048: /**
049: * Crea un datepicker con un formato por omision
050: */
051: public DateField() {
052: this ("dd/MM/yyyy");
053: }
054:
055: /**
056: * Crear un datepicker
057: *
058: * @param sfmt
059: * formato para la fecha en el texto.
060: */
061: public DateField(String sfmt) {
062: sdf = new SimpleDateFormat(sfmt);
063: setLayout(new FlowLayout(0, 0, 0));
064: textField = new JTextField(sfmt.length());
065: URL res = DateField.class.getResource(IMG_NAME);
066: Image img = Toolkit.getDefaultToolkit().getImage(res);
067: MediaTracker m = new MediaTracker(this );
068: m.addImage(img, 0);
069: try {
070: m.waitForAll();
071: } catch (Exception e) {
072: e.printStackTrace();
073: }
074: ImageIcon icon = new ImageIcon(img);
075: openButton = new JButton(icon); //new JButton("...");
076: openButton.setPreferredSize(new Dimension(18, 20));
077: add(textField);
078: add(openButton);
079: openButton.addActionListener(this );
080: textField.addActionListener(this );
081: textField.addMouseListener(new MouseAdapter() {
082: public void mouseClicked(MouseEvent e) {
083: if (e.getClickCount() >= 2) {
084: showHelpWindow();
085: }
086: }
087: });
088: }
089:
090: public Date getDate() {
091: return currentDate;
092: }
093:
094: public void setDate(Date d) {
095: currentDate = d;
096: }
097:
098: public JTextField getTextField() {
099: return textField;
100: }
101:
102: public JButton getOpenButton() {
103: return openButton;
104: }
105:
106: // Soporte a eventos
107: public void addActionListener(ActionListener l) {
108: actionListeners.add(l);
109: }
110:
111: public void removeActionListener(ActionListener l) {
112: actionListeners.remove(l);
113: }
114:
115: protected void triggerAction() {
116: for (int i = 0; i < actionListeners.size(); i++) {
117: ActionListener l = (ActionListener) actionListeners.get(i);
118: ActionEvent a = new ActionEvent(this , Event.ACTION_EVENT,
119: "");
120: try {
121: l.actionPerformed(a);
122: } catch (Exception e) {
123: e.printStackTrace();
124: }
125: }
126: }
127:
128: // Listeners
129:
130: public void actionPerformed(ActionEvent e) {
131: // System.out.println("DatePicker.actionPerformed()");
132: if (e.getSource() == openButton) {
133: if (helpWindow == null)
134: showHelpWindow();
135: return;
136: } else if (e.getSource() == bOk) {
137: assignDate(jCalendar.getCalendar().getTime());
138: } else if (e.getSource() == bClear) {
139: assignDate(null);
140: } else if (e.getSource() == textField) {
141: try {
142: String text = textField.getText().trim();
143: if (!text.equals(""))
144: assignDate(sdf.parse(text));
145: else
146: assignDate(null);
147: } catch (ParseException e1) {
148: }
149: }
150: hideHelpWindow();
151: }
152:
153: private void assignDate(Date d) {
154: currentDate = d;
155: if (d != null)
156: textField.setText(sdf.format(currentDate));
157: else
158: textField.setText("");
159: triggerAction();
160: }
161:
162: // Soporte a la ventana
163:
164: /**
165: * Esconde la ventan de ayuda de seleccion de fecha.
166: */
167: void hideHelpWindow() {
168: if ((helpWindow != null) && (helpWindow.isShowing())) {
169: helpWindow.dispose();
170: helpWindow = null;
171: }
172: }
173:
174: /**
175: * Muestra la ventana de ayuda de seleccion de fecha.
176: */
177: void showHelpWindow() {
178: Point p = getLocationOnScreen();
179: p.y += getHeight();
180: helpWindow = new JDialog();
181: helpWindow.setUndecorated(true);
182: helpWindow.setLocation(p);
183: jCalendar = new JCalendar();
184: Calendar cal = Calendar.getInstance();
185: if (currentDate != null)
186: cal.setTime(currentDate);
187: jCalendar.setCalendar(cal);
188: helpWindow.getContentPane().setLayout(new BorderLayout());
189: helpWindow.getContentPane().add(jCalendar, BorderLayout.CENTER);
190:
191: JPanel panel = new JPanel(new FlowLayout());
192: bOk = new JButton("Ok");
193: bCancel = new JButton("Cancel");
194: bClear = new JButton("Clear");
195: panel.add(bOk);
196: panel.add(bCancel);
197: panel.add(bClear);
198: for (int i = 0; i < panel.getComponentCount(); i++)
199: ((JButton) panel.getComponent(i)).addActionListener(this );
200: helpWindow.getContentPane().add(panel, BorderLayout.SOUTH);
201: helpWindow.pack();
202:
203: helpWindow.addWindowFocusListener(new WindowFocusListener() {
204: public void windowGainedFocus(WindowEvent e) {
205: // System.out.println("windowGainedFocus");
206: }
207:
208: public void windowLostFocus(WindowEvent e) {
209: // System.out.println("windowLostFocus");
210: hideHelpWindow();
211: }
212: });
213: helpWindow.setVisible(true);
214: }
215:
216: /**
217: * Ejemplo
218: * @param args
219: */
220: public static void main(String[] args) {
221: JFrame f = new JFrame();
222: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
223: f.getContentPane().setLayout(new FlowLayout());
224: f.getContentPane().add(new JLabel("Fecha"));
225: f.getContentPane().add(new DateField("dd/MM/yyyy"));
226: f.pack();
227: f.setVisible(true);
228: }
229: }
|