Пример #1
52
 /**
  * Show the given message in a dialog box or independent window, depending on whether the source
  * component is contained in a Frame or not.
  *
  * @param c The Controller that calls this method, or null if it is not called by a Controller.
  *     (The Controller, if any, will be notified when the error message is cleared.)
  * @param message The message to display.
  */
 public void setErrorMessage(Controller c, String message) {
   if (popup != null) clearErrorMessage();
   if (message == null) return;
   errorSource = c;
   errorMessage = message;
   Component parent = source;
   while (parent != null && !(parent instanceof Frame)) parent = parent.getParent();
   if (parent != null) popup = new Dialog((Frame) parent, "Error Message", true); // modal dialog
   else popup = new Frame("Error Message"); // independent window
   popup.setBackground(Color.white);
   popup.add(new MC(message), BorderLayout.CENTER);
   Panel buttonBar = new Panel();
   buttonBar.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
   Button OK = new Button("    OK    ");
   OK.addActionListener(this);
   buttonBar.add(OK);
   popup.add(buttonBar, BorderLayout.SOUTH);
   popup.pack();
   if (parent == null) popup.setLocation(100, 80);
   else popup.setLocation(parent.getLocation().x + 50, parent.getLocation().y + 30);
   popup.addWindowListener(
       new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
           popup.dispose();
         }
       });
   popup.show(); // make the dialog visible.
 }
Пример #2
0
    private void showJPopupMenu(MouseEvent e) {
      try {
        if (e.isPopupTrigger() && menu != null) {
          if (window == null) {

            if (isWindows) {
              window = new JDialog((Frame) null);
              ((JDialog) window).setUndecorated(true);
            } else {
              window = new JWindow((Frame) null);
            }
            window.setAlwaysOnTop(true);
            Dimension size = menu.getPreferredSize();

            Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
            if (e.getY() > centerPoint.getY()) window.setLocation(e.getX(), e.getY() - size.height);
            else window.setLocation(e.getX(), e.getY());

            window.setVisible(true);

            menu.show(((RootPaneContainer) window).getContentPane(), 0, 0);

            // popup works only for focused windows
            window.toFront();
          }
        }
      } catch (Exception ignored) {
      }
    }
 private static void centerWindow(Window w, Component owner) {
   // center based on the owner component, if it is not null
   // otherwise, center based on the center of the screen
   if (owner != null) {
     Point p = owner.getLocation();
     p.x += owner.getWidth() / 2;
     p.y += owner.getHeight() / 2;
     SwingUtilities.convertPointToScreen(p, owner);
     w.setLocation(p);
   } else {
     w.setLocation(WindowUtils.getPointForCentering(w));
   }
 }
Пример #4
0
  /**
   * Tries to load/restore the window state of the given window.
   *
   * @param aNamespace the namespace to use for the window state;
   * @param aProperties the properties to read from;
   * @param aWindow the window to load the state for.
   */
  public static void loadWindowState(final Preferences aProperties, final Window aWindow) {
    // Special case: for FileDialog/JFileChooser we also should restore the
    // properties...
    loadFileDialogState(aProperties, aWindow);

    try {
      final int xPos = aProperties.getInt("winXpos", -1);
      final int yPos = aProperties.getInt("winYpos", -1);
      if ((xPos >= 0) && (yPos >= 0)) {
        aWindow.setLocation(xPos, yPos);
      }
    } catch (NumberFormatException exception) {
      // Ignore...
    }

    if (isNonResizableWindow(aWindow)) {
      // In case the window cannot be resized, don't restore its width &
      // height...
      return;
    }

    try {
      final int width = aProperties.getInt("winWidth", -1);
      final int height = aProperties.getInt("winHeight", -1);
      if ((width >= 0) && (height >= 0)) {
        aWindow.setSize(width, height);
      }
    } catch (NumberFormatException exception) {
      // Ignore...
    }
  }
Пример #5
0
  /**
   * Uninstalls the sheet on the owner. This method is invoked immediately after the JSheet is
   * hidden.
   */
  protected void uninstallSheet() {
    if (isInstalled) {
      Window owner = getOwner();
      if (owner != null) {
        if (isExperimentalSheet()) {
          owner.removeWindowListener(windowEventHandler);
        } else {
          // Note: We mustn't change the windows focusable state
          // because
          // this also affects the focusable state of the JSheet.
          // owner.setFocusableWindowState(true);
          owner.setEnabled(true);
          // ((JFrame) owner).setResizable(true);
          owner.removeComponentListener(ownerMovementHandler);

          if (shiftBackLocation != null) {
            owner.setLocation(shiftBackLocation);
          }
          if (oldFocusOwner != null) {
            owner.toFront();
            oldFocusOwner.requestFocus();
          }
        }
      }
      isInstalled = false;
    }
  }
Пример #6
0
  /**
   * Applies this geometry to a window. Makes sure that the window is not placed outside of the
   * coordinate range of all available screens.
   *
   * @param window the window
   */
  public void applySafe(Window window) {
    Point p = new Point(topLeft);

    Rectangle virtualBounds = new Rectangle();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    for (GraphicsDevice gd : gs) {
      if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
        virtualBounds = virtualBounds.union(gd.getDefaultConfiguration().getBounds());
      }
    }

    if (p.x < virtualBounds.x) {
      p.x = virtualBounds.x;
    } else if (p.x > virtualBounds.x + virtualBounds.width - extent.width) {
      p.x = virtualBounds.x + virtualBounds.width - extent.width;
    }

    if (p.y < virtualBounds.y) {
      p.y = virtualBounds.y;
    } else if (p.y > virtualBounds.y + virtualBounds.height - extent.height) {
      p.y = virtualBounds.y + virtualBounds.height - extent.height;
    }

    window.setLocation(p);
    window.setSize(extent);
  }
Пример #7
0
  public static void centerWindow(Window window) {
    Preconditions.checkArgument(window != null);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = ((int) screenSize.getWidth() - window.getWidth()) >> 1;
    int y = ((int) screenSize.getHeight() - window.getHeight()) >> 1;
    window.setLocation(x, y);
  }
Пример #8
0
 /**
  * This method sets the floating location of the JToolBar.
  *
  * @param x The x coordinate for the floating frame.
  * @param y The y coordinate for the floating frame.
  */
 public void setFloatingLocation(int x, int y) {
   // x,y are the coordinates of the new JFrame created to store the toolbar
   // XXX: The floating location is bogus is not floating.
   floatFrame.setLocation(x, y);
   floatFrame.invalidate();
   floatFrame.validate();
   floatFrame.repaint();
 }
Пример #9
0
  /*
   * Places the Window object at the center of the screen
   */
  public static void centerWindow(Window win) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    Dimension dlgSize = win.getSize();

    win.setLocation(
        (screenSize.width - dlgSize.width) / 2, (screenSize.height - dlgSize.height) / 2);
  }
Пример #10
0
  /**
   * Method description
   *
   * @param window
   */
  public static void centerWindow(Window window) {

    Dimension dimension = window.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenSize.width - dimension.width) / 2;
    int y = (screenSize.height - dimension.height) / 2;
    window.setLocation(x, y);
    window.requestFocus();
  }
  private static void setLocation(
      final RelativePoint p, final PopupComponent popup, Component content) {
    if (popup == null) return;

    final Window wnd = popup.getWindow();
    assert wnd != null;

    wnd.setLocation(p.getScreenPoint());
  }
Пример #12
0
 public static void center(Window window) {
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frameSize = window.getSize();
   frameSize.height =
       ((frameSize.height > screenSize.height) ? screenSize.height : frameSize.height);
   frameSize.width = ((frameSize.width > screenSize.width) ? screenSize.width : frameSize.width);
   window.setLocation(
       (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
 }
Пример #13
0
  /**
   * Keskitä ikkuna toiseen nähden.
   *
   * @param child Keskitettävä ikkuna.
   * @param parent Ikkuna, jonka suhteen keskitetään.
   */
  public static void centerChild(Window child, Window parent) {
    Point parentCorner = parent.getLocation();
    double x = parentCorner.getX();
    double y = parentCorner.getY();
    x += (parent.getWidth() - child.getWidth()) / 2;
    y += (parent.getHeight() - child.getHeight()) / 2;
    Point newLocation = new Point((int) x, (int) y);

    child.setLocation(newLocation);
  }
 public static Window moveTo(
     JComponent content, Point screenPoint, final Dimension headerCorrectionSize) {
   setDefaultCursor(content);
   final Window wnd = SwingUtilities.getWindowAncestor(content);
   if (headerCorrectionSize != null) {
     screenPoint.y -= headerCorrectionSize.height;
   }
   wnd.setLocation(screenPoint);
   return wnd;
 }
Пример #15
0
 public static void setPositionRelativeToParent(
     Window w, Component parent, int hOffset, int vOffset) {
   try {
     Point p = parent.getLocationOnScreen();
     int x = (int) p.getX() + hOffset;
     int y = (int) p.getY() + vOffset;
     w.setLocation(x, y);
     w.pack();
   } catch (Throwable t) {
   }
 }
Пример #16
0
 public static void center(Window window, int deltaX, int deltaY) {
   // Center the window
   boolean bSmall = false;
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frameSize = window.getSize();
   if (frameSize.height > screenSize.height) {
     frameSize.height = screenSize.height;
     bSmall = true;
   }
   if (frameSize.width > screenSize.width) {
     frameSize.width = screenSize.width;
     bSmall = true;
   }
   if (bSmall) {
     window.setLocation((screenSize.width - frameSize.width) / 2, 0);
   } else {
     window.setLocation(
         deltaX + (screenSize.width - frameSize.width) / 2,
         deltaY + (screenSize.height - frameSize.height) / 2);
   }
 }
Пример #17
0
  public static void locateCenter(Window pWin) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();

    int screenHeight = d.height;
    int screenWidth = d.width;

    int curHeight = pWin.getHeight();
    int curWidth = pWin.getWidth();

    pWin.setLocation((screenWidth - curWidth) / 2, (screenHeight - curHeight) / 2);
  }
  private void setSizeAndDimensions(
      @NotNull JTable table,
      @NotNull JBPopup popup,
      @NotNull RelativePoint popupPosition,
      @NotNull List<UsageNode> data) {
    JComponent content = popup.getContent();
    Window window = SwingUtilities.windowForComponent(content);
    Dimension d = window.getSize();

    int width = calcMaxWidth(table);
    width = (int) Math.max(d.getWidth(), width);
    Dimension headerSize = ((AbstractPopup) popup).getHeaderPreferredSize();
    width = Math.max((int) headerSize.getWidth(), width);
    width = Math.max(myWidth, width);

    if (myWidth == -1) myWidth = width;
    int newWidth = Math.max(width, d.width + width - myWidth);

    myWidth = newWidth;

    int rowsToShow = Math.min(30, data.size());
    Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
    Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
    dimension = rectangle.getSize();
    Point location = window.getLocation();
    if (!location.equals(rectangle.getLocation())) {
      window.setLocation(rectangle.getLocation());
    }

    if (!data.isEmpty()) {
      TableScrollingUtil.ensureSelectionExists(table);
    }
    table.setSize(dimension);
    // table.setPreferredSize(dimension);
    // table.setMaximumSize(dimension);
    // table.setPreferredScrollableViewportSize(dimension);

    Dimension footerSize = ((AbstractPopup) popup).getFooterPreferredSize();

    int newHeight =
        (int) (dimension.height + headerSize.getHeight() + footerSize.getHeight())
            + 4 /* invisible borders, margins etc*/;
    Dimension newDim = new Dimension(dimension.width, newHeight);
    window.setSize(newDim);
    window.setMinimumSize(newDim);
    window.setMaximumSize(newDim);

    window.validate();
    window.repaint();
    table.revalidate();
    table.repaint();
  }
 public static Window setSize(JComponent content, final Dimension size) {
   final Window popupWindow = SwingUtilities.windowForComponent(content);
   final Point location = popupWindow.getLocation();
   popupWindow.setLocation(location.x, location.y);
   Insets insets = content.getInsets();
   if (insets != null) {
     size.width += insets.left + insets.right;
     size.height += insets.top + insets.bottom;
   }
   content.setPreferredSize(size);
   popupWindow.pack();
   return popupWindow;
 }
Пример #20
0
    /** Handles mouse dragged event */
    public void mouseDragged(MouseEvent ev) {
      Window w = (Window) ev.getSource();

      if (isMovingWindow) {
        Point windowPt;
        try {
          windowPt = (Point) AccessController.doPrivileged(getLocationAction);
          windowPt.x = windowPt.x - dragOffsetX;
          windowPt.y = windowPt.y - dragOffsetY;
          w.setLocation(windowPt);
          windowMoved = true;
        } catch (PrivilegedActionException e) {
        }
      }
    }
Пример #21
0
  /**
   * Sets the Location of the External Window.
   *
   * @param externalWindow ExternalWindow Object.
   */
  private void setLocationOfExternalWindow(Window externalWindow) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenDimension = tk.getScreenSize();

    //  Get Absolute Location and Bounds, relative to Screen
    Rectangle containerBounds = cytoPanelContainer.getBounds();
    containerBounds.setLocation(cytoPanelContainer.getLocationOnScreen());

    Point p =
        CytoPanelUtil.getLocationOfExternalWindow(
            screenDimension, containerBounds, externalWindow.getSize(), compassDirection, false);

    externalWindow.setLocation(p);
    externalWindow.setVisible(true);
  }
Пример #22
0
  /**
   * Show the window.
   *
   * @param container - Window of JFrame to show
   */
  private void showPopup(Window container) {
    if (visibleComponent.isEnabled()) {
      Point pt = visibleComponent.getLocationOnScreen();
      pt.translate(0, visibleComponent.getHeight());
      container.setLocation(pt);
      container.toFront();

      ApplicationManager.setCurrentlySelectedField(fieldName);
      if (container instanceof OntologySelector) {
        ((OntologySelector) container).makeVisible();
      } else {
        container.setVisible(true);
        container.requestFocusInWindow();
      }
    }
  }
Пример #23
0
 public static void center(Window frame, int left, int right, int top, int bottom) {
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Dimension frameSize = frame.getSize();
   if (frameSize.height > (screenSize.height - top - bottom)) {
     frameSize.height = screenSize.height - top - bottom;
   }
   if (frameSize.width > screenSize.width - left - right) {
     frameSize.width = screenSize.width - left - right;
   }
   if (!frameSize.equals(frame.getSize())) {
     frame.setSize(frameSize);
   }
   frame.setLocation(
       left + (screenSize.width - frameSize.width) / 2,
       top + (screenSize.height - frameSize.height) / 2);
 }
  public static final void centerOnParent(Window _owner, Window client) {
    Dimension pd;
    Point pp;
    Dimension dd = client.getSize();

    if (_owner != null) {
      pd = _owner.getSize();
      pp = _owner.getLocationOnScreen();
    } else {
      Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
      pd = screenSize;
      pp = new Point(0, 0);
    }
    int h = (pd.height - dd.height) / 2;
    int w = (pd.width - dd.width) / 2;

    client.setLocation(pp.x + w, pp.y + h);
  }
Пример #25
0
  public static void centerDialog(final Window dialog) {
    final GraphicsConfiguration config =
        GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice()
            .getDefaultConfiguration();

    final Rectangle screenBounds = config.getBounds();
    final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);

    screenBounds.x = screenBounds.x + insets.left;
    screenBounds.y = screenBounds.y + insets.top;
    screenBounds.width = screenBounds.width - (insets.left + insets.right);
    screenBounds.height = screenBounds.height - (insets.top + insets.bottom);

    // remember that casting doubles to int's always rounds down.
    dialog.setLocation(
        ((screenBounds.width / 2) + screenBounds.x - (dialog.getWidth() / 2)),
        ((screenBounds.height / 2) + screenBounds.y - (dialog.getHeight() / 2)));
  }
Пример #26
0
  /**
   * Set the location of the specified component to match the current value of the attribute. If the
   * value of the attribute has not been set, then do nothing.
   *
   * @param component The component whose location is to be set.
   * @return True if successful.
   */
  public boolean setLocation(Window component) {
    try {
      IntMatrixToken token = (IntMatrixToken) getToken();

      if (token != null) {
        int x = token.getElementAt(0, 0);
        int y = token.getElementAt(0, 1);

        // NOTE: As usual with swing, it's not obvious what the
        // right way to do this is. The following seems to work,
        // found by trial and error.  Even then, the layout
        // manager feels free to override it.
        component.setLocation(x, y);
      }

      return true;
    } catch (Exception ex) {
      return false;
    }
  }
Пример #27
0
  /**
   * Listens to a JDialog to save and restore windows bounds automatically.
   *
   * <p>{@code setVisible(false)} and {@code dispose()} must not be used to close the window.
   * Instead, dispatch a window closing event.
   *
   * <PRE>
   * dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
   * </PRE>
   *
   * and the dialog must be set to dispose on close
   *
   * <PRE>
   * setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
   * </PRE>
   *
   * @param w {@code Window} to listen to
   * @param prefNode String identifier to preference node to save and restore from
   * @param key the key to save and restore from
   */
  private static void addBoundsListener(final Window w, final String prefNode, final String key) {
    String bounds = Preferences.userRoot().node(prefNode).get(key, null);

    if (bounds != null) { // restore to previous size and position

      if (w instanceof JDialog) {
        if (((JDialog) w).isResizable()) {
          w.setBounds(decodeRectangle(bounds));
        } else {
          w.setLocation(decodeRectangle(bounds).getLocation());
        }
      } else {
        w.setBounds(decodeRectangle(bounds));
      }

      Window owner = w.getOwner();

      if (owner != null) {
        w.setLocationRelativeTo(owner);
      }
    }

    /* listen for a window closing event and deal with it */
    w.addWindowListener(
        new WindowAdapter() {

          @Override
          public void windowClosing(WindowEvent evt) {
            // save position and size
            Preferences p = Preferences.userRoot().node(prefNode);

            p.put(key, encodeRectangle(w.getBounds()));
            w.removeWindowListener(this); // make GC easy
          }
        });

    if (w instanceof JDialog) {
      addEscapeListener((JDialog) w);
    }
  }
Пример #28
0
  public static final void centerWindow(Window win) {
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension winDim = win.getSize();

    // System.out.println("[i] Screen: "+screenDim.width+"x"+screenDim.height);
    // System.out.println("[i] WinDim: "+winDim.width+"x"+winDim.height);

    // wenn das Fenster gro���er als der Desktop ist, Fenster auf
    // Desktopgroesse verkleinern
    if (screenDim.width < winDim.width) {
      win.setSize(screenDim.width, winDim.height);
    }
    if (screenDim.height < winDim.height) {
      win.setSize(winDim.width, screenDim.height);
    }
    // Fenster zentrieren
    int x = (screenDim.width - winDim.width) / 2;
    int y = (screenDim.height - winDim.height) / 2;

    System.out.println("[i] setLocation x=" + x + ", y=" + y);
    win.setLocation(x, y);
  }
Пример #29
0
    public void mouseDragged(MouseEvent ev) {
      Window w = (Window) ev.getSource();
      Point pt = ev.getPoint();

      if (isMovingWindow) {
        Point windowPt;
        try {
          windowPt = (Point) AccessController.doPrivileged(getLocationAction);
          windowPt.x = windowPt.x - dragOffsetX;
          windowPt.y = windowPt.y - dragOffsetY;
          w.setLocation(windowPt);
        } catch (PrivilegedActionException e) {
        }
      } else if (dragCursor != 0) {
        Rectangle r = w.getBounds();
        Rectangle startBounds = new Rectangle(r);
        Dimension min = w.getMinimumSize();

        switch (dragCursor) {
          case Cursor.E_RESIZE_CURSOR:
            adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
            break;
          case Cursor.S_RESIZE_CURSOR:
            adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          case Cursor.N_RESIZE_CURSOR:
            adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
            break;
          case Cursor.W_RESIZE_CURSOR:
            adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
            break;
          case Cursor.NE_RESIZE_CURSOR:
            adjust(
                r,
                min,
                0,
                pt.y - dragOffsetY,
                pt.x + (dragWidth - dragOffsetX) - r.width,
                -(pt.y - dragOffsetY));
            break;
          case Cursor.SE_RESIZE_CURSOR:
            adjust(
                r,
                min,
                0,
                0,
                pt.x + (dragWidth - dragOffsetX) - r.width,
                pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          case Cursor.NW_RESIZE_CURSOR:
            adjust(
                r,
                min,
                pt.x - dragOffsetX,
                pt.y - dragOffsetY,
                -(pt.x - dragOffsetX),
                -(pt.y - dragOffsetY));
            break;
          case Cursor.SW_RESIZE_CURSOR:
            adjust(
                r,
                min,
                pt.x - dragOffsetX,
                0,
                -(pt.x - dragOffsetX),
                pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          default:
            break;
        }
        if (!r.equals(startBounds)) {
          w.setBounds(r);
          // Defer repaint/validate on mouseReleased unless dynamic
          // layout is active.
          if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
            w.validate();
            getRootPane().repaint();
          }
        }
      }
    }
 public static void center(Window window) {
   Dimension dw = window.getSize();
   Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
   window.setLocation((ds.width - dw.width) / 2, (ds.height - dw.height) / 2);
 }