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);
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));
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));
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));
203 setMessage(mainPanel);
204 setOptions(
new Object[] { okButton, cancelButton });
205 dialog.getRootPane().setDefaultButton(okButton);
258 private boolean doFind(
final boolean forward) {
259 final String findString =
findInput.getText().trim();
260 final Collection<MatchCriteria<G, A, R>> matchCriterias =
new ArrayList<>();
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 : gameObjects.size()) - 1;