下面列出了java.awt.Component#getParent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Replaces an existing component with a new one.
*
* @param existingComponent the component that should be removed
* and replaced with {@code newComponent}
* @param newComponent the component to put in
* {@code existingComponent}'s place
* @throws IllegalArgumentException if either of the components are
* {@code null} or {@code existingComponent} is not being managed
* by this layout manager
*/
public void replace(Component existingComponent, Component newComponent) {
if (existingComponent == null || newComponent == null) {
throw new IllegalArgumentException("Components must be non-null");
}
// Make sure all the components have been registered, otherwise we may
// not update the correct Springs.
if (springsChanged) {
registerComponents(horizontalGroup, HORIZONTAL);
registerComponents(verticalGroup, VERTICAL);
}
ComponentInfo info = componentInfos.remove(existingComponent);
if (info == null) {
throw new IllegalArgumentException("Component must already exist");
}
host.remove(existingComponent);
if (newComponent.getParent() != host) {
host.add(newComponent);
}
info.setComponent(newComponent);
componentInfos.put(newComponent, info);
invalidateHost();
}
private void initPopup(Component owner, Component contents, int x, int y, Popup popup) {
this.owner = owner;
this.contents = contents;
this.popup = popup;
this.x = x;
this.y = y;
boolean mac = false;
try {
mac = System.getProperty("os.name").toLowerCase().startsWith("mac");
} catch (SecurityException e) {
// do nothing
}
if (mac) {
((JComponent) contents).setBorder(Borders.getPopupMenuBorder());
} else if (((JComponent) contents).getBorder() instanceof DummyBorder) {
if ((owner != null) //
&& (((owner instanceof JMenu) && ((JMenu) owner).isTopLevelMenu()) //
|| ((owner.getParent() != null) && (owner.getParent() instanceof javax.swing.JToolBar)) //
|| (owner instanceof javax.swing.JComboBox))) {
((JComponent) contents).setBorder(Borders.getPopupBorder());
} else {
((JComponent) contents).setBorder(Borders.getShadowedPopupMenuBorder());
}
}
}
private boolean accept(Component aComponent) {
if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
aComponent.isFocusable() && aComponent.isEnabled())) {
return false;
}
// Verify that the Component is recursively enabled. Disabling a
// heavyweight Container disables its children, whereas disabling
// a lightweight Container does not.
if (!(aComponent instanceof Window)) {
for (Container enableTest = aComponent.getParent();
enableTest != null;
enableTest = enableTest.getParent())
{
if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
return false;
}
if (enableTest instanceof Window) {
break;
}
}
}
return true;
}
void addListeners(Component ancestor, boolean addToFirst) {
Component a;
firstInvisibleAncestor = null;
for (a = ancestor;
firstInvisibleAncestor == null;
a = a.getParent()) {
if (addToFirst || a != ancestor) {
a.addComponentListener(this);
if (a instanceof JComponent) {
JComponent jAncestor = (JComponent)a;
jAncestor.addPropertyChangeListener(this);
}
}
if (!a.isVisible() || a.getParent() == null || a instanceof Window) {
firstInvisibleAncestor = a;
}
}
if (firstInvisibleAncestor instanceof Window &&
firstInvisibleAncestor.isVisible()) {
firstInvisibleAncestor = null;
}
}
private boolean accept(Component aComponent) {
if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
aComponent.isFocusable() && aComponent.isEnabled())) {
return false;
}
// Verify that the Component is recursively enabled. Disabling a
// heavyweight Container disables its children, whereas disabling
// a lightweight Container does not.
if (!(aComponent instanceof Window)) {
for (Container enableTest = aComponent.getParent();
enableTest != null;
enableTest = enableTest.getParent())
{
if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
return false;
}
if (enableTest instanceof Window) {
break;
}
}
}
return true;
}
private JEditorPane findPane(Component component) {
while (component != null) {
if (component instanceof JEditorPane) {
return (JEditorPane) component;
}
component = component.getParent();
}
return null;
}
/**
* Returns true if the parent component contains the child. (Does not return
* true if the parent and child arguments are the same.)
*
* @param parent
* @param child
* @return
*/
private static boolean contains(Component parent, Component child) {
Component c = child;
while (c != null) {
c = c.getParent();
if (parent == c) {
return true;
}
}
return false;
}
public void removeDropTarget(DropTarget dt) {
Component comp = target;
while(!(comp == null || comp instanceof Window)) {
comp = comp.getParent();
}
if (comp instanceof Window) {
XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
if (wpeer != null) {
wpeer.removeDropTarget();
}
}
}
public synchronized void notifyChangeRequestByHotKey(Component comp) {
while (!(comp instanceof Frame || comp instanceof Dialog)) {
if (comp == null) {
// no Frame or Dialog found in containment hierarchy.
return;
}
comp = comp.getParent();
}
notifyChangeRequest(comp);
}
/**
* @param comp The {@link Component} to use. May be {@code null}.
* @return The most logical {@link Window} associated with the component.
*/
public static Window getWindowForComponent(Component comp) {
while (true) {
if (comp == null) {
return JOptionPane.getRootFrame();
}
if (comp instanceof Frame || comp instanceof Dialog) {
return (Window) comp;
}
comp = comp.getParent();
}
}
public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
if (!(c instanceof JMenuItem)) {
return;
}
JMenuItem b = (JMenuItem) c;
ButtonModel model = b.getModel();
g.translate( x, y );
if ( c.getParent() instanceof JMenuBar ) {
if ( model.isArmed() || model.isSelected() ) {
g.setColor( MetalLookAndFeel.getControlDarkShadow() );
g.drawLine( 0, 0, w - 2, 0 );
g.drawLine( 0, 0, 0, h - 1 );
g.drawLine( w - 2, 2, w - 2, h - 1 );
g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
g.drawLine( w - 1, 1, w - 1, h - 1 );
g.setColor( MetalLookAndFeel.getMenuBackground() );
g.drawLine( w - 1, 0, w - 1, 0 );
}
} else {
if ( model.isArmed() || ( c instanceof JMenu && model.isSelected() ) ) {
g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
g.drawLine( 0, 0, w - 1, 0 );
g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
g.drawLine( 0, h - 1, w - 1, h - 1 );
} else {
g.setColor( MetalLookAndFeel.getPrimaryControlHighlight() );
g.drawLine( 0, 0, 0, h - 1 );
}
}
g.translate( -x, -y );
}
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*
* @since 1.5
* @return <code>true</code> if the background is completely opaque
* and differs from the JList's background;
* <code>false</code> otherwise
*/
@Override
public boolean isOpaque() {
Color back = getBackground();
Component p = getParent();
if (p != null) {
p = p.getParent();
}
// p should now be the JList.
boolean colorMatch = (back != null) && (p != null) &&
back.equals(p.getBackground()) &&
p.isOpaque();
return !colorMatch && super.isOpaque();
}
/**
* @param component The component whose ancestor chain is to be looked at.
* @param type The type of ancestor being looked for.
* @return The ancestor, or {@code null}.
*/
@SuppressWarnings("unchecked")
public static <T> T getAncestorOfType(Component component, Class<T> type) {
if (component == null) {
return null;
}
Container parent = component.getParent();
while (parent != null && !type.isAssignableFrom(parent.getClass())) {
parent = parent.getParent();
}
return (T) parent;
}
private void processFocusedComponent(Component c) {
Component cc = c;
while (c != null) {
if (c == ProfilerTopComponent.this) {
lastFocusOwner = cc;
return;
}
c = c.getParent();
}
}
static boolean isToolBarButton( Component c ) {
return c.getParent() instanceof JToolBar ||
(c instanceof AbstractButton && clientPropertyEquals( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON ));
}
protected Container getParent(Component client) {
return client.getParent();
}
public void stateChanged(ChangeEvent ev) {
if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {
uninstall();
return;
}
MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
MenuElement[] p = msm.getSelectedPath();
JPopupMenu popup = getActivePopup(p);
if (popup != null && !popup.isFocusable()) {
// Do nothing for non-focusable popups
return;
}
if (lastPathSelected.length != 0 && p.length != 0 ) {
if (!checkInvokerEqual(p[0],lastPathSelected[0])) {
removeItems();
lastPathSelected = new MenuElement[0];
}
}
if (lastPathSelected.length == 0 && p.length > 0) {
// menu posted
JComponent invoker;
if (popup == null) {
if (p.length == 2 && p[0] instanceof JMenuBar &&
p[1] instanceof JMenu) {
// a menu has been selected but not open
invoker = (JComponent)p[1];
popup = ((JMenu)invoker).getPopupMenu();
} else {
return;
}
} else {
Component c = popup.getInvoker();
if(c instanceof JFrame) {
invoker = ((JFrame)c).getRootPane();
} else if(c instanceof JDialog) {
invoker = ((JDialog)c).getRootPane();
} else if(c instanceof JApplet) {
invoker = ((JApplet)c).getRootPane();
} else {
while (!(c instanceof JComponent)) {
if (c == null) {
return;
}
c = c.getParent();
}
invoker = (JComponent)c;
}
}
// remember current focus owner
lastFocused = KeyboardFocusManager.
getCurrentKeyboardFocusManager().getFocusOwner();
// request focus on root pane and install keybindings
// used for menu navigation
invokerRootPane = SwingUtilities.getRootPane(invoker);
if (invokerRootPane != null) {
invokerRootPane.addFocusListener(rootPaneFocusListener);
invokerRootPane.requestFocus(true);
invokerRootPane.addKeyListener(this);
focusTraversalKeysEnabled = invokerRootPane.
getFocusTraversalKeysEnabled();
invokerRootPane.setFocusTraversalKeysEnabled(false);
menuInputMap = getInputMap(popup, invokerRootPane);
addUIInputMap(invokerRootPane, menuInputMap);
addUIActionMap(invokerRootPane, menuActionMap);
}
} else if (lastPathSelected.length != 0 && p.length == 0) {
// menu hidden -- return focus to where it had been before
// and uninstall menu keybindings
removeItems();
} else {
if (popup != lastPopup) {
receivedKeyPressed = false;
}
}
// Remember the last path selected
lastPathSelected = p;
lastPopup = popup;
}
public void stateChanged(ChangeEvent ev) {
if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {
uninstall();
return;
}
MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
MenuElement[] p = msm.getSelectedPath();
JPopupMenu popup = getActivePopup(p);
if (popup != null && !popup.isFocusable()) {
// Do nothing for non-focusable popups
return;
}
if (lastPathSelected.length != 0 && p.length != 0 ) {
if (!checkInvokerEqual(p[0],lastPathSelected[0])) {
removeItems();
lastPathSelected = new MenuElement[0];
}
}
if (lastPathSelected.length == 0 && p.length > 0) {
// menu posted
JComponent invoker;
if (popup == null) {
if (p.length == 2 && p[0] instanceof JMenuBar &&
p[1] instanceof JMenu) {
// a menu has been selected but not open
invoker = (JComponent)p[1];
popup = ((JMenu)invoker).getPopupMenu();
} else {
return;
}
} else {
Component c = popup.getInvoker();
if(c instanceof JFrame) {
invoker = ((JFrame)c).getRootPane();
} else if(c instanceof JDialog) {
invoker = ((JDialog)c).getRootPane();
} else if(c instanceof JApplet) {
invoker = ((JApplet)c).getRootPane();
} else {
while (!(c instanceof JComponent)) {
if (c == null) {
return;
}
c = c.getParent();
}
invoker = (JComponent)c;
}
}
// remember current focus owner
lastFocused = KeyboardFocusManager.
getCurrentKeyboardFocusManager().getFocusOwner();
// request focus on root pane and install keybindings
// used for menu navigation
invokerRootPane = SwingUtilities.getRootPane(invoker);
if (invokerRootPane != null) {
invokerRootPane.addFocusListener(rootPaneFocusListener);
invokerRootPane.requestFocus(true);
invokerRootPane.addKeyListener(this);
focusTraversalKeysEnabled = invokerRootPane.
getFocusTraversalKeysEnabled();
invokerRootPane.setFocusTraversalKeysEnabled(false);
menuInputMap = getInputMap(popup, invokerRootPane);
addUIInputMap(invokerRootPane, menuInputMap);
addUIActionMap(invokerRootPane, menuActionMap);
}
} else if (lastPathSelected.length != 0 && p.length == 0) {
// menu hidden -- return focus to where it had been before
// and uninstall menu keybindings
removeItems();
} else {
if (popup != lastPopup) {
receivedKeyPressed = false;
}
}
// Remember the last path selected
lastPathSelected = p;
lastPopup = popup;
}
@Override
protected boolean isTableCellEditor( Component c ) {
return c.getParent() instanceof JTable;
}
/** Convenience method that returns the first JLayeredPane which
* contains the specified component. Note that all JFrames have a
* JLayeredPane at their root, so any component in a JFrame will
* have a JLayeredPane parent.
*
* @param c the Component to check
* @return the JLayeredPane that contains the component, or
* null if no JLayeredPane is found in the component
* hierarchy
* @see JFrame
* @see JRootPane
*/
public static JLayeredPane getLayeredPaneAbove(Component c) {
if(c == null) return null;
Component parent = c.getParent();
while(parent != null && !(parent instanceof JLayeredPane))
parent = parent.getParent();
return (JLayeredPane)parent;
}