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

53
LINES

< > BotCompany Repo | #1015389 // ButtonImageLoader - for buttons that represent images on disk

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

Libraryless. Click here for Pure Java version (9891L/58K).

sclass ButtonImageLoader {
  DynamicStack stack;
  Set<JButton> loadedButtons = weakIdentityHashSet();
  DeQ loadQ;
  JScrollPane scrollPane;
  ChangeListener changeListener;
  settable int imageHeight = 128;

  *(DynamicStack *stack) {
    bindToComponent(stack, -> {
      scrollPane = enclosingScrollPane(stack);
      changeListener = scrollPaneOnScroll(scrollPane, r { update() });
      update();
    }, -> {
      if (scrollPane != null) {
        scrollPane.getViewport().removeChangeListener(changeListener);
        scrollPane = null;
        changeListener = null;
      }
    });
  }
  
  void update() swing {
    //new L<Runnable> l;
    for (JButton b : reversed((L<JButton>) (L) stackElementsShowing(stack))) {
      //if (add(loadedButtons, b)) pcall { // will strangely not work
      if (add_byContains(loadedButtons, b)) pcall {
        TextImageAction tia = cast metaGet(b, TextImageAction.class);
        if (tia == null) continue with print("no tia");
        File f = tia.file;
        if (f == null) continue with print("no file");
        qAdd(r { setAButtonsImage(b, f) });
      }
    }
    //if (nempty(l)) qAdd(chainRunnables_pcall(l));
  }
  
  void qAdd(Runnable r) {
    if (loadQ == null) loadQ = startDeQ();
    loadQ.addFirst(r);
  }
  
  // overridable
  void setAButtonsImage(JButton b, File f) {
    setButtonImage(b, loadPreviewImage(f));
    //print("Loaded: " + f);
  }
  
  // overridable
  BufferedImage loadPreviewImage(File f) {
    ret loadPreviewImageWithExactHeight(imageHeight, f);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015389
Snippet name: ButtonImageLoader - for buttons that represent images on disk
Eternal ID of this version: #1015389/19
Text MD5: 667a79683467fa7e64b72d508a59dfe3
Transpilation MD5: e29a1c6a5cc4bb412931bc9e6e15271a
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-15 21:51:52
Source code size: 1591 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 508 / 1120
Version history: 18 change(s)
Referenced in: [show references]