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 * BasicDialog.java
024 *
025 * Created on April 5, 2006, 4:39 PM
026 */
027
028 package org.jbudget.gui;
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.util.ArrayList;
035 import java.util.List;
036 import javax.swing.JButton;
037 import javax.swing.JDialog;
038 import javax.swing.JPanel;
039 import javax.swing.JSeparator;
040 import javax.swing.SpringLayout;
041
042 /**
043 * A class that ceates a dialog window with two buttons.
044 * It is a base class for more complex dialog windows.
045 * @author petrov
046 */
047 abstract public class BasicDialog extends JDialog
048 {
049
050 //================================ Data ==================================//
051
052 /** List of action listeners that will be notified when OK and Cancel
053 * buttons are pressed. */
054 final private List<ActionListener>
055 listeners = new ArrayList<ActionListener>(1);
056
057 final protected JButton okButton = new JButton("OK");
058 final protected JButton cancelButton = new JButton("Cancel");
059
060 /** Panel that contains the dialog data. This should be filled by the
061 * children classes. */
062 final protected JPanel inputPanel = new JPanel();
063
064 /** Panel containing the control buttons. */
065 final protected JPanel controlPanel = new JPanel();
066
067 /** Separator between the inputPanel and the controlPanel. */
068 final private JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
069 private final SpringLayout controlPanelLayout = new SpringLayout();
070 private final boolean alwaysDispose;
071
072 //============================== Constructors ============================//
073
074 public BasicDialog(java.awt.Frame parent, boolean modular)
075 {
076 this(parent, modular, true);
077 }
078
079 /** Creates a new instance of BasicDialog.
080 *@param alwaysDispose if true the dialog is hidded and disposed after the
081 * OK button is pressed even if okAction() returns false.
082 */
083 public BasicDialog(java.awt.Frame parent,
084 boolean modular,
085 boolean alwaysDispose)
086 {
087 super(parent, modular);
088
089 this.alwaysDispose = alwaysDispose;
090
091 inputPanel.setLayout(new BorderLayout());
092 separator.setMinimumSize(new java.awt.Dimension(100, 2));
093 separator.setPreferredSize(new java.awt.Dimension(100, 2));
094 inputPanel.add(separator, java.awt.BorderLayout.SOUTH);
095
096 JPanel mainPanel = new JPanel();
097 mainPanel.setLayout(new BorderLayout());
098 mainPanel.add(inputPanel, BorderLayout.CENTER);
099 mainPanel.add(controlPanel, BorderLayout.SOUTH);
100
101 setContentPane(mainPanel);
102
103 controlPanel.setLayout(controlPanelLayout);
104
105 controlPanel.add(okButton);
106 controlPanelLayout.putConstraint(SpringLayout.EAST, okButton,
107 -20, SpringLayout.EAST, controlPanel);
108 controlPanelLayout.putConstraint(SpringLayout.SOUTH, okButton,
109 -5, SpringLayout.SOUTH, controlPanel);
110
111 controlPanel.add(cancelButton);
112 controlPanelLayout.putConstraint(SpringLayout.WEST, cancelButton,
113 20, SpringLayout.WEST, controlPanel);
114 controlPanelLayout.putConstraint(SpringLayout.SOUTH, cancelButton,
115 -5, SpringLayout.SOUTH, controlPanel);
116
117 controlPanel.setPreferredSize(new Dimension(100, 37));
118 controlPanel.setMinimumSize(new Dimension(0, 37));
119 controlPanel.setMaximumSize(new Dimension(5000, 37));
120
121 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
122
123 // Cancel button is pressed. The window is closed....
124 cancelButton.addActionListener(new CancelListener());
125
126 // OK button is pressed.
127 okButton.addActionListener(new OKListener());
128 }
129
130 /**
131 * Creates a new istance of BasicDialog with an extra button. The button's
132 * actions are not handled by the dialog.
133 */
134 public BasicDialog(java.awt.Frame parent, boolean modular, JButton aButton)
135 {
136 this(parent, modular);
137
138 controlPanel.add(aButton);
139 controlPanelLayout.putConstraint(SpringLayout.WEST, aButton,
140 20, SpringLayout.EAST, cancelButton);
141 controlPanelLayout.putConstraint(SpringLayout.SOUTH, aButton,
142 -5, SpringLayout.SOUTH, controlPanel);
143
144 }
145
146 //================================== Methods =============================//
147
148 /** Adds a button to the dialog. */
149 public void addButton(JButton aButton)
150 {
151 controlPanel.add(aButton);
152 controlPanelLayout.putConstraint(SpringLayout.WEST, aButton,
153 20, SpringLayout.EAST, cancelButton);
154 controlPanelLayout.putConstraint(SpringLayout.SOUTH, aButton,
155 -5, SpringLayout.SOUTH, controlPanel);
156 }
157
158 /** A function that is called automatically when the OK button is
159 * pressed before the listeners are notified. If this function returns
160 * true the listeners are notified and the window is closed, if false is
161 * returned the behavior of the dialog depends on the value of the
162 * alwaysDispose field.
163 */
164 abstract protected boolean okAction() throws Exception;
165
166 /** adds a listener that will be notified when the OK button is pressed */
167 public void addActionListener(ActionListener l)
168 {
169 listeners.add(l);
170 }
171
172 /** Sends ActionEvent to all registered listeners */
173 private void notifyListeners(String action)
174 {
175 ActionEvent event = new ActionEvent(this, 0, action);
176 for (ActionListener l : listeners)
177 l.actionPerformed(event);
178 }
179
180 /** Inner class that is used to handle the OK button events
181 * and other events that cause the dialog to close and notfy
182 * listeners of the new input
183 */
184 public class OKListener implements ActionListener
185 {
186 public void actionPerformed(ActionEvent e)
187 {
188 try
189 {
190 if (okAction())
191 {
192 setVisible(false);
193 notifyListeners("OK");
194 dispose();
195 }
196 else if (alwaysDispose)
197 {
198 setVisible(false);
199 notifyListeners("CANCEL");
200 dispose();
201 }
202 }
203 catch (Exception ex)
204 {
205 /*If an exception is thrown don't do anything */
206 }
207 }
208 }
209
210 /** Inner class that is used to handle the OK button events
211 * and other events that cause the dialog to close and notfy
212 * listeners of the new input
213 */
214 public class CancelListener implements ActionListener
215 {
216 public void actionPerformed(ActionEvent e)
217 {
218 setVisible(false);
219 notifyListeners("CANCEL");
220 dispose();
221 }
222 }
223 }