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

159
LINES

< > BotCompany Repo | #1034288 // G22DatabasesPanel

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

Transpiled version (52662L) is out of date.

srecord noeq G22DatabasesPanel(G22Utils g22utils) {
  delegate GazelleDB to G22Utils.
  delegate masterStuff to g22utils.
  
  JObjectTable<GazelleDB> table;
  FileWatchService dirWatcher;
  ReliableSingleThread rstUpdate = new(r _updateTable);
  
  cachedVisualize {
    table = swing(-> new JObjectTable);
    table.defaultAction(runnableToIVF1(rThread openDB));
    table.itemToMap = db -> {
      IG22LoadedDB loaded = db.loadedDB();
      ret litorderedmap(
        "Name" := db.name,
        "Directory" := f2s(db.dir),
        "Loaded" := loaded == null ? "No"
          : (loaded.hidden() ? "Yes (hidden)" : "Yes");      };

    var tbl = table.visualize();
    bindToComponent(tbl, -> {
      dirWatcher = new FileWatchService;
      dirWatcher.addNonRecursiveListener(g22utils.databasesMotherDir(), file -> {
        print("G22DatabasesPanel File change: " + file);
        rstUpdate!;
      });
      masterStuff().onLoadedDBsChange(rstUpdate);
      rstUpdate!;
    }, -> {
      masterStuff().removeLoadedDBsChangeListener(rstUpdate);
      dispose dirWatcher;
    });

    ret withTopAndBottomMargin(jCenteredRaisedSection("Gazelle projects on this computer",
      northAndCenterWithMargins(
        jline(
          toolTip("Create a new Gazelle project", jbutton("New project...", rThread newDatabase))
        ),
        centerAndEastWithMargin(
          tbl,
          vstackWithSpacing_fixed(
            // BUTTONS defined here
            
            DefaultButtonBorder(toolTip("Open selected project, keep other projects open too",
              tableDependentButton(table.table, "Open project", rThread openDB))),
            toolTip("Open selected project in hidden mode [without a window]",
              tableDependentButton(table.table, "Open hidden", rThread openDBHidden)),
            toolTip("Open selected project, close all other projects",
              tableDependentButton(table.table, "Switch to project", rThread switchToDB)),
            tableDependentButton(table.table, "Close project", rThread closeDB),
            tableDependentButton(table.table, "Delete project...", rThread deleteDB),
            tableDependentButton(table.table, "Browse files", rThread browse),
            tableDependentButton(table.table, "Export project as zip...", rThread exportAsZip),
            jThreadedButton("Import project from zip...", rThread importZip),
          ))));
  }
  
  void newDatabase {
    temp g22utils.enter();
    
    var tf = jtextfield();
    var lblDir = jlabel(" ");
    var lblStatus = jlabel(" ");
    var button = jbutton("Create project", r {
      temp g22utils.enter();
      
      S name = trim(tf.getText());
      File dir = newFile(g22utils.databasesMotherDir(), name);
      createEmptyConceptsFileInDir(dir);
      disposeFrame(tf);
      
      masterStuff().openDatabase(dir);
    });
    disableButton(button);
    onEnter(tf, button);
    
    onChange(tf, r {
      S name = trim(tf.getText());
      bool ok;
      S status = " ", path = " ";
      if (isValidFileName(name)) {
        File dir = newFile(g22utils.databasesMotherDir(), name);
        path = f2s(dir);
        if (dirExists(dir))
          status = "A project with that name exists";
        else
          ok = true;
      } else if (!empty(name))
        status = "Project name must be a valid file name";
      setEnabled(button, ok);
      setText(lblStatus, status);
      setText(lblDir, path);
    });
    
    showForm_makeFrame("New Gazelle Project",
      vstackWithSpacing(
        withLabel("Name for new project", tf),
        lblDir,
        lblStatus,
        button);
  }

  void _updateTable { table.setData_force(g22utils.gazelleDBs()); }
  
  void browse { desktopOpen(table.selected().dir); }
  void openDB { masterStuff().openDatabase(table.selected().dir); }
  void openDBHidden { masterStuff().openDatabase(table.selected().dir, true); }
  void switchToDB { masterStuff().switchToDatabase(table.selected().dir); }
  void closeDB { masterStuff().closeDatabase(table.selected().dir); }
  
  void deleteDB {
    var db = table.selected();
    if (db == null) ret;
    
    var dir = db.dir();
    if (g22utils.isConceptsDir(dir))
      ret with messageBox("Can't delete current project");
      
    // First, close the databse
    masterStuff().closeDatabase(dir);
    new DeleteFilesDialog("Delete Gazelle project " + db.name())
      .wholeDirectory(dir).show();
  }
  
  void exportAsZip {
    File dir = table.selected().dir;
    
    new JFileChooser fileChooser;
    fileChooser.setDialogTitle("Export project as zip");
    fileChooser.setSelectedFile(javaxBackupDir(dir.getName() + "-" + ymdMinusHMS() + ".zip"));
    if (fileChooser.showSaveDialog(visualize()) == JFileChooser.APPROVE_OPTION) {
      File zip = fileChooser.getSelectedFile();
      dir2zip(dir, zip, dir.getName() + "/");
      fileSavedInfoBox(zip);
      desktopOpen(dirOfFile(zip));
    }
  }
  
  void importZip { pcall-messagebox {
    new JFileChooser fc;
    fc.setDialogTitle("Import project from zip");
    fc.setCurrentDirectory(javaxBackupDir());
    if (fc.showOpenDialog(visualize()) == JFileChooser.APPROVE_OPTION) {
      File zip = fc.getSelectedFile();
      S baseDir = baseDirInZip(zip);
      S projectName = takeFirst(baseDir, smartIndexOf(baseDir, '/'));
      if (empty(projectName))
        ret with infoMessage("No base dir in zip");
        
      assertValidFileName(projectName);
      File dir = g22utils.dirOfProjectNamed(projectName);
      if (isDirectory(dir))
        ret with infoMessage("Sorry, won't overwrite existing project " + projectName + " (for now)");
        
      zip2dir(zip, dir.getParentFile());
      infoBox("Imported project " + projectName);
    }
  }}
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034288
Snippet name: G22DatabasesPanel
Eternal ID of this version: #1034288/72
Text MD5: 4829585b2291efb5a1047eeb6070d55e
Author: stefan
Category: javax / gazelle v
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-12 19:04:35
Source code size: 5903 bytes / 159 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 614
Version history: 71 change(s)
Referenced in: [show references]