Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

105
LINES

< > BotCompany Repo | #1034548 // InstantNeverHideToolTip

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9771L) is out of date.

// instantiate only once per component!
sclass InstantNeverHideToolTip > MouseAdapter {
  JComponent component;
  JToolTip toolTip;
  Popup popup;
  settable Pt offset = pt(20, 20);
  int x, y; // popup position
  S text;
  IF0<S> calculateText;
  IF1<Pt, S> calculateTextFromCoordinates;
  settable bool verbose;
  Pt mousePt = pt(0, 0); // last mouse position in component
  
  *(IF0<S> *calculateText, JComponent component) { this(component); }
  *(IF1<Pt, S> *calculateTextFromCoordinates, JComponent component) { this(component); }
  *(S *text, JComponent component) { this(component); }
  
  *(JComponent *component) {
    swing {
      component.addMouseListener(InstantNeverHideToolTip.this);
      component.addMouseMotionListener(InstantNeverHideToolTip.this);
      toolTip = component.createToolTip();
      
      // Hide also when component is removed from hierarchy
      bindToComponent(component, null, l0 hideToolTip);
    }
  }
  
  @Override public void mouseEntered(MouseEvent e) {
    mousePt(ptFromEvent(e));
    showToolTip();
  }
  
  void mousePt(Pt p) {
    mousePt = p;
    if (p != null) {
      var p2 = translatePt(convertPointToScreen(p, component), offset);
      x = p2.x;
      y = p2.y;
    }
  }
  
  @Override public void mouseDragged(MouseEvent e) {
    mouseMoved(e);
  }
  
  @Override public void mouseMoved(MouseEvent e) {
    mousePt(ptFromEvent(e));
    if (verbose) printVars(+mousePt, +calculateTextFromCoordinates);
    if (calculateTextFromCoordinates != null) {
      if (toolTipShowing())
        text = null; // force moving tooltip around with mouse cursor
      updateText();
    }
  }
  
  void updateText() swing {
    if (calculateText != null)
      setText(calculateText!);
    else if (calculateTextFromCoordinates != null)
      setText(calculateTextFromCoordinates.get(mousePt));
  }
  
  public void showToolTip() {
    updateText();
    showToolTip_noUpdateText();
  }
  
  public void showToolTip_noUpdateText() {
    if (empty(text)) ret;
    toolTip.setTipText(text);
    popup = PopupFactory.getSharedInstance().getPopup(component, toolTip, x, y);
    popup.show();
  }

  public void mouseExited(MouseEvent e) {
    hideToolTip();
  }
  
  bool toolTipShowing() { ret popup != null; }
  
  void hideToolTip() {
    if (popup != null) {
      popup.hide();
      popup = null;
    }
  }
  
  S getText() { ret text; }
  
  void setText(S text) swing {
    if (verbose) printVars("setText", +text);
    if (eq(text, getText())) ret;
    InstantNeverHideToolTip.this.text = text;
    if (verbose) printVars("setText", showing := toolTipShowing(), +x, +y);
    if (toolTipShowing()) {
      hideToolTip();
      showToolTip_noUpdateText();
    }
  }
  
  selfType offset(int pixels) {
    ret offset(pt(pixels, pixels));
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034548
Snippet name: InstantNeverHideToolTip
Eternal ID of this version: #1034548/30
Text MD5: f54186a7221340130ff000b511351e4e
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-07 04:59:31
Source code size: 2895 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 345 / 764
Version history: 29 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1036083 - InstantNeverHideToolTip backup