20 package net.sf.gridarta.gui.dialog.browsearchetypes;
22 import java.io.BufferedWriter;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.WeakHashMap;
33 import java.util.regex.Pattern;
34 import javax.swing.table.AbstractTableModel;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import org.jetbrains.annotations.NotNull;
43 import org.jetbrains.annotations.Nullable;
61 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
92 private final List<String>
attributes =
new ArrayList<>();
98 private final List<Class<?>>
classes =
new ArrayList<>();
104 private final Map<String, Class<?>>
classMap =
new WeakHashMap<>();
117 return archetypes.size();
122 return 2 + attributes.size();
127 public Object
getValueAt(
final int rowIndex,
final int columnIndex) {
128 if (rowIndex < 0 || rowIndex >= archetypes.size()) {
132 if (columnIndex == 0) {
134 }
else if (columnIndex == 1) {
136 }
else if (columnIndex >= 0 && columnIndex < 2 + attributes.size()) {
137 final String attribute = attributes.get(columnIndex - 2);
139 final Class<?> attributeClass = classes.get(columnIndex - 2);
140 if (attributeClass == Long.class) {
141 if (value.isEmpty()) {
145 return Long.valueOf(value);
146 }
catch (
final NumberFormatException ignored) {
159 }
else if (column == 1) {
161 }
else if (column >= 0 && column < 2 + attributes.size()) {
162 return attributes.get(column - 2);
164 return super.getColumnName(column);
170 if (columnIndex < 2) {
172 }
else if (columnIndex >= 0 && columnIndex < 2 + attributes.size()) {
173 return classes.get(columnIndex - 2);
175 return super.getColumnClass(columnIndex);
186 boolean include =
false;
187 for (
final String attribute : attributes) {
188 if (!archetype.getAttributeString(attribute).isEmpty()) {
194 archetypes.add(archetype);
197 fireTableStructureChanged();
206 attributes.add(attribute);
217 final Class<?> existingClass = classMap.get(attribute);
218 if (existingClass != null) {
219 return existingClass;
223 final String value = archetype.getAttributeString(attribute);
224 if (value.isEmpty()) {
229 Long.parseLong(value);
230 }
catch (
final NumberFormatException ignored) {
231 classMap.put(attribute, String.class);
235 classMap.put(attribute, Long.class);
246 final String attribute;
248 attribute = attributes.remove(index - 2);
249 classes.remove(index - 2);
250 }
catch (
final IndexOutOfBoundsException ignored) {
263 @SuppressWarnings(
"TypeMayBeWeakened")
264 public R
get(final
int index) {
265 return archetypes.get(index);
273 public void saveAsCsv(@NotNull
final File file)
throws IOException {
274 try (OutputStream outputStream =
new FileOutputStream(file)) {
275 try (Writer writer =
new OutputStreamWriter(outputStream)) {
276 try (BufferedWriter bufferedWriter =
new BufferedWriter(writer)) {
279 for (
int column = 0; column < columns; column++) {
281 bufferedWriter.write(
",");
285 bufferedWriter.newLine();
286 for (
int row = 0; row < rows; row++) {
287 for (
int column = 0; column < columns; column++) {
289 bufferedWriter.write(
",");
292 writeCsvValue(bufferedWriter, value == null ?
"" : value.toString());
294 bufferedWriter.newLine();
307 private static void writeCsvValue(@NotNull
final Writer writer, @NotNull
final String value)
throws IOException {
308 if (PATTERN_PLAIN_CSV_VALUE.matcher(value).matches()) {
312 writer.write(PATTERN_QUOTE.matcher(value).replaceAll(
"\"\""));
Class<?> getColumnClass(final int columnIndex)
Object getValueAt(final int rowIndex, final int columnIndex)
static final Pattern PATTERN_PLAIN_CSV_VALUE
If a value written into a CSV file matches this regex, it will not be surrounded by " characters...
Collection< R > getArchetypes()
Returns a read-only collection of all Archetypes.
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet from which archetypes are shown.
void addAttribute(@NotNull final String attribute)
Adds a column for an attribute to the table.
BrowseArchetypesTableModel(@NotNull final ArchetypeSet< G, A, R > archetypeSet)
Creates a new instance.
static final long serialVersionUID
The serial version UID.
static final Pattern PATTERN_QUOTE
A Pattern that matches a single " character.
String getArchetypeName()
Returns the name of this archetype.
R get(final int index)
Return one archetype.
void saveAsCsv(@NotNull final File file)
Saves the current contents as a CSV file.
String getAttributeString(@NotNull String attributeName, boolean queryArchetype)
Returns the requested attribute value of this GameObject as String.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
A javax.swing.table.TableModel that displays a selected set of attributes within a list of archetypes...
Base package of all Gridarta classes.
final List< String > attributes
The shown attributes.
Reflects a game object (object on a map).
String getColumnName(final int column)
GameObjects are the objects based on Archetypes found on maps.
String getBestName()
Returns the name which is best appropriate to describe this GameObject.
Utility class for ActionBuilder related functions.
Class<?> getAttributeClass(@NotNull final String attribute)
Returns the class for rendering attribute values.
final List< R > archetypes
The shown archetypes.
static void writeCsvValue(@NotNull final Writer writer, @NotNull final String value)
Writes a value into a CSV value.
Interface that captures similarities between different ArchetypeSet implementations.
void update()
Updates archetypes to include all archetypes from archetypeSet that define at least one show attribut...
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
String removeAttribute(final int index)
Removes an attribute from the table.
Interface for MapArchObjects.
final Map< String, Class<?> > classMap
Caches mapping of attribute name to table column class.
final List< Class<?> > classes
The classes corresponding to attributes.