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

178
LINES

< > BotCompany Repo | #1034288 // G22DatabasesPanel

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

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

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 {
    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();
      // TODO: subdirs but not backup/
      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();
      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);
      }
    });
  }
}

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/88
Text MD5: 09553cb026130d77bdee54e512311df1
Transpilation MD5: 95b71a8ddd99dc2d27c3092edc977243
Author: stefan
Category: javax / gazelle v
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-12-05 06:50:30
Source code size: 6563 bytes / 178 lines
Pitched / IR pitched: No / No
Views / Downloads: 284 / 832
Version history: 87 change(s)
Referenced in: [show references]