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 import org.jbudget.Core.Budget;
042
043 /**
044 *
045 * @author petrov
046 */
047 public class AccountSelectorDialog extends JDialog
048 {
049 private final AccountSelectorPanel panel;
050
051 private final JButton okButton = new JButton("OK");
052 private final JButton cancelButton = new JButton("Cancel");
053
054 /** A guard variable. It is set to true when either "OK" or "Cancel"
055 * buttons are pressed.
056 */
057 private boolean selectionMade = false;
058
059 /** The id of the selected account */
060 private int selectedAccountId = -1;
061
062 /** Creates a new instance of AccountSelectorDialog .
063 * @param showUnused if true, only accounts not registered with any budget
064 * are shown.
065 */
066 public AccountSelectorDialog(JFrame parent, boolean showUnused)
067 {
068 super(parent,"Choose an Account", true);
069
070 panel = new AccountSelectorPanel(showUnused);
071
072 initActions();
073 arrangeGui();
074
075 pack();
076 setLocationRelativeTo(parent);
077 setVisible(true);
078 }
079
080 public void validate()
081 {
082 super.validate();
083 panel.validate();
084 }
085
086 /** Creates a window with a list of all budgets */
087 public AccountSelectorDialog(JFrame parent)
088 {
089 this(parent, false);
090 }
091
092 /** Creates a list of accounts registered with a given budget, or a list of
093 * all accounts that have not been registered with the budget.
094 * @param showUnregistered if true, only accounts not registered with the
095 * budget are shown.
096 */
097 public AccountSelectorDialog(JFrame parent,
098 Budget budget,
099 boolean showUnregistered)
100 {
101 super(parent,"Choose an Account", true);
102
103 panel = new AccountSelectorPanel(budget, showUnregistered);
104
105 initActions();
106 arrangeGui();
107
108
109 pack();
110 if (parent != null)
111 setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
112 parent.getY() + parent.getHeight()/2 - getHeight()/2);
113 setVisible(true);
114 }
115
116 /** Shows a list of accounts registered with a given budget. */
117 public AccountSelectorDialog(JFrame parent, Budget budget)
118 {
119 this(parent, budget, false);
120 }
121
122 /** Arranges gui components */
123 private void arrangeGui()
124 {
125 setTitle("Select an Account");
126 JPanel innerPanel = new JPanel();
127 innerPanel.setLayout(new BorderLayout());
128 innerPanel.add(panel,BorderLayout.CENTER);
129 JPanel controlPanel = new JPanel();
130 controlPanel.setPreferredSize(new Dimension(0,40));
131 controlPanel.setMinimumSize(new Dimension(100, 30));
132 innerPanel.add(controlPanel, BorderLayout.SOUTH);
133 SpringLayout layout = new SpringLayout();
134 layout.putConstraint(SpringLayout.WEST, cancelButton, 10,
135 SpringLayout.WEST, controlPanel);
136 layout.putConstraint(SpringLayout.NORTH, cancelButton, 5,
137 SpringLayout.NORTH, controlPanel);
138
139 layout.putConstraint(SpringLayout.EAST, okButton, -10,
140 SpringLayout.EAST, controlPanel);
141 layout.putConstraint(SpringLayout.NORTH, okButton, 5,
142 SpringLayout.NORTH, controlPanel);
143 controlPanel.setLayout(layout);
144 controlPanel.add(cancelButton);
145 controlPanel.add(okButton);
146
147 setContentPane(innerPanel);
148 }
149
150 /** Initializes buttons and other gui component actions. */
151 private void initActions()
152 {
153 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
154
155 okButton.addActionListener(new ActionListener(){
156 public void actionPerformed(ActionEvent ev){
157 selectedAccountId = panel.getSelectedAccountID();
158 selectionMade = true;
159 close();
160 }
161 });
162
163 cancelButton.addActionListener(new ActionListener(){
164 public void actionPerformed(ActionEvent ev){
165 selectedAccountId = -1;
166 selectionMade = true;
167 close();
168 }
169 });
170
171 addWindowListener(new WindowAdapter(){
172 public void windowClosing(WindowEvent ev){
173 if (!selectionMade){
174 selectedAccountId = -1;
175 selectionMade = true;
176 close();
177 }
178 }
179 });
180 }
181
182 /** Frees resources */
183 public void dispose(){
184 super.dispose();
185 panel.dispose();
186 }
187
188 /** Displays a dialog and waits for the selection. Returns the ID of
189 * the selected budget or -1 if the dialog was closed without making a
190 * selection.
191 */
192 public static int showDialog(JFrame parent){
193 AccountSelectorDialog dialog = new AccountSelectorDialog(parent);
194 dialog.dispose();
195 return dialog.selectedAccountId;
196 }
197
198 /** Displays a dialog and waits for the selection. Returns the ID of
199 * the selected budget or -1 if the dialog was closed without making a
200 * selection.
201 */
202 public static int showDialog(JFrame parent, boolean showUnused){
203 AccountSelectorDialog dialog =
204 new AccountSelectorDialog(parent, showUnused);
205 dialog.dispose();
206 return dialog.selectedAccountId;
207 }
208
209 /** Displays a dialog and waits for the selection. Returns the ID of
210 * the selected budget or -1 if the dialog was closed without making a
211 * selection.
212 */
213 public static int showDialog(JFrame parent, Budget budget){
214 AccountSelectorDialog dialog = new AccountSelectorDialog(parent, budget);
215 dialog.dispose();
216 return dialog.selectedAccountId;
217 }
218
219 /** Displays a dialog and waits for the selection. Returns the ID of
220 * the selected budget or -1 if the dialog was closed without making a
221 * selection.
222 */
223 public static int showDialog(JFrame parent,
224 Budget budget,
225 boolean showUnregistered){
226 AccountSelectorDialog dialog =
227 new AccountSelectorDialog(parent, budget, showUnregistered);
228 dialog.dispose();
229 return dialog.selectedAccountId;
230 }
231
232
233
234 /** Close the dialog */
235 private void close(){
236 setVisible(false);
237 dispose();
238 }
239 }