Ejemplo n.º 1
0
  /**
   * Invoked after the tree has drastically changed structure from a given node down. If the path
   * returned by e.getPath() is of length one and the first element does not identify the current
   * root node the first element should become the new root of the tree.
   *
   * <p>e.path() holds the path to the node.
   *
   * <p>e.childIndices() returns null.
   */
  public void treeStructureChanged(TreeModelEvent e) {
    if (e != null) {
      TreePath changedPath = SwingUtilities2.getTreePath(e, getModel());
      FHTreeStateNode changedNode = getNodeForPath(changedPath, false, false);

      // Check if root has changed, either to a null root, or
      // to an entirely new root.
      if (changedNode == root
          || (changedNode == null
              && ((changedPath == null && treeModel != null && treeModel.getRoot() == null)
                  || (changedPath != null && changedPath.getPathCount() <= 1)))) {
        rebuild(true);
      } else if (changedNode != null) {
        boolean wasExpanded, wasVisible;
        FHTreeStateNode parent = (FHTreeStateNode) changedNode.getParent();

        wasExpanded = changedNode.isExpanded();
        wasVisible = changedNode.isVisible();

        int index = parent.getIndex(changedNode);
        changedNode.collapse(false);
        parent.remove(index);

        if (wasVisible && wasExpanded) {
          int row = changedNode.getRow();
          parent.resetChildrenRowsFrom(row, index, changedNode.getChildIndex());
          changedNode = getNodeForPath(changedPath, false, true);
          changedNode.expand();
        }
        if (treeSelectionModel != null && wasVisible && wasExpanded)
          treeSelectionModel.resetRowSelection();
        if (wasVisible) this.visibleNodesChanged();
      }
    }
  }
Ejemplo n.º 2
0
 /**
  * Determines the preferred span for this view along an axis.
  *
  * @param axis may be either View.X_AXIS or View.Y_AXIS
  * @return the span the view would like to be rendered into &gt;= 0. Typically the view is told to
  *     render into the span that is returned, although there is no guarantee. The parent may
  *     choose to resize or break the view.
  */
 public float getPreferredSpan(int axis) {
   switch (axis) {
     case View.X_AXIS:
       Segment buff = SegmentCache.getSharedSegment();
       Document doc = getDocument();
       int width;
       try {
         FontMetrics fm = getFontMetrics();
         doc.getText(0, doc.getLength(), buff);
         width = Utilities.getTabbedTextWidth(buff, fm, 0, this, 0);
         if (buff.count > 0) {
           Component c = getContainer();
           firstLineOffset =
               j86.sun.swing.SwingUtilities2.getLeftSideBearing(
                   (c instanceof JComponent) ? (JComponent) c : null, fm, buff.array[buff.offset]);
           firstLineOffset = Math.max(0, -firstLineOffset);
         } else {
           firstLineOffset = 0;
         }
       } catch (BadLocationException bl) {
         width = 0;
       }
       SegmentCache.releaseSharedSegment(buff);
       return width + firstLineOffset;
     default:
       return super.getPreferredSpan(axis);
   }
 }
Ejemplo n.º 3
0
  /**
   * Invoked after a node (or a set of siblings) has changed in some way. The node(s) have not
   * changed locations in the tree or altered their children arrays, but other attributes have
   * changed and may affect presentation. Example: the name of a file has changed, but it is in the
   * same location in the file system.
   *
   * <p>e.path() returns the path the parent of the changed node(s).
   *
   * <p>e.childIndices() returns the index(es) of the changed node(s).
   */
  public void treeNodesChanged(TreeModelEvent e) {
    if (e != null) {
      int changedIndexs[];
      FHTreeStateNode changedParent =
          getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
      int maxCounter;

      changedIndexs = e.getChildIndices();
      /* Only need to update the children if the node has been
      expanded once. */
      // PENDING(scott): make sure childIndexs is sorted!
      if (changedParent != null) {
        if (changedIndexs != null && (maxCounter = changedIndexs.length) > 0) {
          Object parentValue = changedParent.getUserObject();

          for (int counter = 0; counter < maxCounter; counter++) {
            FHTreeStateNode child = changedParent.getChildAtModelIndex(changedIndexs[counter]);

            if (child != null) {
              child.setUserObject(treeModel.getChild(parentValue, changedIndexs[counter]));
            }
          }
          if (changedParent.isVisible() && changedParent.isExpanded()) visibleNodesChanged();
        }
        // Null for root indicates it changed.
        else if (changedParent == root && changedParent.isVisible() && changedParent.isExpanded()) {
          visibleNodesChanged();
        }
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * Invoked after nodes have been removed from the tree. Note that if a subtree is removed from the
   * tree, this method may only be invoked once for the root of the removed subtree, not once for
   * each individual set of siblings removed.
   *
   * <p>e.path() returns the former parent of the deleted nodes.
   *
   * <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending
   * order.
   */
  public void treeNodesRemoved(TreeModelEvent e) {
    if (e != null) {
      int changedIndexs[];
      int maxCounter;
      TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
      FHTreeStateNode changedParentNode = getNodeForPath(parentPath, false, false);

      changedIndexs = e.getChildIndices();
      // PENDING(scott): make sure that changedIndexs are sorted in
      // ascending order.
      if (changedParentNode != null
          && changedIndexs != null
          && (maxCounter = changedIndexs.length) > 0) {
        Object[] children = e.getChildren();
        boolean isVisible = (changedParentNode.isVisible() && changedParentNode.isExpanded());

        for (int counter = maxCounter - 1; counter >= 0; counter--) {
          changedParentNode.removeChildAtModelIndex(changedIndexs[counter], isVisible);
        }
        if (isVisible) {
          if (treeSelectionModel != null) treeSelectionModel.resetRowSelection();
          if (treeModel.getChildCount(changedParentNode.getUserObject()) == 0
              && changedParentNode.isLeaf()) {
            // Node has become a leaf, collapse it.
            changedParentNode.collapse(false);
          }
          visibleNodesChanged();
        } else if (changedParentNode.isVisible()) visibleNodesChanged();
      }
    }
  }
Ejemplo n.º 5
0
  static void drawStringInRect(
      JComponent c,
      Graphics g,
      String aString,
      int x,
      int y,
      int width,
      int height,
      int justification) {
    FontMetrics fontMetrics;
    int drawWidth, startX, startY, delta;

    if (g.getFont() == null) {
      //            throw new InconsistencyException("No font set");
      return;
    }
    fontMetrics = SwingUtilities2.getFontMetrics(c, g);
    if (fontMetrics == null) {
      //            throw new InconsistencyException("No metrics for Font " + font());
      return;
    }

    if (justification == CENTER) {
      drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
      if (drawWidth > width) {
        drawWidth = width;
      }
      startX = x + (width - drawWidth) / 2;
    } else if (justification == RIGHT) {
      drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
      if (drawWidth > width) {
        drawWidth = width;
      }
      startX = x + width - drawWidth;
    } else {
      startX = x;
    }

    delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
    if (delta < 0) {
      delta = 0;
    }

    startY = y + height - delta - fontMetrics.getDescent();

    SwingUtilities2.drawString(c, g, aString, startX, startY);
  }
Ejemplo n.º 6
0
    /**
     * Paints the border for the specified component with the specified position and size.
     *
     * @param c the component for which this border is being painted
     * @param g the paint graphics
     * @param x the x position of the painted border
     * @param y the y position of the painted border
     * @param width the width of the painted border
     * @param height the height of the painted border
     */
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
      if (!(c instanceof JPopupMenu)) {
        return;
      }

      Font origFont = g.getFont();
      Color origColor = g.getColor();
      JPopupMenu popup = (JPopupMenu) c;

      String title = popup.getLabel();
      if (title == null) {
        return;
      }

      g.setFont(font);

      FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
      int fontHeight = fm.getHeight();
      int descent = fm.getDescent();
      int ascent = fm.getAscent();
      Point textLoc = new Point();
      int stringWidth = SwingUtilities2.stringWidth(popup, fm, title);

      textLoc.y = y + ascent + TEXT_SPACING;
      textLoc.x = x + ((width - stringWidth) / 2);

      g.setColor(background);
      g.fillRect(
          textLoc.x - TEXT_SPACING,
          textLoc.y - (fontHeight - descent),
          stringWidth + (2 * TEXT_SPACING),
          fontHeight - descent);
      g.setColor(foreground);
      SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

      MotifGraphicsUtils.drawGroove(
          g, x, textLoc.y + TEXT_SPACING, width, GROOVE_HEIGHT, shadowColor, highlightColor);

      g.setFont(origFont);
      g.setColor(origColor);
    }
Ejemplo n.º 7
0
  /**
   * Invoked after nodes have been inserted into the tree.
   *
   * <p>e.path() returns the parent of the new nodes
   *
   * <p>e.childIndices() returns the indices of the new nodes in ascending order.
   */
  public void treeNodesInserted(TreeModelEvent e) {
    if (e != null) {
      int changedIndexs[];
      FHTreeStateNode changedParent =
          getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
      int maxCounter;

      changedIndexs = e.getChildIndices();
      /* Only need to update the children if the node has been
      expanded once. */
      // PENDING(scott): make sure childIndexs is sorted!
      if (changedParent != null
          && changedIndexs != null
          && (maxCounter = changedIndexs.length) > 0) {
        boolean isVisible = (changedParent.isVisible() && changedParent.isExpanded());

        for (int counter = 0; counter < maxCounter; counter++) {
          changedParent.childInsertedAtModelIndex(changedIndexs[counter], isVisible);
        }
        if (isVisible && treeSelectionModel != null) treeSelectionModel.resetRowSelection();
        if (changedParent.isVisible()) this.visibleNodesChanged();
      }
    }
  }
Ejemplo n.º 8
0
  /**
   * Compute and return the location of the icons origin, the location of origin of the text
   * baseline, and a possibly clipped version of the compound labels string. Locations are computed
   * relative to the viewR rectangle.
   */
  private static String layoutMenuItem(
      JComponent c,
      FontMetrics fm,
      String text,
      FontMetrics fmAccel,
      String acceleratorText,
      Icon icon,
      Icon checkIcon,
      Icon arrowIcon,
      int verticalAlignment,
      int horizontalAlignment,
      int verticalTextPosition,
      int horizontalTextPosition,
      Rectangle viewR,
      Rectangle iconR,
      Rectangle textR,
      Rectangle acceleratorR,
      Rectangle checkIconR,
      Rectangle arrowIconR,
      int textIconGap,
      int menuItemGap) {

    SwingUtilities.layoutCompoundLabel(
        c,
        fm,
        text,
        icon,
        verticalAlignment,
        horizontalAlignment,
        verticalTextPosition,
        horizontalTextPosition,
        viewR,
        iconR,
        textR,
        textIconGap);

    /* Initialize the acceelratorText bounds rectangle textR.  If a null
     * or and empty String was specified we substitute "" here
     * and use 0,0,0,0 for acceleratorTextR.
     */
    if ((acceleratorText == null) || acceleratorText.equals("")) {
      acceleratorR.width = acceleratorR.height = 0;
      acceleratorText = "";
    } else {
      acceleratorR.width = SwingUtilities2.stringWidth(c, fmAccel, acceleratorText);
      acceleratorR.height = fmAccel.getHeight();
    }

    /* Initialize the checkIcon bounds rectangle checkIconR.
     */

    if (checkIcon != null) {
      checkIconR.width = checkIcon.getIconWidth();
      checkIconR.height = checkIcon.getIconHeight();
    } else {
      checkIconR.width = checkIconR.height = 0;
    }

    /* Initialize the arrowIcon bounds rectangle arrowIconR.
     */

    if (arrowIcon != null) {
      arrowIconR.width = arrowIcon.getIconWidth();
      arrowIconR.height = arrowIcon.getIconHeight();
    } else {
      arrowIconR.width = arrowIconR.height = 0;
    }

    Rectangle labelR = iconR.union(textR);
    if (MotifGraphicsUtils.isLeftToRight(c)) {
      textR.x += checkIconR.width + menuItemGap;
      iconR.x += checkIconR.width + menuItemGap;

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + viewR.width - arrowIconR.width - menuItemGap - acceleratorR.width;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x;
      arrowIconR.x = viewR.x + viewR.width - menuItemGap - arrowIconR.width;
    } else {
      textR.x -= (checkIconR.width + menuItemGap);
      iconR.x -= (checkIconR.width + menuItemGap);

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + arrowIconR.width + menuItemGap;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x + viewR.width - checkIconR.width;
      arrowIconR.x = viewR.x + menuItemGap;
    }

    // Align the accelertor text and the check and arrow icons vertically
    // with the center of the label rect.
    acceleratorR.y = labelR.y + (labelR.height / 2) - (acceleratorR.height / 2);
    arrowIconR.y = labelR.y + (labelR.height / 2) - (arrowIconR.height / 2);
    checkIconR.y = labelR.y + (labelR.height / 2) - (checkIconR.height / 2);

    /*
    System.out.println("Layout: v=" +viewR+"  c="+checkIconR+" i="+
    iconR+" t="+textR+" acc="+acceleratorR+" a="+arrowIconR);
    */
    return text;
  }
Ejemplo n.º 9
0
  /**
   * This method is not being used to paint menu item since 6.0 This code left for compatibility
   * only. Do not use or override it, this will not cause any visible effect.
   */
  public static void paintMenuItem(
      Graphics g,
      JComponent c,
      Icon checkIcon,
      Icon arrowIcon,
      Color background,
      Color foreground,
      int defaultTextIconGap) {

    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();

    Dimension size = b.getSize();
    Insets i = c.getInsets();

    Rectangle viewRect = new Rectangle(size);

    viewRect.x += i.left;
    viewRect.y += i.top;
    viewRect.width -= (i.right + viewRect.x);
    viewRect.height -= (i.bottom + viewRect.y);

    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle acceleratorRect = new Rectangle();
    Rectangle checkRect = new Rectangle();
    Rectangle arrowRect = new Rectangle();

    Font holdf = g.getFont();
    Font f = c.getFont();
    g.setFont(f);
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
    FontMetrics fmAccel =
        SwingUtilities2.getFontMetrics(c, g, UIManager.getFont("MenuItem.acceleratorFont"));

    if (c.isOpaque()) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
        g.setColor(background);
      } else {
        g.setColor(c.getBackground());
      }
      g.fillRect(0, 0, size.width, size.height);
    }

    // get Accelerator text
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";
    if (accelerator != null) {
      int modifiers = accelerator.getModifiers();
      if (modifiers > 0) {
        acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
        acceleratorText += "+";
      }
      acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
    }

    // layout the text and icon
    String text =
        layoutMenuItem(
            c,
            fm,
            b.getText(),
            fmAccel,
            acceleratorText,
            b.getIcon(),
            checkIcon,
            arrowIcon,
            b.getVerticalAlignment(),
            b.getHorizontalAlignment(),
            b.getVerticalTextPosition(),
            b.getHorizontalTextPosition(),
            viewRect,
            iconRect,
            textRect,
            acceleratorRect,
            checkRect,
            arrowRect,
            b.getText() == null ? 0 : defaultTextIconGap,
            defaultTextIconGap);

    // Paint the Check
    Color holdc = g.getColor();
    if (checkIcon != null) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground);
      checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
      g.setColor(holdc);
    }

    // Paint the Icon
    if (b.getIcon() != null) {
      Icon icon;
      if (!model.isEnabled()) {
        icon = b.getDisabledIcon();
      } else if (model.isPressed() && model.isArmed()) {
        icon = b.getPressedIcon();
        if (icon == null) {
          // Use default icon
          icon = b.getIcon();
        }
      } else {
        icon = b.getIcon();
      }

      if (icon != null) {
        icon.paintIcon(c, g, iconRect.x, iconRect.y);
      }
    }

    // Draw the Text
    if (text != null && !text.equals("")) {
      // Once BasicHTML becomes public, use BasicHTML.propertyKey
      // instead of the hardcoded string below!
      View v = (View) c.getClientProperty("html");
      if (v != null) {
        v.paint(g, textRect);
      } else {
        int mnemIndex = b.getDisplayedMnemonicIndex();

        if (!model.isEnabled()) {
          // *** paint the text disabled
          g.setColor(b.getBackground().brighter());
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x, textRect.y + fmAccel.getAscent());
          g.setColor(b.getBackground().darker());
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);

        } else {
          // *** paint the text normally
          if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
            g.setColor(foreground);
          } else {
            g.setColor(b.getForeground());
          }
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
        }
      }
    }

    // Draw the Accelerator Text
    if (acceleratorText != null && !acceleratorText.equals("")) {

      // Get the maxAccWidth from the parent to calculate the offset.
      int accOffset = 0;
      Container parent = b.getParent();
      if (parent != null && parent instanceof JComponent) {
        JComponent p = (JComponent) parent;
        Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
        int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width;

        // Calculate the offset, with which the accelerator texts will be drawn with.
        accOffset = maxValue - acceleratorRect.width;
      }

      g.setFont(UIManager.getFont("MenuItem.acceleratorFont"));
      if (!model.isEnabled()) {
        // *** paint the acceleratorText disabled
        g.setColor(b.getBackground().brighter());
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset,
            acceleratorRect.y + fm.getAscent());
        g.setColor(b.getBackground().darker());
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset - 1,
            acceleratorRect.y + fm.getAscent() - 1);
      } else {
        // *** paint the acceleratorText normally
        if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
          g.setColor(foreground);
        } else {
          g.setColor(b.getForeground());
        }
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset,
            acceleratorRect.y + fmAccel.getAscent());
      }
    }

    // Paint the Arrow
    if (arrowIcon != null) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground);
      if (!(b.getParent() instanceof JMenuBar)) arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
    }

    g.setColor(holdc);
    g.setFont(holdf);
  }