Search in sources :

Example 1 with MapViewEvent

use of aimax.osm.viewer.MapViewEvent in project aima-java by aimacode.

the class MapViewPopup method actionPerformed.

@Override
public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == entityInfoMenuItem) {
        MapNode mNode = pane.getRenderer().getNextNode(x, y);
        if (mNode != null)
            pane.showMapEntityInfoDialog(mNode, pane.isDebugModeEnabled());
    } else if (ae.getSource() == clearMenuItem) {
        pane.getMap().clearMarkersAndTracks();
    } else if (ae.getSource() == createMarkerMenuItem) {
        PositionPanel panel = new PositionPanel();
        int res = JOptionPane.showConfirmDialog(pane, panel, "Specify a Position", JOptionPane.OK_CANCEL_OPTION);
        if (res == JOptionPane.OK_OPTION) {
            float lat = panel.getLat();
            float lon = panel.getLon();
            if (!Float.isNaN(lat) && !Float.isNaN(lon)) {
                pane.getMap().addMarker(lat, lon);
                pane.adjustToCenter(lat, lon);
            }
        }
    } else if (ae.getSource() == removeMarkerMenuItem) {
        pane.removeNearestMarker(x, y);
    } else if (ae.getSource() == loadMarkersMenuItem) {
        XMLDecoder decoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Load Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
            }
            if (xmlFile != null && xmlFile.exists()) {
                pane.getMap().clearMarkersAndTracks();
                decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(xmlFile)));
                int size = (Integer) decoder.readObject();
                for (int i = 0; i < size; i++) {
                    WritablePosition pos = (WritablePosition) decoder.readObject();
                    pane.getMap().addMarker(pos.getLat(), pos.getLon());
                }
                pane.fireMapViewEvent(new MapViewEvent(pane, MapViewEvent.Type.MARKER_ADDED));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (decoder != null)
                decoder.close();
        }
    } else if (ae.getSource() == saveMarkersMenuItem) {
        XMLEncoder encoder = null;
        try {
            File xmlFile = null;
            if (getFileChooser().showDialog(pane, "Save Markers") == JFileChooser.APPROVE_OPTION) {
                xmlFile = getFileChooser().getSelectedFile();
                if (!xmlFile.getPath().contains("."))
                    xmlFile = new File(xmlFile.getPath() + ".xml");
                encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(xmlFile)));
                encoder.writeObject(pane.getMap().getMarkers().size());
                for (MapNode node : pane.getMap().getMarkers()) encoder.writeObject(new WritablePosition(node));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (encoder != null)
                encoder.close();
        }
    } else if (ae.getSource() == functionsMenuItem) {
        JOptionPane.showMessageDialog(pane, MapViewPane.FUNCTION_DESCRIPTION.split("\\|"), "Function Description", JOptionPane.INFORMATION_MESSAGE);
    } else if (ae.getSource() == debugMenuItem) {
        pane.enableDebugMode(debugMenuItem.isSelected());
    }
}
Also used : MapViewEvent(aimax.osm.viewer.MapViewEvent) MapNode(aimax.osm.data.entities.MapNode) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLEncoder(java.beans.XMLEncoder) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

MapNode (aimax.osm.data.entities.MapNode)1 MapViewEvent (aimax.osm.viewer.MapViewEvent)1 XMLDecoder (java.beans.XMLDecoder)1 XMLEncoder (java.beans.XMLEncoder)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1