001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    *****************************************************************************/
021    
022    /*
023     * MonthSelectorDialog.java
024     *
025     * Created on June 2, 2006, 3:29 PM
026     */
027    
028    package org.jbudget.gui.month;
029    
030    import java.awt.BorderLayout;
031    import java.awt.event.WindowAdapter;
032    import java.awt.event.WindowEvent;
033    import javax.swing.JDialog;
034    import javax.swing.JFrame;
035    import javax.swing.JPanel;
036    import org.jbudget.util.Pair;
037    
038    /**
039     * A Dialog window that allows selection of a single month.
040     * @author petrov
041     */
042    public class MonthSelectorDialog extends JDialog
043    {
044      private final MonthSelectorPanel panel;
045      
046      private int selectedCMonth = -1;
047      private int selectedCYear = -1;
048      boolean selectionMade = false;
049      
050      /** Creates a new instance of MonthSelectorDialog */
051      public MonthSelectorDialog(JFrame parent, boolean activateValidMonths)
052      {
053        super(parent, "Choose a month", true);
054        panel = new MonthSelectorPanel(activateValidMonths);
055        panel.addSelectionListener(new MonthSelectorPanel.SelectionListener(){
056          public void monthSelected(int cMonth, int cYear){
057            selectedCMonth = cMonth;
058            selectedCYear = cYear;
059            close();
060          }
061          public void monthDeselected(int cMonth, int cYear){
062           //TODO: FIXME.
063          }
064          public void yearChanged(int cYear){}
065        });
066        
067        addWindowListener(new WindowAdapter(){
068          public void windowClosing(WindowEvent ev){
069            close();
070          }
071        });
072        
073        JPanel contentPanel = new JPanel();
074        contentPanel.setLayout(new BorderLayout());
075        contentPanel.add(panel, BorderLayout.CENTER);
076        
077        setContentPane(contentPanel);
078        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
079        pack();
080        setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
081            parent.getY() + parent.getHeight()/2 - getHeight()/2);
082        setVisible(true);
083      }
084      
085      /** closes the dialog */
086      synchronized private void close()
087      {
088        selectionMade = true;
089        setVisible(false);
090        dispose();
091      }
092      
093      /** Displays the dialog and returns a Pair<Int, Int> that contains the
094       *  calendar month and year. If nothing was selected returns null.
095       *  This method has to be called from a thread that is not
096       *  the main AWT thread or the GUI will freeze.
097       */
098      public static Pair<Integer, Integer> showDialog(
099          JFrame parent, boolean activateValidMonths)
100      {
101        MonthSelectorDialog dialog = 
102            new MonthSelectorDialog(parent, activateValidMonths);
103        
104        if (dialog.selectedCMonth == -1 || dialog.selectedCYear == -1)
105          return null;
106        return new Pair<Integer,Integer>(new Integer(dialog.selectedCMonth),
107                                         new Integer(dialog.selectedCYear));
108      }
109      
110      /** add a selection listener */
111      public void addSelectionListener(
112          MonthSelectorPanel.SelectionListener listener) {
113        panel.addSelectionListener(listener);
114      }
115      
116      /** remove a selection listener */
117      public void removeSelectionListener(
118          MonthSelectorPanel.SelectionListener listener) {
119        panel.removeSelectionListener(listener);
120      }
121      
122    }