Package slash.navigation.converter.gui.dnd

Source Code of slash.navigation.converter.gui.dnd.PositionSelection

/*
    This file is part of RouteConverter.

    RouteConverter is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    RouteConverter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with RouteConverter; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Copyright (C) 2007 Christian Pesch. All Rights Reserved.
*/

package slash.navigation.converter.gui.dnd;

import slash.navigation.base.BaseNavigationFormat;
import slash.navigation.base.BaseNavigationPosition;
import slash.navigation.common.NavigationPosition;
import slash.navigation.base.SimpleRoute;
import slash.navigation.simple.GlopusFormat;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import static slash.navigation.base.NavigationFormats.asFormatForPositions;
import static slash.navigation.base.RouteCharacteristics.Waypoints;

/**
* Acts as a container for drag and drop operations with {@link BaseNavigationPosition}s.
*
* @author Christian Pesch
*/

public class PositionSelection implements Transferable {
    private static final Logger log = Logger.getLogger(PositionSelection.class.getName());
    public static final DataFlavor positionFlavor = new DataFlavor(PositionSelection.class, "List of Positions");
    public static final DataFlavor stringFlavor = DataFlavor.stringFlavor;

    private final List<NavigationPosition> positions;
    private final BaseNavigationFormat format;
    private final String string;

    public PositionSelection(List<NavigationPosition> positions, BaseNavigationFormat format) {
        this.positions = positions;
        this.format = format;
        this.string = createStringFor(positions);
    }

    private String createStringFor(List<NavigationPosition> sourcePositions) {
        GlopusFormat targetFormat = new GlopusFormat();
        List<BaseNavigationPosition> targetPositions = new ArrayList<BaseNavigationPosition>();
        try {
            targetPositions = asFormatForPositions(sourcePositions, targetFormat);
        } catch (IOException e) {
            log.severe("Cannot convert " + sourcePositions + " for selection: " + e);
        }
        SimpleRoute targetRoute = targetFormat.createRoute(Waypoints, null, targetPositions);

        StringWriter writer = new StringWriter();
        targetFormat.write(targetRoute, new PrintWriter(writer), 0, targetPositions.size());
        return writer.toString();
    }

    public List<NavigationPosition> getPositions() {
        return positions;
    }

    public BaseNavigationFormat getFormat() {
        return format;
    }

    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{positionFlavor, stringFlavor};
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        for (DataFlavor f : getTransferDataFlavors()) {
            if (f.equals(flavor)) {
                return true;
            }
        }
        return false;
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (positionFlavor.equals(flavor))
            return this;
        if (stringFlavor.equals(flavor))
            return string;
        throw new UnsupportedFlavorException(flavor);
    }
}
TOP

Related Classes of slash.navigation.converter.gui.dnd.PositionSelection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.