001: package org.jsqltool.gui.graphics;
002:
003: import javax.swing.*;
004: import javax.swing.text.*;
005: import com.toedter.calendar.*;
006: import org.jsqltool.utils.Options;
007: import org.jsqltool.utils.ImageLoader;
008: import java.awt.*;
009: import java.util.*;
010: import java.text.SimpleDateFormat;
011: import java.awt.event.*;
012: import java.beans.PropertyChangeListener;
013: import java.beans.PropertyChangeEvent;
014:
015: /**
016: * <p>Title: JSqlTool Project</p>
017: * <p>Description: Date input field (read only) + combo box for set a date from a calendar.
018: * </p>
019: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
020: *
021: * <p> This file is part of JSqlTool project.
022: * This library is free software; you can redistribute it and/or
023: * modify it under the terms of the (LGPL) Lesser General Public
024: * License as published by the Free Software Foundation;
025: *
026: * GNU LESSER GENERAL PUBLIC LICENSE
027: * Version 2.1, February 1999
028: *
029: * This library is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032: * Library General Public License for more details.
033: *
034: * You should have received a copy of the GNU Library General Public
035: * License along with this library; if not, write to the Free
036: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
037: *
038: * The author may be contacted at:
039: * maurocarniel@tin.it</p>
040: *
041: * @author Mauro Carniel
042: * @version 1.0
043: */
044: public class CalendarCombo extends JPanel {
045:
046: /** calendar */
047: private JCalendar calendar = new JCalendar();
048:
049: /** date input field (read only) */
050: private JTextField dateBox = new JTextField(12);
051:
052: /** button icon */
053: private Icon icon = ImageLoader.getInstance().getIcon("down.gif");
054:
055: /** button used to opne the calendar */
056: private JButton button = new JButton(icon);
057:
058: /** layout used in this panel */
059: private GridBagLayout gridBagLayout1 = new GridBagLayout();
060:
061: /** popup menu used to host the calendar */
062: private JPopupMenu menu = new JPopupMenu();
063:
064: /** date format used to set date input field content */
065: private SimpleDateFormat sdf = new SimpleDateFormat(Options
066: .getInstance().getDateFormat());
067:
068: /** flag used to set popup menu visibility */
069: private boolean menuIsVisible = false;
070:
071: /** flag used to indicate popup just opened */
072: private boolean firstTime = false;
073:
074: public CalendarCombo() {
075: try {
076: jbInit();
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081:
082: private void jbInit() throws Exception {
083: this .setLayout(gridBagLayout1);
084: dateBox.setBackground(Color.white);
085: button
086: .addActionListener(new CalendarCombo_button_actionAdapter(
087: this ));
088: this .add(dateBox, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
089: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
090: new Insets(0, 0, 0, 0), 0, 0));
091: this .add(button, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
092: GridBagConstraints.WEST, GridBagConstraints.NONE,
093: new Insets(0, 0, 0, 0), 0, 0));
094: button.setPreferredSize(new Dimension(icon.getIconWidth() + 6,
095: dateBox.getPreferredSize().height));
096: button.setMaximumSize(new Dimension(icon.getIconWidth() + 6,
097: dateBox.getPreferredSize().height));
098: button.setMinimumSize(new Dimension(icon.getIconWidth() + 6,
099: dateBox.getPreferredSize().height));
100: dateBox.setEditable(false);
101: menu.add(calendar);
102: calendar.setLocale(new Locale(Options.getInstance()
103: .getLanguage()));
104: calendar.getDayChooser().addPropertyChangeListener(
105: new PropertyChangeListener() {
106: public void propertyChange(PropertyChangeEvent evt) {
107: if (menuIsVisible && firstTime) {
108: menuIsVisible = false;
109: firstTime = false;
110: dateBox.setText(sdf.format(calendar
111: .getCalendar().getTime()));
112: menu.setVisible(false);
113: } else if (!firstTime)
114: firstTime = true;
115: }
116: });
117: }
118:
119: /**
120: * @return selected date
121: */
122: public Date getDate() {
123: return calendar.getCalendar() == null ? null : calendar
124: .getCalendar().getTime();
125: }
126:
127: /**
128: * Set the date.
129: * @param date date to set
130: */
131: public final void setDate(Date date) {
132: if (date == null) {
133: dateBox.setText("");
134: return;
135: }
136: Calendar cal = Calendar.getInstance();
137: cal.setTime(date);
138: calendar.setCalendar(cal);
139: dateBox.setText(sdf.format(calendar.getCalendar().getTime()));
140: }
141:
142: void button_actionPerformed(ActionEvent e) {
143: if (menuIsVisible) {
144: menu.setVisible(false);
145: menuIsVisible = false;
146: } else {
147: menuIsVisible = true;
148: firstTime = false;
149: menu.show(button, button.getWidth()
150: - calendar.getPreferredSize().width - 6, button
151: .getY()
152: + button.getHeight());
153: menu.setInvoker(null);
154: }
155: }
156:
157: }
158:
159: class CalendarCombo_button_actionAdapter implements
160: java.awt.event.ActionListener {
161: CalendarCombo adaptee;
162:
163: CalendarCombo_button_actionAdapter(CalendarCombo adaptee) {
164: this .adaptee = adaptee;
165: }
166:
167: public void actionPerformed(ActionEvent e) {
168: adaptee.button_actionPerformed(e);
169: }
170: }
|