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.io.File;
019    import java.net.URL;
020    import java.util.regex.Pattern;
021    import java.util.*;
022    import java.text.*;
023    
024    import javax.swing.*;
025    
026    /**
027     * Some common utility functions for UI development
028     *
029     * @author     Adam Olsen
030     * @created    November 30, 2004
031     * @version    1.0
032     */
033    public class Standard
034    {
035            private static Standard instance;
036            private int currentX = 20;
037            private int currentY = 40;
038            private ResourceBundle bundle = null;
039    
040            /**
041             * Constructor is private.  Only the static methods should be used.
042             */
043            private Standard()
044            {
045            }
046    
047            /**
048             * Aplies a font to a container and all of it's child components
049             *
050             * @param  component  the component to apply the font to
051             * @param  font       the font to apply
052             */
053            public static void recursivelyApplyFont( Component component, Font font )
054            {
055                    component.setFont( font );
056                    if( !( component instanceof Container ) )
057                    {
058                            return;
059                    }
060    
061                    Component components[] = ( (Container)component ).getComponents();
062                    for( int i = 0; i < components.length; i++ )
063                    {
064                            recursivelyApplyFont( components[i], font );
065                    }
066            }
067    
068            /**
069             * Displays a warning dialog
070             *
071             * @param  parent   this dialog's parent
072             * @param  title    the dialog title
073             * @param  message  the message
074             * @return          Description of the Return Value
075             */
076            public static boolean warningMessage( Container parent, String title, String message )
077            {
078                    JOptionPane.showMessageDialog( parent, message, title, JOptionPane.WARNING_MESSAGE );
079                    return false;
080            }
081    
082            /**
083             * Displays a notice dialog
084             *
085             * @param  parent   this dialog's parent
086             * @param  title    the dialog title
087             * @param  message  the message
088             */
089            public static void noticeMessage( Container parent, String title, String message )
090            {
091                    JOptionPane.showMessageDialog( parent, message, title, JOptionPane.INFORMATION_MESSAGE );
092            }
093    
094            /**
095             * Gets an Image from the resources (usually the jar or current directory that the app is running from)
096             *
097             * @param  icon  the name of the image to get
098             * @return       the requested image, or <tt>null</tt> if it could not be found
099             */
100            public static Image getImage( String icon )
101            {
102                    if( instance == null )
103                    {
104                            instance = new Standard();
105                    }
106    
107                    Toolkit toolkit = Toolkit.getDefaultToolkit();
108                    URL imageUrl = instance.getClass().getClassLoader().getResource( "images/" + icon );
109                    if( imageUrl == null )
110                    {
111                            return null;
112                    }
113    
114                    return toolkit.createImage( imageUrl );
115            }
116    
117            /**
118             * Gets an Icon from the resources (usually the jar or current directory that the app is running from)
119             *
120             * @param  icon  the name of the image to get
121             * @return       the requested icon, or <tt>null</tt> if it could not be found
122             */
123            public static ImageIcon getIcon( String icon )
124            {
125                    if( instance == null )
126                    {
127                            instance = new Standard();
128                    }
129    
130                    Toolkit toolkit = Toolkit.getDefaultToolkit();
131                    URL iconUrl = instance.getClass().getClassLoader().getResource( icon );
132    
133                    if( iconUrl == null )
134                    {
135                            com.valhalla.Logger.debug( "Required image not found " + icon );
136                            return null;
137                    }
138    
139                    return new ImageIcon( iconUrl );
140            }
141    
142            /**
143             * Places a Frame on window in a cascade fashion.  Tracks the last location of the
144             * last window to produce the cascade effect
145             *
146             * @param  container  the container to cascade
147             */
148            public static void cascadePlacement( Container container )
149            {
150                    if( instance == null )
151                    {
152                            instance = new Standard();
153                    }
154    
155                    Toolkit toolkit = Toolkit.getDefaultToolkit();
156                    Dimension sSize = toolkit.getScreenSize();
157                    Dimension cSize = container.getSize();
158    
159                    if( instance.currentX + cSize.width > sSize.width - 50 )
160                    {
161                            instance.currentX = 20;
162                    }
163                    if( instance.currentY + cSize.height > sSize.height - 50 )
164                    {
165                            instance.currentY = 40;
166                    }
167    
168                    container.setLocation( instance.currentX, instance.currentY );
169                    instance.currentX += 100;
170                    instance.currentY += 80;
171            }
172    
173            /**
174             * Sets the default resource bundle to be used with the Standard lib
175             *
176             * @param  bundle  The new bundle value
177             */
178            public static void setBundle( ResourceBundle bundle )
179            {
180                    if( instance == null )
181                    {
182                            instance = new Standard();
183                    }
184    
185                    instance.bundle = bundle;
186            }
187    
188            /**
189             * Throws an error if a field is blank
190             *
191             * @param  field          the value of the field
192             * @param  name           the name of the field
193             * @exception  Exception  to be caught if the field was blank
194             */
195            public static void assure( String field, String name )
196                    throws Exception
197            {
198                    if( field == null || field.equals( "" ) )
199                    {
200                            String message = MessageFormat.format( instance.bundle.getString( "fieldBlank" ), new Object[]{name} );
201                            warningMessage( null, "Error", message );
202                            throw new Exception( message );
203                    }
204            }
205    }
206