1 7 8 package javax.swing.plaf.basic; 9 10 import java.awt.event.ActionEvent ; 11 import java.awt.KeyboardFocusManager ; 12 import java.awt.Component ; 13 import java.awt.Point ; 14 import java.awt.Rectangle ; 15 import java.beans.PropertyChangeEvent ; 16 import java.beans.PropertyChangeListener ; 17 import javax.swing.*; 18 import javax.swing.plaf.*; 19 import sun.swing.DefaultLookup; 20 import sun.swing.UIAction; 21 22 29 public class BasicRootPaneUI extends RootPaneUI implements 30 PropertyChangeListener { 31 private static RootPaneUI rootPaneUI = new BasicRootPaneUI (); 32 33 public static ComponentUI createUI(JComponent c) { 34 return rootPaneUI; 35 } 36 37 public void installUI(JComponent c) { 38 installDefaults((JRootPane)c); 39 installComponents((JRootPane)c); 40 installListeners((JRootPane)c); 41 installKeyboardActions((JRootPane)c); 42 } 43 44 45 public void uninstallUI(JComponent c) { 46 uninstallDefaults((JRootPane)c); 47 uninstallComponents((JRootPane)c); 48 uninstallListeners((JRootPane)c); 49 uninstallKeyboardActions((JRootPane)c); 50 } 51 52 protected void installDefaults(JRootPane c){ 53 LookAndFeel.installProperty(c, "opaque", Boolean.FALSE); 54 } 55 56 protected void installComponents(JRootPane root) { 57 } 58 59 protected void installListeners(JRootPane root) { 60 root.addPropertyChangeListener(this); 61 } 62 63 protected void installKeyboardActions(JRootPane root) { 64 InputMap km = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, root); 65 SwingUtilities.replaceUIInputMap(root, 66 JComponent.WHEN_IN_FOCUSED_WINDOW, km); 67 km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, 68 root); 69 SwingUtilities.replaceUIInputMap(root, 70 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); 71 72 LazyActionMap.installLazyActionMap(root, BasicRootPaneUI .class, 73 "RootPane.actionMap"); 74 updateDefaultButtonBindings(root); 75 } 76 77 protected void uninstallDefaults(JRootPane root) { 78 } 79 80 protected void uninstallComponents(JRootPane root) { 81 } 82 83 protected void uninstallListeners(JRootPane root) { 84 root.removePropertyChangeListener(this); 85 } 86 87 protected void uninstallKeyboardActions(JRootPane root) { 88 SwingUtilities.replaceUIInputMap(root, JComponent. 89 WHEN_IN_FOCUSED_WINDOW, null); 90 SwingUtilities.replaceUIActionMap(root, null); 91 } 92 93 InputMap getInputMap(int condition, JComponent c) { 94 if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { 95 return (InputMap)DefaultLookup.get(c, this, 96 "RootPane.ancestorInputMap"); 97 } 98 99 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { 100 return createInputMap(condition, c); 101 } 102 return null; 103 } 104 105 ComponentInputMap createInputMap(int condition, JComponent c) { 106 return new RootPaneInputMap(c); 107 } 108 109 static void loadActionMap(LazyActionMap map) { 110 map.put(new Actions(Actions.PRESS)); 111 map.put(new Actions(Actions.RELEASE)); 112 map.put(new Actions(Actions.POST_POPUP)); 113 } 114 115 120 void updateDefaultButtonBindings(JRootPane root) { 121 InputMap km = SwingUtilities.getUIInputMap(root, JComponent. 122 WHEN_IN_FOCUSED_WINDOW); 123 while (km != null && !(km instanceof RootPaneInputMap)) { 124 km = km.getParent(); 125 } 126 if (km != null) { 127 km.clear(); 128 if (root.getDefaultButton() != null) { 129 Object [] bindings = (Object [])DefaultLookup.get(root, this, 130 "RootPane.defaultButtonWindowKeyBindings"); 131 if (bindings != null) { 132 LookAndFeel.loadKeyBindings(km, bindings); 133 } 134 } 135 } 136 } 137 138 143 public void propertyChange(PropertyChangeEvent e) { 144 if(e.getPropertyName().equals("defaultButton")) { 145 JRootPane rootpane = (JRootPane)e.getSource(); 146 updateDefaultButtonBindings(rootpane); 147 if (rootpane.getClientProperty("temporaryDefaultButton") == null) { 148 rootpane.putClientProperty("initialDefaultButton", e.getNewValue()); 149 } 150 } 151 } 152 153 154 static class Actions extends UIAction { 155 public static final String PRESS = "press"; 156 public static final String RELEASE = "release"; 157 public static final String POST_POPUP = "postPopup"; 158 159 Actions(String name) { 160 super(name); 161 } 162 163 public void actionPerformed(ActionEvent evt) { 164 JRootPane root = (JRootPane)evt.getSource(); 165 JButton owner = root.getDefaultButton(); 166 String key = getName(); 167 168 if (key == POST_POPUP) { Component c = KeyboardFocusManager 170 .getCurrentKeyboardFocusManager() 171 .getFocusOwner(); 172 173 if(c instanceof JComponent) { 174 JComponent src = (JComponent) c; 175 JPopupMenu jpm = src.getComponentPopupMenu(); 176 if(jpm != null) { 177 Point pt = src.getPopupLocation(null); 178 if(pt == null) { 179 Rectangle vis = src.getVisibleRect(); 180 pt = new Point (vis.x+vis.width/2, 181 vis.y+vis.height/2); 182 } 183 jpm.show(c, pt.x, pt.y); 184 } 185 } 186 } 187 else if (owner != null 188 && SwingUtilities.getRootPane(owner) == root) { 189 if (key == PRESS) { 190 owner.doClick(20); 191 } 192 } 193 } 194 195 public boolean isEnabled(Object sender) { 196 String key = getName(); 197 if(key == POST_POPUP) { 198 MenuElement[] elems = MenuSelectionManager 199 .defaultManager() 200 .getSelectedPath(); 201 if(elems != null && elems.length != 0) { 202 return false; 203 } 205 206 Component c = KeyboardFocusManager 207 .getCurrentKeyboardFocusManager() 208 .getFocusOwner(); 209 if(c instanceof JComponent) { 210 JComponent src = (JComponent) c; 211 return src.getComponentPopupMenu() != null; 212 } 213 214 return false; 215 } 216 217 if (sender != null && sender instanceof JRootPane) { 218 JButton owner = ((JRootPane)sender).getDefaultButton(); 219 return (owner != null && owner.getModel().isEnabled()); 220 } 221 return true; 222 } 223 } 224 225 private static class RootPaneInputMap extends ComponentInputMapUIResource { 226 public RootPaneInputMap(JComponent c) { 227 super(c); 228 } 229 } 230 } 231 | Popular Tags |