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
21
22
23
24
25
26
27 public class RoadLayoutEditor implements EventListener, Consumer<XsdTreeNode>
28 {
29
30
31 private final OtsEditor editor;
32
33
34
35
36
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
48
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
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
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 }