while (currentMap.getTotalLayers() > 1) { try { currentMap.mergeLayerDown( currentMap.getTotalLayers() - 1); } catch (Exception ex) {}
if( JOptionPane.showConfirmDialog(appFrame, "Do you wish to merge tile images, and create a new tile set?", "Merge Tiles?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION ) { TileMergeHelper tmh = new TileMergeHelper(currentMap); int len = currentMap.getTotalLayers();  TileLayer newLayer = tmh.merge(0, len, true); currentMap.removeAllLayers(); currentMap.addLayer(newLayer); currentMap.addTileset(tmh.getSet()); } else {  while (currentMap.getTotalLayers() > 1) { try { currentMap.mergeLayerDown( currentMap.getTotalLayers() - 1); } catch (Exception ex) {} }
    private void doLayerStateChange(ActionEvent event) {        if (currentMap == null) {            return;        }        String command = event.getActionCommand();        Vector layersBefore = new Vector(currentMap.getLayerVector());        if (command.equals("Add Layer")) {            currentMap.addLayer();            setCurrentLayer(currentMap.getTotalLayers() - 1);        } else if (command.equals("Duplicate Layer")) {            if (currentLayer >= 0) {                try {                    MapLayer clone =                        (MapLayer)getCurrentLayer().clone();                    clone.setName(clone.getName() + " copy");                    currentMap.addLayer(clone);                } catch (CloneNotSupportedException ex) {                    ex.printStackTrace();                }                setCurrentLayer(currentMap.getTotalLayers() - 1);            }        } else if (command.equals("Move Layer Up")) {            if (currentLayer >= 0) {                try {                    currentMap.swapLayerUp(currentLayer);                    setCurrentLayer(currentLayer + 1);                } catch (Exception ex) {                    System.out.println(ex.toString());                }            }        } else if (command.equals("Move Layer Down")) {            if (currentLayer >= 0) {                try {                    currentMap.swapLayerDown(currentLayer);                    setCurrentLayer(currentLayer - 1);                } catch (Exception ex) {                    System.out.println(ex.toString());                }            }        } else if (command.equals("Delete Layer")) {            if (currentLayer >= 0) {                currentMap.removeLayer(currentLayer);                setCurrentLayer(currentLayer < 0 ? 0 : currentLayer);            }        } else if (command.equals("Merge Down")) {            if (currentLayer >= 0) {                try {                    currentMap.mergeLayerDown(currentLayer);                    setCurrentLayer(currentLayer - 1);                } catch (Exception ex) {                    System.out.println(ex.toString());                }            }        } else if (command.equals("Merge All")) {            while (currentMap.getTotalLayers() > 1) {                try {                    currentMap.mergeLayerDown(                            currentMap.getTotalLayers() - 1);                } catch (Exception ex) {}            }            setCurrentLayer(0);        }        undoSupport.postEdit(new MapLayerStateEdit(currentMap, layersBefore,                    new Vector(currentMap.getLayerVector()), command));    }