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