Search in sources :

Example 1 with TimetableImage

use of net.parostroj.timetable.model.TimetableImage in project grafikon by jub77.

the class LSImage method createTimetableImage.

public TimetableImage createTimetableImage(TrainDiagram diagram) {
    String newId = this.id != null ? this.id : IdGenerator.getInstance().getId();
    TimetableImage image = diagram.getPartFactory().createImage(newId, filename, imageWidth, imageHeight);
    return image;
}
Also used : TimetableImage(net.parostroj.timetable.model.TimetableImage)

Example 2 with TimetableImage

use of net.parostroj.timetable.model.TimetableImage in project grafikon by jub77.

the class EditImagesDialog method deleteButtonActionPerformed.

private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
    TimetableImage selected = (TimetableImage) ((Wrapper<?>) imagesList.getSelectedValue()).getElement();
    diagram.getImages().remove(selected);
    listModel.removeIndex(imagesList.getSelectedIndex());
    // remove temp file
    if (!selected.getImageFile().delete()) {
        log.debug("Cannot remove temporary file: {}", selected.getImageFile().getPath());
    }
}
Also used : TimetableImage(net.parostroj.timetable.model.TimetableImage)

Example 3 with TimetableImage

use of net.parostroj.timetable.model.TimetableImage in project grafikon by jub77.

the class LoadSaveImages method loadTimetableImages.

/**
 * loads images for timetable.
 *
 * @param diagram train diagram
 * @param zipFile zip file
 * @throws java.io.IOException
 */
public void loadTimetableImages(TrainDiagram diagram, ZipFile zipFile) throws IOException {
    for (TimetableImage image : diagram.getImages()) {
        ZipEntry entry = zipFile.getEntry("images/" + image.getFilename());
        if (entry != null) {
            File tempFile = File.createTempFile("gt_", ".temp");
            InputStream is = zipFile.getInputStream(entry);
            ReadableByteChannel ic = Channels.newChannel(is);
            try (FileOutputStream os = new FileOutputStream(tempFile)) {
                FileChannel oc = os.getChannel();
                oc.transferFrom(ic, 0, entry.getSize());
            }
            image.setImageFile(tempFile);
            tempFile.deleteOnExit();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) TimetableImage(net.parostroj.timetable.model.TimetableImage) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 4 with TimetableImage

use of net.parostroj.timetable.model.TimetableImage in project grafikon by jub77.

the class LoadSaveImages method saveTimetableImages.

/**
 * saves images for timetable.
 *
 * @param diagram train diagram
 * @param os zip output stream
 * @throws java.io.IOException
 */
public void saveTimetableImages(TrainDiagram diagram, ZipOutputStream os) throws IOException {
    for (TimetableImage image : diagram.getImages()) {
        // copy image to zip
        ZipEntry entry = new ZipEntry("images/" + image.getFilename());
        if (image.getImageFile() == null)
            // skip images without image file
            continue;
        File imageFile = image.getImageFile();
        entry.setSize(imageFile.length());
        os.putNextEntry(entry);
        WritableByteChannel oc = Channels.newChannel(os);
        try (FileInputStream is = new FileInputStream(imageFile)) {
            FileChannel ic = is.getChannel();
            ic.transferTo(0, ic.size(), oc);
        }
    }
}
Also used : TimetableImage(net.parostroj.timetable.model.TimetableImage) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) WritableByteChannel(java.nio.channels.WritableByteChannel) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream)

Example 5 with TimetableImage

use of net.parostroj.timetable.model.TimetableImage in project grafikon by jub77.

the class FileLoadSaveImpl method save.

@Override
public void save(TrainDiagram diagram, ZipOutputStream zipOutput) throws LSException {
    try {
        // save metadata
        zipOutput.putNextEntry(new ZipEntry(METADATA));
        this.createMetadata(METADATA_KEY_MODEL_VERSION).store(zipOutput, null);
        FileLoadSaveAttachments attachments = new FileLoadSaveAttachments(DATA_ATTACHMENTS);
        // increase save version (increment by one)
        diagram.setSaveVersion(diagram.getSaveVersion() + 1);
        // update save timestamp
        diagram.setSaveTimestamp(new Date());
        // save train diagram
        this.save(zipOutput, DATA_TRAIN_DIAGRAM, new LSTrainDiagram(diagram));
        // save net
        this.save(zipOutput, DATA_NET, new LSNet(diagram.getNet()));
        int cnt = 0;
        // save train type categories
        for (TrainTypeCategory category : diagram.getTrainTypeCategories()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAIN_TYPE_CATEGORIES, "xml", cnt++), new LSTrainTypeCategory(category));
        }
        cnt = 0;
        // save routes
        for (Route route : diagram.getRoutes()) {
            this.save(zipOutput, this.createEntryName(DATA_ROUTES, "xml", cnt++), new LSRoute(route));
        }
        cnt = 0;
        // save train types
        for (TrainType trainType : diagram.getTrainTypes()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAIN_TYPES, "xml", cnt++), new LSTrainType(trainType));
        }
        cnt = 0;
        // save trains
        for (Train train : diagram.getTrains()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAINS, "xml", cnt++), new LSTrain(train));
        }
        cnt = 0;
        // save engine classes
        for (EngineClass engineClass : diagram.getEngineClasses()) {
            this.save(zipOutput, this.createEntryName(DATA_ENGINE_CLASSES, "xml", cnt++), new LSEngineClass(engineClass));
        }
        cnt = 0;
        // save text items
        for (TextItem item : diagram.getTextItems()) {
            this.save(zipOutput, this.createEntryName(DATA_TEXT_ITEMS, "xml", cnt++), new LSTextItem(item));
        }
        cnt = 0;
        // save output templates
        for (OutputTemplate template : diagram.getOutputTemplates()) {
            LSOutputTemplate lsOutputTemplate = Boolean.TRUE.equals(properties.get("inline.output.template.attachments")) ? new LSOutputTemplate(template) : new LSOutputTemplate(template, attachments);
            this.save(zipOutput, this.createEntryName(DATA_OUTPUT_TEMPLATES, "xml", cnt++), lsOutputTemplate);
        }
        cnt = 0;
        // save diagram change sets
        for (String version : diagram.getChangesTracker().getVersions()) {
            DiagramChangeSet set = diagram.getChangesTracker().getChangeSet(version);
            if (!set.getChanges().isEmpty())
                this.save(zipOutput, this.createEntryName(DATA_CHANGES, "xml", cnt++), new LSDiagramChangeSet(set));
        }
        cnt = 0;
        // save trains cycles
        for (TrainsCycle cycle : diagram.getCycles()) {
            this.save(zipOutput, this.createEntryName(DATA_TRAINS_CYCLES, "xml", cnt++), new LSTrainsCycle(cycle));
        }
        cnt = 0;
        // save outputs
        for (Output output : diagram.getOutputs()) {
            this.save(zipOutput, this.createEntryName(DATA_OUTPUTS, "xml", cnt++), new LSOutput(output));
        }
        // save images
        cnt = 0;
        FileLoadSaveImages saveImages = new FileLoadSaveImages(DATA_IMAGES);
        for (TimetableImage image : diagram.getImages()) {
            this.save(zipOutput, createEntryName(DATA_IMAGES, "xml", cnt++), new LSImage(image));
            saveImages.saveTimetableImage(image, zipOutput);
        }
        // save attachments
        attachments.save(zipOutput);
        // save freight net
        this.save(zipOutput, FREIGHT_NET, new LSFreightNet(diagram.getFreightNet()));
    } catch (IOException ex) {
        throw new LSException(ex);
    }
}
Also used : TrainsCycle(net.parostroj.timetable.model.TrainsCycle) TextItem(net.parostroj.timetable.model.TextItem) ZipEntry(java.util.zip.ZipEntry) Output(net.parostroj.timetable.model.Output) TrainTypeCategory(net.parostroj.timetable.model.TrainTypeCategory) TrainType(net.parostroj.timetable.model.TrainType) Route(net.parostroj.timetable.model.Route) TimetableImage(net.parostroj.timetable.model.TimetableImage) DiagramChangeSet(net.parostroj.timetable.model.changes.DiagramChangeSet) IOException(java.io.IOException) Date(java.util.Date) OutputTemplate(net.parostroj.timetable.model.OutputTemplate) EngineClass(net.parostroj.timetable.model.EngineClass) Train(net.parostroj.timetable.model.Train) LSException(net.parostroj.timetable.model.ls.LSException)

Aggregations

TimetableImage (net.parostroj.timetable.model.TimetableImage)10 ZipEntry (java.util.zip.ZipEntry)3 TrainDiagramPartFactory (net.parostroj.timetable.model.TrainDiagramPartFactory)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileChannel (java.nio.channels.FileChannel)2 ZipFile (java.util.zip.ZipFile)2 BufferedImage (java.awt.image.BufferedImage)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 Date (java.util.Date)1 JFileChooser (javax.swing.JFileChooser)1 Wrapper (net.parostroj.timetable.gui.wrappers.Wrapper)1 EngineClass (net.parostroj.timetable.model.EngineClass)1 Output (net.parostroj.timetable.model.Output)1 OutputTemplate (net.parostroj.timetable.model.OutputTemplate)1 Route (net.parostroj.timetable.model.Route)1