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

📄 hyperlink.java

📁 采用java SWT图形库的特效代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public boolean isReparentable ()
  {
    checkWidget ();
    return false;
  }


  /**
   * Check the style bits to ensure that no invalid styles are applied.
   */

  private static int checkStyle(int style)
  {
    style = style & SWT.NO_FOCUS;

    // [NOTE] The following transparency workaround was taken from CLabel
    //TEMPORARY CODE
    /*
     * The default background on carbon and some GTK themes is not a solid color 
     * but a texture.  To show the correct default background, we must allow
     * the operating system to draw it and therefore, we can not use the 
     * NO_BACKGROUND style.  The NO_BACKGROUND style is not required on platforms
     * that use double buffering which is true in both of these cases.
     */
    String platform = SWT.getPlatform();
    if ("carbon".equals(platform) || "gtk".equals(platform)) return style;
    return style | SWT.NO_BACKGROUND;
  }


  public Point computeSize(int wHint, int hHint, boolean changed)
  {
    checkWidget();
    Point e = getTotalSize(text);
    if (wHint != SWT.DEFAULT) e.x = wHint;
    if (hHint != SWT.DEFAULT) e.y = hHint;
    return e;
  }


  /**
   * Compute the minimum size.
   */

  private Point getTotalSize(String text)
  {
    Point size = new Point(0, 0);
    GC gc = new GC(this);

    if (text != null && text.length() > 0)
    {
      Point e = gc.textExtent(text, SWT.DRAW_MNEMONIC);
      size.x += e.x;
      size.y = Math.max(size.y, e.y);
    }
    else size.y = Math.max(size.y, gc.getFontMetrics().getHeight());

    gc.dispose();
    return size;
  }


  /**
   * Return the Hyperlink's displayed text.
   * 
   * @return the text of the hyperlink or null
   */

  public String getText()
  {
    return text;
  }


  private void onPaint(PaintEvent event)
  {
    Rectangle rect = cachedClientArea; // getClientArea();
    if (rect.width == 0 || rect.height == 0) return;

    Point extent = getTotalSize(text);

    GC gc = event.gc;

    if ((getStyle() & SWT.NO_BACKGROUND) != 0)
    {
      gc.setBackground(getBackground());
      gc.fillRectangle(rect);
    }

    if(isFocusControl()) gc.drawFocus(rect.x, rect.y, rect.width, rect.height);

    Color textFG, lineFG;
    
    if(cursorInControl)
    {
      textFG = isActive ? activeForeground : hoverForeground;
      lineFG = isActive ? activeUndeline : hoverUnderline;
    }
    else
    {
      textFG = /* isActive ? mouseOverForeground : */ normalForeground;
      lineFG = /* isActive ? mouseOverUnderline : */ normalUnderline;
    }

    if(textFG == null) textFG = normalForeground;
    if(textFG == null) textFG = getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);

    int textHeight = gc.getFontMetrics().getHeight();

    gc.setForeground(textFG);
    gc.drawText(text, rect.x, rect.y + (rect.height - textHeight) / 2, SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC);

    int uy = (rect.y + (rect.height - textHeight) / 2) + gc.getFontMetrics().getAscent() + gc.getFontMetrics().getLeading() + 1;
    int lineWidth = extent.x > rect.width ? rect.width : extent.x;

    if(lineFG != null)
    {
      if(lineFG != textFG) gc.setForeground(lineFG);
      gc.drawLine(rect.x, uy, rect.x + lineWidth, uy);
    }
  }


  public void setForeground(Color color)
  {
    super.setForeground(color);
    this.normalForeground = color;
    redraw();
  }


  public void setHoverForeground(Color color)
  {
    this.hoverForeground = color;
    redraw();
  }


  public void setActiveForeground(Color color)
  {
    this.activeForeground = color;
    redraw();
  }
  
  
  public void setUnderline(Color color)
  {
    this.normalUnderline = color;
    redraw();
  }


  public void setHoverUnderline(Color color)
  {
    this.hoverUnderline = color;
    redraw();
  }


  public void setActiveUnderline(Color color)
  {
    this.activeUndeline = color;
    redraw();
  }


  public Color getHoverForeground()
  {
    return this.hoverForeground;
  }


  public Color getActiveForeground()
  {
    return this.activeForeground;
  }


  public Color getUnderline()
  {
    return this.normalUnderline;
  }


  public Color getHoverUnderline()
  {
    return this.hoverUnderline;
  }


  public Color getActiveUnderline()
  {
    return this.activeUndeline;
  }


  public void setBackground(Color color)
  {
    super.setBackground(color);
    redraw();
  }


  public void setFont(Font font)
  {
    super.setFont(font);
    redraw();
  }


  /**
   * Set the Hyperlink's displayed text.
   * The value <code>null</code> clears it.
   * <p>
   * Mnemonics are indicated by an '&amp' that causes the next
   * character to be the mnemonic.  When the user presses a
   * key sequence that matches the mnemonic, a selection
   * event occurs. On most platforms, the mnemonic appears
   * underlined but may be emphasised in a platform specific
   * manner.  The mnemonic indicator character '&amp' can be
   * escaped by doubling it in the string, causing a single
   * '&amp' to be displayed.
   * </p>
   * 
   * @param text the text to be displayed in the hyperlink or null
   * 
   * @exception SWTException <ul>
   *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
   * </ul>
   */

  public void setText(String text)
  {
    checkWidget();
    if (text == null) text = "";
    if (!text.equals(this.text))
    {
      this.text = text;
      int i = text.indexOf('&');
      if(i == -1 || i == text.length()-1) mnemonic = -1;
      else mnemonic = Character.toLowerCase(text.charAt(i+1));
      redraw();
    }
  }
  
  
  /**  
   * Adds the listener to receive events.
   *
   * @param listener the listener
   *
   * @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
   * when called from the wrong thread
   * @exception SWTError(ERROR_WIDGET_DISPOSED)
   * when the widget has been disposed
   * @exception SWTError(ERROR_NULL_ARGUMENT)
   * when listener is null
   */

  public void addSelectionListener(SelectionListener listener)
  {
    checkWidget();
    if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    TypedListener typedListener = new TypedListener(listener);
    addListener(SWT.Selection, typedListener);
    addListener(SWT.DefaultSelection, typedListener);
  }


  /**  
   * Removes the listener.
   *
   * @param listener the listener
   *
   * @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
   * when called from the wrong thread
   * @exception SWTError(ERROR_WIDGET_DISPOSED)
   * when the widget has been disposed
   * @exception SWTError(ERROR_NULL_ARGUMENT)
   * when listener is null
   */

  public void removeSelectionListener(SelectionListener listener)
  {
    checkWidget();
    if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    removeListener(SWT.Selection, listener);
    removeListener(SWT.DefaultSelection, listener);
  }
}

⌨️ 快捷键说明

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