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

110
LINES

< > BotCompany Repo | #1035443 // JG22Network - Swing representation of a G22Network

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

Uses 2164K of libraries. Click here for Pure Java version (54222L/285K).

sclass JG22Network is Swingable {
  G22Utils g22utils;
  new MainPanel mainPanel;
  new G22Network network;
  Map<G22NetworkElement, JG22NetworkElement> elementToComponent = syncMap();
  ReliableSingleThread rstUpdate = rstWithPreDelay(0.25, l0 updateImpl);
  
  // example instance of the network so we can test it immediately
  settable volatile G22NetworkInstance networkInstance;
  
  // margin on bottom/right
  transient int margin = 50;
  
  class MainPanel extends JPanel {
    *() { super(null); } // no layout manager
    
    // to allow overlapping components
    public bool isOptimizedDrawingEnabled() { false; }

    public Dimension getPreferredSize() {
      Rect r = combinedChildBounds(this);
      if (r == null) r = rect(0, 0, 0, 0);
      //_print("JG22Network.getPreferredSize = " + r);
      ret new Dimension(r.x2()+margin, r.y2()+margin);
    }
    
    void revalidateMe() {
      //_print("JG22Network.revalidateMe");
      //awtLater(0.5, -> {
        revalidateIncludingFullCenterContainer(this);
        repaint();
      //});
    }
  }

  *(G22Utils *g22utils, G22Network *network) {
    network.onChangeAndNow(l0 networkChanged);
    for (element : network.elements)
      visualizeElement(element);
  }
  
  void networkChanged { rstUpdate.trigger(); }
  
  void updateImpl {
    if (networkInstance == null)
      makeInstance();
    visualizePorts();
  }
  
  void visualizePorts { awt {
    // Remove old visualizations
    for (ports : directChildrenOfType(mainPanel, JG22NetworkPort))
      removeFromParent(ports);
      
    // Create new ones
    for (e : network.elements) {
      var c = elementToComponent.get(e);
      if (c == null)
        print("No component for " + e);
      else
        for (port : e.ports) {
          print("Visualizing port " + port);
          var p = new JG22NetworkPort(g22utils, c, port);
          p.init();
          mainPanel.add(p);
        }
    }
    
    mainPanel.repaint();
  }}
  
  void makeInstance {
    var networkInstance = new G22NetworkInstance(network);
    networkInstance.init(g22utils);
    networkInstance(networkInstance);
    print("Network instance made");
  }
  
  void visualizeElement(G22NetworkElement element) swing {
    var c = new JG22NetworkElement(g22utils, this, element);
    elementToComponent.put(element, c);
    mainPanel.add(c.visualize());
    mainPanel.revalidateMe();
  }
  
  void newInstance {
    networkInstance = null;
    rstUpdate.trigger();
  }
  
  void newElement {
    new G22NetworkElement element;
    element.network(network);
    network.elements.add(element);
    network.change();
    visualizeElement(element);
  }
  
  void deleteElement(JG22NetworkElement element) {
    network.elements.remove(element.element);
    network.change();
    mainPanel.remove(element.visualize());
    mainPanel.repaint();
  }
  
  cachedVisual
    withRightAlignedButtons(/*jscroll_center_borderless*/jscroll(mainPanel),
      "New instance", rThread newInstance,
      "New element", rThread newElement);
}

Author comment

Began life as a copy of #1035441

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035443
Snippet name: JG22Network - Swing representation of a G22Network
Eternal ID of this version: #1035443/47
Text MD5: dfc174cf2f9a060e9fa2f8fdb90ff8ac
Transpilation MD5: c668422c89ccccdd1ae2308fb3e15472
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-14 03:34:24
Source code size: 3138 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 74 / 190
Version history: 46 change(s)
Referenced in: [show references]