[97] | 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 | |
---|
[100] | 11 | import org.apache.logging.log4j.Logger; |
---|
| 12 | import org.apache.logging.log4j.LogManager; |
---|
[97] | 13 | |
---|
| 14 | import com.framsticks.gui.controls.Control; |
---|
[105] | 15 | import com.framsticks.util.ExceptionHandler; |
---|
[97] | 16 | import com.framsticks.util.FramsticksException; |
---|
| 17 | import com.framsticks.util.dispatching.Dispatcher; |
---|
[101] | 18 | import com.framsticks.util.dispatching.Dispatching; |
---|
[97] | 19 | import com.framsticks.util.dispatching.RunAt; |
---|
| 20 | |
---|
[105] | 21 | public class StatusBar implements ExceptionHandler { |
---|
[100] | 22 | private static final Logger log = LogManager.getLogger(StatusBar.class); |
---|
[97] | 23 | |
---|
| 24 | protected JTextField statusBar; |
---|
| 25 | protected JPanel swing; |
---|
[105] | 26 | protected ExceptionHandler exceptionHandler; |
---|
[101] | 27 | protected final Dispatcher<?> dispatcher; |
---|
[97] | 28 | |
---|
| 29 | /** |
---|
| 30 | * |
---|
| 31 | */ |
---|
[101] | 32 | public StatusBar(Dispatcher<?> dispatcher) { |
---|
| 33 | this.dispatcher = dispatcher; |
---|
[97] | 34 | } |
---|
| 35 | |
---|
[101] | 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 | |
---|
[97] | 50 | @Override |
---|
[101] | 51 | @SuppressWarnings({"rawtypes", "unchecked"}) |
---|
[97] | 52 | public void handle(final FramsticksException exception) { |
---|
[101] | 53 | dispatcher.dispatch(new RunAt(this) { |
---|
[97] | 54 | |
---|
| 55 | @Override |
---|
| 56 | protected void runAt() { |
---|
[100] | 57 | log.error("error: {}", exception.getMessage()); |
---|
[97] | 58 | statusBar.setText(exception.getShortMessage(new StringBuilder()).toString()); |
---|
[98] | 59 | if (exceptionHandler != null) { |
---|
| 60 | exceptionHandler.handle(exception); |
---|
| 61 | } |
---|
[97] | 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 | |
---|
[98] | 85 | /** |
---|
| 86 | * @return the exceptionHandler |
---|
| 87 | */ |
---|
[105] | 88 | public ExceptionHandler getExceptionHandler() { |
---|
[98] | 89 | return exceptionHandler; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * @param exceptionHandler the exceptionHandler to set |
---|
| 94 | */ |
---|
[105] | 95 | public void setExceptionHandler(ExceptionHandler exceptionHandler) { |
---|
[98] | 96 | this.exceptionHandler = exceptionHandler; |
---|
| 97 | } |
---|
| 98 | |
---|
[97] | 99 | } |
---|