| 1 | /* Copyright 2014 Malcolm Herring
|
|---|
| 2 | *
|
|---|
| 3 | * This is free software: you can redistribute it and/or modify
|
|---|
| 4 | * it under the terms of the GNU General Public License as published by
|
|---|
| 5 | * the Free Software Foundation, version 3 of the License.
|
|---|
| 6 | *
|
|---|
| 7 | * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | package scedit;
|
|---|
| 11 |
|
|---|
| 12 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 13 |
|
|---|
| 14 | import java.awt.Color;
|
|---|
| 15 | import java.awt.Dimension;
|
|---|
| 16 | import java.awt.event.ActionEvent;
|
|---|
| 17 | import java.util.Map.Entry;
|
|---|
| 18 | import java.util.Set;
|
|---|
| 19 |
|
|---|
| 20 | import javax.swing.JFrame;
|
|---|
| 21 | import javax.swing.SwingUtilities;
|
|---|
| 22 | import javax.swing.WindowConstants;
|
|---|
| 23 |
|
|---|
| 24 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 25 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 26 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 31 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 32 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 35 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
|---|
| 36 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
|---|
| 37 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
|---|
| 38 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
|---|
| 39 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
|---|
| 40 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
|---|
| 41 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
|---|
| 42 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
|---|
| 43 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
|---|
| 44 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
|---|
| 45 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
|---|
| 46 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
|---|
| 47 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 48 |
|
|---|
| 49 | import panels.PanelMain;
|
|---|
| 50 | import panels.PanelS57;
|
|---|
| 51 | import panels.ShowFrame;
|
|---|
| 52 | import s57.S57map;
|
|---|
| 53 | import s57.S57map.Feature;
|
|---|
| 54 |
|
|---|
| 55 | public class SCeditAction extends JosmAction implements ActiveLayerChangeListener, DataSelectionListener {
|
|---|
| 56 | private static String title = tr("SeaChart Editor");
|
|---|
| 57 | public static JFrame editFrame = null;
|
|---|
| 58 | public static ShowFrame showFrame = null;
|
|---|
| 59 | private boolean isOpen = false;
|
|---|
| 60 | public static PanelMain panelMain = null;
|
|---|
| 61 | public static PanelS57 panelS57 = null;
|
|---|
| 62 | public static S57map map = null;
|
|---|
| 63 | public DataSet data = null;
|
|---|
| 64 |
|
|---|
| 65 | private final DataSetListener dataSetListener = new DataSetListener() {
|
|---|
| 66 |
|
|---|
| 67 | @Override
|
|---|
| 68 | public void dataChanged(DataChangedEvent e) {
|
|---|
| 69 | makeMap();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override
|
|---|
| 73 | public void nodeMoved(NodeMovedEvent e) {
|
|---|
| 74 | makeMap();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @Override
|
|---|
| 78 | public void otherDatasetChange(AbstractDatasetChangedEvent e) {
|
|---|
| 79 | makeMap();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | public void primitivesAdded(PrimitivesAddedEvent e) {
|
|---|
| 84 | makeMap();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @Override
|
|---|
| 88 | public void primitivesRemoved(PrimitivesRemovedEvent e) {
|
|---|
| 89 | makeMap();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | @Override
|
|---|
| 93 | public void relationMembersChanged(RelationMembersChangedEvent e) {
|
|---|
| 94 | makeMap();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public void tagsChanged(TagsChangedEvent e) {
|
|---|
| 99 | makeMap();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | @Override
|
|---|
| 103 | public void wayNodesChanged(WayNodesChangedEvent e) {
|
|---|
| 104 | makeMap();
|
|---|
| 105 | }
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | public SCeditAction() {
|
|---|
| 109 | super(title, "SC", title, null, true);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | @Override
|
|---|
| 113 | public void actionPerformed(ActionEvent arg0) {
|
|---|
| 114 | SwingUtilities.invokeLater(new Runnable() {
|
|---|
| 115 | @Override
|
|---|
| 116 | public void run() {
|
|---|
| 117 | if (!isOpen)
|
|---|
| 118 | createFrame();
|
|---|
| 119 | else
|
|---|
| 120 | editFrame.toFront();
|
|---|
| 121 | isOpen = true;
|
|---|
| 122 | }
|
|---|
| 123 | });
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | protected void createFrame() {
|
|---|
| 127 | editFrame = new JFrame(title);
|
|---|
| 128 | editFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
|---|
| 129 | editFrame.addWindowListener(new java.awt.event.WindowAdapter() {
|
|---|
| 130 | @Override
|
|---|
| 131 | public void windowClosing(java.awt.event.WindowEvent e) {
|
|---|
| 132 | closeDialog();
|
|---|
| 133 | }
|
|---|
| 134 | });
|
|---|
| 135 | editFrame.setSize(new Dimension(480, 480));
|
|---|
| 136 | editFrame.setLocation(100, 200);
|
|---|
| 137 | editFrame.setResizable(true);
|
|---|
| 138 | editFrame.setAlwaysOnTop(true);
|
|---|
| 139 | editFrame.setVisible(true);
|
|---|
| 140 | panelMain = new PanelMain();
|
|---|
| 141 | editFrame.add(panelMain);
|
|---|
| 142 |
|
|---|
| 143 | panelS57 = new PanelS57();
|
|---|
| 144 | editFrame.add(panelS57);
|
|---|
| 145 |
|
|---|
| 146 | showFrame = new ShowFrame(tr("Seamark Inspector"));
|
|---|
| 147 | showFrame.setSize(new Dimension(300, 300));
|
|---|
| 148 | showFrame.setLocation(50, 400);
|
|---|
| 149 | showFrame.setResizable(false);
|
|---|
| 150 | showFrame.setAlwaysOnTop(true);
|
|---|
| 151 | showFrame.setEnabled(true);
|
|---|
| 152 | showFrame.setVisible(false);
|
|---|
| 153 |
|
|---|
| 154 | getLayerManager().addAndFireActiveLayerChangeListener(this);
|
|---|
| 155 | SelectionEventManager.getInstance().addSelectionListener(this);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public void closeDialog() {
|
|---|
| 159 | if (isOpen) {
|
|---|
| 160 | getLayerManager().removeActiveLayerChangeListener(this);
|
|---|
| 161 | editFrame.setVisible(false);
|
|---|
| 162 | editFrame.dispose();
|
|---|
| 163 | data = null;
|
|---|
| 164 | map = null;
|
|---|
| 165 | }
|
|---|
| 166 | isOpen = false;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | @Override
|
|---|
| 170 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
|---|
| 171 | if (e.getPreviousDataLayer() != null) {
|
|---|
| 172 | e.getPreviousDataLayer().getDataSet().removeDataSetListener(dataSetListener);
|
|---|
| 173 | }
|
|---|
| 174 | OsmDataLayer newLayer = getLayerManager().getEditLayer();
|
|---|
| 175 | if (newLayer != null) {
|
|---|
| 176 | newLayer.getDataSet().addDataSetListener(dataSetListener);
|
|---|
| 177 | data = newLayer.getDataSet();
|
|---|
| 178 | makeMap();
|
|---|
| 179 | } else {
|
|---|
| 180 | data = null;
|
|---|
| 181 | map = null;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | @Override
|
|---|
| 186 | public void selectionChanged(SelectionChangeEvent event) {
|
|---|
| 187 | OsmPrimitive nextFeature = null;
|
|---|
| 188 | OsmPrimitive feature = null;
|
|---|
| 189 |
|
|---|
| 190 | showFrame.setVisible(false);
|
|---|
| 191 | panelMain.clearMark();
|
|---|
| 192 | if (map != null) {
|
|---|
| 193 | Set<OsmPrimitive> selection = event.getSelection();
|
|---|
| 194 | for (OsmPrimitive osm : selection) {
|
|---|
| 195 | nextFeature = osm;
|
|---|
| 196 | if (selection.size() == 1) {
|
|---|
| 197 | if (nextFeature.compareTo(feature) != 0) {
|
|---|
| 198 | feature = nextFeature;
|
|---|
| 199 | Feature id = map.index.get(feature.getUniqueId());
|
|---|
| 200 | if (id != null) {
|
|---|
| 201 | panelMain.parseMark(id);
|
|---|
| 202 | showFrame.setVisible(true);
|
|---|
| 203 | showFrame.showFeature(feature, map);
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | } else {
|
|---|
| 207 | showFrame.setVisible(false);
|
|---|
| 208 | PanelMain.setStatus(tr("Select only one object"), Color.orange);
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 | if (nextFeature == null) {
|
|---|
| 212 | feature = null;
|
|---|
| 213 | panelMain.clearMark();
|
|---|
| 214 | showFrame.setVisible(false);
|
|---|
| 215 | PanelMain.setStatus(tr("Select a map object"), Color.yellow);
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void makeMap() {
|
|---|
| 221 | map = new S57map(true);
|
|---|
| 222 | if (data != null) {
|
|---|
| 223 | double minlat = 90;
|
|---|
| 224 | double maxlat = -90;
|
|---|
| 225 | double minlon = 180;
|
|---|
| 226 | double maxlon = -180;
|
|---|
| 227 | for (Bounds bounds : data.getDataSourceBounds()) {
|
|---|
| 228 | if (bounds.getMinLat() < minlat) {
|
|---|
| 229 | minlat = bounds.getMinLat();
|
|---|
| 230 | }
|
|---|
| 231 | if (bounds.getMaxLat() > maxlat) {
|
|---|
| 232 | maxlat = bounds.getMaxLat();
|
|---|
| 233 | }
|
|---|
| 234 | if (bounds.getMinLon() < minlon) {
|
|---|
| 235 | minlon = bounds.getMinLon();
|
|---|
| 236 | }
|
|---|
| 237 | if (bounds.getMaxLon() > maxlon) {
|
|---|
| 238 | maxlon = bounds.getMaxLon();
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | map.addNode(1, maxlat, minlon);
|
|---|
| 242 | map.addNode(2, minlat, minlon);
|
|---|
| 243 | map.addNode(3, minlat, maxlon);
|
|---|
| 244 | map.addNode(4, maxlat, maxlon);
|
|---|
| 245 | map.bounds.minlat = Math.toRadians(minlat);
|
|---|
| 246 | map.bounds.maxlat = Math.toRadians(maxlat);
|
|---|
| 247 | map.bounds.minlon = Math.toRadians(minlon);
|
|---|
| 248 | map.bounds.maxlon = Math.toRadians(maxlon);
|
|---|
| 249 | for (Node node : data.getNodes()) {
|
|---|
| 250 | LatLon coor = node.getCoor();
|
|---|
| 251 | if (coor != null) {
|
|---|
| 252 | map.addNode(node.getUniqueId(), coor.lat(), coor.lon());
|
|---|
| 253 | for (Entry<String, String> entry : node.getKeys().entrySet()) {
|
|---|
| 254 | map.addTag(entry.getKey(), entry.getValue());
|
|---|
| 255 | }
|
|---|
| 256 | map.tagsDone(node.getUniqueId());
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | for (Way way : data.getWays()) {
|
|---|
| 260 | if (way.getNodesCount() > 0) {
|
|---|
| 261 | map.addEdge(way.getUniqueId());
|
|---|
| 262 | for (Node node : way.getNodes()) {
|
|---|
| 263 | map.addToEdge((node.getUniqueId()));
|
|---|
| 264 | }
|
|---|
| 265 | for (Entry<String, String> entry : way.getKeys().entrySet()) {
|
|---|
| 266 | map.addTag(entry.getKey(), entry.getValue());
|
|---|
| 267 | }
|
|---|
| 268 | map.tagsDone(way.getUniqueId());
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 | for (Relation rel : data.getRelations()) {
|
|---|
| 272 | if (rel.isMultipolygon() && (rel.getMembersCount() > 0)) {
|
|---|
| 273 | map.addArea(rel.getUniqueId());
|
|---|
| 274 | for (RelationMember mem : rel.getMembers()) {
|
|---|
| 275 | if (mem.getType() == OsmPrimitiveType.WAY)
|
|---|
| 276 | map.addToArea(mem.getUniqueId(), (mem.getRole().equals("outer")));
|
|---|
| 277 | }
|
|---|
| 278 | for (Entry<String, String> entry : rel.getKeys().entrySet()) {
|
|---|
| 279 | map.addTag(entry.getKey(), entry.getValue());
|
|---|
| 280 | }
|
|---|
| 281 | map.tagsDone(rel.getUniqueId());
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 | map.mapDone();
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|