View Javadoc
1   package org.opentrafficsim.editor.extensions;
2   
3   import java.rmi.RemoteException;
4   import java.time.LocalDateTime;
5   import java.util.function.Consumer;
6   
7   import javax.swing.JComponent;
8   import javax.swing.JLabel;
9   
10  import org.djutils.event.Event;
11  import org.djutils.event.EventListener;
12  import org.opentrafficsim.editor.OtsEditor;
13  import org.opentrafficsim.editor.XsdTreeNode;
14  import org.opentrafficsim.editor.XsdTreeNodeRoot;
15  
16  /**
17   * Editor for route.
18   * <p>
19   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * </p>
22   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23   */
24  public class RouteEditor implements EventListener, Consumer<XsdTreeNode>
25  {
26      /** */
27      private static final long serialVersionUID = 20230313L;
28  
29      /** Editor. */
30      private OtsEditor editor;
31  
32      /**
33       * Constructor.
34       * @param editor OtsEditor; editor.
35       * @throws RemoteException if listener cannot be added.
36       */
37      public RouteEditor(final OtsEditor editor) throws RemoteException
38      {
39          editor.addTab("Route", null, buildRoutePane(), null);
40          editor.addListener(this, OtsEditor.NEW_FILE);
41          this.editor = editor;
42      }
43  
44      /**
45       * Temporary stub to create route pane.
46       * @return JComponent; component.
47       */
48      private static JComponent buildRoutePane()
49      {
50          JLabel route = new JLabel("route");
51          route.setOpaque(true);
52          route.setHorizontalAlignment(JLabel.CENTER);
53          return route;
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public void notify(final Event event) throws RemoteException
59      {
60          // TODO: this is a dummy implementation
61          if (event.getType().equals(OtsEditor.NEW_FILE))
62          {
63              XsdTreeNodeRoot root = (XsdTreeNodeRoot) event.getContent();
64              root.addListener(this, XsdTreeNodeRoot.NODE_CREATED);
65          }
66          else if (event.getType().equals(XsdTreeNodeRoot.NODE_CREATED))
67          {
68              XsdTreeNode node = (XsdTreeNode) ((Object[]) event.getContent())[0];
69              if (node.isType("Ots.Demand.Route"))
70              {
71                  node.addConsumer("Show in panel...", this);
72                  node.addConsumer("Compute shortest...", new Consumer<XsdTreeNode>()
73                  {
74                      /** {@inheritDoc} */
75                      @Override
76                      public void accept(final XsdTreeNode t)
77                      {
78                          System.out.println("We are not going to do that.");
79                      }
80                  });
81              }
82          }
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public void accept(final XsdTreeNode t)
88      {
89          JLabel label = ((JLabel) this.editor.getTab("Route"));
90          label.setText(LocalDateTime.now().toString());
91          this.editor.focusTab("Route");
92      }
93  }