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

110
LINES

< > BotCompany Repo | #1034689 // JTextFileEditor

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

Libraryless. Click here for Pure Java version (16387L/99K).

srecord noeq JTextFileEditor(File file) is Swingable {
  settable bool autoSave;
  settable bool wordWrap = true;
  gettable bool uneditable;
  
  gettable JTextArea textArea;
  JButton btnReload, btnSave;
  JLabel lblChanged;
  volatile S fileContents, savedText;
  FileWatchService fileWatcher;
  Q q = startQ();
  RSTOverQ rstLoad = rstWithPreDelay(0.5, q, r _load);
  RSTOverQ rstSave;
  JPanel buttons;
  SingleComponentPanel scpButtons = singleComponentPanel();

  cachedVisualize {
    assertNotNull(file);
    rstSave = rstWithPreDelay(autoSave ? 0.5 : 0.0, q, r _save);
    savedText = fileContents = loadTextFile(file);
    textArea = makeTextArea();
    if (uneditable) makeUneditable(textArea);
    if (wordWrap) enableWordWrapForTextArea(textArea);
    setText(savedText);
    onChange(textArea, -> {
      updateButtons();
      if (autoSave) rstSave!;
    });
    
    // Ctrl+S to save text
    addKeyListener(textArea(), ctrlLetterKeyListener('s', rstSave));
    
    bindToComponent(textArea, -> {
      fileWatcher = new FileWatchService;
      fileWatcher.addNonRecursiveListener(dirOfFile(file),
        f -> { if (sameFile(f, file)) rstLoad!; });
      rstLoad!;
    }, -> { dispose fileWatcher; });
    
    scpButtons.set(buttons = makeButtons());
    
    ret northAndCenter(
      withSideMargin(10, 20, centerAndEastWithMargin(
        new JFilePathLabel(file).iconOnTheLeft(true).minWidth(true).visualize(),
        scpButtons)),
      withMargin(textAreaLayoutComponent())
    );
  }
  
  JPanel makeButtons() {
    btnSave = btnReload = null;
    ret jline(
      uneditable ? null
        : autoSave
          ? (lblChanged = jlabel())
          : (btnSave = disableButton(jbutton("Save", rstSave))),
      uneditable ? null : (btnReload = jbutton("Reload", r reload))
    );
  }
  
  swappable JComponent textAreaLayoutComponent() { ret jscroll(textArea()); }
  
  void updateButtons {
    bool changed = !eq(savedText, getText(textArea));
    setEnabled(btnSave, changed);
    setEnabled(btnReload, !eq(fileContents, savedText));
    main setText(lblChanged, changed ? "*" : "");
  }
  
  void _save {
    if (!uneditable)
      saveTextFileVerbose(file, savedText = getText(textArea));
  }
  
  void reload {
    savedText = fileContents;
    setText(savedText);
    updateButtons();
  }
  
  void _load {
    print("Loading " + file);
    fileContents = loadTextFile(file);
    updateButtons();
    if (uneditable) reload();
  }
  
  swappable JTextArea makeTextArea() {
    ret jTypeWriterTextArea();
  }
  
  void setText(S text) {
    main setText(textArea(), text);
  }
  
  JPanel buttons() {
    visualize();
    ret buttons;
  }
  
  selfType uneditable(bool b) {
    if (uneditable == b) this;
    uneditable = b;
    if (buttons != null) {
      scpButtons.set(buttons = makeButtons());
      setEditable(textArea(), true);
    }
    this;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034689
Snippet name: JTextFileEditor
Eternal ID of this version: #1034689/46
Text MD5: 7d72869325f23ca1fd2aa32903c12c16
Transpilation MD5: 1e16a9634ca0390f12af803f6687e344
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-30 00:51:32
Source code size: 2998 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 234 / 516
Version history: 45 change(s)
Referenced in: [show references]