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

88
LINES

< > BotCompany Repo | #1024202 - JFastLogView_noWrap

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

Libraryless. Click here for Pure Java version (3289L/21K).

sclass JFastLogView_noWrap extends JComponent implements Scrollable {
  LS lines = syncList();
  bool endsWithNewLine, verbose;
  
  public Dimension getPreferredScrollableViewportSize() { ret getPreferredSize(); }
    
  public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 20; }

  public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
    ret (direction == SwingConstants.HORIZONTAL ? visibleRect.width : visibleRect.height)*5/6;
  }

  public bool getScrollableTracksViewportWidth() { false; }
  public bool getScrollableTracksViewportHeight() { false; }
    
  public void paint(Graphics g) {
    //if (verbose) _print("JFastLogView_noWrap painting");
    int w = getWidth(), h = getHeight();
    g.setColor(getBackground());
    g.fillRect(0, 0, w, h);
    g.setColor(getForeground());
    FontMetrics fm = componentFontMetrics(this);
    int fh = fm.getHeight();
    Rectangle clip = g.getClipBounds();
    int start, end;
    if (clip == null) { start = 0; end = l(lines); }
    else {
      start = max(0, clip.y/fh);
      end = min(l(lines), idiv_ceil(clip.y+clip.height, fh));
    }
    //printVars(+start, +end);
    int y = fm.getAscent()+start*fh;
    for (int i = start; i < end; i++) {
      S s = get(lines, i);
      if (s != null) g.drawString(s, 0, y);
      y += fh;
    }
    //if (verbose) _print("JFastLogView_noWrap painted");
  }
  
  public Dimension getPreferredSize() {
    FontMetrics fm = componentFontMetrics(this);
    if (fm == null) ret new Dimension(50, 50); // not renderable yet
    int fh = fm.getHeight();
    int w = 0;
    for i over lines: {
      S s = get(lines, i);
      w = max(w, fm.stringWidth(unnull(s))); // TODO: just count chars
    }
    ret new Dimension(w, fh*l(lines));
  }
  
  *() {}
  *(S text) { setText(text); }
  
  {
    componentPopupMenuItem(this, "Copy full text", r copyFullText);
  }
  
  // API
  
  // returns true if changed
  bool setLines(Collection<S> lines) {
    LS newList = asSyncList(lines);
    if (eq(this.lines, newList)) false;
    this.lines = newList;
    _revalidate(this);
    true;
  }
  
  // returns true if changed
  bool setText(S text) {
    endsWithNewLine = endsWithNewLine(text);
    ret setLines(lines(text));
  }
  
  void append(S text) {
    if (nempty(text)) setText(getText() + text);
  }
  
  S getText() {
    ret lines_rtrimIf(!endsWithNewLine, cloneList(lines));
  }
  
  void copyFullText {
    copyTextToClipboard(getText());
  }
}

download  show line numbers  debug dex   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pzhvpgtvlbxg, tvejysmllsmz

No comments. add comment

Snippet ID: #1024202
Snippet name: JFastLogView_noWrap
Eternal ID of this version: #1024202/26
Text MD5: 225a024411f869d3c8e1f753156ea8df
Transpilation MD5: 23ca11be3dd16e5c502b7c4a27bfc3a2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-30 23:39:42
Source code size: 2620 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 126 / 503
Version history: 25 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1024202 & http://1024202.tinybrain.de