001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 005 import com.croftsoft.core.lang.ClassLib; 006 import com.croftsoft.core.lang.lifecycle.Lifecycle; 007 008 /********************************************************************* 009 * Lifecycle management for a Frame. 010 * 011 * <p /> 012 * 013 * @version 014 * 2001-07-24 015 * @since 016 * 2001-05-17 017 * @author 018 * <a href="https://www.croftsoft.com/">David W. Croft</a> 019 *********************************************************************/ 020 021 public final class FrameManager 022 implements Lifecycle 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private Frame frame; 028 029 private String title; 030 031 private String iconName; 032 033 private Dimension size; 034 035 ////////////////////////////////////////////////////////////////////// 036 ////////////////////////////////////////////////////////////////////// 037 038 public static void main ( String [ ] args ) 039 ////////////////////////////////////////////////////////////////////// 040 { 041 FrameManager frameManager = new FrameManager ( "Frame Manager" ); 042 043 frameManager.init ( ); 044 045 frameManager.getFrame ( ).addWindowListener ( 046 new ShutdownWindowListener ( frameManager ) ); 047 048 frameManager.start ( ); 049 } 050 051 ////////////////////////////////////////////////////////////////////// 052 ////////////////////////////////////////////////////////////////////// 053 054 public FrameManager ( 055 Frame frame, 056 String title, 057 Dimension size, 058 String iconName ) 059 ////////////////////////////////////////////////////////////////////// 060 { 061 this.frame = frame; 062 063 this.title = title; 064 065 this.size = size; 066 067 this.iconName = iconName; 068 } 069 070 public FrameManager ( String title ) 071 ////////////////////////////////////////////////////////////////////// 072 { 073 this ( null, title, null, null ); 074 } 075 076 ////////////////////////////////////////////////////////////////////// 077 ////////////////////////////////////////////////////////////////////// 078 079 public synchronized void init ( ) 080 ////////////////////////////////////////////////////////////////////// 081 { 082 if ( frame == null ) 083 { 084 frame = new Frame ( ); 085 } 086 087 if ( title != null ) 088 { 089 frame.setTitle ( title ); 090 } 091 092 if ( iconName != null ) 093 { 094 try 095 { 096 Image frameIconImage 097 = ClassLib.getResourceAsImage ( getClass ( ), iconName ); 098 099 frame.setIconImage ( frameIconImage ); 100 } 101 catch ( Exception ex ) 102 { 103 ex.printStackTrace ( ); 104 } 105 } 106 107 if ( size == null ) 108 { 109 size = Toolkit.getDefaultToolkit ( ).getScreenSize ( ); 110 } 111 112 WindowLib.centerOnScreen ( frame, size ); 113 } 114 115 public synchronized void start ( ) 116 ////////////////////////////////////////////////////////////////////// 117 { 118 frame.show ( ); 119 } 120 121 public synchronized void stop ( ) 122 ////////////////////////////////////////////////////////////////////// 123 { 124 frame.setVisible ( false ); 125 } 126 127 public synchronized void destroy ( ) 128 ////////////////////////////////////////////////////////////////////// 129 { 130 stop ( ); 131 132 frame.dispose ( ); 133 134 frame = null; 135 } 136 137 ////////////////////////////////////////////////////////////////////// 138 ////////////////////////////////////////////////////////////////////// 139 140 public Frame getFrame ( ) { return frame; } 141 142 ////////////////////////////////////////////////////////////////////// 143 ////////////////////////////////////////////////////////////////////// 144 }