1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | |
---|
4 | import java.awt.BorderLayout; |
---|
5 | import java.awt.Dimension; |
---|
6 | // import java.awt.Dimension; |
---|
7 | |
---|
8 | import javax.swing.JPanel; |
---|
9 | import javax.swing.JTextField; |
---|
10 | |
---|
11 | import org.apache.logging.log4j.Logger; |
---|
12 | import org.apache.logging.log4j.LogManager; |
---|
13 | |
---|
14 | import com.framsticks.gui.controls.Control; |
---|
15 | import com.framsticks.util.ExceptionHandler; |
---|
16 | import com.framsticks.util.FramsticksException; |
---|
17 | import com.framsticks.util.dispatching.Dispatcher; |
---|
18 | import com.framsticks.util.dispatching.Dispatching; |
---|
19 | import com.framsticks.util.dispatching.RunAt; |
---|
20 | |
---|
21 | public class StatusBar implements ExceptionHandler { |
---|
22 | private static final Logger log = LogManager.getLogger(StatusBar.class); |
---|
23 | |
---|
24 | protected JTextField statusBar; |
---|
25 | protected JPanel swing; |
---|
26 | protected ExceptionHandler exceptionHandler; |
---|
27 | protected final Dispatcher<?> dispatcher; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | */ |
---|
32 | public StatusBar(Dispatcher<?> dispatcher) { |
---|
33 | this.dispatcher = dispatcher; |
---|
34 | } |
---|
35 | |
---|
36 | @SuppressWarnings({"rawtypes", "unchecked"}) |
---|
37 | public void showInfo(final Object value) { |
---|
38 | Dispatching.dispatchIfNotActive(dispatcher, new RunAt(this) { |
---|
39 | |
---|
40 | @Override |
---|
41 | protected void runAt() { |
---|
42 | String text = value.toString(); |
---|
43 | log.info("info: {}", text); |
---|
44 | statusBar.setText(text); |
---|
45 | |
---|
46 | } |
---|
47 | }); |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | @SuppressWarnings({"rawtypes", "unchecked"}) |
---|
52 | public void handle(final FramsticksException exception) { |
---|
53 | dispatcher.dispatch(new RunAt(this) { |
---|
54 | |
---|
55 | @Override |
---|
56 | protected void runAt() { |
---|
57 | log.error("error: {}", exception.getMessage()); |
---|
58 | statusBar.setText(exception.getShortMessage(new StringBuilder()).toString()); |
---|
59 | if (exceptionHandler != null) { |
---|
60 | exceptionHandler.handle(exception); |
---|
61 | } |
---|
62 | } |
---|
63 | }); |
---|
64 | } |
---|
65 | |
---|
66 | public void initializeGui() { |
---|
67 | statusBar = new JTextField(); |
---|
68 | statusBar.setEditable(false); |
---|
69 | |
---|
70 | swing = new JPanel(); |
---|
71 | swing.setMaximumSize(new Dimension(Integer.MAX_VALUE, Control.LINE_HEIGHT)); |
---|
72 | |
---|
73 | swing.setLayout(new BorderLayout()); |
---|
74 | swing.add(statusBar, BorderLayout.CENTER); |
---|
75 | // swing.add(statusBar); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * @return the swing |
---|
80 | */ |
---|
81 | public JPanel getSwing() { |
---|
82 | return swing; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * @return the exceptionHandler |
---|
87 | */ |
---|
88 | public ExceptionHandler getExceptionHandler() { |
---|
89 | return exceptionHandler; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * @param exceptionHandler the exceptionHandler to set |
---|
94 | */ |
---|
95 | public void setExceptionHandler(ExceptionHandler exceptionHandler) { |
---|
96 | this.exceptionHandler = exceptionHandler; |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|