Skip to content

Commit

Permalink
Merge pull request #4972 from bubblobill/tokenPropertiesEditorImprove…
Browse files Browse the repository at this point in the history
…ment

Changed behaviour of adding token property. Added JSON export
  • Loading branch information
cwisniew authored Oct 10, 2024
2 parents e095cf1 + 340f1e7 commit be5aa69
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/rptools/maptool/client/ui/MapToolFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -885,6 +886,10 @@ public JFileChooser getSavePropsFileChooser() {
savePropsFileChooser = new JFileChooser();
savePropsFileChooser.setCurrentDirectory(AppPreferences.saveDirectory.get());
savePropsFileChooser.addChoosableFileFilter(propertiesFilter);
savePropsFileChooser.setFileFilter(propertiesFilter);
savePropsFileChooser.setFileFilter(new FileNameExtensionFilter("JSON", "json"));
savePropsFileChooser.setFileFilter(new FileNameExtensionFilter("Text", "txt"));
savePropsFileChooser.setFileFilter(savePropsFileChooser.getChoosableFileFilters()[0]);
savePropsFileChooser.setDialogTitle(I18N.getText("msg.title.exportProperties"));
}
savePropsFileChooser.setAcceptAllFileFilterUsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import static org.apache.commons.text.WordUtils.capitalize;
import static org.apache.commons.text.WordUtils.uncapitalize;

import com.google.protobuf.util.JsonFormat;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -47,6 +49,7 @@
import net.rptools.maptool.model.LightSource;
import net.rptools.maptool.model.ShapeType;
import net.rptools.maptool.model.SightType;
import net.rptools.maptool.server.proto.CampaignPropertiesDto;
import net.rptools.maptool.util.LightSyntax;
import net.rptools.maptool.util.PersistenceUtil;
import net.rptools.maptool.util.SightSyntax;
Expand Down Expand Up @@ -171,7 +174,7 @@ private void initAddRepoButton() {
button.addActionListener(
e -> {
String newRepo = getNewServerTextField().getText();
if (newRepo == null || newRepo.length() == 0) {
if (newRepo == null || newRepo.isEmpty()) {
return;
}
// TODO: Check for uniqueness
Expand Down Expand Up @@ -415,6 +418,7 @@ private void initExportButton() {
// END HACK

JFileChooser chooser = MapTool.getFrame().getSavePropsFileChooser();

boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
Expand All @@ -440,8 +444,21 @@ private void initExportButton() {
}
}
try {
PersistenceUtil.saveCampaignProperties(campaign, chooser.getSelectedFile());
MapTool.showInformation("Properties Saved.");
if (selectedFile.getName().endsWith(".mtprops")) {
PersistenceUtil.saveCampaignProperties(campaign, chooser.getSelectedFile());
MapTool.showInformation("Properties Saved.");
} else {
MapTool.showMessage(
"CampaignPropertiesDialog.export.message",
"msg.title.exportProperties",
JOptionPane.INFORMATION_MESSAGE,
null);
CampaignPropertiesDto campaignPropertiesDto =
MapTool.getCampaign().getCampaignProperties().toDto();
FileOutputStream fos = new FileOutputStream(chooser.getSelectedFile());
fos.write(JsonFormat.printer().print(campaignPropertiesDto).getBytes());
}

} catch (IOException ioe) {
MapTool.showError("Could not save properties: ", ioe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,15 @@ public void initPropertyAddButton() {
finalizeCellEditing();
JTable propertiesTable = getTokenPropertiesTable();
var model = getTokenPropertiesTableModel();
model.addProperty();
// selected row is -1 for no selection causing property to be appended to list
// instead of inserted
int selectedRow = propertiesTable.getSelectedRow();
model.addProperty(selectedRow);
int count = model.getRowCount();
propertiesTable.scrollRectToVisible(
propertiesTable.getCellRect(count - 1, 0, true));
propertiesTable.getCellRect(
selectedRow == -1 ? count - 1 : selectedRow, 0, true));
propertiesTable.repaint();
}));
button.setEnabled(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}

/** Adds a new token property, with a generated name. */
public void addProperty() {
public void addProperty(int selectedRow) {
var properties = tokenTypeMap.get(tokenType);

// First find a unique name, there are so few entries we don't have to worry
Expand All @@ -160,7 +160,12 @@ public void addProperty() {

if (free) {
var prop = new TokenProperty(newName);
properties.add(prop);
// append if there is no selection, otherwise insert at selectedRow
if (selectedRow == -1) {
properties.add(prop);
} else {
properties.add(selectedRow, prop);
}
break;
}
seq++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ CampaignPropertiesDialog.button.importPredefined = Import Predefined
CampaignPropertiesDialog.button.importPredefined.tooltip = Import predefined properties and settings for the selected system
CampaignPropertiesDialog.button.import.tooltip = Import predefined properties from a file
CampaignPropertiesDialog.button.export.tooltip = Export campaign properties to file
CampaignPropertiesDialog.export.message = Campaign properties will be exported in JSON format and cannot be imported.
CampaignPropertiesDialog.combo.importPredefined.tooltip = System to import properties for
CampaignPropertiesDialog.button.import = Import Predefined
campaignPropertiesDialog.newTokenTypeName = Enter the name for the new token type
Expand Down

0 comments on commit be5aa69

Please sign in to comment.