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(); }
// cache selectedRosterEntries so that multiple calls to this // between selection changes will not require the creation of a new array @Override @edu.umd.cs.findbugs.annotations.SuppressWarnings( value = "EI_EXPOSE_REP", justification = "Want to give access to mutable, original roster objects") public RosterEntry[] getSelectedRosterEntries() { if (selectedRosterEntries == null) { int[] rows = dataTable.getSelectedRows(); selectedRosterEntries = new RosterEntry[rows.length]; for (int idx = 0; idx < rows.length; idx++) { selectedRosterEntries[idx] = Roster.instance() .getEntryForId(sorter.getValueAt(rows[idx], RosterTableModel.IDCOL).toString()); } } return selectedRosterEntries; }
@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); } } } }
public RosterTable(boolean editable, int selectionMode) { super(); dataModel = new RosterTableModel(editable); sorter = new TableSorter(dataModel); dataTable = new JTable(sorter); sorter.setTableHeader(dataTable.getTableHeader()); dataScroll = new JScrollPane(dataTable); // set initial sort TableSorter tmodel = ((TableSorter) dataTable.getModel()); tmodel.setSortingStatus(RosterTableModel.ADDRESSCOL, TableSorter.ASCENDING); // Use a "Numeric, if not, Alphanumeric" comparator tmodel.setColumnComparator(String.class, new jmri.util.PreferNumericComparator()); // allow reordering of the columns dataTable.getTableHeader().setReorderingAllowed(true); // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541) dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); dataTable.setColumnModel(columnModel); dataModel.setColumnModel(columnModel); dataTable.createDefaultColumnsFromModel(); dataTable.setAutoCreateColumnsFromModel(false); TableColumn tc = columnModel.getColumnByModelIndex(RosterTableModel.PROTOCOL); columnModel.setColumnVisible(tc, false); for (String s : Roster.instance().getAllAttributeKeys()) { if (!s.contains("RosterGroup") && !s.toLowerCase().startsWith("sys") && !s.toUpperCase().startsWith("VSD")) { // NOI18N String[] r = s.split("(?=\\p{Lu})"); // NOI18N StringBuilder sb = new StringBuilder(); sb.append(r[0].trim()); // System.out.println("'"+r[0]+","); for (int j = 1; j < r.length; j++) { // System.out.println("'"+r[j]+","); sb.append(" "); sb.append(r[j].trim()); } TableColumn c = new TableColumn(dataModel.getColumnCount()); c.setHeaderValue((sb.toString()).trim()); dataTable.addColumn(c); dataModel.addColumn(c.getHeaderValue()); columnModel.setColumnVisible(c, false); } } // resize columns as requested resetColumnWidths(); // general GUI config setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // install items in GUI add(dataScroll); // set Viewport preferred size from size of table java.awt.Dimension dataTableSize = dataTable.getPreferredSize(); // width is right, but if table is empty, it's not high // enough to reserve much space. dataTableSize.height = Math.max(dataTableSize.height, 400); dataTableSize.width = Math.max(dataTableSize.width, 400); dataScroll.getViewport().setPreferredSize(dataTableSize); dataTable.setSelectionMode(selectionMode); MouseListener mouseHeaderListener = new tableHeaderListener(); dataTable.getTableHeader().addMouseListener(mouseHeaderListener); dataTable.setDefaultEditor(Object.class, new RosterCellEditor()); dataTable .getSelectionModel() .addListSelectionListener( tableSelectionListener = new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { selectedRosterEntries = null; // clear cached list of selections if (dataTable.getSelectedRowCount() == 1) { re = Roster.instance() .getEntryForId( sorter .getValueAt( dataTable.getSelectedRow(), RosterTableModel.IDCOL) .toString()); } else if (dataTable.getSelectedRowCount() > 1) { re = null; } // leave last selected item visible if no selection } else if (e.getFirstIndex() == -1) { // A reorder of the table might of occured therefore we are going to make sure // that the selected item is still in view moveTableViewToSelected(); } } }); }