⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 titledtab.java

📁 修正了jdk1.6中对托盘事件产生异常的bug.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   *
   * @param highlighted true for highlight, otherwise false
   */
  public void setHighlighted(boolean highlighted) {
    super.setHighlighted(highlighted);  
    updateCurrentStatePanel();
  }

  /**
   * <p>
   * Sets if this TitledTab should be enabled or disabled
   * </p>
   * 
   * <p>
   * <strong>Note:</strong> since ITP 1.5.0 this method will change the enabled property 
   * in the {@link TitledTabProperties} for this tab. Enabled/disabled can be controlled by 
   * modifying the property or this method.
   * </p>
   *
   * @param enabled true for enabled, otherwise false
   */
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    updateCurrentStatePanel();
  }

  /**
   * Gets the text for the normal state
   *
   * @return the text or null if no text
   */
  public String getText() {
    return properties.getNormalProperties().getText();
  }

  /**
   * Sets the text for the normal state
   *
   * @param text the text or null for no text
   */
  public void setText(String text) {
    properties.getNormalProperties().setText(text);
  }

  /**
   * Gets the icon for the normal state
   *
   * @return the icon or null if none
   */
  public Icon getIcon() {
    return properties.getNormalProperties().getIcon();
  }

  /**
   * Sets the icon for the normal state
   *
   * @param icon the icon or null for no icon
   */
  public void setIcon(Icon icon) {
    properties.getNormalProperties().setIcon(icon);
  }

  /**
   * Gets the TitledTabProperties
   *
   * @return the TitledTabProperties for this TitledTab
   */
  public TitledTabProperties getProperties() {
    return properties;
  }

  /**
   * Gets the text for the normal state.
   *
   * Same as getText().
   *
   * @return the text or null if no text
   * @see #getText
   * @since ITP 1.1.0
   */
  public String toString() {
    return getText();
  }

  /**
   * Adds a MouseListener to receive mouse events from this TitledTab.
   *
   * @param l the MouseListener
   */
  public synchronized void addMouseListener(MouseListener l) {
    if (mouseListeners == null)
      mouseListeners = new ArrayList(2);
    mouseListeners.add(l);
  }

  /**
   * Removes a MouseListener
   *
   * @param l the MouseListener to remove
   */
  public synchronized void removeMouseListener(MouseListener l) {
    if (mouseListeners != null) {
      mouseListeners.remove(l);

      if (mouseListeners.size() == 0)
        mouseListeners = null;
    }
  }

  /**
   * Gets the mouse listeners
   *
   * @return the mouse listeners
   */
  public synchronized MouseListener[] getMouseListeners() {
    MouseListener[] listeners = new MouseListener[0];

    if (mouseListeners != null) {
      Object[] l = mouseListeners.toArray();
      listeners = new MouseListener[l.length];
      for (int i = 0; i < l.length; i++)
        listeners[i] = (MouseListener) l[i];
    }

    return listeners;
  }

  /**
   * Adds a MouseMotionListener to receive mouse events from this TitledTab.
   *
   * @param l the MouseMotionListener
   */
  public synchronized void addMouseMotionListener(MouseMotionListener l) {
    if (mouseMotionListeners == null)
      mouseMotionListeners = new ArrayList(2);

    mouseMotionListeners.add(l);
  }

  /**
   * Removes a MouseMotionListener
   *
   * @param l the MouseMotionListener to remove
   */
  public synchronized void removeMouseMotionListener(MouseMotionListener l) {
    if (mouseMotionListeners != null) {
      mouseMotionListeners.remove(l);

      if (mouseMotionListeners.size() == 0)
        mouseMotionListeners = null;
    }
  }

  /**
   * Gets the mouse motion listeners
   *
   * @return the mouse motion listeners
   */
  public synchronized MouseMotionListener[] getMouseMotionListeners() {
    MouseMotionListener[] listeners = new MouseMotionListener[0];

    if (mouseMotionListeners != null) {
      Object[] l = mouseMotionListeners.toArray();
      listeners = new MouseMotionListener[l.length];
      for (int i = 0; i < l.length; i++)
        listeners[i] = (MouseMotionListener) l[i];
    }

    return listeners;
  }

  /**
   * Gets the Shape for the current active rendering state.
   *
   * @return the Shape for the active rendering state, null if no special shape
   * @since ITP 1.2.0
   */
  public Shape getShape() {
    Shape shape = currentStatePanel.getShape();

    if (shape == null)
      return null;

    Point p = SwingUtilities.convertPoint(currentStatePanel, 0, 0, this);
    return new TranslatingShape(shape, p.x, p.y);
  }

  protected void setTabbedPanel(TabbedPanel tabbedPanel) {
    if (tabbedPanel == null)
      HoverManager.getInstance().removeHoverable(eventPanel);

    super.setTabbedPanel(tabbedPanel);

    if (tabbedPanel != null)
      HoverManager.getInstance().addHoverable(eventPanel);
  }

  private Insets getBorderInsets(Border border) {
    return border == null ? InsetsUtil.EMPTY_INSETS : border.getBorderInsets(this);
  }

  private void updateBorders() {
    Direction tabAreaOrientation = getTabAreaOrientation();
    int raised = properties.getHighlightedRaised();
    Insets notRaised = InsetsUtil.setInset(InsetsUtil.EMPTY_INSETS, tabAreaOrientation, raised);
    Border normalBorder = new EmptyBorder(notRaised);

    Insets maxInsets = properties.getBorderSizePolicy() == TitledTabBorderSizePolicy.INDIVIDUAL_SIZE ?
                       null :
                       InsetsUtil.max(
                           getBorderInsets(properties.getNormalProperties().getComponentProperties().getBorder()),
                           InsetsUtil.max(getBorderInsets(
                               properties.getHighlightedProperties().getComponentProperties().getBorder()),
                                          getBorderInsets(
                                              properties.getDisabledProperties().getComponentProperties().getBorder())));

    Insets normalInsets = InsetsUtil.rotate(properties.getNormalProperties().getDirection(),
                                            properties.getNormalProperties().getComponentProperties().getInsets());

    Insets disabledInsets = InsetsUtil.rotate(properties.getDisabledProperties().getDirection(),
                                              properties.getDisabledProperties().getComponentProperties().getInsets());

    int edgeInset = Math.min(InsetsUtil.getInset(normalInsets,
                                                 tabAreaOrientation.getOpposite()),
                             InsetsUtil.getInset(disabledInsets,
                                                 tabAreaOrientation.getOpposite()));

    int normalLowered = Math.min(edgeInset, raised);

    Border innerNormalBorder = getInnerBorder(properties.getNormalProperties(),
                                              tabAreaOrientation,
                                              -normalLowered,
                                              maxInsets);
    Border innerHighlightBorder = getInnerBorder(properties.getHighlightedProperties(),
                                                 tabAreaOrientation,
                                                 raised - normalLowered,
                                                 maxInsets);
    Border innerDisabledBorder = getInnerBorder(properties.getDisabledProperties(),
                                                tabAreaOrientation,
                                                -normalLowered,
                                                maxInsets);

    normalStatePanel.setBorders(normalBorder, innerNormalBorder);
    highlightedStatePanel.setBorders(null, innerHighlightBorder);
    disabledStatePanel.setBorders(normalBorder, innerDisabledBorder);
  }

  private void doUpdateTab(Map changes) {
    boolean updateBorders = false;

    if (changes == null) {
      // Init all
      updateBorders = true;

      setFocusableComponent(properties.getFocusable() ? this : null);
      focusBorder.setEnabled(properties.getFocusMarkerEnabled());

      updateHoverListener(properties.getHoverListener());
      layout.setUseSelectedComponentSize(properties.getSizePolicy() == TitledTabSizePolicy.INDIVIDUAL_SIZE);
    }
    else {
      Map m = (Map) changes.get(properties.getMap());
      if (m != null) {
        Set keySet = m.keySet();

        if (keySet.contains(TitledTabProperties.FOCUSABLE)) {
          setFocusableComponent(properties.getFocusable() ? this : null);
        }

        if (keySet.contains(TitledTabProperties.FOCUS_MARKER_ENABLED)) {
          focusBorder.setEnabled(properties.getFocusMarkerEnabled());
          currentStatePanel.getLabel().repaint();
        }

        if (keySet.contains(TitledTabProperties.HOVER_LISTENER)) {
          updateHoverListener((HoverListener) ((ValueChange) m.get(TitledTabProperties.HOVER_LISTENER)).getNewValue());
        }

        if (keySet.contains(TitledTabProperties.SIZE_POLICY)) {
          layout.setUseSelectedComponentSize(
              ((TitledTabSizePolicy) ((ValueChange) m.get(TitledTabProperties.SIZE_POLICY)).getNewValue()) == TitledTabSizePolicy.INDIVIDUAL_SIZE);
        }

        if (keySet.contains(TitledTabProperties.HIGHLIGHTED_RAISED_AMOUNT) || keySet.contains(
            TitledTabProperties.BORDER_SIZE_POLICY)) {
          updateBorders = true;
        }

        if (keySet.contains(TitledTabProperties.ENABLED)) {
          doSetEnabled(properties.getEnabled());
        }
      }
    }

    updateBorders = normalStatePanel.updateState(changes, properties.getNormalProperties()) || updateBorders;
    updateBorders = highlightedStatePanel.updateState(changes, properties.getHighlightedProperties()) || updateBorders;
    updateBorders = disabledStatePanel.updateState(changes, properties.getDisabledProperties()) || updateBorders;

    if (updateBorders)
      updateBorders();
  }

  private void updateHoverListener(HoverListener newHoverListener) {
    HoverListener oldHoverListener = hoverListener;
    hoverListener = newHoverListener;
    if (HoverManager.getInstance().isHovered(eventPanel)) {
      if (oldHoverListener != null)
        oldHoverListener.mouseExited(new HoverEvent(TitledTab.this));
      if (hoverListener != null)
        hoverListener.mouseEntered(new HoverEvent(TitledTab.this));
    }
  }

  private Border getInnerBorder(TitledTabStateProperties properties,
                                Direction tabOrientation,
                                int raised,
                                Insets maxInsets) {
    Direction tabDir = properties.getDirection();
    Insets insets = InsetsUtil.rotate(tabDir, properties.getComponentProperties().getInsets());

    if (maxInsets != null)
      insets = InsetsUtil.add(insets,
                              InsetsUtil.sub(maxInsets,
                                             getBorderInsets(properties.getComponentProperties().getBorder())));

    Border border = properties.getComponentProperties().getBorder();
    Border innerBorder = new EmptyBorder(InsetsUtil.add(insets,
                                                        InsetsUtil.setInset(InsetsUtil.EMPTY_INSETS,
                                                                            tabOrientation.getOpposite(),
                                                                            raised)));
    return border == null ? innerBorder : new CompoundBorder(border, innerBorder);
  }

  private Direction getTabAreaOrientation() {
    return getTabbedPanel() == null ?
           lastTabAreaOrientation : getTabbedPanel().getProperties().getTabAreaOrientation();
  }

  private void updateTabAreaOrientation(Direction newDirection) {
    if (lastTabAreaOrientation != newDirection) {
      lastTabAreaOrientation = newDirection;
      updateBorders();

      normalStatePanel.updateShapedPanel(properties.getNormalProperties());
      highlightedStatePanel.updateShapedPanel(properties.getHighlightedProperties());
      disabledStatePanel.updateShapedPanel(properties.getDisabledProperties());
    }
  }

  private void updateCurrentStatePanel() {
    StatePanel newStatePanel = normalStatePanel;
    if (!isEnabled())
      newStatePanel = disabledStatePanel;
    else if (isHighlighted())
      newStatePanel = highlightedStatePanel;

    eventPanel.setToolTipText(newStatePanel.getToolTipText());

    if (currentStatePanel != newStatePanel) {
      if (currentStatePanel != null)
        currentStatePanel.deactivate();
      currentStatePanel = newStatePanel;
      currentStatePanel.activate();
    }
    layout.showComponent(currentStatePanel);
  }

  private MouseEvent convertMouseEvent(MouseEvent e) {
    Point p = SwingUtilities.convertPoint((JComponent) e.getSource(), e.getPoint(), TitledTab.this);
    return new MouseEvent(TitledTab.this, e.getID(), e.getWhen(), e.getModifiers(),
                          (int) p.getX(), (int) p.getY(), e.getClickCount(),
                          !e.isConsumed() && e.isPopupTrigger(), e.getButton());
  }
  
  private void doSetEnabled(boolean enabled) {
    super.setEnabled(enabled);
    updateCurrentStatePanel();
  }

  public void setUI(PanelUI ui) {
    if (getUI() != UI)
      super.setUI(UI);
  }

  public void updateUI() {
    setUI(UI);
  }

  public void setOpaque(boolean opaque) {
    // Ignore
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -