/*********************************************************************** Copyright (C) 2008-2009, Brent Allen Parrish This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ************************************************************************/ import java.awt.Point; import java.awt.Dimension; import java.awt.Window; import java.awt.Toolkit; import javax.swing.UIManager; import javax.swing.SwingUtilities; import javax.swing.UnsupportedLookAndFeelException; /** * WinUtil is a static utilities class for common window operations. * * @author Brent Allen Parrish * @author Marty Hall * @version %I%, %G% * @since 1.0 */ public class WindowUtil { /** * Centers window locations within its parent object. * Passing null to parent parameter centers window * within the screen, otherwise the child window is centered within * the parent window. centerWindow returns if * child parameter is passed as a null value. * * @param parent reference to the parent window. * @param child reference to child window. * @return java.util.Dimension object. * @since 1.0 */ public static Dimension centerWindow(Window parent, Window child) { Dimension center; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); if(child == null) { return null; } if(parent != null) { Point frameLoc = parent.getLocationOnScreen(); Dimension frameSize = parent.getSize(); center = new Dimension((frameLoc.x + (frameSize.width / 2) - (child.getWidth()) / 2), (frameLoc.y + (frameSize.height / 2) - (child.getHeight()) / 2)); } else { center = new Dimension((screenSize.width / 2) - (child.getWidth() / 2), (screenSize.height / 2) - (child.getHeight() / 2)); } return center; } /** * Returns just the class name -- no package info. * * @return java.lang.String of the class name minus package information. * @since 1.0 */ public static String getClassName(Object o) { String classString = o.getClass().getName(); int dotIndex = classString.lastIndexOf("."); return classString.substring(dotIndex+1); } /** * Attempts to set this LAF by passing className as a string * parameter and calling the UIManager#setLookAndFeel method. * * @param className java.lang.String of a valid * javax.swing.LookAndFeel class. * @see java.lang.ClassNotFoundException * @see java.lang.InstantiationException * @see java.lang.IllegalAccessException * @see javax.swing.UnsupportedLookAndFeelException * @since 1.0 */ public static void setLookAndFeelByClassName(String className) throws Exception { try { UIManager.setLookAndFeel(className); } catch(ClassNotFoundException cfe) { System.err.println("The [" + className + "] could not be found.\n\n"); printExceptionError(cfe.getCause(), cfe.getMessage(), true); cfe.printStackTrace(); } catch(InstantiationException ie) { printExceptionError(ie.getCause(), ie.getMessage(), true); ie.printStackTrace(); } catch(IllegalAccessException iae) { printExceptionError(iae.getCause(), iae.getMessage(), true); iae.printStackTrace(); } catch(UnsupportedLookAndFeelException ule) { System.err.println("The [" + className + "] is unsupported on this system.\n\n"); printExceptionError(ule.getCause(), ule.getMessage(), true); ule.printStackTrace(); } } /** * Prints an exception cause message and accepts a flag to indicate whether to print * a seperator of form \n---------- STACK TRACE OUTPUT ----------\n * * @param cause java.lang.Exception#cause message. * @param msg java.lang.Exception#message message. * @param seperator indicates whether to print the stack trace output seperator. */ public static void printExceptionError(Throwable cause, String msg, boolean seperator) { System.err.println("CAUSE: " + cause); System.err.println("MESSAGE:" + msg); if(seperator) { System.err.println("\n---------- STACK TRACE OUTPUT ----------\n"); } } /** * Tell system to use native look and feel, as in previous releases. * Metal (Java) Look and Feel (LAF) is the default otherwise. * * @see javax.swing.UIManager * @since 1.0 */ public static void setNativeLookAndFeel() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.err.println("Error setting native LAF: " + e); } } /** * Tell system to use Windows Classic LAF. * * @since 1.0 */ public static void setClassicLookAndFeel() { try { setLookAndFeelByClassName("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch (Exception e) { System.err.println("Error setting Windows Classic LAF: " + e); } } /** * Tell system to use Metal (Java) LAF. * * @since 1.0 */ public static void setJavaLookAndFeel() { try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { System.err.println("Error setting Java(Metal) LAF: " + e); } } /** * Tell system to use Motif LAF. * * @see com.woven_media.colorsafe.WindowUtil#setLookAndFeelByClassName * @since 1.0 */ public static void setMotifLookAndFeel() { try { setLookAndFeelByClassName("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } catch (Exception e) { System.err.println("Error setting CDE/Motif LAF: " + e); } } } // End class