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.*; 018 import java.awt.event.*; 019 import javax.swing.*; 020 import javax.swing.event.*; 021 import java.util.*; 022 023 /** 024 * This class allows for the creation of a non-modal option dialog 025 * 026 * @author Adam Olsen 027 * @created October 22, 2004 028 * @version 1.0 029 */ 030 public class NMOptionDialog extends JDialog 031 { 032 private JPanel mainPanel; 033 private ArrayList listeners = new ArrayList(); 034 private JPanel buttonPanel = new JPanel(); 035 private ButtonListener listener = new ButtonListener(); 036 private Hashtable buttons = new Hashtable(); 037 038 /** 039 * Information type 040 */ 041 public static int INFORMATION = 1; 042 /** 043 * warning type 044 */ 045 public static int WARNING = 2; 046 /** 047 * question type 048 */ 049 public static int QUESTION = 3; 050 /** 051 * error type 052 */ 053 public static int ERROR = 4; 054 055 /** 056 * Creates the non-modal option dialog 057 * 058 * @param parent the dialog's parent 059 * @param title the text to use in the title bar of the dialog 060 * @param message the message to display in the dialog's JLabel 061 * @param icon the icon to display in the icon section 062 */ 063 public NMOptionDialog( JFrame parent, String title, String message, int icon ) 064 { 065 super( parent, title ); 066 mainPanel = (JPanel)getContentPane(); 067 mainPanel.setLayout( new BorderLayout() ); 068 069 JLabel messageLabel = new JLabel( "<html>" + message + "</html>", SwingConstants.CENTER ); 070 messageLabel.setBorder( BorderFactory.createEmptyBorder( 5, 15, 5, 15 ) ); 071 mainPanel.add( messageLabel, BorderLayout.CENTER ); 072 mainPanel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); 073 JPanel bottomPanel = new JPanel(); 074 bottomPanel.setLayout( new BoxLayout( bottomPanel, BoxLayout.X_AXIS ) ); 075 bottomPanel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); 076 bottomPanel.add( Box.createHorizontalGlue() ); 077 bottomPanel.add( buttonPanel ); 078 bottomPanel.add( Box.createHorizontalGlue() ); 079 080 buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) ); 081 082 mainPanel.add( bottomPanel, BorderLayout.SOUTH ); 083 JLabel iconLabel = new JLabel( getIconFromNum( icon ) ); 084 iconLabel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 5 ) ); 085 mainPanel.add( iconLabel, BorderLayout.WEST ); 086 087 pack(); 088 setLocationRelativeTo( null ); 089 addWindowListener( 090 new WindowAdapter() 091 { 092 public void windowClosing( WindowEvent e ) 093 { 094 } 095 } ); 096 } 097 098 /** 099 * Constructor that creates an NMOptionDialog with an Information icon 100 * 101 * @param parent the dialog's parent 102 * @param title the text to use in the title bar of the dialog 103 * @param message the message to display in the dialog's JLabel 104 */ 105 public NMOptionDialog( JFrame parent, String title, String message ) 106 { 107 this( parent, title, message, INFORMATION ); 108 } 109 110 /** 111 * Returns the icon corresponding to the number value 112 * 113 * @param num the number of the icon to get 114 * @return the icon corresponding to the specified number 115 */ 116 public Icon getIconFromNum( int num ) 117 { 118 String name = "OptionPane."; 119 if( num == ERROR ) 120 { 121 name += "error"; 122 } 123 else if( num == INFORMATION ) 124 { 125 name += "information"; 126 } 127 else if( num == WARNING ) 128 { 129 name += "warning"; 130 } 131 else if( num == QUESTION ) 132 { 133 name += "question"; 134 } 135 136 name += "Icon"; 137 138 return UIManager.getIcon( name ); 139 } 140 141 /** 142 * Creates a message dialog with only an "OK" button 143 * 144 * @param parent the dialog' parent 145 * @param title the text to use in the title bar of the dialog 146 * @param message the message to display in the dialog's label 147 * @return a NMOptionDialog with only an OK button 148 */ 149 public static NMOptionDialog createMessageDialog( JFrame parent, String title, String message ) 150 { 151 NMOptionDialog d = new NMOptionDialog( parent, title, message, INFORMATION ); 152 d.addButton( "OK", 1 ); 153 d.show(); 154 return d; 155 } 156 157 /** 158 * Adds a button to the dialog 159 * 160 * @param text the text to put on the button 161 * @param num the number of the button 162 */ 163 public void addButton( String text, int num ) 164 { 165 JButton button = new JButton( text ); 166 buttonPanel.add( button ); 167 button.addActionListener( listener ); 168 pack(); 169 validate(); 170 repaint(); 171 172 buttons.put( text, new Integer( num ) ); 173 } 174 175 /** 176 * Adds a listener to this dialog 177 * 178 * @param l The feature to be added to the OptionListener attribute 179 */ 180 public void addOptionListener( NMOptionListener l ) 181 { 182 listeners.add( l ); 183 } 184 185 /** 186 * Called by the NMOption panel buttons 187 * 188 * @author synic 189 * @created October 22, 2004 190 */ 191 class ButtonListener implements ActionListener 192 { 193 /** 194 * Called by the NMOption panel buttons 195 * 196 * @param e the event 197 */ 198 public void actionPerformed( ActionEvent e ) 199 { 200 JButton button = (JButton)e.getSource(); 201 String name = button.getText(); 202 Integer num = (Integer)buttons.get( name ); 203 204 for( int i = 0; i < listeners.size(); i++ ) 205 { 206 NMOptionListener l = (NMOptionListener)listeners.get( i ); 207 l.buttonClicked( num.intValue() ); 208 } 209 210 dispose(); 211 } 212 } 213 } 214