1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import sun.awt.ModalExclude; 12 13 39 public class Popup { 40 43 private Component component; 44 45 64 protected Popup(Component owner, Component contents, int x, int y) { 65 this(); 66 if (contents == null) { 67 throw new IllegalArgumentException ("Contents must be non-null"); 68 } 69 reset(owner, contents, x, y); 70 } 71 72 75 protected Popup() { 76 } 77 78 82 public void show() { 83 Component component = getComponent(); 84 85 if (component != null) { 86 component.show(); 87 } 88 } 89 90 98 public void hide() { 99 Component component = getComponent(); 100 101 if (component instanceof JWindow ) { 102 component.hide(); 103 ((JWindow )component).getContentPane().removeAll(); 104 } 105 dispose(); 106 } 107 108 111 void dispose() { 112 Component component = getComponent(); 113 114 if (component instanceof JWindow ) { 115 ((Window)component).dispose(); 116 component = null; 117 } 118 } 119 120 123 void reset(Component owner, Component contents, int ownerX, int ownerY) { 124 if (getComponent() == null) { 125 component = createComponent(owner); 126 } 127 128 Component c = getComponent(); 129 130 if (c instanceof JWindow ) { 131 JWindow component = (JWindow )getComponent(); 132 133 component.setLocation(ownerX, ownerY); 134 component.getContentPane().add(contents, BorderLayout.CENTER); 135 contents.invalidate(); 136 if(component.isVisible()) { 137 pack(); 140 } 141 } 142 } 143 144 145 149 void pack() { 150 Component component = getComponent(); 151 152 if (component instanceof Window) { 153 ((Window)component).pack(); 154 } 155 } 156 157 164 private Window getParentWindow(Component owner) { 165 Window window = null; 166 167 if (owner instanceof Window) { 168 window = (Window)owner; 169 } 170 else if (owner != null) { 171 window = SwingUtilities.getWindowAncestor(owner); 172 } 173 if (window == null) { 174 window = new DefaultFrame(); 175 } 176 return window; 177 } 178 179 184 Component createComponent(Component owner) { 185 if (GraphicsEnvironment.isHeadless()) { 186 return null; 188 } 189 return new HeavyWeightWindow(getParentWindow(owner)); 190 } 191 192 196 Component getComponent() { 197 return component; 198 } 199 200 201 204 static class HeavyWeightWindow extends JWindow implements ModalExclude { 205 HeavyWeightWindow(Window parent) { 206 super(parent); 207 setFocusableWindowState(false); 208 setName("###overrideRedirect###"); 209 } 210 211 public void update(Graphics g) { 212 paint(g); 213 } 214 215 public void show() { 216 this.pack(); 217 super.show(); 218 } 219 } 220 221 222 228 static class DefaultFrame extends Frame { 229 } 230 } 231 | Popular Tags |