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 * BudgetSelectorDialog.java
024 *
025 * Created on June 7, 2006, 8:24 AM
026 */
027
028 package org.jbudget.gui.budget;
029
030 import java.awt.BorderLayout;
031 import java.awt.Dimension;
032 import java.awt.event.ActionEvent;
033 import java.awt.event.ActionListener;
034 import java.awt.event.WindowAdapter;
035 import java.awt.event.WindowEvent;
036 import javax.swing.JButton;
037 import javax.swing.JDialog;
038 import javax.swing.JFrame;
039 import javax.swing.JPanel;
040 import javax.swing.SpringLayout;
041
042 /**
043 *
044 * @author petrov
045 */
046 public class BudgetSelectorDialog extends JDialog
047 {
048 private final BudgetSelectorPanel panel;
049
050 private final JButton okButton = new JButton("OK");
051 private final JButton cancelButton = new JButton("Cancel");
052
053 /** A guard variable. It is set to true when either "OK" or "Cancel"
054 * buttons are pressed.
055 */
056 private boolean selectionMade = false;
057
058 /** The id of the selected budget */
059 private int selectedBudgetId = -1;
060
061 /** Creates a new instance of BudgetSelectorDialog.
062 * @param showUnused if true only unused budgets are shown.
063 */
064 public BudgetSelectorDialog(JFrame parent, boolean showUnused)
065 {
066 super(parent,"Choose a budget", true);
067
068 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
069
070
071
072 okButton.addActionListener(new ActionListener(){
073 public void actionPerformed(ActionEvent ev){
074 selectedBudgetId = panel.getSelectedBudgetID();
075 selectionMade = true;
076 close();
077 }
078 });
079
080 cancelButton.addActionListener(new ActionListener(){
081 public void actionPerformed(ActionEvent ev){
082 selectedBudgetId = -1;
083 selectionMade = true;
084 close();
085 }
086 });
087
088 addWindowListener(new WindowAdapter(){
089 public void windowClosing(WindowEvent ev){
090 if (!selectionMade){
091 selectedBudgetId = -1;
092 selectionMade = true;
093 close();
094 }
095 }
096 });
097
098 setTitle("Select a budget");
099
100 panel = new BudgetSelectorPanel(showUnused);
101
102 JPanel innerPanel = new JPanel();
103 innerPanel.setLayout(new BorderLayout());
104 innerPanel.add(panel,BorderLayout.CENTER);
105 JPanel controlPanel = new JPanel();
106 controlPanel.setPreferredSize(new Dimension(0,40));
107 controlPanel.setMinimumSize(new Dimension(100, 30));
108 innerPanel.add(controlPanel, BorderLayout.SOUTH);
109 SpringLayout layout = new SpringLayout();
110 layout.putConstraint(SpringLayout.WEST, cancelButton, 10,
111 SpringLayout.WEST, controlPanel);
112 layout.putConstraint(SpringLayout.NORTH, cancelButton, 5,
113 SpringLayout.NORTH, controlPanel);
114
115 layout.putConstraint(SpringLayout.EAST, okButton, -10,
116 SpringLayout.EAST, controlPanel);
117 layout.putConstraint(SpringLayout.NORTH, okButton, 5,
118 SpringLayout.NORTH, controlPanel);
119 controlPanel.setLayout(layout);
120 controlPanel.add(cancelButton);
121 controlPanel.add(okButton);
122
123 setContentPane(innerPanel);
124
125 pack();
126 setLocationRelativeTo(parent);
127 setVisible(true);
128 }
129
130 /** Frees resources */
131 public void dispose(){
132 super.dispose();
133 panel.dispose();
134 }
135
136 /** Displays a dialog and waits for the selection. Returns the ID of
137 * the selected budget or -1 if the dialog was closed without making a
138 * selection.
139 */
140 public static int showDialog(JFrame parent){
141 BudgetSelectorDialog dialog = new BudgetSelectorDialog(parent, false);
142 dialog.dispose();
143 return dialog.selectedBudgetId;
144 }
145
146 /** Displays a dialog and waits for the selection. Returns the ID of
147 * the selected budget or -1 if the dialog was closed without making a
148 * selection.
149 * @param showUnused if true only unused budgets are shown.
150 */
151 public static int showDialog(JFrame parent, boolean showUnused){
152 BudgetSelectorDialog dialog = new BudgetSelectorDialog(parent, showUnused);
153 dialog.dispose();
154 return dialog.selectedBudgetId;
155 }
156
157 /** Close the dialog */
158 private void close(){
159 setVisible(false);
160 dispose();
161 }
162 }