20 package net.sf.gridarta.gui.dialog.find;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.FlowLayout;
25 import java.awt.Point;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import javax.swing.AbstractButton;
30 import javax.swing.BorderFactory;
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JCheckBox;
35 import javax.swing.JDialog;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39 import javax.swing.WindowConstants;
40 import javax.swing.text.JTextComponent;
58 import net.
sf.japi.swing.action.ActionBuilder;
59 import net.
sf.japi.swing.action.ActionBuilderFactory;
60 import net.
sf.japi.swing.action.ActionMethod;
61 import org.jetbrains.annotations.NotNull;
78 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
107 private final JTextComponent
findInput =
new JTextField(20);
150 dialog = createDialog(parent,
"");
151 dialog.setModal(
false);
152 dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
170 final JPanel mainPanel =
new JPanel();
171 mainPanel.setLayout(
new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
172 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 2, 5));
174 final Container lineFind =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
176 lineFind.add(Box.createVerticalStrut(3));
177 lineFind.add(findInput);
178 lineFind.add(Box.createVerticalStrut(3));
179 mainPanel.add(lineFind);
181 final Container lineWhere =
new JPanel(
new FlowLayout(FlowLayout.LEFT));
183 lineWhere.add(Box.createVerticalStrut(5));
185 final JPanel panelWhere =
new JPanel();
186 panelWhere.setLayout(
new BoxLayout(panelWhere, BoxLayout.Y_AXIS));
187 panelWhere.add(findNameCheckbox);
188 panelWhere.add(findArchCheckbox);
189 panelWhere.add(findMsgCheckbox);
190 panelWhere.add(findFaceCheckbox);
191 panelWhere.add(findSlayingCheckbox);
192 panelWhere.add(findOtherCheckbox);
193 lineWhere.add(panelWhere);
194 mainPanel.add(lineWhere);
196 final JButton okButton =
new JButton(ACTION_BUILDER.createAction(
false,
"findOk",
this));
197 final JButton cancelButton =
new JButton(ACTION_BUILDER.createAction(
false,
"findCancel",
this));
199 findNameCheckbox.setSelected(
true);
200 findMsgCheckbox.setSelected(
true);
201 findSlayingCheckbox.setSelected(
true);
203 setMessage(mainPanel);
204 setOptions(
new Object[] { okButton, cancelButton });
205 dialog.getRootPane().setDefaultButton(okButton);
207 dialog.setLocationRelativeTo(parent);
211 dialog.setTitle(
ActionBuilderUtils.
format(ACTION_BUILDER,
"findTitle", mapView.getMapControl().getMapModel().getMapArchObject().getMapName()));
212 dialog.setVisible(
true);
213 findInput.requestFocusInWindow();
225 if (this.mapView != mapView) {
240 dialog.setVisible(
false);
249 dialog.setVisible(
false);
258 private boolean doFind(
final boolean forward) {
259 final String findString = findInput.getText().trim();
260 final Collection<MatchCriteria<G, A, R>> matchCriterias =
new ArrayList<>();
261 if (findNameCheckbox.isSelected()) {
264 if (findArchCheckbox.isSelected()) {
267 if (findMsgCheckbox.isSelected()) {
270 if (findFaceCheckbox.isSelected()) {
274 if (findSlayingCheckbox.isSelected()) {
277 if (findOtherCheckbox.isSelected()) {
280 return !matchCriterias.isEmpty() &&
doFind(matchCriterias, forward) > 0;
291 final List<G> matchingGameObjects =
new ArrayList<>();
292 final Collection<MapSquare<G, A, R>> matchingMapSquares =
new ArrayList<>();
296 boolean matchesMapSquare =
false;
297 for (
final G gameObject : mapSquare.recursive()) {
299 if (matchCriteria.matches(gameObject)) {
300 matchingGameObjects.add(gameObject);
301 matchesMapSquare =
true;
305 if (matchesMapSquare) {
306 matchingMapSquares.add(mapSquare);
311 return matchingGameObjects.size();
321 final Point p =
new Point();
323 p.x = mapSquare.getMapX();
324 p.y = mapSquare.getMapY();
335 private void setMapCursor(
final boolean forward, @NotNull
final List<G> gameObjects) {
336 if (gameObjects.isEmpty()) {
343 if (selectedGameObject == null) {
346 final int selectedIndex = gameObjects.indexOf(selectedGameObject);
347 if (selectedIndex == -1) {
349 }
else if (forward) {
350 index = selectedIndex + 1 < gameObjects.size() ? selectedIndex + 1 : 0;
352 index = selectedIndex > 0 ? selectedIndex - 1 : gameObjects.size() - 1;
363 if (mapView == this.mapView) {
364 dialog.setVisible(
false);
void findCancel()
Action method for Cancel button.
A MapModel reflects the data of a map.
Graphical User Interface of Gridarta.
final Component parent
The parent component for dialogs.
MapGrid getMapGrid()
Returns the MapGrid of this view.
A MatchCriteria that matches attribute values of all but a set of attributes.
final AbstractButton findMsgCheckbox
The checkbox for matching 'msg' attributes.
G getGameObject()
Returns the selected GameObject.
MapCursor provides methods to move and drag on map.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void unSelect()
Clears all selection and pre-selection flags from the grid.
final AbstractButton findNameCheckbox
The checkbox for matching 'name' attributes.
void setMapCursor(final boolean forward, @NotNull final List< G > gameObjects)
Moves the cursor to the next or previous matching game object.
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).
Utility class for JTextComponent related functions.
A MatchCriteria that matches an attribute value.
int doFind(@NotNull final Iterable< MatchCriteria< G, A, R >> matchCriterias, final boolean forward)
This method performs the actual find action on a map.
static final ActionBuilder ACTION_BUILDER
Action Builder.
boolean doFind(final boolean forward)
Executes one find operation.
GameObjects are the objects based on Archetypes found on maps.
static final long serialVersionUID
Serial Version UID.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
void findOk()
Action method for Ok button.
final JDialog dialog
The dialog instance.
final AbstractButton findSlayingCheckbox
The checkbox for matching 'slaying' attributes.
void display(@NotNull final MapView< G, A, R > mapView)
Replace objects on the map.
final JTextComponent findInput
The text input field for the string to find.
Base classes for rendering maps.
void setGameObject(@Nullable final G gameObject)
Sets the selected GameObject.
boolean findAgain(@NotNull final MapView< G, A, R > mapView, final boolean forward, final boolean performAction)
Re-executes the previous find operation.
final AbstractButton findArchCheckbox
The checkbox for matching archetype names.
Utility class for ActionBuilder related functions.
A MatchCriteria that matches by object name.
void select(@NotNull final Point pos, @NotNull final SelectionMode selectionMode)
Selects or deselects a single square.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
Currently nothing more than a marker interface for unification.
A map view consists of a map grid and a map cursor, and is attached to a map control.
boolean isBuilt
Whether this find dialog has been displayed.
ADD
All squares that are preselected get selected.
This dialog manages the find action.
MapView< G, A, R > mapView
The MapView to operate on.
void dispose(@NotNull final MapView< G, A, R > mapView)
Disposes the find dialog.
Modes that describe how squares get selected.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
A MatchCriteria that matches by archetype name.
static String format(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final Object... args)
Returns the value of a key.
void selectMapSquares(@NotNull final Iterable< MapSquare< G, A, R >> mapSquares)
Selects a set of MapSquares on the map.
final AbstractButton findFaceCheckbox
The checkbox for matching 'face' or 'animation' attributes.
FindDialog(@NotNull final Component parent)
Creates a new instance.
Criteria for game object matchers.
Interface for MapArchObjects.
final AbstractButton findOtherCheckbox
The checkbox for matching all other attributes.