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

93
LINES

< > BotCompany Repo | #1020205 // DynRecordingTextArea [Text is persistent in session & has its own log file]

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

Libraryless. Click here for Pure Java version (17834L/112K).

abstract sclass DynRecordingTextArea extends DynModule {
  S text;
  switchable S title;
  switchable bool wordWrap;
  S globalID = aGlobalID();

  transient JTextArea ta;
  transient EditRecorder recorder;
  transient ReliableSingleThread rstPersistText = rstWithDelay(1000, r persistText);
  transient JButton popDownButton;
  
  // We have auto-save of deleted modules
  //bool warnOnDelete() { ret nempty(text); }
  
  File logFile() { ret javaxDataDir("Text Area Logs/" + globalID); }
  
  start {
    dm_watchFieldAndNow title(r { setModuleName(title) });
    dm_watchField wordWrap(r {
      enableWordWrapForTextArea(ta, wordWrap);
      if (ta != null && wordWrap) dm_reloadModule();
    });
    
    
    dm_vmBus_onMessages_q(ll("modulePoppedOut", "modulePoppedIn"), (m, myFrame) -> {
      if (dm_isMe(m))
        adjustUIForPoppedOutWindow();
    });
  }
  
  visualize {
    ta = jenableUndoRedo(typeWriterTextArea(text));
    enableWordWrapForTextArea(ta, wordWrap);
    logQuoted(logFile(), "text: " + text);
    recorder = new EditRecorder(voidfunc(EREvent e) {
      logStructure(logFile(), e);
    });
    recorder.text = text;
    main.onChange(ta, rstPersistText);
    ret jscroll(setOpaqueBackground(Color.white, withMargin(ta)));
  }
  
  void persistText enter {
    if (ta == null) ret;
    Pair<S, Int> p = textAndCaretPosition(ta);
    recorder.update(p.a);
    recorder.updateCaret(p.b);
    print("persistText " + now());
    setField(text := getText(ta));
  }
  
  void unvisualize {
    persistText();
    super.unvisualize();
  }
  
  void setText(S text) { main.setText(ta, text); }
  S text() { ret or(main.getText(ta), text); }
  
  S _modifyStructForDuplication(S struct) {
    // TODO: could fail if module gets more complex
    ret jreplace_first(struct, "globalID=*", "globalID=" + quote(aGlobalID()));
  }
  
  enhanceFrame {
    internalFrameMenuItem(f, "Global ID: " + globalID, null);
  }
  
  void adjustUIForPoppedOutWindow swing {
    if ((popDownButton != null) != dm_moduleIsPoppedOut(me())) {
      if (popDownButton != null) {
        removeFromParent(getParent(popDownButton));
        popDownButton = null;
        ret;
      }
      
      Window window = cast getWindow(dm_vis());

      var popBackInButton = findButton(window, "Pop Back In");
      var parent = getParent(popBackInButton);
      print(+parent);
      
      removeFromParent(parent); // hide controls
      
      popDownButton = jPopDownButton_noText(
        "Pop Back In", r { dm_popInModule(me()) },
        jCheckBoxMenuItem("Always On Top", isAlwaysOnTop(window),
          rEnter dm_toggleAlwaysOnTop),
      );
      replaceComponent(ta, -> centerAndEastWithMargin(ta, vstackWithSpacing(popDownButton)));
    }
  }
}

Author comment

Began life as a copy of #1016122

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1020205
Snippet name: DynRecordingTextArea [Text is persistent in session & has its own log file]
Eternal ID of this version: #1020205/10
Text MD5: d8cfc71d0bfb13c632bf5a70347b1812
Transpilation MD5: fddc77addcd775e1b4519612901c84c0
Author: stefan
Category: javax / stefan's os
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-12-25 19:54:57
Source code size: 2839 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 369 / 1001
Version history: 9 change(s)
Referenced in: [show references]