Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions messages/src/main/proto/data_transfer_objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ message LookupTableDto {
bool visible = 5;
bool allow_lookup = 6;
bool pick_once = 7;
google.protobuf.StringValue group = 8;
google.protobuf.StringValue metadata = 9;
google.protobuf.StringValue metadata_type = 10;
}

message LookupEntryDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.MapTool;
Expand All @@ -45,6 +46,7 @@ public LookupTableFunction() {
"tblImage",
"tableImage",
"getTableNames",
"getTableGroups",
"getTableRoll",
"setTableRoll",
"clearTable",
Expand All @@ -67,7 +69,11 @@ public LookupTableFunction() {
"resetTablePicks",
"getTablePickOnce",
"setTablePickOnce",
"getTablePicksLeft");
"getTablePicksLeft",
"getTableGroup",
"setTableGroup",
"getTableMetadata",
"setTableMetadata");
}

/** The singleton instance. */
Expand All @@ -88,18 +94,39 @@ public Object childEvaluate(
throws ParserException {

if ("getTableNames".equalsIgnoreCase(function)) {
/*
getTableNames(delim) - get all/visible table names
getTableNames(delim, group) - get all/visible table names in a named group
*/
FunctionUtil.checkNumberParam("getTableNames", params, 0, 2);
String delim = ",";
String group = null;
if (params.size() > 0) {
delim = params.get(0).toString();
}
if (params.size() > 1) {
group = params.get(1).toString();
}
if ("json".equalsIgnoreCase(delim)) {
JsonArray jsonArray = new JsonArray();
getTableList(MapTool.getPlayer().isGM(), group).forEach(jsonArray::add);
return jsonArray;
}
return StringUtils.join(getTableList(MapTool.getPlayer().isGM(), group), delim);

} else if ("getTableGroups".equalsIgnoreCase(function)) {

FunctionUtil.checkNumberParam("getTableNames", params, 0, 1);
FunctionUtil.checkNumberParam("getTableGroups", params, 0, 1);
String delim = ",";
if (params.size() > 0) {
delim = params.get(0).toString();
}
if ("json".equalsIgnoreCase(delim)) {
JsonArray jsonArray = new JsonArray();
getTableList(MapTool.getPlayer().isGM()).forEach(jsonArray::add);
getTableGroupList(MapTool.getPlayer().isGM()).forEach(jsonArray::add);
return jsonArray;
}
return StringUtils.join(getTableList(MapTool.getPlayer().isGM()), delim);
return StringUtils.join(getTableGroupList(MapTool.getPlayer().isGM()), delim);

} else if ("getTableVisible".equalsIgnoreCase(function)) {

Expand Down Expand Up @@ -468,6 +495,52 @@ public Object childEvaluate(
LookupTable lookupTable = getMaptoolTable(name, function);
return lookupTable.getPicksLeft();

} else if ("getTableGroup".equalsIgnoreCase(function)) {
/*
* getTableGroup(tblName) - get the named table's group
*/
checkTrusted(function);
FunctionUtil.checkNumberParam("getTableGroup", params, 1, 1);
String name = params.get(0).toString();
LookupTable lookupTable = getMaptoolTable(name, function);
return lookupTable.getGroup();

} else if ("setTableGroup".equalsIgnoreCase(function)) {
/*
* setTableGroup(tblName, group) - set the named table's group
*/
checkTrusted(function);
FunctionUtil.checkNumberParam("setTableGroup", params, 2, 2);
String name = params.get(0).toString();
String group = params.get(1).toString();
LookupTable lookupTable = getMaptoolTable(name, function);
lookupTable.setGroup(group);
processMutatedLookupTable(lookupTable, true);
return "";

} else if ("getTableMetadata".equalsIgnoreCase(function)) {
/*
* getTableMetadata(tblName) - get the named table's metadata
*/
checkTrusted(function);
FunctionUtil.checkNumberParam("getTableMetadata", params, 1, 1);
String name = params.get(0).toString();
LookupTable lookupTable = getMaptoolTable(name, function);
return lookupTable.getMetadata();

} else if ("setTableMetadata".equalsIgnoreCase(function)) {
/*
* setTableMetadata(tblName, metadata) - set the named table's metadata
*/
checkTrusted(function);
FunctionUtil.checkNumberParam("setTableMetadata", params, 2, 2);
String name = params.get(0).toString();
String metadata = params.get(1).toString();
LookupTable lookupTable = getMaptoolTable(name, function);
lookupTable.setMetadata(metadata);
processMutatedLookupTable(lookupTable, true);
return "";

} else { // if tbl, table, tblImage or tableImage
FunctionUtil.checkNumberParam(function, params, 1, 3);
String name = params.get(0).toString();
Expand Down Expand Up @@ -570,21 +643,47 @@ private void processMutatedLookupTable(LookupTable lookupTable, boolean dataOnly
}

/**
* * If GM return all tables Otherwise only return visible tables
* * If GM return all tables otherwise only return visible tables, optionally filtered by table
* group
*
* @param isGm boolean Does the calling function has GM privileges
* @param isGm boolean Does the calling function has GM privilege
* @param group String Only return tables in the group
* @return a list of table names
*/
private List<String> getTableList(boolean isGm) {
private List<String> getTableList(boolean isGm, String group) {
List<String> tables = new ArrayList<>();
if (isGm) tables.addAll(MapTool.getCampaign().getLookupTableMap().keySet());
else
MapTool.getCampaign().getLookupTableMap().values().stream()
.filter(LookupTable::getVisible)
.forEachOrdered((lt) -> tables.add(lt.getName()));
var stream = MapTool.getCampaign().getLookupTableMap().values().stream();
if (!isGm) {
stream = stream.filter(LookupTable::getVisible);
}
if (group != null) {
stream = stream.filter(lt -> lt.getGroup().equals(group));
}
stream.forEachOrdered((lt) -> tables.add(lt.getName()));
return tables;
}

/**
* If GM return all table groups otherwise only return groups for visible tables
*
* @param isGm does the calling function has GM privileges
* @return a list of table groups
*/
private List<String> getTableGroupList(boolean isGm) {
return MapTool.getCampaign().getLookupTableMap().values().stream()
// If not GM, only keep visible tables
.filter(lt -> isGm || lt.getVisible())
// Get the group name from the table
.map(LookupTable::getGroup)
// Remove any null groups
.filter(Objects::nonNull)
// Remove duplicate groups
.distinct()
// Sort groups
.sorted()
.collect(Collectors.toList());
}

/**
* Function to return a MapTool table.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.AppConstants;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.MapToolUtil;
import net.rptools.maptool.client.swing.AbeillePanel;
Expand All @@ -48,6 +50,10 @@
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.LookupTable;
import net.rptools.maptool.model.LookupTable.LookupEntry;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rtextarea.RTextScrollPane;

public class EditLookupTablePanel extends AbeillePanel<LookupTable> {
private static final int PICKED_COLUMN_INDEX = 0;
Expand Down Expand Up @@ -99,6 +105,7 @@ public void showDialog(@Nullable LookupTable lookupTable, boolean isNew) {
.setIcon(RessourceManager.getSmallIcon(Icons.TABLEPANEL_TABLE_PLAYER_LOOKUP));
view.getPickOnceIcon().setIcon(RessourceManager.getSmallIcon(Icons.TABLEPANEL_TABLE_PICK_ONCE));

loadTableGroups();
bind(lookupTable);

dialogFactory.display();
Expand Down Expand Up @@ -188,11 +195,73 @@ public void initTableImage() {
replaceComponent(view.getTableImagePlaceholder(), tableImageAssetPanel);
}

public void initMetadataSyntaxArea() {

// configure the syntax text area
RSyntaxTextArea rsta = view.getTableMetadata();
rsta.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
rsta.setCodeFoldingEnabled(true);
rsta.setEditable(true);
rsta.setInsertPairedCharacters(false);
rsta.setLineWrap(true);
rsta.setTabSize(4);
rsta.setWrapStyleWord(true);
rsta.setUseFocusableTips(false);

RTextScrollPane rtsp = view.getTableMetadataScrollPane();
rtsp.setLineNumbersEnabled(true);

// theme the syntax text area
Path themePath =
AppConstants.THEMES_DIR
.toPath()
.resolve(AppPreferences.defaultMacroEditorTheme.get() + ".xml");
try (InputStream in = Files.newInputStream(themePath)) {
Theme.load(in).apply(rsta);
} catch (IOException e) {
System.err.println("Unable to load theme: " + themePath + " " + e);
}

JComboBox<String> syntaxStyle = view.getTableMetadataType();
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_NONE);
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_CSV);
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_HTML);
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_JSON);
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_MARKDOWN);
syntaxStyle.addItem(SyntaxConstants.SYNTAX_STYLE_XML);

syntaxStyle.addActionListener(
e -> {
rsta.setSyntaxEditingStyle((String) syntaxStyle.getSelectedItem());
});
}

/**
* Populate the Table Groups ComboBox with groups already used. Should be outside an {@code init*}
* method as needs to respond to group changes.
*/
public void loadTableGroups() {
view.getTableGroup().removeAllItems();

MapTool.getCampaign().getLookupTableMap().values().stream()
// Get the group name from the table
.map(LookupTable::getGroup)
// Remove any null groups
.filter(Objects::nonNull)
// Remove duplicate groups
.distinct()
// Sort groups
.sorted()
// Add to the ComboBox
.forEach(group -> view.getTableGroup().addItem(group));
}

@Override
public void bind(LookupTable lookupTable) {
super.bind(lookupTable);

view.getTableName().setText(lookupTable.getName());
view.getTableGroup().setSelectedItem(lookupTable.getGroup());
view.getDefaultTableRoll()
.setText(lookupTable.getPickOnce() ? "" : lookupTable.calculateRoll());
tableImageAssetPanel.setImageId(lookupTable.getTableImage());
Expand All @@ -203,6 +272,12 @@ public void bind(LookupTable lookupTable) {
view.getDefaultTableRoll().setEnabled(!lookupTable.getPickOnce());
view.getResetPicks().setEnabled(lookupTable.getPickOnce());

view.getTableMetadata().setText(lookupTable.getMetadata());
view.getTableMetadataType().setSelectedItem(lookupTable.getMetadataType());
if (view.getTableMetadataType().getSelectedItem() == null) {
view.getTableMetadataType().setSelectedItem(SyntaxConstants.SYNTAX_STYLE_NONE);
}

view.getTableName().requestFocusInWindow();

var model = new LookupTableTableModel();
Expand Down Expand Up @@ -241,6 +316,14 @@ public boolean commit() {
}
var isPickOnce = view.getPickOnce().isSelected();

// Get the selected or entered table group from the combo box
Object selectedTableGroup = view.getTableGroup().getSelectedItem();
String group = selectedTableGroup == null ? "" : selectedTableGroup.toString();

String metadata = view.getTableMetadata().getText().trim();
String metadataType =
Objects.requireNonNull(view.getTableMetadataType().getSelectedItem()).toString();

// Before modifying the table, parse and validate all the entries to avoid partial modification
// in case of error.
var entries = new ArrayList<LookupTable.LookupEntry>();
Expand Down Expand Up @@ -284,11 +367,14 @@ public boolean commit() {
// save existing name for later removal from LookupTableMap
String origname = lookupTable.getName();
lookupTable.setName(name);
lookupTable.setGroup(group);
lookupTable.setPickOnce(isPickOnce);
lookupTable.setRoll(isPickOnce ? null : view.getDefaultTableRoll().getText());
lookupTable.setTableImage(tableImageAssetPanel.getImageId());
lookupTable.setVisible(view.getIsVisible().isSelected());
lookupTable.setAllowLookup(view.getAllowLookup().isSelected());
lookupTable.setMetadata(metadata);
lookupTable.setMetadataType(metadataType);

lookupTable.clearEntries();
for (var entry : entries) {
Expand Down
Loading
Loading