下面列出了javax.swing.SwingUtilities#isDescendingFrom ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void setVisible(boolean visible) {
boolean oldVisible = isVisible();
super.setVisible(visible);
JRootPane rootPane = SwingUtilities.getRootPane(this);
if ((rootPane != null) && (isVisible() != oldVisible)) {
if (isVisible()) {
Component focusOwner = KeyboardFocusManager.
getCurrentKeyboardFocusManager().getPermanentFocusOwner();
if ((focusOwner != null) &&
SwingUtilities.isDescendingFrom(focusOwner, rootPane)) {
recentFocusOwner = focusOwner;
}
rootPane.getLayeredPane().setVisible(false);
requestFocusInWindow();
} else {
rootPane.getLayeredPane().setVisible(true);
if (recentFocusOwner != null) {
recentFocusOwner.requestFocusInWindow();
}
recentFocusOwner = null;
}
}
}
private static Dialog findYoungestChildDialogOfParentDialog(Dialog parentDialog,
List<Dialog> openModalDialogs) {
if (parentDialog == null) {
return null;
}
for (Dialog potentialChildDialog : openModalDialogs) {
if (parentDialog == potentialChildDialog) {
continue;
}
if (SwingUtilities.isDescendingFrom(potentialChildDialog, parentDialog)) {
return findYoungestChildDialogOfParentDialog(potentialChildDialog,
openModalDialogs);
}
}
return parentDialog;
}
/** Transfer the focus to the editor pane.
*/
@Deprecated
@Override
public boolean requestFocusInWindow() {
super.requestFocusInWindow();
if (pane != null) {
if ((customComponent != null) && !SwingUtilities.isDescendingFrom(pane, customComponent)) {
return customComponent.requestFocusInWindow();
} else {
return pane.requestFocusInWindow();
}
}
return false;
}
@Override
public void eventDispatched(AWTEvent e) {
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent) e;
if (!SwingUtilities.isDescendingFrom(me.getComponent(), target)) {
return;
}
if (me.getID() == MouseEvent.MOUSE_RELEASED) {
// stop when mouse released
mouseOnScreenPoint = null;
if (timer.isRunning()) {
timer.stop();
}
} else if (me.getID() == MouseEvent.MOUSE_DRAGGED && me.getComponent() == target) {
mouseOnScreenPoint = me.getLocationOnScreen();
} else if (me.getID() == MouseEvent.MOUSE_PRESSED && me.getComponent() == target) {
mouseOnScreenPoint = me.getLocationOnScreen();
timer.start();
}
}
}
/**
* Checks if the focus is still on this component or its child components.
*/
private boolean isFocusInside(Object newFocusedComp) {
if (newFocusedComp instanceof Popup) {
return true;
}
if (newFocusedComp instanceof Component && !SwingUtilities.isDescendingFrom((Component) newFocusedComp, this)) {
// Check if focus is on other window
if (containingWindow == null) {
return false;
}
Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
// if focus is on other window in owner chain, return false
Window parent = containingWindow;
do {
if (parent == focusedWindow) {
return false;
} else {
parent = parent.getOwner();
}
} while (parent != null);
}
return true;
}
protected boolean isFocused( Component c ) {
if( c instanceof JScrollPane ) {
JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null;
if( view != null ) {
if( FlatUIUtils.isPermanentFocusOwner( view ) )
return true;
if( (view instanceof JTable && ((JTable)view).isEditing()) ||
(view instanceof JTree && ((JTree)view).isEditing()) )
{
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if( focusOwner != null )
return SwingUtilities.isDescendingFrom( focusOwner, view );
}
}
return false;
} else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) {
Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent();
return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false;
} else if( c instanceof JSpinner ) {
if( FlatUIUtils.isPermanentFocusOwner( c ) )
return true;
JComponent editor = ((JSpinner)c).getEditor();
if( editor instanceof JSpinner.DefaultEditor ) {
JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField();
if( textField != null )
return FlatUIUtils.isPermanentFocusOwner( textField );
}
return false;
} else
return FlatUIUtils.isPermanentFocusOwner( c );
}
public void paint( BackgroundComponent background, PaintableComponent paintable, Graphics g ){
if( SwingUtilities.isDescendingFrom( background.getComponent(), content )){
/* If we are painting an non-transparent component we paint our custom background image, otherwise
* we just let it shine through */
if( paintable.getTransparency() == Transparency.SOLID ){
Point point = new Point( 0, 0 );
point = SwingUtilities.convertPoint( paintable.getComponent(), point, content );
BufferedImage image = getImage();
if( image != null ){
int w = paintable.getComponent().getWidth();
int h = paintable.getComponent().getHeight();
g.drawImage( image, 0, 0, w, h, point.x, point.y, point.x + w, point.y + h, null );
}
}
/* and now we paint the original content of the component */
Graphics2D g2 = (Graphics2D)g.create();
//g2.setComposite( alpha );
paintable.paintBackground( g2 );
paintable.paintForeground( g );
paintable.paintBorder( g2 );
g2.dispose();
paintable.paintChildren( g );
}
}
/** Overrides superclass method to set default button. */
@Override
public void addNotify() {
super.addNotify();
if (withButtons) {
if (SwingUtilities.isDescendingFrom(replaceButton, this)) {
getRootPane().setDefaultButton(replaceButton);
}
}
}
public boolean isFocused() {
ResultViewPanel rvp = getCurrentResultViewPanel();
if (rvp != null) {
Component owner = FocusManager.getCurrentManager().getFocusOwner();
return owner != null && SwingUtilities.isDescendingFrom(owner, rvp);
} else {
return false;
}
}
/** Transfer the focus to the editor pane.
*/
@Deprecated
@Override
public void requestFocus() {
super.requestFocus();
if (pane != null) {
if ((customComponent != null) && !SwingUtilities.isDescendingFrom(pane, customComponent)) {
customComponent.requestFocus();
} else {
pane.requestFocus();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() != null) {
// absolute parent => tabs
// if any of the data entry forms are selected, unselect the interval table
if (SwingUtilities.isDescendingFrom((java.awt.Component) evt.getNewValue(), absolute.getParent())) {
intervals.getTable().clearSelection();
systemTimes.getTable().clearSelection();
}
else if (intervals.getTable() == evt.getNewValue()) {
systemTimes.getTable().clearSelection();
}
}
}
private boolean switchCurrentEditor() {
final TopComponent tc = TopComponent.getRegistry().getActivated();
if( null == tc || !TopComponentTracker.getDefault().isEditorTopComponent( tc ) )
return false;
final WindowManagerImpl wmi = WindowManagerImpl.getInstance();
final JFrame mainWnd = ( JFrame ) wmi.getMainWindow();
if( SwingUtilities.isDescendingFrom( tc, mainWnd.getContentPane() ) )
return true;
JPanel panel = new JPanel( new BorderLayout() );
panel.add( tc, BorderLayout.CENTER );
try {
mainWnd.setContentPane( panel );
} catch( IndexOutOfBoundsException e ) {
Logger.getLogger(EditorOnlyDisplayer.class.getName()).log(Level.INFO, "Error while switching current editor.", e);
//#245541 - something is broken in the component hierarchy, let's try restoring to the default mode
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
cancel(false);
}
});
}
mainWnd.invalidate();
mainWnd.revalidate();
mainWnd.repaint();
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
tc.requestFocusInWindow();
}
});
return true;
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
for (JComponent dc : logComponents) {
if (SwingUtilities.isDescendingFrom(dc, c)) {
String boundsMsg = ViewUtils.toString(new Rectangle(x, y, w, h));
ViewHierarchyImpl.REPAINT_LOG.log(Level.FINER,
"Component-REPAINT: " + boundsMsg + // NOI18N
" c:" + ViewUtils.toString(c), // NOI18N
new Exception("Component-Repaint of " + boundsMsg + " cause:")); // NOI18N
break;
}
}
super.addDirtyRegion(c, x, y, w, h);
}
public void notifyActiveTopComponentChanged(Component activeTopComponent) {
if (activeTopComponent != null) {
JTextComponent activeTextComponent = activeTextComponentRef.get();
if (activeTextComponent != null) {
if (!SwingUtilities.isDescendingFrom(activeTextComponent, activeTopComponent)) {
// A top component was focused that does not contain focused text component
// so notify that there's in fact no active text component
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("EditorRegistryWatcher: TopComponent without active JTextComponent\n");
}
updateActiveActionInPresenters(null);
}
}
}
}
public void actionPerformed (ActionEvent evt) {
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
// move focus away only from navigator AWT children,
// but not combo box to preserve its ESC functionality
if (lastActivatedRef == null ||
focusOwner == null ||
!SwingUtilities.isDescendingFrom(focusOwner, navigatorTC.getTopComponent()) ||
focusOwner instanceof JComboBox) {
return;
}
TopComponent prevFocusedTc = lastActivatedRef.get();
if (prevFocusedTc != null) {
prevFocusedTc.requestActive();
}
}
static void refreshStatusLine() {
LOG.fine("StatusLineFactories.refreshStatusLine()\n");
List<? extends JTextComponent> componentList = EditorRegistry.componentList();
for (JTextComponent component : componentList) {
boolean underMainWindow = (SwingUtilities.isDescendingFrom(component,
WindowManager.getDefault().getMainWindow()));
EditorUI editorUI = Utilities.getEditorUI(component);
if (LOG.isLoggable(Level.FINE)) {
String componentDesc = component.toString();
Document doc = component.getDocument();
Object streamDesc;
if (doc != null && ((streamDesc = doc.getProperty(Document.StreamDescriptionProperty)) != null)) {
componentDesc = streamDesc.toString();
}
LOG.fine(" underMainWindow=" + underMainWindow + // NOI18N
", text-component: " + componentDesc + "\n");
}
if (editorUI != null) {
StatusBar statusBar = editorUI.getStatusBar();
statusBar.setVisible(!underMainWindow);
boolean shouldUpdateGlobal = underMainWindow && component.isShowing();
if (shouldUpdateGlobal) {
statusBar.updateGlobal();
LOG.fine(" end of refreshStatusLine() - found main window component\n\n"); // NOI18N
return; // First non-docked one found and updated -> quit
}
}
}
clearStatusLine();
LOG.fine(" end of refreshStatusLine() - no components - status line cleared\n\n"); // NOI18N
}
public void focusLost(FocusEvent e) {
boolean isFocusableComponent = focusableComponents.contains(e
.getSource());
boolean isNonEmptyOpposite = e.getOppositeComponent() != null;
if (isFocusableComponent
&& isNonEmptyOpposite
&& !SwingUtilities.isDescendingFrom(e
.getOppositeComponent(), peer)) {
peer.commitOrRevert();
}
}
public static boolean isInsideActiveWindow(Component comp) {
Window activeWindow = KeyboardFocusManager
.getCurrentKeyboardFocusManager().getActiveWindow();
if (activeWindow == null)
return false;
return activeWindow == comp
|| SwingUtilities.isDescendingFrom(comp, activeWindow);
}
/** */
public void activateModeTopComponent(ModeImpl mode, TopComponent tc) {
if(!getModeOpenedTopComponents(mode).contains(tc)) {
return;
}
ModeImpl oldActiveMode = getActiveMode();
//#45650 -some API users call the activation all over again all the time on one item.
// improve performance for such cases.
if (oldActiveMode != null && oldActiveMode.equals(mode)) {
if (tc != null && tc.equals(model.getModeSelectedTopComponent(mode))) {
// #82385, #139319 do repeat activation if focus is not
// owned by tc to be activated
Component fOwn = KeyboardFocusManager.getCurrentKeyboardFocusManager().
getFocusOwner();
if (fOwn != null && SwingUtilities.isDescendingFrom(fOwn, tc)) {
//#70173 - activation request came probably from a sliding
//window in 'hover' mode, so let's hide it
slideOutSlidingWindows( mode );
return;
}
}
}
model.setActiveMode(mode);
model.setModeSelectedTopComponent(mode, tc);
if(isVisible()) {
viewRequestor.scheduleRequest(new ViewRequest(mode,
View.CHANGE_TOPCOMPONENT_ACTIVATED, null, tc));
//restore floating windows if iconified
if( mode.getState() == Constants.MODE_STATE_SEPARATED ) {
Frame frame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, tc);
if( null != frame && frame != WindowManagerImpl.getInstance().getMainWindow()
&& (frame.getExtendedState() & Frame.ICONIFIED) > 0 ) {
frame.setExtendedState(frame.getExtendedState() - Frame.ICONIFIED );
}
}
}
// Notify registry.
WindowManagerImpl.notifyRegistryTopComponentActivated(tc);
if(oldActiveMode != mode) {
WindowManagerImpl.getInstance().doFirePropertyChange(
WindowManagerImpl.PROP_ACTIVE_MODE, oldActiveMode, mode);
}
}
public void layoutContainer(Container parent) {
int selectedIndex = tabPane.getSelectedIndex();
Component visibleComponent = getVisibleComponent();
if (selectedIndex < 0) {
if (visibleComponent != null) {
// The last tab was removed, so remove the component
setVisibleComponent(null);
}
} else {
Component selectedComponent = tabPane.getComponentAt(selectedIndex);
boolean shouldChangeFocus = false;
// In order to allow programs to use a single component
// as the display for multiple tabs, we will not change
// the visible compnent if the currently selected tab
// has a null component. This is a bit dicey, as we don't
// explicitly state we support this in the spec, but since
// programs are now depending on this, we're making it work.
//
if (selectedComponent != null) {
if (selectedComponent != visibleComponent && visibleComponent != null) {
// get the current focus owner
Component currentFocusOwner = KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
// check if currentFocusOwner is a descendant of visibleComponent
if (currentFocusOwner != null
&& SwingUtilities.isDescendingFrom(currentFocusOwner,
visibleComponent)) {
shouldChangeFocus = true;
}
}
setVisibleComponent(selectedComponent);
// make sure only the selected component is visible
Component[] components = parent.getComponents();
for (int i = 0; i < components.length; i++) {
if (!(components[i] instanceof UIResource)
&& components[i].isVisible()
&& components[i] != selectedComponent) {
components[i].setVisible(false);
setConstraint(components[i], "*");
}
}
if (BasicOutlookBarUI.this.nextVisibleComponent != null) {
BasicOutlookBarUI.this.nextVisibleComponent.setVisible(true);
}
}
super.layoutContainer(parent);
if (shouldChangeFocus) {
if (!requestFocusForVisibleComponent0()) {
tabPane.requestFocus();
}
}
}
}