下面列出了javax.swing.SwingUtilities#getWindowAncestor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}.
*/
@Override
public void onDone() {
if (busyCnt.decrementAndGet() == 0) {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
statusPanel.onEnd();
stopper.deleteObservers(); // because busyCnt equals zero
ComputerSleepingManager.INSTANCE.keepAwake(false);
progressButton.setEnabled(false);
progressPanel.done() ;
Window progressWindow = SwingUtilities.getWindowAncestor(progressPanel);
if (progressWindow != null)
progressWindow.setVisible(false) ;
}
enableDisable();
}
protected void repackIfNecessary() {
Window window = SwingUtilities.getWindowAncestor(locationPane);
if (window == null)
return;
window.pack();
// TODO: make this a little smarter
/*
* if(isExpanded()==false) { window.pack(); return; }
*
* Dimension realSize = locationPane.getSize(); Dimension preferredSize
* = locationPane.getPreferredSize();
*
* if(realSize.height<preferredSize.height) { int deltaHeight =
* preferredSize.height-realSize.height; Dimension d = window.getSize();
* d.height += deltaHeight; window.setSize(d); }
*/
}
/**
* Called when the component hierarchy for our text component changes.
* When the text component is added to a new {@link Window}, this method
* registers listeners on that <code>Window</code>.
*
* @param e The event.
*/
@Override
public void hierarchyChanged(HierarchyEvent e) {
// NOTE: e many be null as we call this method at other times.
// System.out.println("Hierarchy changed! " + e);
Window oldParentWindow = parentWindow;
parentWindow = SwingUtilities.getWindowAncestor(textComponent);
if (parentWindow != oldParentWindow) {
if (oldParentWindow != null) {
parentWindowListener.removeFrom(oldParentWindow);
}
if (parentWindow != null) {
parentWindowListener.addTo(parentWindow);
}
}
}
public boolean postProcessKeyEvent(KeyEvent ev) {
if(ev.isConsumed()) {
// do not manage consumed event
return false;
}
if (ev.getKeyCode() == KeyEvent.VK_ALT) {
root = SwingUtilities.getRootPane(ev.getComponent());
winAncestor = (root == null ? null :
SwingUtilities.getWindowAncestor(root));
if (ev.getID() == KeyEvent.KEY_PRESSED) {
if (!altKeyPressed) {
altPressed(ev);
}
altKeyPressed = true;
return true;
} else if (ev.getID() == KeyEvent.KEY_RELEASED) {
if (altKeyPressed) {
altReleased(ev);
} else {
MenuSelectionManager msm =
MenuSelectionManager.defaultManager();
MenuElement[] path = msm.getSelectedPath();
if (path.length <= 0) {
WindowsLookAndFeel.setMnemonicHidden(true);
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
}
}
altKeyPressed = false;
}
root = null;
winAncestor = null;
} else {
altKeyPressed = false;
}
return false;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == currentPanel) {
if ("contentLoaded".equals(evt.getPropertyName())) { // NOI18N
Window w = SwingUtilities.getWindowAncestor(this);
if (w != null) {
w.pack();
}
}
}
}
public boolean postProcessKeyEvent(KeyEvent ev) {
if(ev.isConsumed()) {
// do not manage consumed event
return false;
}
if (ev.getKeyCode() == KeyEvent.VK_ALT) {
root = SwingUtilities.getRootPane(ev.getComponent());
winAncestor = (root == null ? null :
SwingUtilities.getWindowAncestor(root));
if (ev.getID() == KeyEvent.KEY_PRESSED) {
if (!altKeyPressed) {
altPressed(ev);
}
altKeyPressed = true;
return true;
} else if (ev.getID() == KeyEvent.KEY_RELEASED) {
if (altKeyPressed) {
altReleased(ev);
} else {
MenuSelectionManager msm =
MenuSelectionManager.defaultManager();
MenuElement[] path = msm.getSelectedPath();
if (path.length <= 0) {
WindowsLookAndFeel.setMnemonicHidden(true);
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
}
}
altKeyPressed = false;
}
root = null;
winAncestor = null;
} else {
altKeyPressed = false;
}
return false;
}
private void onLabelClick(ActionEvent e) {
LayerNode aLN = (LayerNode) _selectedNode;
MapLayer aLayer = aLN.getMapFrame().getMapView().getLayerByHandle(aLN.getLayerHandle());
if (aLayer.getLayerType() == LayerTypes.VectorLayer) {
VectorLayer layer = (VectorLayer) aLayer;
if (layer.getShapeNum() > 0) {
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
FrmLabelSet aFrmLabel = new FrmLabelSet(frame, false, this.getActiveMapFrame().getMapView());
aFrmLabel.setLayer(layer);
aFrmLabel.setLocationRelativeTo(frame);
aFrmLabel.setVisible(true);
}
}
}
public void addAudio() {
Frame frame = (Frame) SwingUtilities.getWindowAncestor(this);
File file = FileDialogUtils.showOpenDialog(frame, "Open Audio", "wav",
"aif");
if (file == null)
return;
audioFileList.add(file);
}
@Override
public void actionPerformed(ActionEvent evt) {
if(searchDialog == null) {
Window parent = SwingUtilities.getWindowAncestor(DocumentEditor.this);
searchDialog =
(parent instanceof Dialog)
? new SearchDialog((Dialog)parent)
: new SearchDialog((Frame)parent);
searchDialog.pack();
searchDialog.setLocationRelativeTo(DocumentEditor.this);
searchDialog.setResizable(true);
MainFrame.getGuiRoots().add(searchDialog);
}
JTextComponent textPane = getTextComponent();
// if the user never gives the focus to the textPane then
// there will never be any selection in it so we force it
textPane.requestFocusInWindow();
// put the selection of the document into the search text field
if(textPane.getSelectedText() != null) {
searchDialog.patternTextField.setText(textPane.getSelectedText());
}
if(searchDialog.isVisible()) {
searchDialog.toFront();
} else {
searchDialog.setVisible(true);
}
searchDialog.patternTextField.selectAll();
searchDialog.patternTextField.requestFocusInWindow();
}
static Window getWindow(ActionEvent e) {
Object source = e.getSource();
Window window = null;
if (source instanceof Component) {
Component component = (Component) source;
window = SwingUtilities.getWindowAncestor(component);
}
return window;
}
@Override
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog) window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
/**
* zSetPopupLocation, This calculates and sets the appropriate location for the popup windows,
* for both the DatePicker and the TimePicker.
*/
static void zSetPopupLocation(CustomPopup popup, int defaultX, int defaultY, JComponent picker,
JComponent verticalFlipReference, int verticalFlipDistance, int bottomOverlapAllowed) {
// Gather some variables that we will need.
Window topWindowOrNull = SwingUtilities.getWindowAncestor(picker);
Rectangle workingArea = InternalUtilities.getScreenWorkingArea(topWindowOrNull);
int popupWidth = popup.getBounds().width;
int popupHeight = popup.getBounds().height;
// Calculate the default rectangle for the popup.
Rectangle popupRectangle = new Rectangle(defaultX, defaultY, popupWidth, popupHeight);
// If the popup rectangle is below the bottom of the working area, then move it upwards by
// the minimum amount which will ensure that it will never cover the picker component.
if (popupRectangle.getMaxY() > (workingArea.getMaxY() + bottomOverlapAllowed)) {
popupRectangle.y = verticalFlipReference.getLocationOnScreen().y - popupHeight
- verticalFlipDistance;
}
// Confine the popup to be within the working area.
if (popupRectangle.getMaxX() > (workingArea.getMaxX())) {
popupRectangle.x -= (popupRectangle.getMaxX() - workingArea.getMaxX());
}
if (popupRectangle.getMaxY() > (workingArea.getMaxY() + bottomOverlapAllowed)) {
popupRectangle.y -= (popupRectangle.getMaxY() - workingArea.getMaxY());
}
if (popupRectangle.x < workingArea.x) {
popupRectangle.x += (workingArea.x - popupRectangle.x);
}
if (popupRectangle.y < workingArea.y) {
popupRectangle.y += (workingArea.y - popupRectangle.y);
}
// Set the location of the popup.
popup.setLocation(popupRectangle.x, popupRectangle.y);
}
/**
* Finds out the monitor where the user currently has the input focus.
* This method is usually used to help the client code to figure out on
* which monitor it should place newly created windows/frames/dialogs.
*
* @return the GraphicsConfiguration of the monitor which currently has the
* input focus
*/
private static GraphicsConfiguration getCurrentGraphicsConfiguration() {
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (focusOwner != null) {
Window w = SwingUtilities.getWindowAncestor(focusOwner);
if (w != null) {
return w.getGraphicsConfiguration();
}
}
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
public void mouseDragged(MouseEvent e) {
check(e);
Window w = SwingUtilities.getWindowAncestor((Component)e.getSource());
if (Cursor.DEFAULT_CURSOR == cursorType) {
// resize only when mouse pointer in resize areas
return;
}
Rectangle newBounds = computeNewBounds(w, getScreenLoc(e));
if (!w.getBounds().equals(newBounds)) {
w.setBounds(newBounds);
}
}
private void repack() {
contents.animateStates(layoutRunnable);
Window w = SwingUtilities.getWindowAncestor(this);
if (w != null) {
// TODO: this feature never really got off the ground, nor was it
// essential.
// if(animateResizing) {
// contents.resizeWindow();
// } else {
w.pack();
// }
}
}
public CPSDialog ( Component parent, String title ) {
super( (JFrame) SwingUtilities.getWindowAncestor(parent), true );
this.parent = parent;
header = new JXTitledPanel();
header.setBorder(BorderFactory.createEmptyBorder());
setTitle( title );
initContentsPanel();
initButtonPanel();
fillButtonPanel();
buildMainPanel();
super.add( jplMain );
}
private void showSymbolSetForm(ColorBreak aCB) {
switch (_legendScheme.getBreakType()) {
case PointBreak:
PointBreak aPB = (PointBreak) aCB;
if (_frmPointSymbolSet == null) {
_frmPointSymbolSet = new FrmPointSymbolSet((JDialog) SwingUtilities.getWindowAncestor(this), false, this);
_frmPointSymbolSet.setLocationRelativeTo(this);
_frmPointSymbolSet.setVisible(true);
}
_frmPointSymbolSet.setPointBreak(aPB);
_frmPointSymbolSet.setVisible(true);
break;
case PolylineBreak:
PolylineBreak aPLB = (PolylineBreak) aCB;
if (_frmPolylineSymbolSet == null) {
_frmPolylineSymbolSet = new FrmPolylineSymbolSet((JDialog) SwingUtilities.getWindowAncestor(this), false, this);
_frmPolylineSymbolSet.setLocationRelativeTo(this);
_frmPolylineSymbolSet.setVisible(true);
}
_frmPolylineSymbolSet.setPolylineBreak(aPLB);
_frmPolylineSymbolSet.setVisible(true);
break;
case PolygonBreak:
PolygonBreak aPGB = (PolygonBreak) aCB;
if (_frmPolygonSymbolSet == null) {
_frmPolygonSymbolSet = new FrmPolygonSymbolSet((JDialog) SwingUtilities.getWindowAncestor(this), false, this);
_frmPolygonSymbolSet.setLocationRelativeTo(this);
_frmPolygonSymbolSet.setVisible(true);
}
_frmPolygonSymbolSet.setPolygonBreak(aPGB);
_frmPolygonSymbolSet.setVisible(true);
break;
case ColorBreak:
if (_frmColorSymbolSet == null) {
_frmColorSymbolSet = new FrmColorSymbolSet((JDialog) SwingUtilities.getWindowAncestor(this), false, this);
_frmColorSymbolSet.setLocationRelativeTo(this);
_frmColorSymbolSet.setVisible(true);
}
_frmColorSymbolSet.setColorBreak(aCB);
_frmColorSymbolSet.setVisible(true);
break;
}
}
private boolean showUsingRootPaneContainer(Point screenLoc,
CalloutType calloutType) {
Window ownerWindow = owner instanceof Window ? (Window) owner
: SwingUtilities.getWindowAncestor(owner);
RootPaneContainer rpc = ownerWindow instanceof RootPaneContainer
? (RootPaneContainer) ownerWindow
: null;
if (rpc == null)
return false;
JLayeredPane layeredPane = rpc.getLayeredPane();
Point layeredPaneLoc = new Point(screenLoc);
SwingUtilities.convertPointFromScreen(layeredPaneLoc, layeredPane);
if (calloutType == null) {
ui.setCalloutType(CalloutType.TOP_CENTER);
ui.setCalloutSize(0);
} else {
ui.setCalloutType(calloutType);
ui.setCalloutSize(CALLOUT_SIZE);
}
contents.validate();
contents.setSize(contents.getPreferredSize());
Rectangle layeredPaneBounds = new Rectangle(layeredPaneLoc.x,
layeredPaneLoc.y, contents.getWidth(), contents.getHeight());
if (calloutType != null) {
Point calloutTip = ui.getCalloutTip(contents);
layeredPaneBounds.x -= calloutTip.x;
layeredPaneBounds.y -= calloutTip.y;
}
if (new Rectangle(0, 0, layeredPane.getWidth(), layeredPane.getHeight())
.contains(layeredPaneBounds)) {
if (contents.getParent() != layeredPane) {
hide();
layeredPane.add(contents, JLayeredPane.POPUP_LAYER);
}
contents.setBounds(layeredPaneBounds);
contents.setVisible(true);
return true;
}
return false;
}
public void testIfLasterWhenSplashShownThanWaitTillItFinished() throws Exception {
class MyObj extends JComponent {
public int called;
public void addNotify() {
called = 1;
LOG.log(Level.INFO, "addNotify called=" + called, new Exception("Stacktrace"));
super.addNotify();
}
}
MyObj obj = new MyObj();
LOG.info("createDescriptor");
NotifyDescriptor ownerDD = createDescriptor(obj);
LOG.info("createDescriptor = " + ownerDD);
LOG.info("notifyLater");
DialogDisplayer.getDefault ().notifyLater(ownerDD);
LOG.info("done notifyLater");
waitAWT();
LOG.info("check");
assertEquals("No notify yet", 0, obj.called);//fail("Ok");
DialogDisplayerImplTest.postInAwtAndWaitOutsideAwt(new Runnable () {
public void run() {
DialogDisplayerImpl.runDelayed();
}
});
waitAWT();
assertEquals("Now it is showing", 1, obj.called);
assertTrue("Is visible", obj.isShowing());
Window root = SwingUtilities.getWindowAncestor(obj);
assertNotNull("There is parent window", root);
assertTrue("It is a dialog", root instanceof JDialog);
JDialog d = (JDialog)root;
assertEquals("The owner of d is the same as owner of dialog without owner", new JDialog().getParent(), d.getParent());
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
DialogDisplayerImpl.runDelayed();
}
});
}
private static boolean isInFocusedWindow(Component c) {
Window w = SwingUtilities.getWindowAncestor(c);
return w != null && w.isFocused();
}