private boolean sizeChanged() {
    if ((oldComponentInnards == null) || (componentInnards == null)) {
      return true;
    }

    oldComponentInnards.setRect(componentInnards);
    componentInnards = SwingUtilities.calculateInnerArea(progressBar, componentInnards);
    return !oldComponentInnards.equals(componentInnards);
  }
  public void updateLocation(JComponent container) {
    final Rectangle rec = container.getVisibleRect();

    final Dimension iconSize = getPreferredSize();

    final Rectangle newBounds =
        new Rectangle(rec.x + rec.width - iconSize.width, rec.y, iconSize.width, iconSize.height);
    if (!newBounds.equals(getBounds())) {
      setBounds(newBounds);
      container.repaint();
    }
  }
  public void paint(Graphics g) {
    // repaint the whole transformer in case the view component was repainted
    Rectangle clipBounds = g.getClipBounds();
    if (clipBounds != null && !clipBounds.equals(visibleRect)) {
      repaint();
    }
    // clear the background
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());

    if (view != null && at.getDeterminant() != 0) {
      Graphics2D g2 = (Graphics2D) g.create();
      Insets insets = getInsets();
      Rectangle bounds = getBounds();

      // don't forget about insets
      bounds.x += insets.left;
      bounds.y += insets.top;
      bounds.width -= insets.left + insets.right;
      bounds.height -= insets.top + insets.bottom;
      double centerX1 = bounds.getCenterX();
      double centerY1 = bounds.getCenterY();

      Rectangle tb = getTransformedSize();
      double centerX2 = tb.getCenterX();
      double centerY2 = tb.getCenterY();

      // set antialiasing by default
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      if (renderingHints != null) {
        g2.addRenderingHints(renderingHints);
      }
      // translate it to the center of the view component again
      double tx = centerX1 - centerX2 - getX();
      double ty = centerY1 - centerY2 - getY();
      g2.translate((int) tx, (int) ty);
      g2.transform(at);
      view.paint(g2);
      g2.dispose();
    }
    // paint the border
    paintBorder(g);
  }
    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 void show(Component owner, int aScreenX, int aScreenY, final boolean considerForcedXY) {
    if (ApplicationManagerEx.getApplicationEx() != null
        && ApplicationManager.getApplication().isHeadlessEnvironment()) return;
    if (isDisposed()) {
      throw new IllegalStateException(
          "Popup was already disposed. Recreate a new instance to show again");
    }

    assert ApplicationManager.getApplication().isDispatchThread();

    addActivity();

    final boolean shouldShow = beforeShow();
    if (!shouldShow) {
      removeActivity();
      return;
    }

    prepareToShow();

    if (myInStack) {
      myFocusTrackback = new FocusTrackback(this, owner, true);
      myFocusTrackback.setMustBeShown(true);
    }

    Dimension sizeToSet = null;

    if (myDimensionServiceKey != null) {
      sizeToSet = DimensionService.getInstance().getSize(myDimensionServiceKey, myProject);
    }

    if (myForcedSize != null) {
      sizeToSet = myForcedSize;
    }

    if (myMinSize == null) {
      myMinSize = myContent.getMinimumSize();
    }

    if (sizeToSet == null) {
      sizeToSet = myContent.getPreferredSize();
    }

    if (sizeToSet != null) {
      sizeToSet.width = Math.max(sizeToSet.width, myMinSize.width);
      sizeToSet.height = Math.max(sizeToSet.height, myMinSize.height);

      myContent.setSize(sizeToSet);
      myContent.setPreferredSize(sizeToSet);
    }

    Point xy = new Point(aScreenX, aScreenY);
    boolean adjustXY = true;
    if (myDimensionServiceKey != null) {
      final Point storedLocation =
          DimensionService.getInstance().getLocation(myDimensionServiceKey, myProject);
      if (storedLocation != null) {
        xy = storedLocation;
        adjustXY = false;
      }
    }

    if (adjustXY) {
      final Insets insets = myContent.getInsets();
      if (insets != null) {
        xy.x -= insets.left;
        xy.y -= insets.top;
      }
    }

    if (considerForcedXY && myForcedLocation != null) {
      xy = myForcedLocation;
    }

    if (myLocateByContent) {
      final Dimension captionSize = myHeaderPanel.getPreferredSize();
      xy.y -= captionSize.height;
    }

    Rectangle targetBounds = new Rectangle(xy, myContent.getPreferredSize());
    Insets insets = myPopupBorder.getBorderInsets(myContent);
    if (insets != null) {
      targetBounds.x += insets.left;
      targetBounds.y += insets.top;
    }

    Rectangle original = new Rectangle(targetBounds);
    if (myLocateWithinScreen) {
      ScreenUtil.moveRectangleToFitTheScreen(targetBounds);
    }

    if (myMouseOutCanceller != null) {
      myMouseOutCanceller.myEverEntered = targetBounds.equals(original);
    }

    myOwner = IdeFrameImpl.findNearestModalComponent(owner);
    if (myOwner == null) {
      myOwner = owner;
    }

    myRequestorComponent = owner;

    boolean forcedDialog = (SystemInfo.isMac && !(myOwner instanceof IdeFrame)) || myMayBeParent;

    PopupComponent.Factory factory = getFactory(myForcedHeavyweight || myResizable, forcedDialog);
    myNativePopup = factory.isNativePopup();
    myPopup = factory.getPopup(myOwner, myContent, targetBounds.x, targetBounds.y);

    if (myResizable) {
      final JRootPane root = myContent.getRootPane();
      final IdeGlassPaneImpl glass = new IdeGlassPaneImpl(root);
      root.setGlassPane(glass);

      final ResizeComponentListener resizeListener = new ResizeComponentListener(this, glass);
      glass.addMousePreprocessor(resizeListener, this);
      glass.addMouseMotionPreprocessor(resizeListener, this);
    }

    if (myCaption != null && myMovable) {
      final MoveComponentListener moveListener =
          new MoveComponentListener(myCaption) {
            public void mousePressed(final MouseEvent e) {
              super.mousePressed(e);
              if (e.isConsumed()) return;

              if (UIUtil.isCloseClick(e)) {
                if (myCaption.isWithinPanel(e)) {
                  cancel();
                }
              }
            }
          };
      ListenerUtil.addMouseListener(myCaption, moveListener);
      ListenerUtil.addMouseMotionListener(myCaption, moveListener);
      final MyContentPanel saved = myContent;
      Disposer.register(
          this,
          new Disposable() {
            public void dispose() {
              ListenerUtil.removeMouseListener(saved, moveListener);
              ListenerUtil.removeMouseMotionListener(saved, moveListener);
            }
          });
    }

    for (JBPopupListener listener : myListeners) {
      listener.beforeShown(new LightweightWindowEvent(this));
    }

    myPopup.setRequestFocus(myRequestFocus);
    myPopup.show();

    final Window window = SwingUtilities.getWindowAncestor(myContent);

    myWindowListener = new MyWindowListener();
    window.addWindowListener(myWindowListener);

    if (myFocusable) {
      window.setFocusableWindowState(true);
      window.setFocusable(true);
    }

    myWindow = updateMaskAndAlpha(window);

    if (myWindow instanceof JWindow) {
      ((JWindow) myWindow).getRootPane().putClientProperty(KEY, this);
    }

    if (myWindow != null) {
      // dialogwrapper-based popups do this internally through peer,
      // for other popups like jdialog-based we should exclude them manually, but
      // we still have to be able to use IdeFrame as parent
      if (!myMayBeParent && !(myWindow instanceof Frame)) {
        WindowManager.getInstance().doNotSuggestAsParent(myWindow);
      }
    }

    final Runnable afterShow =
        new Runnable() {
          public void run() {
            if (myPreferredFocusedComponent != null && myInStack && myFocusable) {
              myFocusTrackback.registerFocusComponent(myPreferredFocusedComponent);
            }

            removeActivity();

            afterShow();
          }
        };

    if (myRequestFocus) {
      getFocusManager()
          .requestFocus(
              new FocusCommand() {
                @Override
                public ActionCallback run() {
                  if (isDisposed()) {
                    removeActivity();
                    return new ActionCallback.Done();
                  }

                  _requestFocus();

                  final ActionCallback result = new ActionCallback();

                  final Runnable afterShowRunnable =
                      new Runnable() {
                        @Override
                        public void run() {
                          afterShow.run();
                          result.setDone();
                        }
                      };
                  if (myNativePopup) {
                    final FocusRequestor furtherRequestor = getFocusManager().getFurtherRequestor();
                    SwingUtilities.invokeLater(
                        new Runnable() {
                          @Override
                          public void run() {
                            if (isDisposed()) {
                              result.setRejected();
                              return;
                            }

                            furtherRequestor
                                .requestFocus(
                                    new FocusCommand() {
                                      @Override
                                      public ActionCallback run() {
                                        if (isDisposed()) {
                                          return new ActionCallback.Rejected();
                                        }

                                        _requestFocus();

                                        afterShowRunnable.run();

                                        return new ActionCallback.Done();
                                      }
                                    },
                                    true)
                                .notify(result)
                                .doWhenProcessed(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        removeActivity();
                                      }
                                    });
                          }
                        });
                  } else {
                    afterShowRunnable.run();
                  }

                  return result;
                }
              },
              true)
          .doWhenRejected(
              new Runnable() {
                @Override
                public void run() {
                  afterShow.run();
                }
              });
    } else {
      SwingUtilities.invokeLater(
          new Runnable() {
            @Override
            public void run() {
              if (isDisposed()) {
                removeActivity();
                return;
              }

              afterShow.run();
            }
          });
    }
  }
Beispiel #6
0
  public static void main(String args[]) throws Exception {

    try {
      UIManager.setLookAndFeel(new NimbusLookAndFeel());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    SwingUtilities.invokeAndWait(
        () -> {
          JPanel panel0 = new JPanel();
          JPanel panel2 = new JPanel();
          BadPane badPane = new BadPane();
          badPane.add("zero", panel0);
          badPane.add("one", null); // no component
          badPane.add("", panel2); // no title
          badPane.add("", null); // no component, no title
          // but give it that via a tabComponent
          JPanel tabComponent = new JPanel();
          JLabel tabComponentLabel = new JLabel("three");
          tabComponent.add(tabComponentLabel);
          badPane.setTabComponentAt(3, tabComponent);
          JFrame frame = new JFrame();
          frame.add(badPane);
          frame.setSize(300, 300);
          frame.setVisible(true);

          try {
            AccessibleContext ac = badPane.getAccessibleContext();
            Accessible page0 = ac.getAccessibleChild(0);
            if (page0 == null) {
              // Not something being tested, but checking anyway
              throw new RuntimeException("getAccessibleChild(0) is null");
            }
            Accessible page1 = ac.getAccessibleChild(1);
            if (page1 == null) {
              // Not something being tested, but checking anyway
              throw new RuntimeException("getAccessibleChild(1) is null");
            }
            Accessible page2 = ac.getAccessibleChild(2);
            Accessible page3 = ac.getAccessibleChild(3);
            // page0 - page3 are JTabbedPane.Page, a private inner class
            // and is an AccessibleContext
            // and implements Accessible and AccessibleComponent
            AccessibleContext pac0 = page0.getAccessibleContext();
            AccessibleContext pac1 = page1.getAccessibleContext();
            AccessibleContext pac2 = page2.getAccessibleContext();
            AccessibleContext pac3 = page3.getAccessibleContext();

            // test Page.getBounds
            // ensure no IndexOutOfBoundsException
            Rectangle r0 = pac0.getAccessibleComponent().getBounds();
            // make sure second Bounds is different than first
            Rectangle r1 = pac1.getAccessibleComponent().getBounds();
            if (r1.equals(r0)) {
              String msg = "Second tab should not have same bounds as first tab";
              throw new RuntimeException(msg);
            }

            // test Page.getAccessibleStateSet
            // At this point page 0 is selected
            AccessibleStateSet accSS0 = pac0.getAccessibleStateSet();
            if (!accSS0.contains(AccessibleState.SELECTED)) {
              String msg = "Empty title -> AccessibleState.SELECTED not set";
              throw new RuntimeException(msg);
            }
            // select second tab
            badPane.setSelectedIndex(1);
            AccessibleStateSet accSS1 = pac1.getAccessibleStateSet();
            if (!accSS1.contains(AccessibleState.SELECTED)) {
              String msg = "Second tab selected but AccessibleState.SELECTED not set";
              throw new RuntimeException(msg);
            }
            // select third tab
            badPane.setSelectedIndex(2);
            AccessibleStateSet accSS2 = pac2.getAccessibleStateSet();
            if (!accSS1.contains(AccessibleState.SELECTED)) {
              String msg = "Third tab selected but AccessibleState.SELECTED not set";
              throw new RuntimeException(msg);
            }
            // select fourth tab
            badPane.setSelectedIndex(3);
            AccessibleStateSet accSS3 = pac3.getAccessibleStateSet();
            if (!accSS1.contains(AccessibleState.SELECTED)) {
              String msg = "Fourth tab selected but AccessibleState.SELECTED not set";
              throw new RuntimeException(msg);
            }

            // test Page.getAccessibleIndexInParent
            if (pac0.getAccessibleIndexInParent() == -1) {
              String msg = "Empty title -> negative AccessibleIndexInParent";
              throw new RuntimeException(msg);
            }
            if (pac0.getAccessibleIndexInParent() != 0) {
              String msg = "first tab is not at index 0 in parent";
              throw new RuntimeException(msg);
            }
            if (pac1.getAccessibleIndexInParent() != 1) {
              String msg = "second tab (null component) is not at index 1 in parent";
              throw new RuntimeException(msg);
            }
            if (pac2.getAccessibleIndexInParent() != 2) {
              String msg = "third tab (empty title) string is not at index 2 in parent";
              throw new RuntimeException(msg);
            }
            if (pac3.getAccessibleIndexInParent() != 3) {
              String msg =
                  "fourth tab (empty title, null component, has tabComponent) string is not at index 3 in parent";
              throw new RuntimeException(msg);
            }

            // test Page.getAccessibleName
            String accName = pac0.getAccessibleName();
            if (!accName.equals("zero")) {
              String msg = "Empty title -> empty AccessibleName";
              throw new RuntimeException(msg);
            }
            // test Page.getAccessibleName when component is null
            accName = pac1.getAccessibleName();
            if (!accName.equals("one")) {
              String msg = "AccessibleName of null panel not 'one'";
              throw new RuntimeException(msg);
            }

            // test Page.setDisplayedMnemonicIndex
            //  Empty title -> IllegalArgumnetException
            badPane.setDisplayedMnemonicIndexAt(0, 1);

            // test Page.updateDisplayedMnemonicIndex
            badPane.setMnemonicAt(0, KeyEvent.VK_Z);
            if (badPane.getDisplayedMnemonicIndexAt(0) == -1) {
              String msg = "Empty title -> getDisplayedMnemonicIndexAt failure";
              throw new RuntimeException(msg);
            }
          } catch (Exception e) {
            exception = e;
          }
        });
    if (exception != null) {
      System.out.println("Test failed: " + exception.getMessage());
      throw exception;
    } else {
      System.out.println("Test passed.");
    }
  }