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