use of org.pepsoft.worldpainter.importing.ImportCustomItemsDialog in project WorldPainter by Captain-Chaos.
the class App method importCustomItemsFromWorld.
private void importCustomItemsFromWorld(CustomItemsTreeModel.ItemType itemType) {
File dir;
Configuration config = Configuration.getInstance();
if (lastSelectedFile != null) {
dir = lastSelectedFile.getParentFile();
} else if ((config != null) && (config.getWorldDirectory() != null)) {
dir = config.getWorldDirectory();
} else {
dir = DesktopUtils.getDocumentsFolder();
}
File selectedFile = FileUtils.selectFileForOpen(this, "Select a WorldPainter world", dir, new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".world");
}
@Override
public String getDescription() {
return strings.getString("worldpainter.files.world");
}
});
if (selectedFile != null) {
if (!selectedFile.isFile()) {
if (logger.isDebugEnabled()) {
try {
logger.debug("Path not a file according to File.isFile(): \"" + selectedFile + "\" (directory: " + selectedFile.isDirectory() + "; length: " + selectedFile.length() + "; absolutePath: \"" + selectedFile.getAbsolutePath() + "\"; canonicalPath: \"" + selectedFile.getCanonicalPath() + "\")");
} catch (IOException e) {
logger.debug("Path not a file according to File.isFile(): \"" + selectedFile + "\" (directory: " + selectedFile.isDirectory() + "; length: " + selectedFile.length() + "; absolutePath: \"" + selectedFile.getAbsolutePath() + "\")");
logger.warn("I/O error while trying to report canonical path of file: \"" + selectedFile + "\"", e);
}
}
showMessageDialog(this, "The specified path does not exist or is not a file", "File Does Not Exist", ERROR_MESSAGE);
return;
}
if (!selectedFile.canRead()) {
showMessageDialog(this, "WorldPainter is not authorised to read the selected file", "Access Denied", ERROR_MESSAGE);
return;
}
final World2 selectedWorld = ProgressDialog.executeTask(this, new ProgressTask<World2>() {
@Override
public String getName() {
return strings.getString("loading.world");
}
@Override
public World2 execute(ProgressReceiver progressReceiver) throws OperationCancelled {
try {
WorldIO worldIO = new WorldIO();
worldIO.load(new FileInputStream(selectedFile));
World2 world = worldIO.getWorld();
return world;
} catch (UnloadableWorldException e) {
logger.error("Could not load world from file " + selectedFile, e);
if (e.getMetadata() != null) {
logMetadataAsError(e.getMetadata());
}
reportUnloadableWorldException(e);
return null;
} catch (IOException e) {
throw new RuntimeException("I/O error while loading world", e);
}
}
private void appendMetadata(StringBuilder sb, Map<String, Object> metadata) {
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
switch(entry.getKey()) {
case World2.METADATA_KEY_WP_VERSION:
sb.append("Saved with WorldPainter ").append(entry.getValue());
String build = (String) metadata.get(World2.METADATA_KEY_WP_BUILD);
if (build != null) {
sb.append(" (").append(build).append(')');
}
sb.append('\n');
break;
case World2.METADATA_KEY_TIMESTAMP:
sb.append("Saved on ").append(SimpleDateFormat.getDateTimeInstance().format((Date) entry.getValue())).append('\n');
break;
case World2.METADATA_KEY_PLUGINS:
String[][] plugins = (String[][]) entry.getValue();
for (String[] plugin : plugins) {
sb.append("Plugin: ").append(plugin[0]).append(" (").append(plugin[1]).append(")\n");
}
break;
}
}
}
private void logMetadataAsError(Map<String, Object> metadata) {
StringBuilder sb = new StringBuilder("Metadata from world file:\n");
appendMetadata(sb, metadata);
logger.error(sb.toString());
}
private void reportUnloadableWorldException(UnloadableWorldException e) {
try {
String text;
if (e.getMetadata() != null) {
StringBuilder sb = new StringBuilder("WorldPainter could not load the file. The cause may be one of:\n" + "\n" + "* The file is damaged or corrupted\n" + "* The file was created with a newer version of WorldPainter\n" + "* The file was created using WorldPainter plugins which you do not have\n" + "\n");
appendMetadata(sb, e.getMetadata());
text = sb.toString();
} else {
text = "WorldPainter could not load the file. The cause may be one of:\n" + "\n" + "* The file is not a WorldPainter world\n" + "* The file is damaged or corrupted\n" + "* The file was created with a newer version of WorldPainter\n" + "* The file was created using WorldPainter plugins which you do not have";
}
SwingUtilities.invokeAndWait(() -> showMessageDialog(App.this, text, strings.getString("file.damaged"), ERROR_MESSAGE));
} catch (InterruptedException e2) {
throw new RuntimeException("Thread interrupted while reporting unloadable file " + selectedFile, e2);
} catch (InvocationTargetException e2) {
throw new RuntimeException("Invocation target exception while reporting unloadable file " + selectedFile, e2);
}
}
}, false);
if (selectedWorld == null) {
// The file was damaged
return;
}
if (CustomItemsTreeModel.hasCustomItems(selectedWorld, itemType)) {
ImportCustomItemsDialog dialog = new ImportCustomItemsDialog(this, selectedWorld, selectedColourScheme, itemType);
dialog.setVisible(true);
if (!dialog.isCancelled()) {
StringBuilder errors = new StringBuilder();
Set<CustomLayer> existingCustomLayers = null;
boolean refreshView = false, showError = false;
for (Object selectedItem : dialog.getSelectedItems()) {
if (selectedItem instanceof CustomLayer) {
if (existingCustomLayers == null) {
existingCustomLayers = getCustomLayers();
}
if (existingCustomLayers.contains(selectedItem)) {
errors.append("Custom Layer \"" + ((CustomLayer) selectedItem).getName() + "\" already exists\n");
} else {
registerCustomLayer((CustomLayer) selectedItem, false);
}
} else if (selectedItem instanceof MixedMaterial) {
MixedMaterial customMaterial = (MixedMaterial) selectedItem;
int index = findNextCustomTerrainIndex();
if (index == -1) {
errors.append("No free slots for Custom Terrain \"" + customMaterial.getName() + "\"\n");
showError = true;
continue;
}
customMaterial = MixedMaterialManager.getInstance().register(customMaterial);
addButtonForNewCustomTerrain(index, customMaterial, false);
} else if (selectedItem instanceof CustomBiome) {
CustomBiome customBiome = (CustomBiome) selectedItem;
if (!customBiomeManager.addCustomBiome(null, customBiome)) {
errors.append("ID already in use for Custom Biome " + customBiome.getId() + " (\"" + customBiome + "\")\n");
showError = true;
} else {
refreshView = true;
}
} else {
throw new InternalError("Unsupported custom item type " + selectedItem.getClass() + " encountered");
}
}
if (refreshView) {
view.refreshTiles();
}
if (errors.length() > 0) {
JOptionPane.showMessageDialog(App.this, "Not all items have been imported:\n\n" + errors, "Not All Items Imported", showError ? JOptionPane.ERROR_MESSAGE : JOptionPane.WARNING_MESSAGE);
}
}
} else {
String what;
switch(itemType) {
case ALL:
what = "layers, terrains or biomes";
break;
case BIOME:
what = "biomes";
break;
case LAYER:
what = "layers";
break;
case TERRAIN:
what = "terrains";
break;
default:
throw new InternalError();
}
showMessageDialog(this, "The selected world has no custom " + what + ".", "No Custom Items", WARNING_MESSAGE);
}
}
}
Aggregations