Pastes the cut / copied places to layer, if possible @param x @param y @param layer @return false on error or user abort
(final int x, final int y, final Layer layer)
| 127 | * @return false on error or user abort |
| 128 | */ |
| 129 | public static boolean paste(final int x, final int y, final Layer layer) { |
| 130 | if (!canPaste(x, y, layer)) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // ask user |
| 135 | final String title = (copyMode ? "Copy " : "Paste ") + "place(s)"; |
| 136 | final String message = title + "? This can not be undone!" + (copyPlaces.iterator().next().getLayer().getWorld() != layer.getWorld() ? " Pasting to another world might cause problems!" : ""); |
| 137 | final int ret = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); |
| 138 | if (ret == JOptionPane.YES_OPTION) { |
| 139 | // map to translate from old to new place |
| 140 | final HashMap<Place, Place> place_to_new_place = new HashMap<Place, Place>(); |
| 141 | |
| 142 | Place[] places; |
| 143 | |
| 144 | if (copyMode) { |
| 145 | places = copyPlaces.toArray(new Place[copyPlaces.size()]); |
| 146 | } else { |
| 147 | // getPlace movement direction |
| 148 | final int fact_x = x <= copydx ? 1 : -1; |
| 149 | final int fact_y = y <= copydy ? 1 : -1; |
| 150 | |
| 151 | // sort places |
| 152 | final ArrayList<Place> ordered_places = new ArrayList<>(copyPlaces); |
| 153 | Collections.sort(ordered_places, new Comparator<Place>() { |
| 154 | @Override |
| 155 | public int compare(final Place t, final Place t1) { |
| 156 | // order by movement direction: |
| 157 | // places that might collide with other places in the |
| 158 | // list will be moved first |
| 159 | if (fact_x * t.getX() > fact_x * t1.getX()) { |
| 160 | return 1; |
| 161 | } else if (t.getX() == t1.getX()) { |
| 162 | if (fact_y * t.getY() > fact_y * t1.getY()) { |
| 163 | return 1; |
| 164 | } else if (t.getY() == t1.getY()) { |
| 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | return -1; |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | places = ordered_places.toArray(new Place[ordered_places.size()]); |
| 173 | } |
| 174 | |
| 175 | // copy places |
| 176 | for (final Place place : places) { |
| 177 | try { |
| 178 | if (place.getLayer().getWorld() != layer.getWorld()) { |
| 179 | if (place.getPlaceGroup() != null && !layer.getWorld().getPlaceGroups().contains(place.getPlaceGroup())) { |
| 180 | layer.getWorld().addPlaceGroup(place.getPlaceGroup()); |
| 181 | } |
| 182 | } |
| 183 | if (copyMode) { // copy places -> duplicate on new layer |
| 184 | final Place new_place = place.duplicate(); |
| 185 | place_to_new_place.put(place, new_place); |
| 186 | layer.put(new_place, place.getX() - copydx + x, place.getY() - copydy + y); |
no test coverage detected