/** * Returns a point where the given popup menu should be shown. The * point is calculated by adjusting the X and Y coordinates from the * given mouse event so that the popup menu will not be clipped by * the screen boundaries. * * @param popup the popup menu * @param event the mouse event * @return the point where the popup menu should be shown */ public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) { Component source = (Component) event.getSource(); Point topLeftSource = source.getLocationOnScreen(); Point ptRet = getPopupMenuShowPoint(popup, topLeftSource.x + event.getX(), topLeftSource.y + event.getY()); ptRet.translate(-topLeftSource.x, -topLeftSource.y); return ptRet; }
/** * Centers the window over a component (usually another window). * The window must already have been sized. */ public static void centerWindowOnComponent(Window window, Component over) { if ((over == null) || !over.isShowing()) { centerWindowOnScreen(window); return; } Point parentLocation = over.getLocationOnScreen(); Dimension parentSize = over.getSize(); Dimension size = window.getSize(); // Center it. int x = parentLocation.x + (parentSize.width - size.width) / 2; int y = parentLocation.y + (parentSize.height - size.height) / 2; // Now, make sure it's onscreen Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // This doesn't actually work on the Mac, where the screen // doesn't necessarily start at 0,0 if (x + size.width > screenSize.width) x = screenSize.width - size.width; if (x < 0) x = 0; if (y + size.height > screenSize.height) y = screenSize.height - size.height; if (y < 0) y = 0; window.setLocation(x, y); }
private Point getParentPoint(MagePermanent permanent) { if (parentPoint == null) { Component parentComponent = SwingUtilities.getRoot(permanent); parentPoint = parentComponent.getLocationOnScreen(); } return parentPoint; } }
/** * Prepares data to be sent to action callback on client side. * * @return */ private TransferData getTransferDataForMouseEntered() { data.component = this; data.card = this.gameCard; data.popupText = tooltipText; data.gameId = this.gameId; data.locationOnScreen = data.component.getLocationOnScreen(); // we need this for popup data.popupOffsetX = isTapped() ? cardHeight + cardXOffset + POPUP_X_GAP : cardWidth + cardXOffset + POPUP_X_GAP; data.popupOffsetY = 40; return data; }
private void showPopupMenu(Component callingComponent, String actionCommand) { // Get the location of the point 'on the screen' Point p = callingComponent.getLocationOnScreen(); // Show the JPopupMenu via program // Parameter desc // ---------------- // this - represents current frame // 0,0 is the co ordinate where the popup // is shown JPopupMenu menu; if (actionCommand.endsWith("Yes")) { menu = popupMenuAskYes; } else { menu = popupMenuAskNo; } menu.show(this, 0, 0); // Now set the location of the JPopupMenu // This location is relative to the screen menu.setLocation(p.x, p.y + callingComponent.getHeight()); } }
/** * Position the given component at the center of the given parent component or physical screen. * * @param c the component to be positioned * @param parent the component whose center will match the center of the given component. If * null, the given component will match the screen center. * */ static void position(Component c, Component parent) { Dimension d = c.getPreferredSize(); if (parent == null) { Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); c.setLocation(s.width / 2 - d.width / 2, s.height / 2 - d.height / 2); } else { Point p = parent.getLocationOnScreen(); int pw = parent.getWidth(); int ph = parent.getHeight(); c.setLocation(p.x + pw / 2 - d.width / 2, p.y + ph / 2 - d.height / 2); } }
public void showPopup(final Component popupContainer, final Component infoPane) throws InterruptedException { final Component c = MageFrame.getUI().getComponent(MageComponents.DESKTOP_PANE); SwingUtilities.invokeLater(() -> { if (!popupTextWindowOpen || enlargedWindowState != EnlargedWindowState.CLOSED) { return; } if (data.locationOnScreen == null) { data.locationOnScreen = data.component.getLocationOnScreen(); } Point location = new Point((int) data.locationOnScreen.getX() + data.popupOffsetX - 40, (int) data.locationOnScreen.getY() + data.popupOffsetY - 40); location = GuiDisplayUtil.keepComponentInsideParent(location, parentPoint, infoPane, parentComponent); location.translate(-parentPoint.x, -parentPoint.y); popupContainer.setLocation(location); popupContainer.setVisible(true); c.repaint(); } ); } });
@Override public void mousePressed(MouseEvent e) { popup.applyComponentOrientation(ComponentOrientation.getOrientation(I18n.getLocaleDefault())); if (ComponentOrientation.getOrientation(I18n.getLocaleDefault()) == ComponentOrientation.RIGHT_TO_LEFT) { radioCustomMethod.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 6)); } else { radioCustomMethod.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 0)); } popup.show( e.getComponent(), ComponentOrientation.getOrientation(I18n.getLocaleDefault()) == ComponentOrientation.RIGHT_TO_LEFT ? e.getComponent().getX() - e.getComponent().getWidth() - popup.getWidth() : e.getComponent().getX(), e.getComponent().getY() + e.getComponent().getWidth() ); popup.setLocation( ComponentOrientation.getOrientation(I18n.getLocaleDefault()) == ComponentOrientation.RIGHT_TO_LEFT ? e.getComponent().getLocationOnScreen().x + e.getComponent().getWidth() - popup.getWidth() : e.getComponent().getLocationOnScreen().x, e.getComponent().getLocationOnScreen().y + e.getComponent().getWidth() ); } });
private void setPopupVisibility(final Component container, final boolean show) throws InterruptedException { final Component c = MageFrame.getUI().getComponent(MageComponents.DESKTOP_PANE); SwingUtilities.invokeLater(() -> { tooltipCounter += show ? 1 : -1; if (tooltipCounter < 0) { tooltipCounter = 0; } if (tooltipCounter > 0) { Point location = new Point(getLocationOnScreen().x - container.getWidth(), MouseInfo.getPointerInfo().getLocation().y); Component parentComponent = MageFrame.getInstance(); location = GuiDisplayUtil.keepComponentInsideParent(location, parentComponent.getLocationOnScreen(), container, parentComponent); container.setLocation(location); } container.setVisible(tooltipCounter > 0); c.repaint(); }); }
private void showTooltipPopup(final TransferData data, final Component parentComponent, final Point parentPoint) { if (data.component != null) { tooltipDelay = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_SHOW_TOOLTIPS_DELAY, 300); if (tooltipDelay == 0) { return; } } if (cardInfoPane == null) { PopupFactory factory = PopupFactory.getSharedInstance(); if (data.locationOnScreen == null) { if (data.component == null) { return; } data.locationOnScreen = data.component.getLocationOnScreen(); } data.popupText.updateText(); tooltipPopup = factory.getPopup(data.component, data.popupText, (int) data.locationOnScreen.getX() + data.popupOffsetX, (int) data.locationOnScreen.getY() + data.popupOffsetY + 40); tooltipPopup.show(); // hack to get popup to resize to fit text tooltipPopup.hide(); tooltipPopup = factory.getPopup(data.component, data.popupText, (int) data.locationOnScreen.getX() + data.popupOffsetX, (int) data.locationOnScreen.getY() + data.popupOffsetY + 40); tooltipPopup.show(); } else { sumbitShowPopupTask(data, parentComponent, parentPoint); } }
popup.setText( currentText ); Point p = editorComponent.getLocationOnScreen(); popup.setLocation(p.x, p.y + editorComponent.getSize().height); popup.show();
private void handleOverNewView(TransferData data) { // Prevent to show tooltips from panes not in front MagePane topPane = MageFrame.getTopMost(null); if (topPane instanceof GamePane) { if (!((GamePane) topPane).getGameId().equals(data.gameId)) { return; } } else if (data.gameId != null) { return; } hideTooltipPopup(); cancelTimeout(); Component parentComponent = SwingUtilities.getRoot(data.component); Point parentPoint = parentComponent.getLocationOnScreen(); if (data.locationOnScreen == null) { data.locationOnScreen = data.component.getLocationOnScreen(); } ArrowUtil.drawArrowsForTargets(data, parentPoint); ArrowUtil.drawArrowsForSource(data, parentPoint); ArrowUtil.drawArrowsForPairedCards(data, parentPoint); ArrowUtil.drawArrowsForBandedCards(data, parentPoint); ArrowUtil.drawArrowsForEnchantPlayers(data, parentPoint); tooltipCard = data.card; showTooltipPopup(data, parentComponent, parentPoint); }
public void run() { dialog.setVisible(false); location = frame.getLocationOnScreen(); int x = location.x; int y = location.y;
Component parentComponent = SwingUtilities.getRoot(transferData.component); if (cardPreviewPane != null && parentComponent != null) { Point parentPoint = parentComponent.getLocationOnScreen(); transferData.locationOnScreen = transferData.component.getLocationOnScreen(); Point location = new Point((int) transferData.locationOnScreen.getX() + transferData.popupOffsetX - 40, (int) transferData.locationOnScreen.getY() + transferData.popupOffsetY - 40); location = GuiDisplayUtil.keepComponentInsideParent(location, parentPoint, cardPreviewPane, parentComponent);
public void drawWatermarkImage(Graphics graphics, Component c, int x, int y, int width, int height) { if (!c.isShowing()) return; int dx = c.getLocationOnScreen().x; int dy = c.getLocationOnScreen().y; graphics.drawImage(SubstanceCrosshatchWatermark.watermarkImage, x, y, x + width, y + height, x + dx, y + dy, x + dx + width, y + dy + height, null); }
/** * Gets the location of the wrapped component in the form of a point * specifying the component's top-left corner in the screen's coordinate * space. * * @return An instance of {@code Point} representing the top-left corner of * the component's bounds in the coordinate space of the screen. */ public Point getLocationOnScreen() { return getRootContainer().getLocationOnScreen(); }
/** * Move the mouse to the center of the component * * @param component * the wanted component * @return true if successful */ public boolean mouseMove(Component component) { Point location = component.getLocationOnScreen(); location.translate(component.getWidth() / 2, component.getHeight() / 2); return mouseMove(location); }
@Override public void mouseDragged(final MouseEvent e) { final ViewPanel view = (ViewPanel) getParent(); Component comp = view; while(!((comp = comp.getParent()) instanceof ViewContainer)); final Point a = getLocationOnScreen(), b = comp.getLocationOnScreen(); final Point c = new Point(a.x - b.x + e.getX(), a.y - b.y + e.getY()); ((ViewContainer) comp).dragPanel(view, c); move = true; }
@Override public void componentMoved(ComponentEvent e) { try { if (_actualOwnerLocation == null || _actualOwner == null || !_actualOwner.getLocationOnScreen().equals(_actualOwnerLocation)) { ancestorMoved(); } } catch (Exception ex) { // ignore in case IllegalComponentStateException happens in getLocationOnScreen } } };
public static Point getRelLocation(Component c) { if (c == null || !c.isShowing()) { return new Point(0, 0); } Container parent = getRootContainer(c); if ((parent != null) && parent.isShowing()) { Point p1 = c.getLocationOnScreen(); Point p2 = parent.getLocationOnScreen(); return new Point(p1.x - p2.x, p1.y - p2.y); } return new Point(0, 0); }