@Override public void mouseMoved(final MouseEvent e) { if (coords.size() > 2) { if (justCreated) { coords.remove(coords.size() - 1); coords.remove(coords.size() - 1); coords.add(helper.toCoord(e.getX(), e.getY())); coords.add(helper.toCoord(e.getX(), e.getY())); } else { coords.remove(coords.size() - 1); coords.add(helper.toCoord(e.getX(), e.getY())); } erase = EditionHelper.createPolygon(coords); final Geometry line = EditionHelper.createLinearRing(coords); geometry = (multipolygon) ? EditionHelper.createMultiPolygon(subGeometries) : subGeometries.get(0); try { geometry = geometry.difference(erase); } catch (TopologyException ex) { LOGGER.log(Level.INFO, ex.getLocalizedMessage()); } decoration.setGeometries(UnmodifiableArrayList.wrap(new Geometry[] {geometry, line})); return; } super.mouseMoved(e); }
@Override public void mouseClicked(final MouseEvent e) { final int button = e.getButton(); if (button == MouseEvent.BUTTON1) { if (geometry == null) { setCurrentFeature(helper.grabFeature(e.getX(), e.getY(), false)); } else { if (justCreated) { justCreated = false; // we must modify the second point since two point where added at the start coords.remove(2); coords.remove(1); coords.add(helper.toCoord(e.getX(), e.getY())); coords.add(helper.toCoord(e.getX(), e.getY())); } else if (coords.isEmpty()) { justCreated = true; // this is the first point of the geometry we create // add 3 points that will be used when moving the mouse around coords.add(helper.toCoord(e.getX(), e.getY())); coords.add(helper.toCoord(e.getX(), e.getY())); coords.add(helper.toCoord(e.getX(), e.getY())); } else { justCreated = false; coords.add(helper.toCoord(e.getX(), e.getY())); } erase = EditionHelper.createPolygon(coords); final Geometry line = EditionHelper.createLinearRing(coords); geometry = (multipolygon) ? EditionHelper.createMultiPolygon(subGeometries) : subGeometries.get(0); try { geometry = geometry.difference(erase); } catch (TopologyException ex) { LOGGER.log(Level.INFO, ex.getLocalizedMessage()); } decoration.setGeometries(UnmodifiableArrayList.wrap(new Geometry[] {geometry, line})); } } else if (button == MouseEvent.BUTTON3 && feature != null) { // finish editing the feature helper.sourceModifyFeature(feature, geometry, true); reset(); } }