View Javadoc
1   package org.opentrafficsim.editor.extensions;
2   
3   import java.io.IOException;
4   import java.rmi.RemoteException;
5   import java.time.LocalDateTime;
6   import java.util.function.Consumer;
7   
8   import javax.swing.ImageIcon;
9   import javax.swing.JComponent;
10  import javax.swing.JLabel;
11  
12  import org.djutils.event.Event;
13  import org.djutils.event.EventListener;
14  import org.opentrafficsim.editor.OtsEditor;
15  import org.opentrafficsim.editor.XsdPaths;
16  import org.opentrafficsim.editor.XsdTreeNode;
17  import org.opentrafficsim.editor.XsdTreeNodeRoot;
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      /** */
31      private static final long serialVersionUID = 20230313L;
32      
33      /** Editor. */
34      private final OtsEditor editor;
35  
36      /**
37       * Constructor.
38       * @param editor OtsEditor; editor.
39       * @throws IOException if icon cannot be loaded or listener cannot be added.
40       */
41      public RoadLayoutEditor(final OtsEditor editor) throws IOException
42      {
43          ImageIcon roadIcon = OtsEditor.loadIcon("./OTS_road.png", -1, -1, -1, -1);
44          editor.addTab("Road layout", roadIcon, buildRoadLayoutPane(), null);
45          editor.addListener(this, OtsEditor.NEW_FILE);
46          this.editor = editor;
47      }
48  
49      /**
50       * Temporary stub to create road layout pane.
51       * @return JComponent; component.
52       */
53      private static JComponent buildRoadLayoutPane()
54      {
55          JLabel roadLayout = new JLabel("road layout");
56          roadLayout.setOpaque(true);
57          roadLayout.setHorizontalAlignment(JLabel.CENTER);
58          return roadLayout;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public void notify(final Event event) throws RemoteException
64      {
65          // TODO: this is a dummy implementation
66          if (event.getType().equals(OtsEditor.NEW_FILE))
67          {
68              XsdTreeNodeRoot root = (XsdTreeNodeRoot) event.getContent();
69              root.addListener(this, XsdTreeNodeRoot.NODE_CREATED);
70              root.addListener(this, XsdTreeNodeRoot.NODE_REMOVED);
71          }
72          else if (event.getType().equals(XsdTreeNodeRoot.NODE_CREATED))
73          {
74              XsdTreeNode node = (XsdTreeNode) ((Object[]) event.getContent())[0];
75              if (node.getPathString().equals(XsdPaths.DEFINED_ROADLAYOUT)
76                      || node.getPathString().equals(XsdPaths.ROADLAYOUT))
77              {
78                  node.addConsumer("Edit...", this);
79              }
80          }
81      }
82      
83      /** {@inheritDoc} */
84      @Override
85      public void accept(final XsdTreeNode t)
86      {
87          // TODO: this is a dummy implementation
88          JLabel label = (JLabel) this.editor.getTab("Road layout");
89          label.setText(LocalDateTime.now().toString());
90          this.editor.focusTab("Road layout");
91      }
92  
93  }