Java Code Examples for java.awt.Component#addHierarchyListener()

The following examples show how to use java.awt.Component#addHierarchyListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MouseCapture.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Starts redirecting all mouse events to the specified component or one of its children.
 *
 * @param component The target.
 * @param cursor    The cursor to use while the mouse is captured.
 */
public static void start(Component component, Cursor cursor) {
    JRootPane rootPane = SwingUtilities.getRootPane(component);
    if (rootPane != null) {
        Component    glassPane = rootPane.getGlassPane();
        MouseCapture capture   = new MouseCapture(glassPane, component);
        glassPane.addMouseListener(capture);
        glassPane.addMouseMotionListener(capture);
        glassPane.addHierarchyListener(capture);
        if (cursor != null) {
            glassPane.setCursor(cursor);
        }
        MAP.put(component, capture);
        glassPane.setVisible(true);
    }
}
 
Example 2
Source File: UiUtils.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
public static void makeOwningDialogResizable(@Nonnull final Component component, @Nonnull @MustNotContainNull final Runnable... extraActions) {
  final HierarchyListener listener = new HierarchyListener() {
    @Override
    public void hierarchyChanged(@Nonnull final HierarchyEvent e) {
      final Window window = SwingUtilities.getWindowAncestor(component);
      if (window instanceof Dialog) {
        final Dialog dialog = (Dialog) window;
        if (!dialog.isResizable()) {
          dialog.setResizable(true);
          component.removeHierarchyListener(this);

          for (final Runnable r : extraActions) {
            r.run();
          }
        }
      }
    }
  };
  component.addHierarchyListener(listener);
}
 
Example 3
Source File: DescendantMouseListener.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * This updates <code>parentRollover</code> every time the mouse is over any
 * part of the <code>component's</code> parent.
 * <p>
 * (This updates as the component is added to new parents.)
 * <p>
 * The original use case for this is a button that is added to a row of
 * controls. When the mouse is anywhere over the row of controls: the button
 * shows a slightly indicated icon.
 * 
 * @param component
 *            the component whose parent will be monitored.
 * @param parentRollover
 *            the property to update.
 */
public static void installForParentOf(final Component component,
		final BooleanProperty parentRollover) {
	HierarchyListener h = new HierarchyListener() {
		DescendantMouseListener lastListener = null;

		@Override
		public void hierarchyChanged(HierarchyEvent e) {
			Container parent = component.getParent();
			if (lastListener == null || parent != lastListener.container) {
				if (lastListener != null) {
					lastListener.uninstall();
					lastListener = null;
				}
				if (parent != null) {
					lastListener = new DescendantMouseListener(parent,
							parentRollover);
				}
			}
		}

	};
	component.addHierarchyListener(h);
	h.hierarchyChanged(null);
}
 
Example 4
Source File: VisibilityHandler.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public final void handle(Component component) {
    if (component == null)
        throw new NullPointerException("component cannot be null"); // NOI18N

    if (listener != null && component != null)
        component.removeHierarchyListener(listener);

    this.component = component;
    wasVisible = component.isVisible();

    if (listener == null) listener = createListener();
    component.addHierarchyListener(listener);
}
 
Example 5
Source File: SwingHelper.java    From universal-tween-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a listener to the window parent of the given component. Can be
 * before the component is really added to its hierachy.
 * @param source The source component
 * @param listener The listener to add to the window
 */
public static void addWindowListener(final Component source, final WindowListener listener) {
	if (source instanceof Window) {
		((Window)source).addWindowListener(listener);
	} else {
		source.addHierarchyListener(new HierarchyListener() {
			@Override public void hierarchyChanged(HierarchyEvent e) {
				if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
					SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
				}
			}
		});
	}
}
 
Example 6
Source File: GOSwingEventConverter.java    From settlers-remake with MIT License 5 votes vote down vote up
/**
 * Creates a new event converter, that converts swing events to go events.
 * 
 * @param component
 *            The component.
 * @param provider
 *            THe provider to which to send the events.
 */
public GOSwingEventConverter(Component component, GOEventHandlerProvider provider) {
	super(provider);

	component.setFocusTraversalKeysEnabled(false);

	component.addKeyListener(this);
	component.addMouseListener(this);
	component.addMouseMotionListener(this);
	component.addMouseWheelListener(this);
	component.addHierarchyListener(this);

	addReplaceRule(new EventReplacementRule(ReplacableEvent.DRAW, Replacement.COMMAND_SELECT, MOUSE_TIME_TRSHOLD, MOUSE_MOVE_TRESHOLD));
	addReplaceRule(new EventReplacementRule(ReplacableEvent.PAN, Replacement.COMMAND_ACTION, MOUSE_TIME_TRSHOLD, MOUSE_MOVE_TRESHOLD));
}
 
Example 7
Source File: SwingUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static void addWindowListener(final Component source, final WindowListener listener) {
	if (source instanceof Window) {
		((Window) source).addWindowListener(listener);
	} else {
		source.addHierarchyListener(new HierarchyListener() {
			@Override
			public void hierarchyChanged(HierarchyEvent e) {
				if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
					SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
				}
			}
		});
	}
}
 
Example 8
Source File: FileDropDecorator.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
private void makeDropTarget(final Component c)
{
    final DropTarget dt = new DropTarget();
    try
    {  
    	dt.addDropTargetListener( dropListener );
    }  
    catch(TooManyListenersException e )
    {   
    	 logger.error("Do you have another listener attached ?",e);
    }  
    
    c.addHierarchyListener( ( HierarchyEvent evt )->{   
		                Component parent = c.getParent();
		                if( parent == null )
		                	c.setDropTarget( null );
		                else
		                	new DropTarget(c, dropListener);
		        });
    
    if( c.getParent() != null )
        new DropTarget(c, dropListener);
    
    if( c instanceof Container )
    {   
        Container cont = (Container) c;
        for(Component cp : cont.getComponents())
            makeDropTarget(cp );
    }  
}
 
Example 9
Source File: DescendantMouseListener.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Add all our internal listeners from a component and its descendants.
 */
private void addListeners(DefaultMutableTreeNode componentNode) {
	Component c = (Component) componentNode.getUserObject();
	c.addHierarchyListener(hierarchyListener);
	c.addMouseListener(mouseListener);
	c.addMouseMotionListener(mouseListener);
	for (int a = 0; a < componentNode.getChildCount(); a++) {
		addListeners((DefaultMutableTreeNode) componentNode.getChildAt(a));
	}
	if (c instanceof Container)
		((Container) c).addContainerListener(containerListener);
}
 
Example 10
Source File: JExtendedSplitPane.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setRightComponent(Component newRightComponent) {
    if (rightComponent != null) {
        rightComponent.removeHierarchyListener(rightComponentListener);
        rightComponentListener = null;
    }

    super.setRightComponent(newRightComponent);

    if (getRightComponent() != null) {
        rightComponentListener = new VisibilityListener(newRightComponent);
        newRightComponent.addHierarchyListener(rightComponentListener);
    }

    updateVisibility();
}
 
Example 11
Source File: JExtendedSplitPane.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setLeftComponent(Component newLeftComponent) {
    if (leftComponent != null) {
        leftComponent.removeHierarchyListener(leftComponentListener);
        leftComponentListener = null;
    }

    super.setLeftComponent(newLeftComponent);

    if (getLeftComponent() != null) {
        leftComponentListener = new VisibilityListener(newLeftComponent);
        newLeftComponent.addHierarchyListener(leftComponentListener);
    }

    updateVisibility();
}
 
Example 12
Source File: AsyncInitSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Creates a new instance of AsyncInitComponent
 * @param comp4Init Component to be initialized. Mustn't be showing at this
 * time. IllegalStateException is thrown if component is already showing.
 * @param initJob Instance of initialization job.
 */
public AsyncInitSupport(Component comp4Init, AsyncGUIJob initJob) {
    this.comp4Init = comp4Init;
    this.initJob = initJob;
    if (comp4Init.isShowing()) {
        throw new IllegalStateException("Component already shown, can't be inited: " + comp4Init);
    }

    comp4Init.addHierarchyListener(this);
    LOG.log(Level.FINE, "addHierarchyListener for {0}", comp4Init);
}
 
Example 13
Source File: JExtendedSplitPane.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setRightComponent(Component newRightComponent) {
    if (rightComponent != null) {
        rightComponent.removeHierarchyListener(rightComponentListener);
        rightComponentListener = null;
    }

    super.setRightComponent(newRightComponent);

    if (getRightComponent() != null) {
        rightComponentListener = new VisibilityListener(newRightComponent);
        newRightComponent.addHierarchyListener(rightComponentListener);
    }

    updateVisibility();
}
 
Example 14
Source File: JExtendedSplitPane.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setLeftComponent(Component newLeftComponent) {
    if (leftComponent != null) {
        leftComponent.removeHierarchyListener(leftComponentListener);
        leftComponentListener = null;
    }

    super.setLeftComponent(newLeftComponent);

    if (getLeftComponent() != null) {
        leftComponentListener = new VisibilityListener(newLeftComponent);
        newLeftComponent.addHierarchyListener(leftComponentListener);
    }

    updateVisibility();
}
 
Example 15
Source File: VisibilityHandler.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public final void handle(Component component) {
    if (component == null)
        throw new NullPointerException("component cannot be null"); // NOI18N

    if (listener != null && component != null)
        component.removeHierarchyListener(listener);

    this.component = component;
    wasVisible = component.isVisible();

    if (listener == null) listener = createListener();
    component.addHierarchyListener(listener);
}
 
Example 16
Source File: Splitter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setRightComponent(Component newRightComponent) {
    if (rightComponent != null) {
        rightComponent.removeHierarchyListener(rightComponentListener);
        rightComponentListener = null;
    }

    super.setRightComponent(newRightComponent);

    if (getRightComponent() != null) {
        rightComponentListener = new VisibilityListener(newRightComponent);
        newRightComponent.addHierarchyListener(rightComponentListener);
    }

    updateVisibility();
}
 
Example 17
Source File: Splitter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setLeftComponent(Component newLeftComponent) {
    if (leftComponent != null) {
        leftComponent.removeHierarchyListener(leftComponentListener);
        leftComponentListener = null;
    }

    super.setLeftComponent(newLeftComponent);

    if (getLeftComponent() != null) {
        leftComponentListener = new VisibilityListener(newLeftComponent);
        newLeftComponent.addHierarchyListener(leftComponentListener);
    }

    updateVisibility();
}
 
Example 18
Source File: Splitter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setRightComponent(Component newRightComponent) {
    if (rightComponent != null) {
        rightComponent.removeHierarchyListener(rightComponentListener);
        rightComponentListener = null;
    }

    super.setRightComponent(newRightComponent);

    if (getRightComponent() != null) {
        rightComponentListener = new VisibilityListener(newRightComponent);
        newRightComponent.addHierarchyListener(rightComponentListener);
    }

    updateVisibility();
}
 
Example 19
Source File: Splitter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void setLeftComponent(Component newLeftComponent) {
    if (leftComponent != null) {
        leftComponent.removeHierarchyListener(leftComponentListener);
        leftComponentListener = null;
    }

    super.setLeftComponent(newLeftComponent);

    if (getLeftComponent() != null) {
        leftComponentListener = new VisibilityListener(newLeftComponent);
        newLeftComponent.addHierarchyListener(leftComponentListener);
    }

    updateVisibility();
}
 
Example 20
Source File: VisibilityHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public final void handle(Component component) {
    if (component == null)
        throw new NullPointerException("component cannot be null"); // NOI18N

    if (listener != null && component != null)
        component.removeHierarchyListener(listener);

    this.component = component;
    wasVisible = component.isVisible();

    if (listener == null) listener = createListener();
    component.addHierarchyListener(listener);
}