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          
88          /** Text field with appearance control. */
89          class AppearanceControlTextField extends JTextField implements AppearanceControl
90          {
91              /** */
92              private static final long serialVersionUID = 20180207L;
93  
94              /** {@inheritDoc} */
95              @Override
96              public boolean isForeground()
97              {
98                  return false;
99              }
100 
101             /** {@inheritDoc} */
102             @Override
103             public boolean isBackground()
104             {
105                 return false;
106             }
107 
108             /** {@inheritDoc} */
109             @Override
110             public String toString()
111             {
112                 return "AppearanceControlLabel []";
113             }
114         }
115         
116         this.idTextField = new AppearanceControlTextField();
117         this.idTextField.setPreferredSize(new Dimension(100, 0));
118         this.add(this.idTextField);
119         this.trackObject = new JCheckBox("track");
120         this.add(this.trackObject);
121         this.trackObject.setActionCommand("Tracking status changed");
122         this.idTextField.setActionCommand("Id changed");
123         this.typeToSearch.setActionCommand("Type changed");
124         this.trackObject.addActionListener(this);
125         this.idTextField.addActionListener(this);
126         this.typeToSearch.addActionListener(this);
127         this.idTextField.addFocusListener(this);
128         this.idTextField.getDocument().addDocumentListener(this);
129     }
130 
131     /**
132      * Update all values at once.
133      * @param objectKey String; key of the object type to search
134      * @param id String; id of object to search
135      * @param track boolean; if true; track continuously; if false; center on it, but do not track
136      */
137     public void selectAndTrackObject(final String objectKey, final String id, final boolean track)
138     {
139         for (int index = this.typeToSearch.getItemCount(); --index >= 0;)
140         {
141             if (this.typeToSearch.getItemAt(index).getKey().equals(objectKey))
142             {
143                 this.typeToSearch.setSelectedIndex(index);
144             }
145         }
146         this.trackObject.setSelected(track);
147         this.idTextField.setText(id);
148         actionPerformed(null);
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public void actionPerformed(final ActionEvent e)
154     {
155         this.otsAnimationPanel.setAutoPan(this.idTextField.getText(), (ObjectKind<?>) this.typeToSearch.getSelectedItem(),
156                 this.trackObject.isSelected());
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final void focusGained(final FocusEvent e)
162     {
163         actionPerformed(null);
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public final void focusLost(final FocusEvent e)
169     {
170         // Do nothing
171     }
172 
173     /** {@inheritDoc} */
174     @Override
175     public final void insertUpdate(final DocumentEvent e)
176     {
177         actionPerformed(null);
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     public final void removeUpdate(final DocumentEvent e)
183     {
184         actionPerformed(null);
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public final void changedUpdate(final DocumentEvent e)
190     {
191         actionPerformed(null);
192     }
193 
194     /**
195      * Entries in the typeToSearch JComboBox of the OTS search panel.
196      * @param <T> Type of object identified by key
197      */
198     abstract static class ObjectKind<T extends Locatable & Identifiable>
199     {
200         /** The key of this ObjectKind. */
201         private final String key;
202 
203         /**
204          * Construct a new ObjectKind (entry in the combo box).
205          * @param key String; the key of the new ObjectKind
206          */
207         ObjectKind(final String key)
208         {
209             this.key = key;
210         }
211 
212         /**
213          * Retrieve the key.
214          * @return String; the key
215          */
216         public Object getKey()
217         {
218             return this.key;
219         }
220 
221         /**
222          * Lookup an object of type T in an OTS network.
223          * @param network OTSNetwork; the OTS network
224          * @param id String; id of the object to return
225          * @return T; the object in the network of the correct type and matching id, or null if no matching object was found.
226          */
227         abstract T searchNetwork(OTSNetwork network, String id);
228 
229         /**
230          * Produce the text that will appear in the combo box. This method should be overridden to implement localization.
231          */
232         @Override
233         public String toString()
234         {
235             return this.key;
236         }
237     }
238 }