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

203
LINES

< > BotCompany Repo | #1033790 // UIURLSystem - "UI URLs" (free-form case-insensitive URLs to navigate the UI) for Swing

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

Libraryless. Click here for Pure Java version (17927L/107K).

// Note: renderUIUrl etc are called in Swing thread

// TODO: improve AutoComboBox and put it back in

persistable srecord noeq UIURLSystem(transient Enterable owner, transient SimpleLiveValue<S> uiURL) extends MetaWithChangeListeners {
  settable bool showHomeButton = true;
  settable int maxHistoryEntries = 100;
  settable bool showGoButton;
  
  // key:   ui URL
  transient Map<S, Entry> uiMap = syncCIMap();
  
  // these are additionally displayed at the top of the list
  // first URL will be shown as "Home" button (if home button is enabled)
  LS preferredURLs = syncList();
  
  LS history = syncLinkedList();
  LS forwardHistory = syncLinkedList();
  
  sclass Entry {
    S url;
    S toolTip;
    IF0<? extends JComponent> makeComponent;
    
    *(S *url, IF0<? extends JComponent> *makeComponent) {}
    *(S *url, S *toolTip, IF0<? extends JComponent> *makeComponent) {}
  }
  
  settable transient SingleComponentPanel scp;
  transient JComboBox<S> comboBox;
  transient JButton btnHome, btnBack, btnForward;
  
  transient simplyCached JComponent urlBar() {
    //comboBox = autoComboBox(url(), cloneKeys(uiMap));
    comboBox = jcombobox(urlsForComboBox());
    //centerComboBox(comboBox);
    comboBox.setRenderer(
      new CenteredComboBoxRenderer(
        new ListCellRendererWithToolTip<S>(url -> toolTipForURL(url),
          (ListCellRenderer<S>) comboBox.getRenderer())));
    //comboBoxDynamicToolTip(comboBox, url -> toolTipForURL(url));
    
    onSelectedItem(
      bindComboBoxToLiveValue(comboBox, uiURL),
      url -> showUIURL(url)
    );
    
    Runnable cbAction = comboBoxAction(comboBox, url -> showUIURL(url));
    
    JPanel rightButtons = jlineWithSpacing(5, );
    JPanel leftButtons = jlineWithSpacing(5);
    
    if (showGoButton)
      rightButtons.add(jbutton("Go", cbAction));
    
    JComponent panel = withLabel("Show",
      centerAndEastWithMargin(
        comboBox,
        rightButtons));
    
    btnBack = jimageButtonScaledToWidth(16, #1103102, l0 goBack);
    add(rightButtons, btnBack);

    btnForward = jimageButtonScaledToWidth(16, #1103103, l0 goForward);
    add(rightButtons, btnForward);

    if (showHomeButton) {
      S homeURL = homeURL();
      btnHome = jimageButtonScaledToWidth(16, #1103101,
        homeURL != null ? "Go to " + homeURL : "Go to home URL",
        l0 goHome);
      add(rightButtons, btnHome);
    }
    
    uiURL.onChangeAndNow(l0 updateButtons);
    
    if (!emptyContainer(leftButtons))
      panel = westAndCenterWithMargin(leftButtons, panel);
    ret panel;
  }
  
  void updateButtons {
    swing {
      S homeURL = homeURL();
      setEnabled(btnHome, homeURL != null && !eq(homeURL, url()));
      
      S backURL = last(history);
      setEnabled(btnBack, backURL != null);
      setToolTip(btnBack, backURL == null ? "Go back" : "Go back to " + backURL);
      
      S forwardURL = first(forwardHistory);
      printVars("updateButtons", +forwardURL);
      setEnabled(btnForward, forwardURL != null);
      setToolTip(btnForward, forwardURL == null ? "Go forward" : "Go forward to " + forwardURL);
    }
    change();
  }
  
  JComponent renderUIUrl aka renderUIURL aka uiGet(S url) {
    try {
      temp tempEnter(owner);
      var entry = uiMap.get(url);
      if (entry == null) ret jCenteredLabel("URL not found: " + url);
      var component = entry.makeComponent!;
      ret component;
    } catch print e {
      ret jErrorView(e);
    }
  }
  
  bool hasURL(S url) { ret uiMap.containsKey(url); }
  
  void showUIURL(S url) swing {
    if (!eqic(url(), url)) {
      addToHistory(url());
      showUIURL_dontAddToHistory(url);
    }
  }
  
  void addToHistory(S url) {
    printVars("addToHistory", +url);
    if (empty(url) || eqic(last(history), url)) ret;
    removeAllIC_useIterator(history, url);
    history.add(url);
    if (l(history) > maxHistoryEntries) removeFirst(history);
    forwardHistory.clear();
    updateButtons();
  }
  
  void showUIURL_dontAddToHistory(S url) {
    temp tempEnter(owner);
    
    setURL(trim(url));
    go();
  }
  
  void go {
    setComponent(scp, renderUIUrl(url()));
  }
  
  S url() { ret uiURL!; }
  void setURL(S url) {
    assertURLValid(url);
    uiURL.set(url);
  }
  
  swappable void assertURLValid(S url) {
    if (!uiMap.containsKey(url))
      fail("UIURL not found: " + url);
  }
  
  selfType put(WithToolTip<S> url, IF0<? extends JComponent> maker) {
    ret put(url!, url.toolTip(), maker);
  }
  
  selfType put(S url, S toolTip default null, IF0<? extends JComponent> maker) {
    uiMap.put(url, new Entry(url, toolTip, maker));
    setComboBoxItems(comboBox, urlsForComboBox());
    this;
  }
  
  S toolTipForURL(S url) {
    var e = uiMap.get(url);
    ret e?.toolTip;
  }
    
  LS urlsForComboBox() {
    var sorted = cloneKeys(uiMap);
    ret concatLists(listSetIntersection(preferredURLs, sorted), sorted);
  }
  
  void addPreferredUIURL(S uiURL) {
    addIfNotContained(preferredURLs, uiURL);
  }
  
  S homeURL() { ret first(preferredURLs); }
  
  void goHome { showUIURL(homeURL()); }
  
  void goBack swing {
    S url = popLast(history);
    if (url != null) {
      S currentURL = url();
      if (nempty(currentURL))
        forwardHistory.add(0, currentURL);
      printVars("goBack", +url, +currentURL, +forwardHistory);
      updateButtons();
      showUIURL_dontAddToHistory(url);
    }
  }
  
  void goForward swing {
    S url = popFirst(forwardHistory);
    if (url != null) {
      S currentURL = url();
      if (nempty(currentURL))
        history.add(currentURL);
      printVars("goForward", +url, +currentURL, +history);
      updateButtons();
      showUIURL_dontAddToHistory(url);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033790
Snippet name: UIURLSystem - "UI URLs" (free-form case-insensitive URLs to navigate the UI) for Swing
Eternal ID of this version: #1033790/76
Text MD5: dabbd2bd38ce7d11791763fe62b1b16c
Transpilation MD5: 7997ef840c5f99db8a90388321d3eeb2
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-11 16:33:30
Source code size: 5909 bytes / 203 lines
Pitched / IR pitched: No / No
Views / Downloads: 274 / 807
Version history: 75 change(s)
Referenced in: #1034137 - UIURLSystem - "UI URLs" (free-form case-insensitive URLs to navigate the UI) for Swing [backup]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)