Пример #1
0
  public void actionPerformed(ActionEvent e) {
    // obtain a HardcopyWriter to do this
    Roster r = Roster.instance();
    String title = "DecoderPro Roster";
    String rosterGroup = r.getDefaultRosterGroup();
    // 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);
    }
    if (rosterGroup == null) {
      title = title + " All Entries";
    } else {
      title = title + " Group " + rosterGroup + " Entires";
    }
    HardcopyWriter writer = null;
    try {
      writer = new HardcopyWriter(mFrame, title, 10, .5, .5, .5, .5, isPreview);
    } catch (HardcopyWriter.PrintCanceledException ex) {
      log.debug("Print cancelled");
      return;
    }

    // add the image
    ImageIcon icon =
        new ImageIcon(FileUtil.findURL("resources/decoderpro.gif", FileUtil.Location.INSTALLED));
    // we use an ImageIcon because it's guaranteed to have been loaded when ctor is complete
    writer.write(icon.getImage(), new JLabel(icon));
    // Add a number of blank lines, so that the roster entry starts below the decoderpro logo
    int height = icon.getImage().getHeight(null);
    int blanks = (height - writer.getLineAscent()) / writer.getLineHeight();

    try {
      for (int i = 0; i < blanks; i++) {
        String s = "\n";
        writer.write(s, 0, s.length());
      }
    } catch (IOException ex) {
      log.warn("error during printing: " + ex);
    }

    // Loop through the Roster, printing as needed
    List<RosterEntry> l = r.matchingList(null, null, null, null, null, null, null); // take all
    log.debug("Roster list size: " + l.size());
    for (RosterEntry re : l) {
      if (rosterGroup != null) {
        if (re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)) != null
            && re.getAttribute(Roster.getRosterGroupProperty(rosterGroup)).equals("yes")) {
          re.printEntry(writer);
        }
      } else {
        re.printEntry(writer);
      }
    }

    // and force completion of the printing
    writer.close();
  }
  @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);
        }
      }
    }
  }