1 7 package org.ejtools.adwt.service; 8 9 import java.awt.BorderLayout ; 10 import java.awt.Dimension ; 11 import java.awt.Toolkit ; 12 import java.beans.PropertyVetoException ; 13 import java.beans.beancontext.BeanContextServices ; 14 import java.util.Iterator ; 15 import java.util.Vector ; 16 17 import javax.swing.ButtonGroup ; 18 import javax.swing.JInternalFrame ; 19 import javax.swing.WindowConstants ; 20 21 import org.apache.log4j.Logger; 22 import org.ejtools.adwt.action.Command; 23 import org.ejtools.adwt.action.window.CascadeAction; 24 import org.ejtools.adwt.action.window.CloseAction; 25 import org.ejtools.adwt.action.window.CloseAllAction; 26 import org.ejtools.adwt.action.window.TileAction; 27 28 35 public class MDIFrameServiceProvider extends FrameServiceProvider implements MDIFrameService 36 { 37 38 protected MDIDesktopPane desktop = new MDIDesktopPane(); 39 40 protected Vector frames = new Vector (); 41 42 protected ButtonGroup group = new ButtonGroup (); 43 44 private static Logger logger = Logger.getLogger(MDIFrameServiceProvider.class); 45 46 47 48 public MDIFrameServiceProvider() 49 { 50 super(); 51 } 52 53 54 59 public void activate(BeanContextInternalFrame frame) 60 { 61 try 62 { 63 JInternalFrame internalFrame = (JInternalFrame ) frame.getComponent(); 64 internalFrame.setSelected(true); 65 this.desktop.getDesktopManager().activateFrame(internalFrame); 66 } 67 catch (PropertyVetoException e) 68 { 69 } 70 } 71 72 73 78 public BeanContextInternalFrame getActivated() 79 { 80 Iterator it = frames.iterator(); 81 while (it.hasNext()) 82 { 83 BeanContextInternalFrame frame = (BeanContextInternalFrame) it.next(); 84 if (this.isActivated(frame)) 85 { 86 return frame; 87 } 88 } 89 return null; 90 } 91 92 93 99 public boolean isActivated(BeanContextInternalFrame frame) 100 { 101 JInternalFrame selected = this.desktop.getSelectedFrame(); 102 JInternalFrame internalFrame = (JInternalFrame ) frame.getComponent(); 103 return selected.equals(internalFrame); 104 } 105 106 107 112 public void register(BeanContextInternalFrame frame) 113 { 114 JInternalFrame internalFrame = (JInternalFrame ) frame.getComponent(); 115 this.frames.add(frame); 116 this.desktop.add(internalFrame); 117 this.group.add(frame.getAction().getMenuItem()); 118 } 119 120 121 126 public void unregister(BeanContextInternalFrame frame) 127 { 128 JInternalFrame internalFrame = (JInternalFrame ) frame.getComponent(); 129 this.group.remove(frame.getAction().getMenuItem()); 130 this.desktop.remove(internalFrame); 131 this.frames.remove(frame); 132 } 133 134 135 138 protected Class [] getServiceClass() 139 { 140 return new Class []{FrameService.class, MDIFrameService.class}; 141 } 142 143 144 145 protected void initializeBeanContextResources() 146 { 147 super.initializeBeanContextResources(); 148 149 BeanContextServices context = (BeanContextServices ) this.getBeanContext(); 150 151 this.frame.setSize(new Dimension (700, 500)); 152 153 this.scrollPane.getViewport().add(desktop); 154 this.frame.getContentPane().add(BorderLayout.CENTER, scrollPane); 155 156 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 157 this.frame.setLocation(screen.width / 2 - frame.getSize().width / 2, screen.height / 2 - frame.getSize().height / 2); 158 this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 159 160 this.add(new CascadeAction( 161 new Command() 162 { 163 public void execute() 164 { 165 MDIFrameServiceProvider.this.desktop.cascadeFrames(); 166 } 167 } 168 )); 169 170 this.add(new TileAction( 171 new Command() 172 { 173 public void execute() 174 { 175 MDIFrameServiceProvider.this.desktop.tileFrames(); 176 } 177 } 178 )); 179 180 this.add(new CloseAction( 181 new Command() 182 { 183 public void execute() 184 { 185 BeanContextInternalFrame frame = MDIFrameServiceProvider.this.getActivated(); 186 if (frame != null) 187 { 188 frame.close(); 189 } 190 } 191 } 192 )); 193 194 this.add(new CloseAllAction( 195 new Command() 196 { 197 public void execute() 198 { 199 while (!MDIFrameServiceProvider.this.frames.isEmpty()) 200 { 201 BeanContextInternalFrame frame = (BeanContextInternalFrame) MDIFrameServiceProvider.this.frames.lastElement(); 202 frame.close(); 203 } 204 } 205 } 206 )); 207 208 this.useServices(context); 209 this.frame.setVisible(true); 210 211 logger.debug("MDIFrameService added"); 212 } 213 214 215 216 protected void releaseBeanContextResources() 217 { 218 super.releaseBeanContextResources(); 219 220 logger.debug("MDIFrameService removed"); 221 } 222 } 223 | Popular Tags |