View Javadoc
1   package org.opentrafficsim.editor.extensions;
2   
3   import java.io.IOException;
4   import java.time.LocalDateTime;
5   import java.util.function.Consumer;
6   
7   import javax.swing.ImageIcon;
8   import javax.swing.JComponent;
9   import javax.swing.JLabel;
10  
11  import org.djutils.event.Event;
12  import org.djutils.event.EventListener;
13  import org.opentrafficsim.editor.OtsEditor;
14  import org.opentrafficsim.editor.XsdPaths;
15  import org.opentrafficsim.editor.XsdTreeNode;
16  import org.opentrafficsim.editor.XsdTreeNodeRoot;
17  import org.opentrafficsim.editor.decoration.DefaultDecorator;
18  
19  /**
20   * Editor for road layouts.
21   * <p>
22   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
26   */
27  public class RoadLayoutEditor implements EventListener, Consumer<XsdTreeNode>
28  {
29  
30      /** Editor. */
31      private final OtsEditor editor;
32  
33      /**
34       * Constructor.
35       * @param editor editor.
36       * @throws IOException if icon cannot be loaded or listener cannot be added.
37       */
38      public RoadLayoutEditor(final OtsEditor editor) throws IOException
39      {
40          ImageIcon roadIcon = DefaultDecorator.loadIcon("./OTS_road.png", -1, -1, -1, -1);
41          editor.addTab("Road layout", roadIcon, buildRoadLayoutPane(), null);
42          editor.addListener(this, OtsEditor.NEW_FILE);
43          this.editor = editor;
44      }
45  
46      /**
47       * Temporary stub to create road layout pane.
48       * @return component.
49       */
50      private static JComponent buildRoadLayoutPane()
51      {
52          JLabel roadLayout = new JLabel("road layout");
53          roadLayout.setOpaque(true);
54          roadLayout.setHorizontalAlignment(JLabel.CENTER);
55          return roadLayout;
56      }
57  
58      @Override
59      public void notify(final Event event)
60      {
61          // TODO: this is a dummy implementation
62          if (event.getType().equals(OtsEditor.NEW_FILE))
63          {
64              XsdTreeNodeRoot root = (XsdTreeNodeRoot) event.getContent();
65              root.addListener(this, XsdTreeNodeRoot.NODE_CREATED);
66              root.addListener(this, XsdTreeNodeRoot.NODE_REMOVED);
67          }
68          else if (event.getType().equals(XsdTreeNodeRoot.NODE_CREATED))
69          {
70              XsdTreeNode node = (XsdTreeNode) ((Object[]) event.getContent())[0];
71              if (node.getPathString().equals(XsdPaths.DEFINED_ROADLAYOUT) || node.getPathString().equals(XsdPaths.ROADLAYOUT))
72              {
73                  node.addConsumer("Edit...", this);
74              }
75          }
76      }
77  
78      @Override
79      public void accept(final XsdTreeNode t)
80      {
81          // TODO: this is a dummy implementation
82          JLabel label = (JLabel) this.editor.getTab("Road layout").get();
83          label.setText(LocalDateTime.now().toString());
84          this.editor.focusTab("Road layout");
85      }
86  
87  }