Search in sources :

Example 11 with HardcopyWriter

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

the class TrainPrintUtilities method printReport.

/**
     * Print or preview a train manifest, build report, or switch list.
     *
     * @param file File to be printed or previewed
     * @param name Title of document
     * @param isPreview true if preview
     * @param fontName optional font to use when printing document
     * @param isBuildReport true if build report
     * @param logoURL optional pathname for logo
     * @param printerName optional default printer name
     * @param orientation Setup.LANDSCAPE, Setup.PORTRAIT, or Setup.HANDHELD
     * @param fontSize font size
     */
public static void printReport(File file, String name, boolean isPreview, String fontName, boolean isBuildReport, String logoURL, String printerName, String orientation, int fontSize) {
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    Frame mFrame = new Frame();
    boolean isLandScape = false;
    boolean printHeader = true;
    double margin = .5;
    // HardcopyWritter provides default page sizes for portrait and landscape
    Dimension pagesize = null;
    if (orientation.equals(Setup.LANDSCAPE)) {
        margin = .65;
        isLandScape = true;
    }
    if (orientation.equals(Setup.HANDHELD) || orientation.equals(Setup.HALFPAGE)) {
        printHeader = false;
        // add margins to page size
        pagesize = new Dimension(TrainCommon.getPageSize(orientation).width + TrainCommon.PAPER_MARGINS.width, TrainCommon.getPageSize(orientation).height + TrainCommon.PAPER_MARGINS.height);
    }
    try {
        writer = new HardcopyWriter(mFrame, name, fontSize, margin, margin, .5, .5, isPreview, printerName, isLandScape, printHeader, pagesize);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    // set font
    if (!fontName.equals("")) {
        writer.setFontName(fontName);
    }
    // now get the build file to print
    BufferedReader in = null;
    try {
        // NOI18N
        in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    } catch (FileNotFoundException e) {
        log.error("Build file doesn't exist");
        writer.close();
        return;
    } catch (UnsupportedEncodingException e) {
        log.error("Doesn't support UTF-8 encoding");
        writer.close();
        return;
    }
    String line;
    if (!isBuildReport && logoURL != null && !logoURL.equals(Setup.NONE)) {
        ImageIcon icon = new ImageIcon(logoURL);
        if (icon.getIconWidth() == -1) {
            log.error("Logo not found: " + logoURL);
        } else {
            writer.write(icon.getImage(), new JLabel(icon));
        }
    }
    Color c = null;
    while (true) {
        try {
            line = in.readLine();
        } catch (IOException e) {
            log.debug("Print read failed");
            break;
        }
        if (line == null) {
            if (isPreview) {
                try {
                    // need to do this in case the input file was empty to create preview
                    writer.write(" ");
                } catch (IOException e) {
                    log.debug("Print write failed for null line");
                }
            }
            break;
        }
        // check for build report print level
        if (isBuildReport) {
            // no indent
            line = filterBuildReport(line, false);
            if (line.equals("")) {
                continue;
            }
        // printing the train manifest
        } else {
            // determine if there's a line separator
            if (line.length() > 0) {
                boolean horizontialLineSeparatorFound = true;
                for (int i = 0; i < line.length(); i++) {
                    if (line.charAt(i) != HORIZONTAL_LINE_SEPARATOR) {
                        horizontialLineSeparatorFound = false;
                        break;
                    }
                }
                if (horizontialLineSeparatorFound) {
                    writer.write(writer.getCurrentLineNumber(), 0, writer.getCurrentLineNumber(), line.length() + 1);
                    c = null;
                    continue;
                }
            }
            for (int i = 0; i < line.length(); i++) {
                if (line.charAt(i) == VERTICAL_LINE_SEPARATOR) {
                    // make a frame (manifest two column format)
                    if (Setup.isTabEnabled()) {
                        writer.write(writer.getCurrentLineNumber(), 0, writer.getCurrentLineNumber() + 1, 0);
                        writer.write(writer.getCurrentLineNumber(), line.length() + 1, writer.getCurrentLineNumber() + 1, line.length() + 1);
                    }
                    writer.write(writer.getCurrentLineNumber(), i + 1, writer.getCurrentLineNumber() + 1, i + 1);
                }
            }
            line = line.replace(VERTICAL_LINE_SEPARATOR, SPACE);
            // determine if line is a pickup or drop
            if ((!Setup.getPickupEnginePrefix().equals("") && line.startsWith(Setup.getPickupEnginePrefix())) || (!Setup.getPickupCarPrefix().equals("") && line.startsWith(Setup.getPickupCarPrefix())) || (!Setup.getSwitchListPickupCarPrefix().equals("") && line.startsWith(Setup.getSwitchListPickupCarPrefix()))) {
                // log.debug("found a pickup line");
                c = Setup.getPickupColor();
            } else if ((!Setup.getDropEnginePrefix().equals("") && line.startsWith(Setup.getDropEnginePrefix())) || (!Setup.getDropCarPrefix().equals("") && line.startsWith(Setup.getDropCarPrefix())) || (!Setup.getSwitchListDropCarPrefix().equals("") && line.startsWith(Setup.getSwitchListDropCarPrefix()))) {
                // log.debug("found a drop line");
                c = Setup.getDropColor();
            } else if ((!Setup.getLocalPrefix().equals("") && line.startsWith(Setup.getLocalPrefix())) || (!Setup.getSwitchListLocalPrefix().equals("") && line.startsWith(Setup.getSwitchListLocalPrefix()))) {
                // log.debug("found a drop line");
                c = Setup.getLocalColor();
            } else if (!line.startsWith(TrainCommon.TAB)) {
                c = null;
            }
            if (c != null) {
                try {
                    writer.write(c, line + NEW_LINE);
                    continue;
                } catch (IOException e) {
                    log.debug("Print write color failed");
                    break;
                }
            }
        }
        try {
            writer.write(line + NEW_LINE);
        } catch (IOException e) {
            log.debug("Print write failed");
            break;
        }
    }
    // and force completion of the printing
    try {
        in.close();
    } catch (IOException e) {
        log.debug("Print close failed");
    }
    writer.close();
}
Also used : ImageIcon(javax.swing.ImageIcon) Frame(java.awt.Frame) InputStreamReader(java.io.InputStreamReader) Color(java.awt.Color) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 12 with HardcopyWriter

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

the class PrintRoutesAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    log.debug("Print all routes");
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    try {
        writer = new HardcopyWriter(mFrame, MessageFormat.format(Bundle.getMessage("TitleRoutesTable"), new Object[] {}), Control.reportFontSize, .5, .5, .5, .5, isPreview);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    try {
        // prevents exception when using Preview and no routes
        writer.write(" ");
        List<Route> routes = RouteManager.instance().getRoutesByNameList();
        for (int i = 0; i < routes.size(); i++) {
            Route route = routes.get(i);
            writer.write(route.getName() + NEW_LINE);
            printRoute(writer, route);
            if (i != routes.size() - 1) {
                writer.write(FORM_FEED);
            }
        }
    } catch (IOException e1) {
        log.error("Exception in print routes");
    }
    // and force completion of the printing
    writer.close();
}
Also used : IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 13 with HardcopyWriter

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

the class PrintDecoderListAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    try {
        writer = new HardcopyWriter(mFrame, "DecoderPro V" + Version.name() + " Decoder Definitions", 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));
    // Loop through the decoder index, printing as needed
    String lastMfg = "";
    String lastFamily = "";
    DecoderIndexFile f = DecoderIndexFile.instance();
    // take all
    List<DecoderFile> l = f.matchingDecoderList(null, null, null, null, null, null);
    int i = -1;
    log.debug("Roster list size: " + l.size());
    for (i = 0; i < l.size(); i++) {
        DecoderFile d = l.get(i);
        if (!d.getMfg().equals(lastMfg)) {
            printMfg(d, writer);
            lastMfg = d.getMfg();
            lastFamily = "";
        }
        if (!d.getFamily().equals(lastFamily)) {
            printFamily(d, writer);
            lastFamily = d.getFamily();
        }
        if (!d.getFamily().equals(d.getModel())) {
            printEntry(d, writer);
        }
    }
    // and force completion of the printing
    writer.close();
}
Also used : ImageIcon(javax.swing.ImageIcon) JLabel(javax.swing.JLabel) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter)

Example 14 with HardcopyWriter

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

the class PrintCarRosterAction method printCars.

@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "CarManager only provides Car Objects")
private void printCars() {
    boolean landscape = false;
    if (manifestOrientationComboBox.getSelectedItem() != null && manifestOrientationComboBox.getSelectedItem() == Setup.LANDSCAPE) {
        landscape = true;
    }
    int fontSize = (int) fontSizeComboBox.getSelectedItem();
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    try {
        writer = new HardcopyWriter(mFrame, Bundle.getMessage("TitleCarRoster"), fontSize, .5, .5, .5, .5, isPreview, "", landscape, true, null);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    numberCharPerLine = writer.getCharactersPerLine();
    // Loop through the Roster, printing as needed
    String location = "";
    String number;
    String road;
    String type;
    String length = "";
    String weight = "";
    String color = "";
    String owner = "";
    String built = "";
    String load = "";
    String kernel = "";
    String train = "";
    String destination = "";
    String finalDestination = "";
    String returnWhenEmpty = "";
    String value = "";
    String rfid = "";
    String last = "";
    String wait = "";
    String schedule = "";
    String comment = "";
    try {
        printTitleLine(writer);
        String previousLocation = null;
        List<RollingStock> cars = panel.carsTableModel.getCarList(sortByComboBox.getSelectedIndex());
        for (RollingStock rs : cars) {
            Car car = (Car) rs;
            if (printCarsWithLocation.isSelected() && car.getLocation() == null) {
                // car doesn't have a location skip
                continue;
            }
            location = "";
            destination = "";
            finalDestination = "";
            returnWhenEmpty = "";
            if (printCarLocation.isSelected()) {
                if (car.getLocation() != null) {
                    location = car.getLocationName().trim() + " - " + car.getTrackName().trim();
                }
                location = padAttribute(location, LocationManager.instance().getMaxLocationAndTrackNameLength() + 3);
            }
            // Page break between locations?
            if (previousLocation != null && !car.getLocationName().trim().equals(previousLocation) && printPage.isSelected()) {
                writer.pageBreak();
                printTitleLine(writer);
            } else // Add a line between locations?
            if (previousLocation != null && !car.getLocationName().trim().equals(previousLocation) && printSpace.isSelected()) {
                writer.write(NEW_LINE);
            }
            previousLocation = car.getLocationName().trim();
            // car number
            number = padAttribute(car.getNumber().trim(), Control.max_len_string_print_road_number);
            // car road
            road = padAttribute(car.getRoadName().trim(), CarRoads.instance().getMaxNameLength());
            // car type
            type = padAttribute(car.getTypeName().trim(), CarTypes.instance().getMaxFullNameLength());
            if (printCarLength.isSelected()) {
                length = padAttribute(car.getLength().trim(), Control.max_len_string_length_name);
            }
            if (printCarWeight.isSelected()) {
                weight = padAttribute(car.getWeight().trim(), Control.max_len_string_weight_name);
            }
            if (printCarColor.isSelected()) {
                color = padAttribute(car.getColor().trim(), CarColors.instance().getMaxNameLength());
            }
            if (printCarLoad.isSelected()) {
                load = padAttribute(car.getLoadName().trim(), CarLoads.instance().getMaxNameLength());
            }
            if (printCarKernel.isSelected()) {
                kernel = padAttribute(car.getKernelName().trim(), Control.max_len_string_attibute);
            }
            if (printCarOwner.isSelected()) {
                owner = padAttribute(car.getOwner().trim(), CarOwners.instance().getMaxNameLength());
            }
            if (printCarBuilt.isSelected()) {
                built = padAttribute(car.getBuilt().trim(), Control.max_len_string_built_name);
            }
            if (printCarLast.isSelected()) {
                last = padAttribute(car.getLastDate().split(" ")[0], 10);
            }
            if (printCarWait.isSelected()) {
                wait = padAttribute(Integer.toString(car.getWait()), 4);
            }
            if (printCarPickup.isSelected()) {
                schedule = padAttribute(car.getPickupScheduleName(), 10);
            }
            if (printCarValue.isSelected()) {
                value = padAttribute(car.getValue().trim(), Control.max_len_string_attibute);
            }
            if (printCarRfid.isSelected()) {
                rfid = padAttribute(car.getRfid().trim(), Control.max_len_string_attibute);
            }
            if (// pad out train to half of its maximum
            printCarTrain.isSelected()) {
                train = padAttribute(car.getTrainName().trim(), Control.max_len_string_train_name / 2);
            }
            if (printCarDestination.isSelected()) {
                if (car.getDestination() != null) {
                    destination = car.getDestinationName().trim() + " - " + car.getDestinationTrackName();
                }
                destination = padAttribute(destination, LocationManager.instance().getMaxLocationAndTrackNameLength() + 3);
            }
            if (printCarFinalDestination.isSelected()) {
                if (car.getFinalDestination() != null) {
                    finalDestination = car.getFinalDestinationName().trim() + " - " + car.getFinalDestinationTrackName().trim();
                }
                finalDestination = padAttribute(finalDestination, LocationManager.instance().getMaxLocationAndTrackNameLength() + 3);
            }
            if (printCarRWE.isSelected()) {
                if (car.getReturnWhenEmptyDestination() != null) {
                    returnWhenEmpty = car.getReturnWhenEmptyDestinationName().trim() + " - " + car.getReturnWhenEmptyDestTrackName().trim();
                }
                returnWhenEmpty = padAttribute(returnWhenEmpty, LocationManager.instance().getMaxLocationAndTrackNameLength() + 3);
            }
            if (printCarComment.isSelected()) {
                comment = car.getComment().trim();
            }
            String s = number + road + type + length + weight + color + load + kernel + owner + built + last + wait + schedule + value + rfid + location + train + destination + finalDestination + returnWhenEmpty + comment;
            if (s.length() > numberCharPerLine) {
                s = s.substring(0, numberCharPerLine);
            }
            writer.write(s + NEW_LINE);
        }
        // and force completion of the printing
        writer.close();
    } catch (IOException we) {
        log.error("Error printing car roster");
    }
}
Also used : IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 15 with HardcopyWriter

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

the class PrintEngineRosterAction method actionPerformed.

@Override
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "EngineManager only provides Engine Objects")
public void actionPerformed(ActionEvent e) {
    // obtain a HardcopyWriter to do this
    HardcopyWriter writer = null;
    try {
        writer = new HardcopyWriter(mFrame, Bundle.getMessage("TitleEngineRoster"), Control.reportFontSize, .5, .5, .5, .5, isPreview);
    } catch (HardcopyWriter.PrintCanceledException ex) {
        log.debug("Print cancelled");
        return;
    }
    numberCharPerLine = writer.getCharactersPerLine();
    // Loop through the Roster, printing as needed
    String number;
    String road;
    String model;
    String type;
    String length;
    String owner = "";
    String consist = "";
    String built = "";
    String value = "";
    String rfid = "";
    String location;
    List<RollingStock> engines = panel.getSortByList();
    try {
        // header
        String header = padAttribute(Bundle.getMessage("Number"), Control.max_len_string_print_road_number) + padAttribute(Bundle.getMessage("Road"), CarRoads.instance().getMaxNameLength()) + padAttribute(Bundle.getMessage("Model"), EngineModels.instance().getMaxNameLength()) + padAttribute(Bundle.getMessage("Type"), EngineTypes.instance().getMaxNameLength()) + padAttribute(Bundle.getMessage("Len"), Control.max_len_string_length_name) + (panel.sortByConsist.isSelected() ? padAttribute(Bundle.getMessage("Consist"), Control.max_len_string_attibute) : padAttribute(Bundle.getMessage("Owner"), ownerMaxLen)) + (panel.sortByValue.isSelected() ? padAttribute(Setup.getValueLabel(), Control.max_len_string_attibute) : "") + (panel.sortByRfid.isSelected() ? padAttribute(Setup.getRfidLabel(), Control.max_len_string_attibute) : "") + ((!panel.sortByValue.isSelected() && !panel.sortByRfid.isSelected()) ? padAttribute(Bundle.getMessage("Built"), Control.max_len_string_built_name) : "") + Bundle.getMessage("Location") + NEW_LINE;
        writer.write(header);
        for (RollingStock rs : engines) {
            Engine engine = (Engine) rs;
            // loco number
            number = padAttribute(engine.getNumber(), Control.max_len_string_print_road_number);
            road = padAttribute(engine.getRoadName(), CarRoads.instance().getMaxNameLength());
            model = padAttribute(engine.getModel(), EngineModels.instance().getMaxNameLength());
            type = padAttribute(engine.getTypeName(), EngineTypes.instance().getMaxNameLength());
            length = padAttribute(engine.getLength(), Control.max_len_string_length_name);
            if (panel.sortByConsist.isSelected()) {
                consist = padAttribute(engine.getConsistName(), Control.max_len_string_attibute);
            } else {
                owner = padAttribute(engine.getOwner(), ownerMaxLen);
            }
            if (panel.sortByValue.isSelected()) {
                value = padAttribute(engine.getValue(), Control.max_len_string_attibute);
            } else if (panel.sortByRfid.isSelected()) {
                rfid = padAttribute(engine.getRfid(), Control.max_len_string_attibute);
            } else {
                built = padAttribute(engine.getBuilt(), Control.max_len_string_built_name);
            }
            location = "";
            if (!engine.getLocationName().equals(Engine.NONE)) {
                location = engine.getLocationName() + " - " + engine.getTrackName();
            }
            String s = number + road + model + type + length + owner + consist + value + rfid + built + location;
            if (s.length() > numberCharPerLine) {
                s = s.substring(0, numberCharPerLine);
            }
            writer.write(s + NEW_LINE);
        }
    } catch (IOException we) {
        log.error("Error printing ConsistRosterEntry: " + e);
    }
    // and force completion of the printing
    writer.close();
}
Also used : IOException(java.io.IOException) HardcopyWriter(jmri.util.davidflanagan.HardcopyWriter) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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