001 /*
002 * Copyright (C) 2003 Adam Olsen
003 * This program is free software; you can redistribute it and/or modify
004 * it under the terms of the GNU General Public License as published by
005 * the Free Software Foundation; either version 1, or (at your option)
006 * any later version.
007 * This program is distributed in the hope that it will be useful,
008 * but WITHOUT ANY WARRANTY; without even the implied warranty of
009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010 * GNU General Public License for more details.
011 * You should have received a copy of the GNU General Public License
012 * along with this program; if not, write to the Free Software
013 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014 */
015 package com.valhalla.gui;
016
017 import java.awt.BorderLayout;
018 import java.awt.event.ActionEvent;
019 import java.awt.event.ActionListener;
020 import java.awt.event.WindowAdapter;
021 import java.awt.event.WindowEvent;
022 import java.util.ArrayList;
023 import java.util.Hashtable;
024
025 import javax.swing.BorderFactory;
026 import javax.swing.Box;
027 import javax.swing.BoxLayout;
028 import javax.swing.Icon;
029 import javax.swing.JButton;
030 import javax.swing.JDialog;
031 import javax.swing.JFrame;
032 import javax.swing.JLabel;
033 import javax.swing.JPanel;
034 import javax.swing.SwingConstants;
035 import javax.swing.UIManager;
036
037 /**
038 * This class allows for the creation of a non-modal option dialog
039 *
040 * @author Adam Olsen
041 * @created October 22, 2004
042 * @version 1.0
043 */
044 public class NMOptionDialog extends JDialog {
045 private JPanel mainPanel;
046
047 private ArrayList listeners = new ArrayList();
048
049 private JPanel buttonPanel = new JPanel();
050
051 private ButtonListener listener = new ButtonListener();
052
053 private Hashtable buttons = new Hashtable();
054
055 /**
056 * Information type
057 */
058 public static final int INFORMATION = 1;
059
060 /**
061 * warning type
062 */
063 public static final int WARNING = 2;
064
065 /**
066 * question type
067 */
068 public static final int QUESTION = 3;
069
070 /**
071 * error type
072 */
073 public static final int ERROR = 4;
074
075 /**
076 * Creates the non-modal option dialog
077 *
078 * @param parent
079 * the dialog's parent
080 * @param title
081 * the text to use in the title bar of the dialog
082 * @param message
083 * the message to display in the dialog's JLabel
084 * @param icon
085 * the icon to display in the icon section
086 */
087 public NMOptionDialog(JFrame parent, String title, String message, int icon) {
088 super(parent, title);
089 mainPanel = (JPanel) getContentPane();
090 mainPanel.setLayout(new BorderLayout());
091
092 message = message.replaceAll("\n", "<br>");
093 JLabel messageLabel = new JLabel("<html>" + message + "</html>",
094 SwingConstants.CENTER);
095 messageLabel.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
096 mainPanel.add(messageLabel, BorderLayout.CENTER);
097 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
098 JPanel bottomPanel = new JPanel();
099 bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
100 bottomPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
101 bottomPanel.add(Box.createHorizontalGlue());
102 bottomPanel.add(buttonPanel);
103 bottomPanel.add(Box.createHorizontalGlue());
104
105 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
106
107 mainPanel.add(bottomPanel, BorderLayout.SOUTH);
108 JLabel iconLabel = new JLabel(getIconFromNum(icon));
109 iconLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 5));
110 mainPanel.add(iconLabel, BorderLayout.WEST);
111
112 pack();
113 setLocationRelativeTo(null);
114 addWindowListener(new WindowAdapter() {
115 public void windowClosing(WindowEvent e) {
116 }
117 });
118 }
119
120 /**
121 * Constructor that creates an NMOptionDialog with an Information icon
122 *
123 * @param parent
124 * the dialog's parent
125 * @param title
126 * the text to use in the title bar of the dialog
127 * @param message
128 * the message to display in the dialog's JLabel
129 */
130 public NMOptionDialog(JFrame parent, String title, String message) {
131 this(parent, title, message, INFORMATION);
132 }
133
134 /**
135 * Returns the icon corresponding to the number value
136 *
137 * @param num
138 * the number of the icon to get
139 * @return the icon corresponding to the specified number
140 */
141 public Icon getIconFromNum(int num) {
142 String name = "OptionPane.";
143 if (num == ERROR) {
144 name += "error";
145 } else if (num == INFORMATION) {
146 name += "information";
147 } else if (num == WARNING) {
148 name += "warning";
149 } else if (num == QUESTION) {
150 name += "question";
151 }
152
153 name += "Icon";
154
155 return UIManager.getIcon(name);
156 }
157
158 /**
159 * Creates a message dialog with only an "OK" button
160 *
161 * @param parent
162 * the dialog' parent
163 * @param title
164 * the text to use in the title bar of the dialog
165 * @param message
166 * the message to display in the dialog's label
167 * @return a NMOptionDialog with only an OK button
168 */
169 public static NMOptionDialog createMessageDialog(JFrame parent,
170 String title, String message) {
171 NMOptionDialog d = new NMOptionDialog(parent, title, message,
172 INFORMATION);
173 d.addButton("OK", 1);
174 d.setVisible(true);
175 return d;
176 }
177
178 /**
179 * Adds a button to the dialog
180 *
181 * @param text
182 * the text to put on the button
183 * @param num
184 * the number of the button
185 */
186 public void addButton(String text, int num) {
187 JButton button = new JButton(text);
188 buttonPanel.add(button);
189 button.addActionListener(listener);
190 pack();
191 validate();
192 repaint();
193
194 buttons.put(text, new Integer(num));
195 }
196
197 /**
198 * Adds a listener to this dialog
199 *
200 * @param l
201 * The feature to be added to the OptionListener attribute
202 */
203 public void addOptionListener(NMOptionListener l) {
204 listeners.add(l);
205 }
206
207 /**
208 * Called by the NMOption panel buttons
209 *
210 * @author synic
211 * @created October 22, 2004
212 */
213 class ButtonListener implements ActionListener {
214 /**
215 * Called by the NMOption panel buttons
216 *
217 * @param e
218 * the event
219 */
220 public void actionPerformed(ActionEvent e) {
221 JButton button = (JButton) e.getSource();
222 String name = button.getText();
223 Integer num = (Integer) buttons.get(name);
224
225 for (int i = 0; i < listeners.size(); i++) {
226 NMOptionListener l = (NMOptionListener) listeners.get(i);
227 l.buttonClicked(num.intValue());
228 }
229
230 dispose();
231 }
232 }
233 }
234