Example #1
0
  /** Get an array of all the RosterEntry-containing files in the target directory */
  static String[] getAllFileNames() {
    // ensure preferences will be found for read
    FileUtil.createDirectory(LocoFile.getFileLocation());

    // create an array of file names from roster dir in preferences, count entries
    int i;
    int np = 0;
    String[] sp = null;
    if (log.isDebugEnabled()) {
      log.debug("search directory " + LocoFile.getFileLocation());
    }
    File fp = new File(LocoFile.getFileLocation());
    if (fp.exists()) {
      sp = fp.list();
      if (sp != null) {
        for (i = 0; i < sp.length; i++) {
          if (sp[i].endsWith(".xml") || sp[i].endsWith(".XML")) {
            np++;
          }
        }
      } else {
        log.warn("expected directory, but {} was a file", LocoFile.getFileLocation());
      }
    } else {
      log.warn(
          FileUtil.getUserFilesPath() + "roster directory was missing, though tried to create it");
    }

    // Copy the entries to the final array
    String sbox[] = new String[np];
    int n = 0;
    if (sp != null && np > 0) {
      for (i = 0; i < sp.length; i++) {
        if (sp[i].endsWith(".xml") || sp[i].endsWith(".XML")) {
          sbox[n++] = sp[i];
        }
      }
    }
    // The resulting array is now sorted on file-name to make it easier
    // for humans to read
    jmri.util.StringUtil.sort(sbox);

    if (log.isDebugEnabled()) {
      log.debug("filename list:");
      for (i = 0; i < sbox.length; i++) {
        log.debug("      " + sbox[i]);
      }
    }
    return sbox;
  }
Example #2
0
  /** Rebuild the Roster index and store it. */
  public void reindex() {
    Roster roster = new Roster();
    for (String fileName : Roster.getAllFileNames()) {
      // Read file
      try {
        Element loco =
            (new LocoFile())
                .rootFromName(LocoFile.getFileLocation() + fileName)
                .getChild("locomotive");
        if (loco != null) {
          RosterEntry re = new RosterEntry(loco);
          re.setFileName(fileName);
          roster.addEntry(re);
        }
      } catch (JDOMException | IOException ex) {
        log.error("Exception while loading loco XML file: {} execption: {}", fileName, ex);
      }
    }

    this.makeBackupFile(this.getRosterIndexPath());
    try {
      roster.writeFile(this.getRosterIndexPath());
    } catch (IOException ex) {
      log.error("Exception while writing the new roster file, may not be complete: {}", ex);
    }
    this.reloadRosterFile();
    log.info("Roster rebuilt, stored in {}", this.getRosterIndexPath());
  }
  @Override
  public void actionPerformed(ActionEvent event) {

    Roster roster = Roster.instance();
    String rosterGroup = Roster.instance().getDefaultRosterGroup();
    RosterEntry[] entries;
    // rosterGroup may legitimately be null
    // but getProperty returns null if the property cannot be found, so
    // we test that the property exists before attempting to get its value
    if (Beans.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) {
      rosterGroup = (String) Beans.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP);
      log.debug("selectedRosterGroup was {}", rosterGroup);
    }
    if (Beans.hasProperty(wi, "selectedRosterEntries")) {
      entries = (RosterEntry[]) Beans.getProperty(wi, "selectedRosterEntries");
      if (entries != null) {
        log.debug("selectedRosterEntries found {} entries", entries.length);
      } else {
        log.debug("selectedRosterEntries left entries null");
      }
    } else {
      entries = selectRosterEntry(rosterGroup);
      if (entries != null) {
        log.debug("selectRosterEntry(rosterGroup) found {} entries", entries.length);
      } else {
        log.debug("selectRosterEntry(rosterGroup) left entries null");
      }
    }
    if (entries == null) {
      return;
    }
    // get parent object if there is one
    // Component parent = null;
    // if ( event.getSource() instanceof Component) parent = (Component)event.getSource();

    // find the file for the selected entry
    for (RosterEntry re : entries) {
      String filename = roster.fileFromTitle(re.titleString());
      String fullFilename = LocoFile.getFileLocation() + filename;
      log.debug("resolves to [{}], [{}]", filename, fullFilename);

      // prompt for one last chance
      log.debug("rosterGroup now {}", rosterGroup);
      if (rosterGroup == null) {
        if (!userOK(re.titleString(), filename, fullFilename)) {
          return;
        }
        // delete it from roster
        roster.removeEntry(re);
      } else {
        String group = Roster.getRosterGroupProperty(rosterGroup);
        log.debug("removing {} group from entry", group);
        re.deleteAttribute(group);
        re.updateFile();
      }
      Roster.writeRosterFile();

      // backup the file & delete it
      if (rosterGroup == null) {
        try {
          // ensure preferences will be found
          FileUtil.createDirectory(LocoFile.getFileLocation());

          // move original file to backup
          LocoFile df = new LocoFile(); // need a dummy object to do this operation in next line
          df.makeBackupFile(LocoFile.getFileLocation() + filename);

        } catch (Exception ex) {
          log.error("error during locomotive file output: " + ex);
        }
      }
    }
  }