use of org.pepsoft.worldpainter.plugins.CustomObjectManager in project WorldPainter by Captain-Chaos.
the class Bo2LayerEditor method reset.
@Override
public void reset() {
List<WPObject> objects = new ArrayList<>();
fieldName.setText(layer.getName());
selectedColour = layer.getColour();
List<File> files = layer.getFiles();
if (files != null) {
if (files.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Existing layer contains new style objects");
}
// New layer; files stored in object attributes
objects.addAll(layer.getObjectProvider().getAllObjects());
} else {
// Old layer; files stored separately
int missingFiles = 0;
CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
if ((files.size() == 1) && files.get(0).isDirectory()) {
logger.info("Existing custom object layer contains old style directory; migrating to new style");
File[] filesInDir = files.get(0).listFiles((FilenameFilter) CustomObjectManager.getInstance().getFileFilter());
// noinspection ConstantConditions // Cannot happen as we already checked that files.get(0) is an extant directory
for (File file : filesInDir) {
try {
objects.add(customObjectManager.loadObject(file));
} catch (IOException e) {
logger.error("I/O error while trying to load custom object " + file, e);
missingFiles++;
}
}
} else {
logger.info("Existing custom object layer contains old style file list; migrating to new style");
for (File file : files) {
if (file.exists()) {
try {
objects.add(customObjectManager.loadObject(file));
} catch (IOException e) {
logger.error("I/O error while trying to load custom object " + file, e);
missingFiles++;
}
} else {
missingFiles++;
}
}
}
if (missingFiles > 0) {
JOptionPane.showMessageDialog(this, "This is an old custom object layer and " + missingFiles + " objects\ncould NOT be restored because they were missing or\nreading them resulted in an I/O error.\n\nYou will have to re-add these objects before\nsaving the settings, otherwise the existing object\ndata will be gone. You may also cancel the dialog\nwithout affecting the object data.", "Missing Files", JOptionPane.WARNING_MESSAGE);
}
}
} else {
logger.info("Existing custom object layer contains very old style objects with no file information; migrating to new style");
// Very old layer; no file information at all
objects.addAll(layer.getObjectProvider().getAllObjects());
}
listModel.clear();
for (WPObject object : objects) {
listModel.addElement(object.clone());
}
spinnerBlocksPerAttempt.setValue(layer.getDensity());
setLabelColour();
refreshLeafDecaySettings();
settingsChanged();
}
use of org.pepsoft.worldpainter.plugins.CustomObjectManager in project WorldPainter by Captain-Chaos.
the class Bo2LayerEditor method reloadObjects.
private void reloadObjects() {
StringBuilder noFiles = new StringBuilder();
StringBuilder notFound = new StringBuilder();
StringBuilder errors = new StringBuilder();
int[] indices;
if (listObjects.getSelectedIndex() != -1) {
indices = listObjects.getSelectedIndices();
} else {
indices = new int[listModel.getSize()];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}
}
CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
for (int index : indices) {
WPObject object = (WPObject) listModel.getElementAt(index);
File file = object.getAttribute(ATTRIBUTE_FILE);
if (file != null) {
if (file.isFile() && file.canRead()) {
try {
Map<String, Serializable> existingAttributes = object.getAttributes();
object = customObjectManager.loadObject(file);
if (existingAttributes != null) {
Map<String, Serializable> attributes = object.getAttributes();
if (attributes == null) {
attributes = new HashMap<>();
}
attributes.putAll(existingAttributes);
object.setAttributes(attributes);
}
listModel.setElementAt(object, index);
} catch (IOException e) {
logger.error("I/O error while reloading " + file, e);
errors.append(file.getPath()).append('\n');
}
} else {
notFound.append(file.getPath()).append('\n');
}
} else {
noFiles.append(object.getName()).append('\n');
}
}
if ((noFiles.length() > 0) || (notFound.length() > 0)) {
StringBuilder message = new StringBuilder();
message.append("Not all files could be reloaded!\n");
if (noFiles.length() > 0) {
message.append("\nThe following objects came from an old layer and have no filename stored:\n");
message.append(noFiles);
}
if (notFound.length() > 0) {
message.append("\nThe following files were missing or not accessible:\n");
message.append(notFound);
}
JOptionPane.showMessageDialog(this, message, "Not All Files Reloaded", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, indices.length + " objects successfully reloaded", "Success", JOptionPane.INFORMATION_MESSAGE);
}
refreshLeafDecaySettings();
}
use of org.pepsoft.worldpainter.plugins.CustomObjectManager in project WorldPainter by Captain-Chaos.
the class Bo2ObjectTube method load.
/**
* Create a new <code>Bo2ObjectTube</code> with a specific name from a list
* of specific custom object files.
*
* @param name The name of the new <code>Bo2ObjectTube</code>.
* @param files The list of files containing the objects to load.
* @return A new <code>Bo2ObjectTube</code> containing the custom objects
* from the specified file(s).
* @throws IOException If there was an I/O error reading one of the files.
*/
public static Bo2ObjectTube load(String name, Collection<File> files) throws IOException {
if (files.isEmpty()) {
throw new IllegalArgumentException("Cannot create an object tube with no objects");
}
List<WPObject> objects = new ArrayList<>(files.size());
CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
for (File file : files) {
objects.add(customObjectManager.loadObject(file));
}
return new Bo2ObjectTube(name, objects);
}
use of org.pepsoft.worldpainter.plugins.CustomObjectManager in project WorldPainter by Captain-Chaos.
the class Bo2LayerEditor method addFilesOrDirectory.
private void addFilesOrDirectory() {
// Can't use FileUtils.selectFilesForOpen() because it doesn't support
// selecting directories, or adding custom components to the dialog
JFileChooser fileChooser = new JFileChooser();
Configuration config = Configuration.getInstance();
if ((config.getCustomObjectsDirectory() != null) && config.getCustomObjectsDirectory().isDirectory()) {
fileChooser.setCurrentDirectory(config.getCustomObjectsDirectory());
}
fileChooser.setDialogTitle("Select File(s) or Directory");
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
CustomObjectManager.UniversalFileFilter fileFilter = customObjectManager.getFileFilter();
fileChooser.setFileFilter(fileFilter);
WPObjectPreviewer previewer = new WPObjectPreviewer();
previewer.setDimension(App.getInstance().getDimension());
fileChooser.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, previewer);
fileChooser.setAccessory(previewer);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File[] selectedFiles = fileChooser.getSelectedFiles();
if (selectedFiles.length > 0) {
config.setCustomObjectsDirectory(selectedFiles[0].getParentFile());
for (File selectedFile : selectedFiles) {
if (selectedFile.isDirectory()) {
if (fieldName.getText().isEmpty()) {
String name = selectedFiles[0].getName();
if (name.length() > 12) {
name = "..." + name.substring(name.length() - 10);
}
fieldName.setText(name);
}
File[] files = selectedFile.listFiles((FilenameFilter) fileFilter);
// noinspection ConstantConditions // Cannot happen as we already checked selectedFile is an extant directory
if (files.length == 0) {
JOptionPane.showMessageDialog(this, "Directory " + selectedFile.getName() + " does not contain any supported custom object files.", "No Custom Object Files", JOptionPane.ERROR_MESSAGE);
} else {
for (File file : files) {
try {
listModel.addElement(customObjectManager.loadObject(file));
} catch (IOException e) {
logger.error("I/O error while trying to load custom object " + file, e);
JOptionPane.showMessageDialog(this, "I/O error while loading " + file.getName() + "; it was not added", "I/O Error", JOptionPane.ERROR_MESSAGE);
}
}
}
} else {
if (fieldName.getText().isEmpty()) {
String name = selectedFile.getName();
int p = name.lastIndexOf('.');
if (p != -1) {
name = name.substring(0, p);
}
if (name.length() > 12) {
name = "..." + name.substring(name.length() - 10);
}
fieldName.setText(name);
}
try {
listModel.addElement(customObjectManager.loadObject(selectedFile));
} catch (IOException e) {
logger.error("I/O error while trying to load custom object " + selectedFile, e);
JOptionPane.showMessageDialog(this, "I/O error while loading " + selectedFile.getName() + "; it was not added", "I/O Error", JOptionPane.ERROR_MESSAGE);
}
}
}
settingsChanged();
refreshLeafDecaySettings();
}
}
}
Aggregations