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

193
LINES

< > BotCompany Repo | #4000002 // ubaTaeCJ

User-supplied dialog

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,
          jscroll_vertical_borderless(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, "Rename project", rThread renameDB),
            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),
            jThreadedButton("Download project from Gaz.AI...", rThread downloadProject),
          )))));
  }
  
  void newDatabase {
    temp g22utils.enter();
    
    var tf = jtextfield();
    var lblDir = jlabel(" ");
    var lblStatus = jlabel(" ");
    var button = jThreadedButton("Create project", -> {
      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 {
    // Get project directory to export
    File projectDir = table.selected().dir;
    
    // Make up a zip name + place and let user decide where to actually put it
    
    new JFileChooser fileChooser;
    fileChooser.setDialogTitle("Export project as zip");
    fileChooser.setSelectedFile(javaxBackupDir(projectDir.getName() + "-" + ymdMinusHMS() + ".zip"));
    var cbSubdirs = jcheckbox("Include subdirectories", true);
    fileChooser.setAccessory(withLeftMargin(cbSubdirs));

    // Show dialog    
    if (fileChooser.showSaveDialog(visualize()) != JFileChooser.APPROVE_OPTION) ret;
    
    // User has confirmed, proceed with export
    File zip = fileChooser.getSelectedFile();
    S prefix = projectDir.getName() + "/";
    if (isChecked(cbSubdirs))
      dir2zip_recurse(projectDir, zip, prefix);
    else
      dir2zip(projectDir, zip, prefix);
    
    // Show note
    fileSavedInfoBox(zip);
    
    // Try to open in file manager
    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();
      g22utils.importProjectFromZip(zip);
    }
  }}
  
  void downloadProject {
    if (gazAICredentialsFromJavaXSecret() == null)
      ret with messageBox("Please enter Gaz.AI credentials first");
      
    inputText("Project to download", projectName -> { pcall-messagebox {
      var zip = g22utils.downloadProjectFromGazAI(gazAICredentialsFromJavaXSecret(), projectName);
      if (swingConfirm("Install downloaded project?"))
        g22utils.importProjectFromZip(zip);
    }});
  }
  
  void renameDB {
    File dir = table.selected().dir;
    
    inputText("New name", fileName(dir), newName -> {
      assertValidFileName(newName);
      if (!eq(newName, fileName(dir))) {
        bool open = masterStuff().isConceptDirLoaded(dir);
        if (open)
          masterStuff().closeDatabase(dir);
        renameFileVerbose(dir, newName);
        if (open)
          masterStuff().openDatabase(dir);
      }
    });
  }
}

Author comment

1

download  show line numbers   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

Snippet ID: #4000002
Snippet name: ubaTaeCJ
Eternal ID of this version: #4000002/1
Text MD5: 0401533e34f18c86f38648b737aaba11
Author: ubataecj
Category: 1
Type: User-supplied dialog
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2024-04-06 17:29:20
Source code size: 7049 bytes / 193 lines
Pitched / IR pitched: No / No
Views / Downloads: 70 / 28
Referenced in: [show references]