Gridarta Editor
TableModel.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.findarchetypes;
21 
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Comparator;
25 import java.util.List;
26 import javax.swing.table.AbstractTableModel;
32 import net.sf.japi.swing.action.ActionBuilder;
33 import net.sf.japi.swing.action.ActionBuilderFactory;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
48 public class TableModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractTableModel {
49 
53  private static final long serialVersionUID = 1L;
54 
58  @NotNull
59  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
60 
64  @NotNull
65  private static final String[] COLUMN_NAME = { ActionBuilderUtils.getString(ACTION_BUILDER, "findArchetypesColumnName"), ActionBuilderUtils.getString(ACTION_BUILDER, "findArchetypesColumnArch"), ActionBuilderUtils.getString(ACTION_BUILDER, "findArchetypesColumnType"), ActionBuilderUtils.getString(ACTION_BUILDER, "findArchetypesColumnFolder"), };
66 
70  @NotNull
72 
77  @NotNull
78  private final List<R> archetypes = new ArrayList<>();
79 
84  @Nullable
85  private Integer[] sorting;
86 
92  @NotNull
93  private final Comparator<Integer> comparator = new Comparator<Integer>() {
94 
95  @Override
96  public int compare(final Integer o1, final Integer o2) {
97  final Archetype<G, A, R> archetype1 = archetypes.get(o1);
98  final Archetype<G, A, R> archetype2 = archetypes.get(o2);
99 
100  final int cmpBestName = archetype1.getBestName().compareToIgnoreCase(archetype2.getBestName());
101  if (cmpBestName != 0) {
102  return cmpBestName;
103  }
104 
105  final int cmpArchetypeName = archetype1.getArchetypeName().compareToIgnoreCase(archetype2.getArchetypeName());
106  if (cmpArchetypeName != 0) {
107  return cmpArchetypeName;
108  }
109 
110  return archetype1.getArchetypeName().compareTo(archetype2.getArchetypeName());
111  }
112 
113  };
114 
119  public TableModel(@NotNull final ArchetypeTypeSet archetypeTypeSet) {
120  this.archetypeTypeSet = archetypeTypeSet;
121  }
122 
126  public void clear() {
127  archetypes.clear();
128  sorting = null;
129  }
130 
135  public void add(@NotNull final R archetype) {
136  archetypes.add(archetype);
137  }
138 
144  @NotNull
145  @SuppressWarnings("TypeMayBeWeakened")
146  public R get(final int index) {
147  return archetypes.get(sorting[index]);
148  }
149 
150  @Override
151  public int getColumnCount() {
152  return 4;
153  }
154 
155  @Override
156  public int getRowCount() {
157  return archetypes.size();
158  }
159 
160  @NotNull
161  @Override
162  public Object getValueAt(final int rowIndex, final int columnIndex) {
163  final Archetype<G, A, R> archetype = archetypes.get(sorting[rowIndex]);
164  switch (columnIndex) {
165  case 0:
166  return archetype.getBestName();
167 
168  case 1:
169  return archetype.getArchetypeName();
170 
171  case 2:
172  return archetypeTypeSet.getDisplayName(archetype);
173 
174  case 3:
175  return archetype.getEditorFolder();
176  }
177 
178  throw new IllegalArgumentException();
179  }
180 
181  @NotNull
182  @Override
183  public String getColumnName(final int column) {
184  try {
185  return COLUMN_NAME[column];
186  } catch (final ArrayIndexOutOfBoundsException ignored) {
187  return super.getColumnName(column);
188  }
189  }
190 
194  public void sortTable() {
195  sorting = new Integer[archetypes.size()];
196  for (int i = 0; i < sorting.length; i++) {
197  sorting[i] = i;
198  }
199 
200  Arrays.sort(sorting, comparator);
201  }
202 
209  public void finishUpdate() {
210  if (sorting == null && !archetypes.isEmpty()) {
211  throw new IllegalStateException();
212  }
213 
214  fireTableDataChanged();
215  }
216 
223  public int findTableIndex(@Nullable final R archetype) {
224  if (archetype != null) {
225  final int archetypeIndex = archetypes.indexOf(archetype);
226  if (archetypeIndex != -1) {
227  for (final Integer index : sorting) {
228  if (archetypeIndex == sorting[index]) {
229  return index;
230  }
231  }
232  }
233  }
234 
235  if (!archetypes.isEmpty()) {
236  return 0;
237  }
238 
239  return -1;
240  }
241 
242 }
void sortTable()
Sort the table contents by name.
Manages ArchetypeType instances, list, and bitmask definitions.
R get(final int index)
Return one archetype.
The model for displaying a set of archetypes.
Definition: TableModel.java:48
void clear()
Clear the model&#39;s contents.
String getArchetypeName()
Returns the name of this archetype.
String getEditorFolder()
Returns the editor folder.
final Comparator< Integer > comparator
Comparator for sorting the model&#39;s contents.
Definition: TableModel.java:93
void add(@NotNull final R archetype)
Add an archetype to the model.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
static final String [] COLUMN_NAME
The column titles.
Definition: TableModel.java:65
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: TableModel.java:59
GameObjects are the objects based on Archetypes found on maps.
Object getValueAt(final int rowIndex, final int columnIndex)
String getBestName()
Returns the name which is best appropriate to describe this GameObject.
final List< R > archetypes
The model&#39;s contents.
Definition: TableModel.java:78
static final long serialVersionUID
The serial version UID.
Definition: TableModel.java:53
void finishUpdate()
Finish updating the model&#39;s contents.
Utility class for ActionBuilder related functions.
Integer [] sorting
Reflects the current sorting of the model&#39;s contents.
Definition: TableModel.java:85
TableModel(@NotNull final ArchetypeTypeSet archetypeTypeSet)
Creates a new instance.
final ArchetypeTypeSet archetypeTypeSet
The instance for looking up archetype types.
Definition: TableModel.java:71
int findTableIndex(@Nullable final R archetype)
Return the row index of an archetype.
String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns a description of this type.
Defines types of GameObjects with corresponding attributes.