20 package net.sf.gridarta.gui.panel.selectedsquare;
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Container;
25 import java.awt.FlowLayout;
26 import java.awt.GridLayout;
27 import java.awt.Insets;
28 import java.awt.Point;
29 import java.awt.event.InputEvent;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseListener;
33 import java.awt.geom.RectangularShape;
35 import javax.swing.AbstractButton;
36 import javax.swing.Action;
37 import javax.swing.DefaultListModel;
38 import javax.swing.Icon;
39 import javax.swing.ImageIcon;
40 import javax.swing.JButton;
41 import javax.swing.JLabel;
42 import javax.swing.JList;
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.JViewport;
46 import javax.swing.ListSelectionModel;
47 import javax.swing.ScrollPaneConstants;
48 import javax.swing.event.ListSelectionEvent;
49 import javax.swing.event.ListSelectionListener;
69 import org.jetbrains.annotations.NotNull;
70 import org.jetbrains.annotations.Nullable;
122 private final DefaultListModel<G>
model =
new DefaultListModel<>();
134 private final Container
arrows =
new JPanel();
156 public void mapSizeChanged(@NotNull
final Size2D newSize) {
168 public void mapObjectsChanged(@NotNull
final Set<G> gameObjects, @NotNull
final Set<G> transientGameObjects) {
180 public void mapFileChanged(@Nullable
final MapFile oldMapFile) {
185 public void modifiedChanged() {
198 public void activeMapViewChanged(@Nullable
final MapView<G, A, R> mapView) {
201 if (mapView ==
null) {
205 mapModel = mapView.getMapControl().getMapModel();
231 public void selectionChanged(@Nullable
final MapSquare<G, A, R> mapSquare, @Nullable
final G gameObject) {
245 public void valueChanged(
final ListSelectionEvent e) {
258 public void mapCursorChangedPos(@NotNull
final Point location) {
263 public void mapCursorChangedMode() {
268 public void mapCursorChangedGameObject(@Nullable
final MapSquare<G, A, R> mapSquare, @Nullable
final G gameObject) {
275 public void mapCursorChangedSize() {
289 public void mousePressed(
final MouseEvent e) {
291 if (e.getClickCount() > 1) {
293 if (gameObject !=
null) {
321 public SelectedSquareView(@NotNull
final SelectedSquareModel<G, A, R> selectedSquareModel, @NotNull
final GameObjectAttributesDialogFactory<G, A, R> gameObjectAttributesDialogFactory, @NotNull
final ObjectChooser<G, A, R> objectChooser, @NotNull
final MapViewManager<G, A, R> mapViewManager, @NotNull
final MapViewSettings mapViewSettings, @Nullable
final ImageIcon compassIcon, @NotNull
final FaceObjectProviders faceObjectProviders, @NotNull
final Icon unknownSquareIcon, @NotNull
final Action... actions) {
327 setLayout(
new BorderLayout());
331 list.setBackground(Color.lightGray);
332 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
333 final JScrollPane scrollPane =
new JScrollPane(
list);
334 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
335 scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
336 add(scrollPane, BorderLayout.CENTER);
338 for (
final Action tmpAction : actions) {
339 final AbstractButton button =
new JButton(tmpAction);
340 button.setMargin(
new Insets(0, 0, 0, 0));
345 if (compassIcon !=
null) {
346 final Container compass =
new JPanel();
347 compass.add(
new JLabel(compassIcon));
348 add(compass, BorderLayout.NORTH);
352 list.setFocusable(
false);
366 if (mapSquareListBottom) {
367 arrows.setLayout(
new GridLayout(4, 1));
368 add(
arrows, BorderLayout.WEST);
370 arrows.setLayout(
new FlowLayout());
371 add(
arrows, BorderLayout.SOUTH);
384 final int lastIndex =
model.getSize() - 1;
389 final RectangularShape bounds =
list.getCellBounds(lastIndex, lastIndex);
390 final int lowestY = (int) (bounds.getY() + bounds.getHeight());
391 if ((
int) e.getPoint().getY() >= lowestY) {
392 return lastIndex + 1;
395 final int listIndex =
list.locationToIndex(e.getPoint());
396 assert listIndex >= 0;
406 list.setEnabled(
false);
410 list.setEnabled(
true);
422 return actualIndex >=
model.getSize() ? null :
model.getElementAt(actualIndex);
432 if (
list.getSelectedIndex() != actualIndex) {
433 list.setSelectedIndex(actualIndex);
450 final int size =
model.getSize();
451 return index < size ? index : Math.max(0, size - 1);
460 if (mapCursor !=
null && index <
model.getSize()) {
472 if (gameObject !=
null) {
474 if (mapCursor !=
null) {
486 private static boolean isSelect(@NotNull
final InputEvent e) {
487 return (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0;
495 private static boolean isInsert(@NotNull
final InputEvent e) {
496 return (e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.CTRL_MASK)) == InputEvent.BUTTON3_MASK;
504 private static boolean isDelete(@NotNull
final InputEvent e) {
505 return (e.getModifiers() & InputEvent.BUTTON2_MASK) != 0 || (e.getModifiers() & (InputEvent.BUTTON3_MASK | InputEvent.CTRL_MASK)) == (InputEvent.BUTTON3_MASK | InputEvent.CTRL_MASK);
509 if (this.mapModel !=
null) {
513 if (this.mapModel !=
null) {
517 if (this.mapCursor !=
null) {
521 if (this.mapCursor !=
null) {