Search in sources :

Example 6 with HardcopyWriter

use of jmri.util.davidflanagan.HardcopyWriter in project JMRI by JMRI.

the class PrintTrainsByCarTypesAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    // obtain a HardcopyWriter
    try {
        writer = new HardcopyWriter(mFrame, Bundle.getMessage("TitleTrainsByType"), Control.reportFontSize, .5, .5, .5, .5, isPreview);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    // Loop through the car types showing which locations and tracks will
    // service that car type
    String[] carTypes = CarTypes.instance().getNames();
    List<Train> trains = trainManager.getTrainsByNameList();
    try {
        // title line
        String s = Bundle.getMessage("Type") + TAB + Bundle.getMessage("Trains") + TAB + TAB + TAB + Bundle.getMessage("Description") + NEW_LINE;
        writer.write(s);
        // car types
        for (String type : carTypes) {
            s = type + NEW_LINE;
            writer.write(s);
            // trains
            for (Train train : trains) {
                if (train.acceptsTypeName(type)) {
                    StringBuilder sb = new StringBuilder();
                    String name = train.getName();
                    sb.append(TAB + name + " ");
                    int j = MAX_NAME_LENGTH - name.length();
                    while (j > 0) {
                        j--;
                        sb.append(" ");
                    }
                    sb.append(train.getDescription() + NEW_LINE);
                    writer.write(sb.toString());
                }
            }
        }
        // and force completion of the printing
        writer.close();
    } catch (IOException we) {
        log.error("Error printing PrintLocationAction: " + we);
    }
}
Also used : IOException(java.io.IOException) Train(jmri.jmrit.operations.trains.Train) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 7 with HardcopyWriter

use of jmri.util.davidflanagan.HardcopyWriter in project JMRI by JMRI.

the class RosterEntry method printEntryDetails.

/**
     * Print the roster information.
     * <p>
     * Updated to allow for multiline comment and decoder comment fields.
     * Separate write statements for text and line feeds to work around
     * the HardcopyWriter bug that misplaces borders.
     *
     * @param w the writer used to print
     */
public void printEntryDetails(Writer w) {
    int linesadded = -1;
    String title;
    // 3 spaces in front of legend labels
    String leftMargin = "   ";
    // pad remaining spaces for legend using fixed width font, forms "%-19s" in line
    int labelColumn = 19;
    try {
        //int indentWidth = indent.length();
        HardcopyWriter ww = (HardcopyWriter) w;
        int textSpace = ww.getCharactersPerLine() - indentWidth - 1;
        title = String.format("%-" + labelColumn + "s", // I18N ID:
        (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldID"))));
        if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
            linesadded = writeWrappedComment(w, _id, leftMargin + title, textSpaceWithIcon) + linesadded;
        } else {
            linesadded = writeWrappedComment(w, _id, leftMargin + title, textSpace) + linesadded;
        }
        title = String.format("%-" + labelColumn + "s", // I18N Filename:
        (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldFilename"))));
        if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
            linesadded = writeWrappedComment(w, _fileName != null ? _fileName : "<null>", leftMargin + title, textSpaceWithIcon) + linesadded;
        } else {
            linesadded = writeWrappedComment(w, _fileName != null ? _fileName : "<null>", leftMargin + title, textSpace) + linesadded;
        }
        if (!(_roadName.isEmpty())) {
            // I18N Road name:
            title = String.format("%-" + labelColumn + "s", (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldRoadName"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _roadName, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _roadName, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_roadNumber.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Road number:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldRoadNumber"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _roadNumber, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _roadNumber, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_mfg.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Manufacturer:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldManufacturer"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _mfg, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _mfg, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_owner.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Owner:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldOwner"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _owner, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _owner, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_model.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Model:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldModel"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _model, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _model, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_dccAddress.isEmpty())) {
            w.write(newLine, 0, 1);
            title = String.format("%-" + labelColumn + "s", // I18N DCC Address:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldModel"))));
            String s = leftMargin + title + _dccAddress;
            w.write(s, 0, s.length());
            linesadded++;
        }
        // method and print it
        if (!(_comment.isEmpty())) {
            //through the picture.
            for (int i = 0; i < (blanks - linesadded); i++) {
                w.write(newLine, 0, 1);
            }
            //reset the number of blanks to 0.
            if (blanks != 0) {
                blanks = 0;
            }
            title = String.format("%-" + labelColumn + "s", // I18N Comment:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldComment"))));
            linesadded = writeWrappedComment(w, _comment, leftMargin + title, textSpace) + linesadded;
        }
        if (!(_decoderModel.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Decoder Model:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldDecoderModel"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _decoderModel, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _decoderModel, leftMargin + title, textSpace) + linesadded;
            }
        }
        if (!(_decoderFamily.isEmpty())) {
            title = String.format("%-" + labelColumn + "s", // I18N Decoder Family:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldDecoderFamily"))));
            if ((textSpaceWithIcon != 0) && (linesadded < blanks)) {
                linesadded = writeWrappedComment(w, _decoderFamily, leftMargin + title, textSpaceWithIcon) + linesadded;
            } else {
                linesadded = writeWrappedComment(w, _decoderFamily, leftMargin + title, textSpace) + linesadded;
            }
        }
        //If there is a decoderComment field, need to wrap it
        if (!(_decoderComment.isEmpty())) {
            //through the picture.
            for (int i = 0; i < (blanks - linesadded); i++) {
                w.write(newLine, 0, 1);
            }
            //reset the number of blanks to 0.
            if (blanks != 0) {
                blanks = 0;
            }
            title = String.format("%-" + labelColumn + "s", // I18N Decoder Comment:
            (Bundle.getMessage("MakeLabel", Bundle.getMessage("FieldDecoderComment"))));
            linesadded = writeWrappedComment(w, _decoderComment, leftMargin + title, textSpace) + linesadded;
        }
        w.write(newLine, 0, 1);
        for (int i = -1; i < (blanks - linesadded); i++) {
            w.write(newLine, 0, 1);
        }
    } catch (IOException e) {
        log.error("Error printing RosterEntry: " + e);
    }
}
Also used : IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 8 with HardcopyWriter

use of jmri.util.davidflanagan.HardcopyWriter in project JMRI by JMRI.

the class PrintCvAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    try {
        writer = new HardcopyWriter(mFrame, mFrame.getRosterEntry().getId(), 10, .8, .5, .5, .5, isPreview);
        // print the decoder info section, etc
        printInfoSection(writer);
        String s = "\n\n";
        writer.write(s, 0, s.length());
        //Initialize some variables to define the CV table size
        int cvCount = mModel.getRowCount();
        int tableLeft = 1, tableRight = TABLE_COLS * 24 + 1, tableTopRow = 0, tableBottomRow = 0, tableHeight = cvCount / TABLE_COLS;
        if (cvCount % TABLE_COLS > 0) {
            tableHeight++;
        }
        /*Start drawing the table of CVs. Set up the table with 4 columns of CV/Value
             pairs and Draw the table borders and lines.  Each column width is
             16 characters, including the starting vertical line, but not the
             ending one.  Therefore the total table width is 64+1 characters
             The colummn headings take 2 lines
             4 columns of 20 gives 80 CVs possible. NMRA specs only define about 70 CVs
             including all the optional ones plus some Manufacturer ones.  80 should be
             enough, although more can be added by increasing the tableHeight value
             */
        //Set the top row and draw top line to start the table of CVs
        tableTopRow = writer.getCurrentLineNumber();
        writer.write(tableTopRow, tableLeft, tableTopRow, tableRight);
        //set the bottom of the table
        tableBottomRow = tableTopRow + tableHeight + 2;
        //Draw vertical lines for columns
        for (int i = 1; i < 76; i = i + 24) {
            writer.write(tableTopRow, i, tableBottomRow, i);
        }
        //Draw remaining horozontal lines
        writer.write(tableTopRow + 2, tableLeft, tableTopRow + 2, tableRight);
        writer.write(tableBottomRow, tableLeft, tableBottomRow, tableRight);
        //set font to Bold
        writer.setFontStyle(1);
        // print a simple heading with I18N
        // pad with spaces to column width, 3 x insert Value as var %1
        s = String.format("%1$21s%1$24s%1$24s", Bundle.getMessage("Value"));
        writer.write(s, 0, s.length());
        s = "\n";
        writer.write(s, 0, s.length());
        // NOI18N
        s = "            CV  Dec Hex             CV  Dec Hex             CV  Dec Hex\n";
        writer.write(s, 0, s.length());
        //set font back to Normal
        writer.setFontStyle(0);
        /* Create array to hold CV/Value strings to allow reformatting and sorting.
             * Same size as the table drawn above (4 columns*tableHeight; heading rows
             * not included
             */
        String[] cvStrings = new String[TABLE_COLS * tableHeight];
        //blank the array
        for (int i = 0; i < cvStrings.length; i++) {
            cvStrings[i] = "";
        }
        // get each CV and value
        for (int i = 0; i < mModel.getRowCount(); i++) {
            CvValue cv = mModel.getCvByRow(i);
            int value = cv.getValue();
            //convert and pad numbers as needed
            String numString = String.format("%12s", cv.number());
            String valueString = Integer.toString(value);
            String valueStringHex = Integer.toHexString(value).toUpperCase();
            if (value < 16) {
                valueStringHex = "0" + valueStringHex;
            }
            for (int j = 1; j < 3; j++) {
                if (valueString.length() < 3) {
                    valueString = " " + valueString;
                }
            }
            //Create composite string of CV and its decimal and hex values
            s = "  " + numString + "  " + valueString + "  " + valueStringHex + " ";
            //populate printing array - still treated as a single column
            cvStrings[i] = s;
        }
        //sort the array in CV order (just the members with values)
        String temp;
        boolean swap = false;
        do {
            swap = false;
            for (int i = 0; i < mModel.getRowCount() - 1; i++) {
                if (cvSortOrderVal(cvStrings[i + 1].substring(0, 15).trim()) < cvSortOrderVal(cvStrings[i].substring(0, 15).trim())) {
                    temp = cvStrings[i + 1];
                    cvStrings[i + 1] = cvStrings[i];
                    cvStrings[i] = temp;
                    swap = true;
                }
            }
        } while (swap == true);
        //Print the array in three columns
        for (int i = 0; i < tableHeight; i++) {
            s = cvStrings[i] + cvStrings[i + tableHeight] + cvStrings[i + tableHeight * 2] + "\n";
            writer.write(s, 0, s.length());
        }
        //write an extra character to work around the
        //last character truncation bug with HardcopyWriter
        s = " \n";
        writer.write(s, 0, s.length());
    } catch (java.io.IOException ex1) {
        log.error("IO exception while printing");
        return;
    } catch (HardcopyWriter.PrintCanceledException ex2) {
        log.debug("Print cancelled");
        return;
    }
    writer.close();
}
Also used : IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 9 with HardcopyWriter

use of jmri.util.davidflanagan.HardcopyWriter in project JMRI by JMRI.

the class CbusEventTablePane method getMenus.

@Override
public List<JMenu> getMenus() {
    List<JMenu> menuList = new ArrayList<JMenu>();
    ResourceBundle rb = ResourceBundle.getBundle("apps.AppsBundle");
    JMenu fileMenu = new JMenu(Bundle.getMessage("MenuFile"));
    JMenuItem openItem = new JMenuItem(rb.getString("MenuItemOpen"));
    fileMenu.add(openItem);
    JMenuItem saveItem = new JMenuItem(rb.getString("MenuItemSave"));
    fileMenu.add(saveItem);
    saveItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            eventModel.saveAsTable();
        }
    });
    JMenuItem saveAsItem = new JMenuItem(rb.getString("MenuItemSaveAs"));
    fileMenu.add(saveAsItem);
    saveAsItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            eventModel.saveTable();
        }
    });
    // add print menu items
    JMenuItem printItem = new JMenuItem(rb.getString("PrintTable"));
    fileMenu.add(printItem);
    printItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            HardcopyWriter writer = null;
            try {
                writer = new HardcopyWriter(getWindowInterface().getFrame(), getTitle(), 10, .8, .5, .5, .5, false);
            } catch (HardcopyWriter.PrintCanceledException ex) {
                //log.debug("Print cancelled");
                return;
            }
            writer.increaseLineSpacing(20);
            eventModel.printTable(writer);
        }
    });
    JMenuItem previewItem = new JMenuItem(rb.getString("PreviewTable"));
    fileMenu.add(previewItem);
    previewItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            HardcopyWriter writer = null;
            try {
                writer = new HardcopyWriter(getWindowInterface().getFrame(), getTitle(), 10, .8, .5, .5, .5, true);
            } catch (HardcopyWriter.PrintCanceledException ex) {
                //log.debug("Print cancelled");
                return;
            }
            writer.increaseLineSpacing(20);
            eventModel.printTable(writer);
        }
    });
    menuList.add(fileMenu);
    return menuList;
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) ResourceBundle(java.util.ResourceBundle) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 10 with HardcopyWriter

use of jmri.util.davidflanagan.HardcopyWriter in project JMRI by JMRI.

the class PrintLocationsAction method printLocations.

public void printLocations() {
    // obtain a HardcopyWriter
    String title = Bundle.getMessage("TitleLocationsTable");
    if (_location != null) {
        title = _location.getName();
    }
    try {
        writer = new HardcopyWriter(new Frame(), title, Control.reportFontSize, .5, .5, .5, .5, _isPreview);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    charactersPerLine = writer.getCharactersPerLine();
    try {
        // print locations?
        if (printLocations.isSelected()) {
            printLocationsSelected();
        }
        // print schedules?
        if (printSchedules.isSelected()) {
            printSchedulesSelected();
        }
        if (printComments.isSelected()) {
            printCommentsSelected();
        }
        // print detailed report?
        if (printDetails.isSelected()) {
            printDetailsSelected();
        }
        // print analysis?
        if (printAnalysis.isSelected()) {
            printAnalysisSelected();
        }
        // prevent NPE on close
        if (printLocations.isSelected() || printSchedules.isSelected() || printComments.isSelected() || printDetails.isSelected() || // force completion of the printing
        printAnalysis.isSelected()) {
            writer.close();
        }
    } catch (IOException we) {
        log.error("Error printing PrintLocationAction: " + we);
    }
}
Also used : OperationsFrame(jmri.jmrit.operations.OperationsFrame) Frame(java.awt.Frame) IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Aggregations

HardcopyWriter (jmri.util.davidflanagan.HardcopyWriter)20 IOException (java.io.IOException)12 ImageIcon (javax.swing.ImageIcon)4 JLabel (javax.swing.JLabel)4 Train (jmri.jmrit.operations.trains.Train)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Frame (java.awt.Frame)2 TableColumnModel (javax.swing.table.TableColumnModel)2 RollingStock (jmri.jmrit.operations.rollingstock.RollingStock)2 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 ResourceBundle (java.util.ResourceBundle)1