use of org.pepsoft.worldpainter.objects.WPObject 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.objects.WPObject 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.objects.WPObject in project WorldPainter by Captain-Chaos.
the class Bo2LayerEditor method setLeavesNoDecay.
private void setLeavesNoDecay() {
for (Enumeration<WPObject> e = (Enumeration<WPObject>) listModel.elements(); e.hasMoreElements(); ) {
WPObject object = e.nextElement();
object.setAttribute(ATTRIBUTE_LEAF_DECAY_MODE, LEAF_DECAY_OFF);
}
refreshLeafDecaySettings();
}
use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.
the class Bo2LayerEditor method resetLeafDecay.
private void resetLeafDecay() {
for (Enumeration<WPObject> e = (Enumeration<WPObject>) listModel.elements(); e.hasMoreElements(); ) {
WPObject object = e.nextElement();
object.getAttributes().remove(ATTRIBUTE_LEAF_DECAY_MODE);
}
refreshLeafDecaySettings();
}
use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.
the class EditObjectAttributes method ok.
private void ok() {
boolean singleSelection = objects.size() == 1;
for (WPObject object : objects) {
if (singleSelection && (!fieldName.getText().trim().isEmpty())) {
object.setName(fieldName.getText().trim());
}
Map<String, Serializable> attributes = object.getAttributes();
if (attributes == null) {
attributes = new HashMap<>();
}
if (checkBoxFrequencyActive.isSelected()) {
int frequency = (Integer) spinnerFrequency.getValue();
if (frequency != 100) {
attributes.put(ATTRIBUTE_FREQUENCY.key, frequency);
} else {
attributes.remove(ATTRIBUTE_FREQUENCY.key);
}
}
Point3i offset = offsets.get(object);
if ((offset != null) && ((offset.x != 0) || (offset.y != 0) || (offset.z != 0))) {
attributes.put(ATTRIBUTE_OFFSET.key, offset);
} else {
attributes.remove(ATTRIBUTE_OFFSET.key);
}
if (!checkBoxRandomRotation.isMixed()) {
attributes.put(ATTRIBUTE_RANDOM_ROTATION.key, checkBoxRandomRotation.isSelected());
}
if (!checkBoxOnAir.isMixed()) {
attributes.put(ATTRIBUTE_NEEDS_FOUNDATION.key, !checkBoxOnAir.isSelected());
}
if (!checkBoxUnderLava.isMixed()) {
attributes.put(ATTRIBUTE_SPAWN_IN_LAVA.key, checkBoxUnderLava.isSelected());
}
if (!checkBoxUnderWater.isMixed()) {
attributes.put(ATTRIBUTE_SPAWN_IN_WATER.key, checkBoxUnderWater.isSelected());
}
if (!checkBoxOnSolidLand.isMixed()) {
attributes.put(ATTRIBUTE_SPAWN_ON_LAND.key, checkBoxOnSolidLand.isSelected());
}
if (!checkBoxOnWater.isMixed()) {
attributes.put(ATTRIBUTE_SPAWN_ON_WATER.key, checkBoxOnWater.isSelected());
}
if (!checkBoxOnLava.isMixed()) {
attributes.put(ATTRIBUTE_SPAWN_ON_LAVA.key, checkBoxOnLava.isSelected());
}
if (singleSelection || comboBoxCollisionMode.getSelectedIndex() > 0) {
attributes.put(ATTRIBUTE_COLLISION_MODE.key, comboBoxCollisionMode.getSelectedIndex() + (singleSelection ? 1 : 0));
}
if (singleSelection || comboBoxUndergroundMode.getSelectedIndex() > 0) {
attributes.put(ATTRIBUTE_UNDERGROUND_MODE.key, comboBoxUndergroundMode.getSelectedIndex() + (singleSelection ? 1 : 0));
}
if (singleSelection || comboBoxLeafDecayMode.getSelectedIndex() > 0) {
attributes.put(ATTRIBUTE_LEAF_DECAY_MODE.key, comboBoxLeafDecayMode.getSelectedIndex() + (singleSelection ? 1 : 0));
}
if (singleSelection) {
if (checkBoxReplace.isSelected()) {
attributes.put(ATTRIBUTE_REPLACE_WITH_AIR.key, new int[] { comboBoxReplaceBlockId.getSelectedIndex(), (Integer) spinnerReplaceData.getValue() });
} else {
attributes.remove(ATTRIBUTE_REPLACE_WITH_AIR.key);
}
}
if (!checkBoxExtendFoundation.isMixed()) {
attributes.put(ATTRIBUTE_EXTEND_FOUNDATION.key, checkBoxExtendFoundation.isSelected());
}
if (!attributes.isEmpty()) {
object.setAttributes(attributes);
} else {
object.setAttributes(null);
}
}
cancelled = false;
dispose();
}
Aggregations