1 | package com.framsticks.gui.console; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Dimension; |
---|
5 | |
---|
6 | import javax.swing.BorderFactory; |
---|
7 | import javax.swing.JPanel; |
---|
8 | import javax.swing.JScrollPane; |
---|
9 | import javax.swing.JTextPane; |
---|
10 | |
---|
11 | import org.apache.logging.log4j.Logger; |
---|
12 | import org.apache.logging.log4j.LogManager; |
---|
13 | |
---|
14 | import com.framsticks.communication.Connection; |
---|
15 | import com.framsticks.gui.FrameJoinable; |
---|
16 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
17 | import com.framsticks.util.ExceptionHandler; |
---|
18 | import com.framsticks.util.FramsticksException; |
---|
19 | import com.framsticks.util.dispatching.Dispatching; |
---|
20 | import com.framsticks.util.dispatching.Joinable; |
---|
21 | import com.framsticks.util.dispatching.JoinableParent; |
---|
22 | import com.framsticks.util.dispatching.JoinableState; |
---|
23 | import com.framsticks.util.dispatching.RunAt; |
---|
24 | |
---|
25 | @FramsClassAnnotation |
---|
26 | public abstract class Console extends FrameJoinable implements JoinableParent { |
---|
27 | private static final Logger log = LogManager.getLogger(Console.class); |
---|
28 | |
---|
29 | /** |
---|
30 | * Painter coloring manager responses before display. |
---|
31 | */ |
---|
32 | protected ConsolePainter consolePainter; |
---|
33 | |
---|
34 | protected Connection connection; |
---|
35 | |
---|
36 | protected JPanel panel; |
---|
37 | |
---|
38 | /** |
---|
39 | * @param connection |
---|
40 | */ |
---|
41 | public Console() { |
---|
42 | setTitle("console"); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * @return the connection |
---|
47 | */ |
---|
48 | public Connection getConnection() { |
---|
49 | return connection; |
---|
50 | } |
---|
51 | |
---|
52 | @Override |
---|
53 | public String getName() { |
---|
54 | return connection != null ? "console for " + connection.getName() : "console"; |
---|
55 | } |
---|
56 | |
---|
57 | @Override |
---|
58 | protected void initializeGui() { |
---|
59 | super.initializeGui(); |
---|
60 | panel = new JPanel(); |
---|
61 | panel.setLayout(new BorderLayout()); |
---|
62 | panel.setSize(new Dimension(440, 400)); |
---|
63 | panel.setMinimumSize(new Dimension(440, 400)); |
---|
64 | |
---|
65 | JTextPane text = new JTextPane(); |
---|
66 | consolePainter = new ConsolePainter(text); |
---|
67 | |
---|
68 | text.setEditable(false); |
---|
69 | final JScrollPane scrollText = new JScrollPane(text); |
---|
70 | scrollText.setBorder(BorderFactory.createEtchedBorder()); |
---|
71 | |
---|
72 | JPanel scrollPanel = new JPanel(); |
---|
73 | scrollPanel.setLayout(new BorderLayout()); |
---|
74 | scrollPanel.add(scrollText, BorderLayout.CENTER); |
---|
75 | scrollPanel.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7)); |
---|
76 | |
---|
77 | panel.add(scrollPanel, BorderLayout.CENTER); |
---|
78 | |
---|
79 | getSwing().getContentPane().add(panel, BorderLayout.CENTER); |
---|
80 | |
---|
81 | log.debug("initialized gui"); |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
86 | if (joinable == connection) { |
---|
87 | proceedToState(state); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | protected ExceptionHandler getExceptionHandler() { |
---|
92 | return new ExceptionHandler() { |
---|
93 | |
---|
94 | @Override |
---|
95 | public void handle(FramsticksException exception) { |
---|
96 | throw exception; |
---|
97 | |
---|
98 | } |
---|
99 | }; |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | protected void joinableStart() { |
---|
104 | if (connection == null) { |
---|
105 | throw new FramsticksException().msg("connection was not set").arg("console", this); |
---|
106 | } |
---|
107 | super.joinableStart(); |
---|
108 | Dispatching.use(connection, this); |
---|
109 | } |
---|
110 | |
---|
111 | @Override |
---|
112 | protected void joinableInterrupt() { |
---|
113 | Dispatching.drop(connection, this); |
---|
114 | super.joinableInterrupt(); |
---|
115 | } |
---|
116 | |
---|
117 | @Override |
---|
118 | protected void joinableFinish() { |
---|
119 | super.joinableFinish(); |
---|
120 | } |
---|
121 | |
---|
122 | @Override |
---|
123 | protected void joinableJoin() throws InterruptedException { |
---|
124 | Dispatching.join(connection); |
---|
125 | super.joinableJoin(); |
---|
126 | } |
---|
127 | |
---|
128 | protected void dispatchWrite(final String line) { |
---|
129 | dispatch(new RunAt<Console>(getExceptionHandler()) { |
---|
130 | @Override |
---|
131 | protected void runAt() { |
---|
132 | consolePainter.userLine(line); |
---|
133 | } |
---|
134 | }); |
---|
135 | } |
---|
136 | |
---|
137 | } |
---|