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.JLabel;
8   
9   import org.djutils.event.Event;
10  import org.djutils.event.EventListener;
11  import org.opentrafficsim.editor.OtsEditor;
12  import org.opentrafficsim.editor.XsdTreeNode;
13  import org.opentrafficsim.editor.XsdTreeNodeRoot;
14  
15  /**
16   * Editor for TrafCod program.
17   * <p>
18   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22   */
23  public class TrafCodEditor implements EventListener, Consumer<XsdTreeNode>
24  {
25      /** */
26      private static final long serialVersionUID = 20230313L;
27  
28      /** Editor. */
29      private OtsEditor editor;
30  
31      /**
32       * Constructor.
33       * @param editor OtsEditor; editor.
34       * @throws RemoteException if listener cannot be added.
35       */
36      public TrafCodEditor(final OtsEditor editor) throws RemoteException
37      {
38          editor.addListener(this, OtsEditor.NEW_FILE);
39          this.editor = editor;
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public void notify(final Event event) throws RemoteException
45      {
46          // TODO: this is a dummy implementation
47          if (event.getType().equals(OtsEditor.NEW_FILE))
48          {
49              XsdTreeNodeRoot root = (XsdTreeNodeRoot) event.getContent();
50              root.addListener(this, XsdTreeNodeRoot.NODE_CREATED);
51          }
52          else if (event.getType().equals(XsdTreeNodeRoot.NODE_CREATED))
53          {
54              XsdTreeNode node = (XsdTreeNode) ((Object[]) event.getContent())[0];
55              if (node.isType("Ots.Control.TrafCod.Program"))
56              {
57                  node.addConsumer("Configure...", this);
58              }
59          }
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public void accept(final XsdTreeNode t)
65      {
66          JLabel label = (JLabel) this.editor.getTab("Text");
67          label.setText(LocalDateTime.now().toString());
68          this.editor.focusTab("Text");
69      }
70  }