PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/src/mpv5/ui/beans/DateChooser.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 136 lines | 93 code | 19 blank | 24 comment | 9 complexity | 29550936342021cd8131f1db587585a3 MD5 | raw file
  1package mpv5.ui.beans;
  2
  3
  4import java.util.Date;
  5import javax.swing.JSpinner;
  6import javax.swing.JSpinner.DateEditor;
  7import mpv5.ui.dialogs.MiniCalendarDialog;
  8import mpv5.ui.dialogs.MiniCalendarFrame;
  9import mpv5.utils.date.DateConverter;
 10
 11/**
 12 *
 13 *  
 14 */
 15public class DateChooser extends javax.swing.JPanel {
 16
 17    private static final long serialVersionUID = 1L;
 18    private MiniCalendarDialog d;
 19    private boolean asAPopup = false;
 20
 21    /** Creates new form LabeledTextField */
 22    public DateChooser() {
 23        initComponents();
 24        jSpinner1.setEditor(new DateEditor(jSpinner1, DateConverter.getDefaultFormatString()));
 25    }
 26
 27    /**
 28     *
 29     * @return
 30     */
 31    public JSpinner getSpinner() {
 32        return jSpinner1;
 33    }
 34
 35    /**
 36     *
 37     */
 38    public DateChooser(boolean asAPopup) {
 39        initComponents();
 40        jSpinner1.setEditor(new DateEditor(jSpinner1, DateConverter.getDefaultFormatString()));
 41        this.asAPopup = asAPopup;
 42    }
 43
 44    /**
 45     * Determines if the field has some text
 46     * @return
 47     */
 48    public boolean hasDate() {
 49        return (DateConverter.getDate(jSpinner1.getValue()) != null);
 50    }
 51
 52    public void setDate(Date date) {
 53        if (d == null) {
 54            d = new MiniCalendarDialog(jSpinner1, asAPopup);
 55        }
 56
 57        try {
 58            jSpinner1.setValue(date);
 59        } catch (Exception e) {
 60        }
 61        d.setDate(date);
 62    }
 63
 64    public Date getDate() {
 65        if (hasDate()) {
 66            return DateConverter.getDate(jSpinner1.getValue());
 67        } else {
 68            return new Date();
 69        }
 70    }
 71
 72    /** This method is called from within the constructor to
 73     * initialize the form.
 74     * WARNING: Do NOT modify this code. The content of this method is
 75     * always regenerated by the Form Editor.
 76     */
 77    @SuppressWarnings("unchecked")
 78    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 79    private void initComponents() {
 80
 81        jSpinner1 = new javax.swing.JSpinner();
 82        jButton1 = new javax.swing.JButton();
 83
 84        setName("Form"); // NOI18N
 85        setOpaque(false);
 86        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
 87
 88        jSpinner1.setFont(jSpinner1.getFont());
 89        jSpinner1.setModel(new javax.swing.SpinnerDateModel());
 90        jSpinner1.setEditor(new javax.swing.JSpinner.DateEditor(jSpinner1, ""));
 91        jSpinner1.setMinimumSize(new java.awt.Dimension(23, 18));
 92        jSpinner1.setName("jSpinner1"); // NOI18N
 93        jSpinner1.setPreferredSize(new java.awt.Dimension(100, 20));
 94        add(jSpinner1);
 95
 96        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/16/cal.png"))); // NOI18N
 97        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
 98        jButton1.setToolTipText(bundle.getString("DateChooser.jButton1.toolTipText")); // NOI18N
 99        jButton1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true));
100        jButton1.setContentAreaFilled(false);
101        jButton1.setMaximumSize(new java.awt.Dimension(20, 333));
102        jButton1.setMinimumSize(new java.awt.Dimension(18, 18));
103        jButton1.setName("jButton1"); // NOI18N
104        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
105            public void mouseClicked(java.awt.event.MouseEvent evt) {
106                jButton1MouseClicked(evt);
107            }
108        });
109        jButton1.addActionListener(new java.awt.event.ActionListener() {
110            public void actionPerformed(java.awt.event.ActionEvent evt) {
111                jButton1ActionPerformed(evt);
112            }
113        });
114        add(jButton1);
115    }// </editor-fold>//GEN-END:initComponents
116
117    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
118        if (d == null) {
119            d = new MiniCalendarDialog(jSpinner1, asAPopup);
120        }
121
122        d.setVisible(true);
123    }//GEN-LAST:event_jButton1ActionPerformed
124
125    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
126        if (d == null) {
127            d = new MiniCalendarDialog(jSpinner1, asAPopup);
128        }
129        d.setLocation(evt.getLocationOnScreen());
130        d.setVisible(true);
131    }//GEN-LAST:event_jButton1MouseClicked
132    // Variables declaration - do not modify//GEN-BEGIN:variables
133    private javax.swing.JButton jButton1;
134    private javax.swing.JSpinner jSpinner1;
135    // End of variables declaration//GEN-END:variables
136}