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.user;
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 UserSelectorDialog extends JDialog
047 {
048 private final UserSelectorPanel 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 user */
059 private int selectedUserId = -1;
060
061 /** Creates a list of users.*/
062 public UserSelectorDialog(JFrame parent)
063 {
064 super(parent,"Choose an User", true);
065
066 panel = new UserSelectorPanel();
067
068 initActions();
069 arrangeGui();
070
071
072 pack();
073 if (parent != null)
074 {
075 setLocation(parent.getX() + parent.getWidth()/2 - getWidth()/2,
076 parent.getY() + parent.getHeight()/2 - getHeight()/2);
077 }
078 setVisible(true);
079 }
080
081 /** Arranges gui components */
082 private void arrangeGui()
083 {
084 setTitle("Select a User");
085 JPanel innerPanel = new JPanel();
086 innerPanel.setLayout(new BorderLayout());
087 innerPanel.add(panel,BorderLayout.CENTER);
088 JPanel controlPanel = new JPanel();
089 controlPanel.setPreferredSize(new Dimension(0,40));
090 controlPanel.setMinimumSize(new Dimension(100, 30));
091 innerPanel.add(controlPanel, BorderLayout.SOUTH);
092 SpringLayout layout = new SpringLayout();
093 layout.putConstraint(SpringLayout.WEST, cancelButton, 10,
094 SpringLayout.WEST, controlPanel);
095 layout.putConstraint(SpringLayout.NORTH, cancelButton, 5,
096 SpringLayout.NORTH, controlPanel);
097
098 layout.putConstraint(SpringLayout.EAST, okButton, -10,
099 SpringLayout.EAST, controlPanel);
100 layout.putConstraint(SpringLayout.NORTH, okButton, 5,
101 SpringLayout.NORTH, controlPanel);
102 controlPanel.setLayout(layout);
103 controlPanel.add(cancelButton);
104 controlPanel.add(okButton);
105
106 setContentPane(innerPanel);
107 }
108
109 /** Initializes buttons and other gui component actions. */
110 private void initActions()
111 {
112 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
113
114 okButton.addActionListener(new ActionListener(){
115 public void actionPerformed(ActionEvent ev){
116 selectedUserId = panel.getSelectedUID();
117 selectionMade = true;
118 close();
119 }
120 });
121
122 cancelButton.addActionListener(new ActionListener(){
123 public void actionPerformed(ActionEvent ev){
124 selectedUserId = -1;
125 selectionMade = true;
126 close();
127 }
128 });
129
130 addWindowListener(new WindowAdapter(){
131 @Override
132 public void windowClosing(WindowEvent ev){
133 if (!selectionMade){
134 selectedUserId = -1;
135 selectionMade = true;
136 close();
137 }
138 }
139 });
140 }
141
142 /** Frees resources */
143 @Override
144 public void dispose()
145 {
146 super.dispose();
147 panel.dispose();
148 }
149
150 /** Displays a dialog and waits for the selection. Returns the ID of
151 * the selected budget or -1 if the dialog was closed without making a
152 * selection.
153 */
154 public static int showDialog(JFrame parent)
155 {
156 UserSelectorDialog dialog = new UserSelectorDialog(parent);
157 dialog.dispose();
158 return dialog.selectedUserId;
159 }
160
161 /** Close the dialog */
162 private void close()
163 {
164 setVisible(false);
165 dispose();
166 }
167 }