View Javadoc
1   package nl.tudelft.simulation.dsol.swing.gui.animation.panel;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   
6   import javax.swing.BoxLayout;
7   import javax.swing.JPanel;
8   
9   import org.djutils.logger.CategoryLogger;
10  
11  import nl.tudelft.simulation.dsol.swing.ButtonUtil;
12  import nl.tudelft.simulation.dsol.swing.animation.d2.AnimationPanel;
13  
14  /**
15   * The ButtonPanel with the home button, grid button, and full extent button are synchronously coupled to the animation panel.
16   * There are no other interpretations than making direct changes in the animation panel. The functions to show/hide the grid and
17   * to change the extent are basic functions of the AnimationPanel and can be expected to always be present.
18   * <p>
19   * Copyright (c) 2021-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
20   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
21   * project is distributed under a three-clause BSD-style license, which can be found at
22   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
23   * </p>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   */
26  public class ButtonPanel extends JPanel implements ActionListener
27  {
28      /** */
29      private static final long serialVersionUID = 20210214L;
30  
31      /** the animation panel on which the buttons work. */
32      private final AnimationPanel animationPanel;
33  
34      /**
35       * Create a button panel; delegate the construction to the init() method, which can be overridden.
36       * @param animationPanel AnimationPanel; the animation panel on which the buttons work
37       */
38      public ButtonPanel(final AnimationPanel animationPanel)
39      {
40          this.animationPanel = animationPanel;
41          init();
42      }
43  
44      /**
45       * Construct the panel; this method can be overridden for another look and feel, or for adding buttons.
46       */
47      public void init()
48      {
49          setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
50          add(ButtonUtil.makeButton(this, "allButton", "/resources/Expand.png", "ZoomAll", "Zoom whole network", true));
51          add(ButtonUtil.makeButton(this, "homeButton", "/resources/Home.png", "Home", "Zoom to original extent", true));
52          add(ButtonUtil.makeButton(this, "gridButton", "/resources/Grid.png", "Grid", "Toggle grid on/off", true));
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public void actionPerformed(final ActionEvent actionEvent)
58      {
59          String actionCommand = actionEvent.getActionCommand();
60          try
61          {
62              if (actionCommand.equals("Home"))
63              {
64                  this.animationPanel.home();
65              }
66              if (actionCommand.equals("ZoomAll"))
67              {
68                  this.animationPanel.zoomAll();
69              }
70              if (actionCommand.equals("Grid"))
71              {
72                  this.animationPanel.showGrid(!this.animationPanel.isShowGrid());
73              }
74          }
75          catch (Exception exception)
76          {
77              CategoryLogger.always().warn(exception);
78          }
79      }
80  }