View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.Dimension;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.awt.event.FocusEvent;
7   import java.awt.event.FocusListener;
8   
9   import javax.swing.BoxLayout;
10  import javax.swing.JCheckBox;
11  import javax.swing.JComboBox;
12  import javax.swing.JLabel;
13  import javax.swing.JPanel;
14  import javax.swing.JTextField;
15  import javax.swing.event.DocumentEvent;
16  import javax.swing.event.DocumentListener;
17  
18  import org.opentrafficsim.base.Identifiable;
19  import org.opentrafficsim.core.gtu.GTU;
20  import org.opentrafficsim.core.network.Link;
21  import org.opentrafficsim.core.network.Node;
22  import org.opentrafficsim.core.network.OTSNetwork;
23  
24  import nl.tudelft.simulation.dsol.animation.Locatable;
25  
26  /**
27   * The OTS search panel.
28   * <p>
29   * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * <p>
32   * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
33   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
35   */
36  public class OTSSearchPanel extends JPanel implements ActionListener, FocusListener, DocumentListener
37  {
38      /** ... */
39      private static final long serialVersionUID = 20200127L;
40  
41      /** The animation panel. */
42      private final OTSAnimationPanel otsAnimationPanel;
43  
44      /** The type-of-object-to-search-for selector. */
45      private final JComboBox<ObjectKind<?>> typeToSearch;
46  
47      /** Id of the object to search for. */
48      private final JTextField idTextField;
49  
50      /** Track object check box. */
51      private final JCheckBox trackObject;
52  
53      /**
54       * Construct a new OTSSearchPanel.
55       * @param otsAnimationPanel OTSAnimationPanel; the animation panel
56       */
57      public OTSSearchPanel(final OTSAnimationPanel otsAnimationPanel)
58      {
59          this.otsAnimationPanel = otsAnimationPanel;
60          this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
61          this.add(new JLabel("    ")); // insert some white space in the GUI
62          this.add(new JLabel(OTSControlPanel.loadIcon("/View.png")));
63          ObjectKind<?>[] objectKinds = new ObjectKind[] { new ObjectKind<GTU>("GTU")
64          {
65              @Override
66              GTU searchNetwork(final OTSNetwork network, final String id)
67              {
68                  return network.getGTU(id);
69              }
70          }, new ObjectKind<Node>("Node")
71          {
72              @Override
73              Node searchNetwork(final OTSNetwork network, final String id)
74              {
75                  return network.getNode(id);
76              }
77          }, new ObjectKind<Link>("Link")
78          {
79              @Override
80              Link searchNetwork(final OTSNetwork network, final String id)
81              {
82                  return network.getLink(id);
83              }
84          } };
85          this.typeToSearch = new JComboBox<ObjectKind<?>>(objectKinds);
86          this.add(this.typeToSearch);
87          this.idTextField = new JTextField("");
88          this.idTextField.setPreferredSize(new Dimension(100, 0));
89          this.add(this.idTextField);
90          this.trackObject = new JCheckBox("track");
91          this.add(this.trackObject);
92          this.trackObject.setActionCommand("Tracking status changed");
93          this.idTextField.setActionCommand("Id changed");
94          this.typeToSearch.setActionCommand("Type changed");
95          this.trackObject.addActionListener(this);
96          this.idTextField.addActionListener(this);
97          this.typeToSearch.addActionListener(this);
98          this.idTextField.addFocusListener(this);
99          this.idTextField.getDocument().addDocumentListener(this);
100     }
101 
102     /**
103      * Update all values at once.
104      * @param objectKey String; key of the object type to search
105      * @param id String; id of object to search
106      * @param track boolean; if true; track continuously; if false; center on it, but do not track
107      */
108     public void selectAndTrackObject(final String objectKey, final String id, final boolean track)
109     {
110         for (int index = this.typeToSearch.getItemCount(); --index >= 0;)
111         {
112             if (this.typeToSearch.getItemAt(index).getKey().equals(objectKey))
113             {
114                 this.typeToSearch.setSelectedIndex(index);
115             }
116         }
117         this.trackObject.setSelected(track);
118         this.idTextField.setText(id);
119         actionPerformed(null);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public void actionPerformed(final ActionEvent e)
125     {
126         this.otsAnimationPanel.setAutoPan(this.idTextField.getText(), (ObjectKind<?>) this.typeToSearch.getSelectedItem(),
127                 this.trackObject.isSelected());
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public final void focusGained(final FocusEvent e)
133     {
134         actionPerformed(null);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public final void focusLost(final FocusEvent e)
140     {
141         // Do nothing
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public final void insertUpdate(final DocumentEvent e)
147     {
148         actionPerformed(null);
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final void removeUpdate(final DocumentEvent e)
154     {
155         actionPerformed(null);
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public final void changedUpdate(final DocumentEvent e)
161     {
162         actionPerformed(null);
163     }
164 
165     /**
166      * Entries in the typeToSearch JComboBox of the OTS search panel.
167      * @param <T> Type of object identified by key
168      */
169     abstract static class ObjectKind<T extends Locatable & Identifiable>
170     {
171         /** The key of this ObjectKind. */
172         private final String key;
173 
174         /**
175          * Construct a new ObjectKind (entry in the combo box).
176          * @param key String; the key of the new ObjectKind
177          */
178         ObjectKind(final String key)
179         {
180             this.key = key;
181         }
182 
183         /**
184          * Retrieve the key.
185          * @return String; the key
186          */
187         public Object getKey()
188         {
189             return this.key;
190         }
191 
192         /**
193          * Lookup an object of type T in an OTS network.
194          * @param network OTSNetwork; the OTS network
195          * @param id String; id of the object to return
196          * @return T; the object in the network of the correct type and matching id, or null if no matching object was found.
197          */
198         abstract T searchNetwork(OTSNetwork network, String id);
199 
200         /**
201          * Produce the text that will appear in the combo box. This method should be overridden to implement localization.
202          */
203         @Override
204         public String toString()
205         {
206             return this.key;
207         }
208     }
209 }