use of org.pepsoft.util.swing.ProgressTask in project WorldPainter by Captain-Chaos.
the class App method exportImage.
private void exportImage() {
final Set<String> extensions = new HashSet<>(Arrays.asList(ImageIO.getReaderFileSuffixes()));
StringBuilder sb = new StringBuilder(strings.getString("supported.image.formats"));
sb.append(" (");
boolean first = true;
for (String extension : extensions) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append("*.");
sb.append(extension);
}
sb.append(')');
final String description = sb.toString();
// NOI18N
String defaultname = world.getName().replaceAll("\\s", "").toLowerCase() + ((dimension.getDim() == DIM_NORMAL) ? "" : ("_" + dimension.getName().toLowerCase())) + ".png";
File selectedFile = FileUtils.selectFileForSave(App.this, "Export as image file", new File(defaultname), new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String filename = f.getName();
int p = filename.lastIndexOf('.');
if (p != -1) {
String extension = filename.substring(p + 1).toLowerCase();
return extensions.contains(extension);
} else {
return false;
}
}
@Override
public String getDescription() {
return description;
}
});
if (selectedFile != null) {
final String type;
int p = selectedFile.getName().lastIndexOf('.');
if (p != -1) {
type = selectedFile.getName().substring(p + 1).toUpperCase();
} else {
// NOI18N
type = "PNG";
selectedFile = new File(selectedFile.getParentFile(), selectedFile.getName() + ".png");
}
if (selectedFile.exists()) {
if (showConfirmDialog(App.this, strings.getString("the.file.already.exists"), strings.getString("overwrite.file"), YES_NO_OPTION) != YES_OPTION) {
return;
}
}
final File file = selectedFile;
// noinspection ConstantConditions // Can't happen for non-cancelable task
if (!ProgressDialog.executeTask(App.this, new ProgressTask<Boolean>() {
@Override
public String getName() {
return strings.getString("exporting.image");
}
@Override
public Boolean execute(ProgressReceiver progressReceiver) throws OperationCancelled {
// the file, and we can't report progress for that
try {
return ImageIO.write(view.getImage(), type, file);
} catch (IOException e) {
throw new RuntimeException("I/O error while exporting image", e);
}
}
}, false)) {
showMessageDialog(App.this, MessageFormat.format(strings.getString("format.0.not.supported"), type));
}
}
}
use of org.pepsoft.util.swing.ProgressTask in project WorldPainter by Captain-Chaos.
the class MapImportDialog method importWorld.
private void importWorld() {
final File levelDatFile = new File(fieldFilename.getText());
final Set<Point> chunksToSkip = checkBoxImportOutliers.isSelected() ? null : mapStatistics.outlyingChunks;
final JavaMapImporter.ReadOnlyOption readOnlyOption;
if (radioButtonReadOnlyAll.isSelected()) {
readOnlyOption = JavaMapImporter.ReadOnlyOption.ALL;
} else if (radioButtonReadOnlyManMade.isSelected()) {
readOnlyOption = JavaMapImporter.ReadOnlyOption.MAN_MADE;
} else if (radioButtonReadOnlyManMadeAboveGround.isSelected()) {
readOnlyOption = JavaMapImporter.ReadOnlyOption.MAN_MADE_ABOVE_GROUND;
} else {
readOnlyOption = JavaMapImporter.ReadOnlyOption.NONE;
}
app.setWorld(null);
importedWorld = ProgressDialog.executeTask(this, new ProgressTask<World2>() {
@Override
public String getName() {
return strings.getString("importing.world");
}
@Override
public World2 execute(ProgressReceiver progressReceiver) throws OperationCancelled {
try {
Level level = Level.load(levelDatFile);
int maxHeight = level.getMaxHeight();
int waterLevel;
if (level.getVersion() == SUPPORTED_VERSION_1) {
waterLevel = maxHeight / 2 - 2;
} else {
waterLevel = 62;
}
int terrainLevel = waterLevel - 4;
TileFactory tileFactory = TileFactoryFactory.createNoiseTileFactory(0, Terrain.GRASS, maxHeight, terrainLevel, waterLevel, false, true, 20, 1.0);
Set<Integer> dimensionsToImport = new HashSet<>(3);
dimensionsToImport.add(Constants.DIM_NORMAL);
if (checkBoxImportNether.isSelected()) {
dimensionsToImport.add(Constants.DIM_NETHER);
}
if (checkBoxImportEnd.isSelected()) {
dimensionsToImport.add(Constants.DIM_END);
}
final JavaMapImporter importer = new JavaMapImporter(tileFactory, levelDatFile, false, chunksToSkip, readOnlyOption, dimensionsToImport);
World2 world = importer.doImport(progressReceiver);
if (importer.getWarnings() != null) {
try {
SwingUtilities.invokeAndWait(() -> {
Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon");
Toolkit.getDefaultToolkit().beep();
int selectedOption = JOptionPane.showOptionDialog(MapImportDialog.this, strings.getString("the.import.process.generated.warnings"), strings.getString("import.warnings"), JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, warningIcon, new Object[] { strings.getString("review.warnings"), strings.getString("ok") }, null);
if (selectedOption == 0) {
ImportWarningsDialog warningsDialog = new ImportWarningsDialog(MapImportDialog.this, strings.getString("import.warnings"));
warningsDialog.setWarnings(importer.getWarnings());
warningsDialog.setVisible(true);
}
});
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return world;
} catch (IOException e) {
throw new RuntimeException("I/O error while importing world", e);
}
}
}, true);
if (importedWorld == null) {
// The import was cancelled
cancel();
return;
}
importedWorld.setDirty(false);
Configuration config = Configuration.getInstance();
config.setSavesDirectory(levelDatFile.getParentFile().getParentFile());
ok();
}
use of org.pepsoft.util.swing.ProgressTask in project WorldPainter by Captain-Chaos.
the class MapImportDialog method analyseMap.
private void analyseMap() {
mapStatistics = null;
resetStats();
File levelDatFile = new File(fieldFilename.getText());
final File worldDir = levelDatFile.getParentFile();
// Check if it's a valid level.dat file before we commit
int version;
try {
Level testLevel = Level.load(levelDatFile);
version = testLevel.getVersion();
} catch (IOException e) {
logger.error("IOException while analysing map " + levelDatFile, e);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
return;
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException while analysing map " + levelDatFile, e);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
return;
} catch (NullPointerException e) {
logger.error("NullPointerException while analysing map " + levelDatFile, e);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
return;
}
// Other sanity checks
if ((version != SUPPORTED_VERSION_1) && (version != SUPPORTED_VERSION_2)) {
logger.error("Unsupported Minecraft version while analysing map " + levelDatFile);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("unsupported.minecraft.version"), strings.getString("unsupported.version"), JOptionPane.ERROR_MESSAGE);
return;
}
File regionDir = new File(worldDir, "region");
if (!regionDir.isDirectory()) {
logger.error("Region directory missing while analysing map " + levelDatFile);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("the.region.folder.is.missing"), strings.getString("region.folder.missing"), JOptionPane.ERROR_MESSAGE);
return;
}
final Pattern regionFilePattern = (version == SUPPORTED_VERSION_1) ? Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mcr") : Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mca");
final File[] regionFiles = regionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
if ((regionFiles == null) || (regionFiles.length == 0)) {
logger.error("Region files missing while analysing map " + levelDatFile);
JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("the.region.folder.contains.no.region.files"), strings.getString("region.files.missing"), JOptionPane.ERROR_MESSAGE);
return;
}
// Check for Nether and End
boolean netherPresent = false, endPresent = false;
File netherRegionDir = new File(worldDir, "DIM-1/region");
if (netherRegionDir.isDirectory()) {
File[] netherRegionFiles = netherRegionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
if ((netherRegionFiles != null) && (netherRegionFiles.length > 0)) {
netherPresent = true;
}
}
File endRegionDir = new File(worldDir, "DIM1/region");
if (endRegionDir.isDirectory()) {
File[] endRegionFiles = endRegionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
if ((endRegionFiles != null) && (endRegionFiles.length > 0)) {
endPresent = true;
}
}
checkBoxImportNether.setEnabled(netherPresent);
checkBoxImportNether.setSelected(netherPresent);
checkBoxImportEnd.setEnabled(endPresent);
checkBoxImportEnd.setSelected(endPresent);
mapStatistics = ProgressDialog.executeTask(this, new ProgressTask<MapStatistics>() {
@Override
public String getName() {
return "Analyzing map...";
}
@Override
public MapStatistics execute(ProgressReceiver progressReceiver) throws OperationCancelled {
MapStatistics stats = new MapStatistics();
int chunkCount = 0;
List<Integer> xValues = new ArrayList<>(), zValues = new ArrayList<>();
List<Point> chunks = new ArrayList<>();
int count = 0;
for (File file : regionFiles) {
String[] nameFrags = file.getName().split("\\.");
int regionX = Integer.parseInt(nameFrags[1]);
int regionZ = Integer.parseInt(nameFrags[2]);
try {
RegionFile regionFile = new RegionFile(file);
try {
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
if (regionFile.containsChunk(x, z)) {
chunkCount++;
int chunkX = regionX * 32 + x, chunkZ = regionZ * 32 + z;
if (chunkX < stats.lowestChunkX) {
stats.lowestChunkX = chunkX;
}
if (chunkX > stats.highestChunkX) {
stats.highestChunkX = chunkX;
}
if (chunkZ < stats.lowestChunkZ) {
stats.lowestChunkZ = chunkZ;
}
if (chunkZ > stats.highestChunkZ) {
stats.highestChunkZ = chunkZ;
}
xValues.add(chunkX);
zValues.add(chunkZ);
chunks.add(new Point(chunkX, chunkZ));
}
}
}
} finally {
regionFile.close();
}
} catch (IOException e) {
throw new RuntimeException("I/O error while analyzing map " + worldDir, e);
}
count++;
progressReceiver.setProgress((float) count / (regionFiles.length + 1));
}
stats.chunkCount = chunkCount;
if (chunkCount == 0) {
// Completely empty map (wrong region file format)?
progressReceiver.setProgress(1.0f);
return stats;
}
Collections.sort(xValues);
int p1 = xValues.size() / 4;
float q1 = xValues.get(p1) * 0.75f + xValues.get(p1 + 1) * 0.25f;
int p2 = xValues.size() / 2;
float q2 = (xValues.get(p2) + xValues.get(p2 + 1)) / 2f;
int p3 = xValues.size() * 3 / 4;
float q3 = xValues.get(p3) * 0.25f + xValues.get(p3 + 1) * 0.75f;
float iqr = q3 - q1;
int lowerLimit = (int) (q2 - iqr * 1.5f);
int upperLimit = (int) (q2 + iqr * 1.5f);
for (Point chunk : chunks) {
if ((chunk.x < lowerLimit) || (chunk.x > upperLimit)) {
stats.outlyingChunks.add(chunk);
}
}
Collections.sort(zValues);
p1 = zValues.size() / 4;
q1 = zValues.get(p1) * 0.75f + zValues.get(p1 + 1) * 0.25f;
p2 = zValues.size() / 2;
q2 = (zValues.get(p2) + zValues.get(p2 + 1)) / 2f;
p3 = zValues.size() * 3 / 4;
q3 = zValues.get(p3) * 0.25f + zValues.get(p3 + 1) * 0.75f;
iqr = q3 - q1;
lowerLimit = (int) (q2 - iqr * 1.5f);
upperLimit = (int) (q2 + iqr * 1.5f);
for (Point chunk : chunks) {
if ((chunk.y < lowerLimit) || (chunk.y > upperLimit)) {
stats.outlyingChunks.add(chunk);
}
}
if (!stats.outlyingChunks.isEmpty()) {
chunks.stream().filter(chunk -> !stats.outlyingChunks.contains(chunk)).forEach(chunk -> {
if (chunk.x < stats.lowestChunkXNoOutliers) {
stats.lowestChunkXNoOutliers = chunk.x;
}
if (chunk.x > stats.highestChunkXNoOutliers) {
stats.highestChunkXNoOutliers = chunk.x;
}
if (chunk.y < stats.lowestChunkZNoOutliers) {
stats.lowestChunkZNoOutliers = chunk.y;
}
if (chunk.y > stats.highestChunkZNoOutliers) {
stats.highestChunkZNoOutliers = chunk.y;
}
});
} else {
stats.lowestChunkXNoOutliers = stats.lowestChunkX;
stats.highestChunkXNoOutliers = stats.highestChunkX;
stats.lowestChunkZNoOutliers = stats.lowestChunkZ;
stats.highestChunkZNoOutliers = stats.highestChunkZ;
}
progressReceiver.setProgress(1.0f);
return stats;
}
}, true);
if ((mapStatistics != null) && (mapStatistics.chunkCount > 0)) {
int width = mapStatistics.highestChunkXNoOutliers - mapStatistics.lowestChunkXNoOutliers + 1;
int length = mapStatistics.highestChunkZNoOutliers - mapStatistics.lowestChunkZNoOutliers + 1;
int area = (mapStatistics.chunkCount - mapStatistics.outlyingChunks.size());
labelWidth.setText(FORMATTER.format(width * 16) + " blocks (from " + FORMATTER.format(mapStatistics.lowestChunkXNoOutliers << 4) + " to " + FORMATTER.format((mapStatistics.highestChunkXNoOutliers << 4) + 15) + "; " + FORMATTER.format(width) + " chunks)");
labelLength.setText(FORMATTER.format(length * 16) + " blocks (from " + FORMATTER.format(mapStatistics.lowestChunkZNoOutliers << 4) + " to " + FORMATTER.format((mapStatistics.highestChunkZNoOutliers << 4) + 15) + "; " + FORMATTER.format(length) + " chunks)");
labelArea.setText(FORMATTER.format(area * 256L) + " blocksĀ² (" + FORMATTER.format(area) + " chunks)");
if (!mapStatistics.outlyingChunks.isEmpty()) {
// There are outlying chunks
int widthWithOutliers = mapStatistics.highestChunkX - mapStatistics.lowestChunkX + 1;
int lengthWithOutliers = mapStatistics.highestChunkZ - mapStatistics.lowestChunkZ + 1;
int areaOfOutliers = mapStatistics.outlyingChunks.size();
labelOutliers1.setVisible(true);
labelOutliers2.setVisible(true);
labelWidthWithOutliers.setText(FORMATTER.format(widthWithOutliers * 16) + " blocks (" + FORMATTER.format(widthWithOutliers) + " chunks)");
labelWidthWithOutliers.setVisible(true);
labelOutliers3.setVisible(true);
labelLengthWithOutliers.setText(FORMATTER.format(lengthWithOutliers * 16) + " blocks (" + FORMATTER.format(lengthWithOutliers) + " chunks)");
labelLengthWithOutliers.setVisible(true);
labelOutliers4.setVisible(true);
labelAreaOutliers.setText(FORMATTER.format(areaOfOutliers * 256L) + " blocksĀ² (" + FORMATTER.format(areaOfOutliers) + " chunks)");
labelAreaOutliers.setVisible(true);
checkBoxImportOutliers.setVisible(true);
// The dialog may need to become bigger:
pack();
}
}
}
use of org.pepsoft.util.swing.ProgressTask in project WorldPainter by Captain-Chaos.
the class App method exportHeightMap.
private void exportHeightMap(boolean highRes) {
final Set<String> extensions = new HashSet<>(Arrays.asList(ImageIO.getReaderFileSuffixes()));
StringBuilder sb = new StringBuilder(strings.getString("supported.image.formats"));
sb.append(" (");
boolean first = true;
for (String extension : extensions) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append("*.");
sb.append(extension);
}
sb.append(')');
final String description = sb.toString();
// NOI18N
String defaultname = world.getName().replaceAll("\\s", "").toLowerCase() + ((dimension.getDim() == DIM_NORMAL) ? "" : ("_" + dimension.getName().toLowerCase())) + (highRes ? "_high-res-heightmap.png" : "_heightmap.png");
Configuration config = Configuration.getInstance();
File dir = config.getHeightMapsDirectory();
if ((dir == null) || (!dir.isDirectory())) {
dir = DesktopUtils.getImagesFolder();
}
File defaultFile = new File(dir, defaultname);
File selectedFile = FileUtils.selectFileForSave(App.this, highRes ? "Export as high resolution height map image file" : "Export as height map image file", defaultFile, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String filename = f.getName();
int p = filename.lastIndexOf('.');
if (p != -1) {
String extension = filename.substring(p + 1).toLowerCase();
return extensions.contains(extension);
} else {
return false;
}
}
@Override
public String getDescription() {
return description;
}
});
if (selectedFile != null) {
final String type;
int p = selectedFile.getName().lastIndexOf('.');
if (p != -1) {
type = selectedFile.getName().substring(p + 1).toUpperCase();
} else {
// NOI18N
type = "PNG";
selectedFile = new File(selectedFile.getParentFile(), selectedFile.getName() + ".png");
}
if (selectedFile.exists()) {
if (showConfirmDialog(App.this, strings.getString("the.file.already.exists"), strings.getString("overwrite.file"), YES_NO_OPTION) != YES_OPTION) {
return;
}
}
config.setHeightMapsDirectory(selectedFile.getParentFile());
final File file = selectedFile;
// noinspection ConstantConditions // Can't happen for non-cancelable task
if (!ProgressDialog.executeTask(App.this, new ProgressTask<Boolean>() {
@Override
public String getName() {
return strings.getString("exporting.height.map");
}
@Override
public Boolean execute(ProgressReceiver progressReceiver) throws OperationCancelled {
// the file, and we can't report progress for that
try {
BufferedImage image = new BufferedImage(dimension.getWidth() * TILE_SIZE, dimension.getHeight() * TILE_SIZE, ((dimension.getMaxHeight() <= 256) && (!highRes)) ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_USHORT_GRAY);
WritableRaster raster = image.getRaster();
for (Tile tile : dimension.getTiles()) {
int tileOffsetX = (tile.getX() - dimension.getLowestX()) * TILE_SIZE;
int tileOffsetY = (tile.getY() - dimension.getLowestY()) * TILE_SIZE;
if (highRes) {
for (int dx = 0; dx < TILE_SIZE; dx++) {
for (int dy = 0; dy < TILE_SIZE; dy++) {
raster.setSample(tileOffsetX + dx, tileOffsetY + dy, 0, tile.getRawHeight(dx, dy));
}
}
} else {
for (int dx = 0; dx < TILE_SIZE; dx++) {
for (int dy = 0; dy < TILE_SIZE; dy++) {
raster.setSample(tileOffsetX + dx, tileOffsetY + dy, 0, tile.getIntHeight(dx, dy));
}
}
}
}
return ImageIO.write(image, type, file);
} catch (IOException e) {
throw new RuntimeException("I/O error while exporting image", e);
}
}
}, false)) {
showMessageDialog(App.this, MessageFormat.format(strings.getString("format.0.not.supported"), type));
}
}
}
use of org.pepsoft.util.swing.ProgressTask in project WorldPainter by Captain-Chaos.
the class ExportProgressDialog method getTask.
@Override
protected ProgressTask<Map<Integer, ChunkFactory.Stats>> getTask() {
return new ProgressTask<Map<Integer, ChunkFactory.Stats>>() {
@Override
public String getName() {
return "Exporting world " + name;
}
@Override
public Map<Integer, ChunkFactory.Stats> execute(ProgressReceiver progressReceiver) throws OperationCancelled {
progressReceiver = new TaskbarProgressReceiver(App.getInstance(), progressReceiver);
progressReceiver.setMessage("Exporting world " + name);
WorldExporter exporter = PlatformManager.getInstance().getExporter(world);
try {
backupDir = exporter.selectBackupDir(new File(baseDir, FileUtils.sanitiseName(name)));
return exporter.export(baseDir, name, backupDir, progressReceiver);
} catch (IOException e) {
throw new RuntimeException("I/O error while exporting world", e);
}
}
};
}
Aggregations