20 package net.sf.gridarta.gui.dialog.findarchetypes;
22 import java.awt.Component;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Rectangle;
26 import java.awt.event.MouseAdapter;
27 import java.awt.event.MouseEvent;
28 import java.awt.event.MouseListener;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JOptionPane;
32 import javax.swing.JPanel;
33 import javax.swing.JScrollPane;
34 import javax.swing.JTable;
35 import javax.swing.JTextField;
36 import javax.swing.JViewport;
37 import javax.swing.ListSelectionModel;
38 import javax.swing.ScrollPaneConstants;
39 import javax.swing.WindowConstants;
40 import javax.swing.event.DocumentEvent;
41 import javax.swing.event.DocumentListener;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
44 import javax.swing.text.JTextComponent;
53 import net.
sf.japi.swing.action.ActionBuilder;
54 import net.
sf.japi.swing.action.ActionBuilderFactory;
55 import net.
sf.japi.swing.action.ActionMethod;
56 import org.jetbrains.annotations.NotNull;
57 import org.jetbrains.annotations.Nullable;
74 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
92 private final JTextComponent
nameField =
new JTextField();
138 final JPanel panel =
new JPanel(
new GridBagLayout());
140 SwingUtils.
addAction(nameField, ACTION_BUILDER.createAction(
false,
"findArchetypesScrollUp",
this));
141 SwingUtils.
addAction(nameField, ACTION_BUILDER.createAction(
false,
"findArchetypesScrollDown",
this));
142 SwingUtils.
addAction(nameField, ACTION_BUILDER.createAction(
false,
"findArchetypesFindArchetype",
this));
144 final DocumentListener documentListener =
new DocumentListener() {
147 public void changedUpdate(@NotNull
final DocumentEvent e) {
152 public void insertUpdate(@NotNull
final DocumentEvent e) {
157 public void removeUpdate(@NotNull
final DocumentEvent e) {
162 nameField.getDocument().addDocumentListener(documentListener);
165 resultTable =
new JTable(resultTableModel);
166 resultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
167 resultTable.setRowSelectionAllowed(
true);
168 resultTable.setColumnSelectionAllowed(
false);
169 final ListSelectionListener listSelectionListener =
new ListSelectionListener() {
172 public void valueChanged(
final ListSelectionEvent e) {
177 resultTable.getSelectionModel().addListSelectionListener(listSelectionListener);
179 final MouseListener mouseListener =
new MouseAdapter() {
182 public void mousePressed(
final MouseEvent e) {
184 if (e.getClickCount() > 1) {
190 resultTable.addMouseListener(mouseListener);
191 scrollPane =
new JScrollPane(resultTable);
192 scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
193 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
194 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
196 final GridBagConstraints gbc =
new GridBagConstraints();
202 gbc.fill = GridBagConstraints.HORIZONTAL;
204 panel.add(nameField, gbc);
208 gbc.fill = GridBagConstraints.BOTH;
210 panel.add(scrollPane, gbc);
212 final JButton searchButton =
new JButton(ACTION_BUILDER.createAction(
false,
"findArchetypesSearch",
this));
213 final JButton closeButton =
new JButton(ACTION_BUILDER.createAction(
false,
"findArchetypesClose",
this));
214 final JOptionPane optionPane =
new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null,
new JButton[] { searchButton, closeButton, }, nameField);
216 dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
217 dialog.setModal(
false);
218 dialog.setResizable(
true);
219 dialog.setSize(500, 250);
220 dialog.setLocationRelativeTo(parent);
227 scrollPane.getVerticalScrollBar().setValue(0);
229 if (dialog.isShowing()) {
232 nameField.setText(
"");
233 resultTable.clearSelection();
235 resultTableModel.
clear();
239 dialog.setVisible(
true);
242 nameField.selectAll();
243 nameField.requestFocusInWindow();
253 nameField.selectAll();
263 @Nullable
final R selectedArchetype;
265 resultTableModel.
clear();
266 final String name = nameField.getText();
267 if (name.length() <= 0 || name.equals(previousSearch)) {
273 previousSearch = name;
275 final CharSequence lowerCaseName = name.toLowerCase();
276 R exactArchetypeNameMatch = null;
277 R exactDisplayNameMatch = null;
278 for (
final R archetype : archetypeChooserControl) {
279 if (!archetype.isTail()) {
280 final String archetypeName = archetype.getArchetypeName();
281 final String objName = archetype.getObjName();
282 if (archetypeName.toLowerCase().contains(lowerCaseName) || objName.toLowerCase().contains(lowerCaseName)) {
283 if (exactArchetypeNameMatch != null) {
285 }
else if (archetypeName.equals(name)) {
286 exactArchetypeNameMatch = archetype;
287 }
else if (exactDisplayNameMatch != null) {
289 }
else if (objName.equals(name)) {
290 exactDisplayNameMatch = archetype;
292 resultTableModel.
add(archetype);
297 if (exactArchetypeNameMatch != null) {
298 selectedArchetype = exactArchetypeNameMatch;
299 }
else if (exactDisplayNameMatch != null) {
300 selectedArchetype = exactDisplayNameMatch;
302 selectedArchetype = null;
310 final int index = resultTableModel.
findTableIndex(selectedArchetype);
320 dialog.setVisible(
false);
328 final int index = resultTable.getSelectedRow();
333 final R archetype = resultTableModel.
get(index);
343 final int index = resultTable.getSelectedRow();
354 final int index = resultTable.getSelectedRow();
355 if (index != -1 && index + 1 < resultTableModel.
getRowCount()) {
368 nameField.selectAll();
376 resultTable.setRowSelectionInterval(index, index);
377 resultTable.scrollRectToVisible(
new Rectangle(resultTable.getCellRect(index, 0,
true)));
final JScrollPane scrollPane
The scroll pane which contains the search results table resultTable.
void sortTable()
Sort the table contents by name.
Dialog used to find archetypes.
void findArchetypesSearch()
Action method for "search" button.
Graphical User Interface of Gridarta.
void highlightSelectedEntry()
Highlights the selected row from resultTable in the insertion object chooser.
final ObjectChooser< G, A, R > objectChooser
The insertion object chooser to use when selecting search results.
Reading and writing of Atrinik maps.
Manages ArchetypeType instances, list, and bitmask definitions.
R get(final int index)
Return one archetype.
void doSearch(final boolean force)
Searches for the current name.
void clear()
Clear the model's contents.
void findArchetypesScrollDown()
Action method to scroll down one line in the result table.
void moveArchetypeChooserToFront()
Move the Archetype Chooser in front of the Pickmap Chooser.
Utility class for Swing related functions.
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.
static final int MINIMUM_AUTO_SEARCH_LENGTH
Do not perform an auto-search until the input name reaches this length.
void findArchetypesFindArchetype()
Action method to search for the archetype, ignoring MINIMUM_AUTO_SEARCH_LENGTH.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
void selectRow(final int index)
Selects one row in the result table.
final ArchetypeChooserControl< G, A, R > archetypeChooserControl
The archetype set to search.
GameObjects are the objects based on Archetypes found on maps.
String previousSearch
The previously processed search string.
void selectArchetype(@NotNull final R archetype)
Select an archetype.
final TableModel< G, A, R > resultTableModel
The table model for the search results table resultTable.
void finishUpdate()
Finish updating the model's contents.
Utility class for ActionBuilder related functions.
void show()
Shows the dialog.
final JTextComponent nameField
The name input field.
FindArchetypesDialog(@NotNull final Component parent, @NotNull final ArchetypeChooserControl< G, A, R > archetypeChooserControl, @NotNull final ObjectChooser< G, A, R > objectChooser, @NotNull final ArchetypeTypeSet archetypeTypeSet)
Creates a new instance.
final JTable resultTable
The search results table.
int findTableIndex(@Nullable final R archetype)
Return the row index of an archetype.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
Common base interface for ObjectChoosers.
void findArchetypesScrollUp()
Action method to scroll up one line in the result table.
void findArchetypesClose()
Action method for "close" button.
Defines types of GameObjects with corresponding attributes.
final JDialog dialog
The complete dialog.
That control of the archetype chooser.
Interface for MapArchObjects.
static final ActionBuilder ACTION_BUILDER
Action Builder.
static void addAction(@NotNull final JComponent textComponent, @NotNull final Action action)
Adds an accelerator key for a component.