[97] | 1 | package com.framsticks.gui; |
---|
| 2 | |
---|
| 3 | import java.awt.BorderLayout; |
---|
| 4 | import java.awt.Container; |
---|
| 5 | import java.awt.event.WindowAdapter; |
---|
| 6 | import java.awt.event.WindowEvent; |
---|
| 7 | import java.util.concurrent.atomic.AtomicBoolean; |
---|
| 8 | |
---|
| 9 | import javax.swing.JFrame; |
---|
| 10 | import javax.swing.UIManager; |
---|
| 11 | |
---|
[100] | 12 | import org.apache.logging.log4j.Logger; |
---|
| 13 | import org.apache.logging.log4j.LogManager; |
---|
[97] | 14 | |
---|
[105] | 15 | import com.framsticks.util.ExceptionHandler; |
---|
[97] | 16 | import com.framsticks.util.FramsticksException; |
---|
| 17 | import com.framsticks.util.dispatching.RunAt; |
---|
| 18 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
| 19 | |
---|
[105] | 20 | public abstract class FrameJoinable extends SwingJoinable<JFrame> implements ExceptionHandler { |
---|
[97] | 21 | private static final Logger log = |
---|
[100] | 22 | LogManager.getLogger(FrameJoinable.class); |
---|
[97] | 23 | |
---|
| 24 | protected String title; |
---|
| 25 | |
---|
| 26 | protected final StatusBar statusBar; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * |
---|
| 31 | */ |
---|
| 32 | public FrameJoinable() { |
---|
[101] | 33 | statusBar = new StatusBar(this); |
---|
[97] | 34 | } |
---|
| 35 | |
---|
| 36 | public void setTitle(String title) { |
---|
| 37 | this.title = title; |
---|
| 38 | if (hasSwing()) { |
---|
| 39 | getSwing().setTitle(title); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * @return the statusBar |
---|
| 45 | */ |
---|
| 46 | public StatusBar getStatusBar() { |
---|
| 47 | return statusBar; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | @Override |
---|
| 51 | public String getName() { |
---|
| 52 | return title; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | private final static AtomicBoolean sentGuiInitialization = new AtomicBoolean(false); |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | @Override |
---|
| 59 | protected void joinableStart() { |
---|
| 60 | |
---|
| 61 | if (sentGuiInitialization.compareAndSet(false, true)) { |
---|
| 62 | dispatch(new RunAt<FrameJoinable>(ThrowExceptionHandler.getInstance()) { |
---|
| 63 | @Override |
---|
| 64 | protected void runAt() { |
---|
| 65 | try { |
---|
| 66 | boolean found = false; |
---|
| 67 | // for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { |
---|
[100] | 68 | // log.info("look and feel available: {}", info.getName()); |
---|
[97] | 69 | // if ("Nimbus".equals(info.getName())) { |
---|
| 70 | // UIManager.setLookAndFeel(info.getClassName()); |
---|
| 71 | // found = true; |
---|
| 72 | // break; |
---|
| 73 | // } |
---|
| 74 | // } |
---|
| 75 | if (!found) { |
---|
| 76 | UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); |
---|
| 77 | } |
---|
| 78 | JFrame.setDefaultLookAndFeelDecorated(true); |
---|
| 79 | } catch (Exception e) { |
---|
| 80 | throw new FramsticksException().msg("failed to initialize Look&Feel").cause(e); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | }); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | super.joinableStart(); |
---|
| 87 | dispatch(new RunAt<FrameJoinable>(ThrowExceptionHandler.getInstance()) { |
---|
| 88 | @Override |
---|
| 89 | protected void runAt() { |
---|
| 90 | getSwing().setVisible(true); |
---|
| 91 | } |
---|
| 92 | }); |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | @Override |
---|
| 97 | protected void joinableInterrupt() { |
---|
| 98 | super.joinableInterrupt(); |
---|
| 99 | |
---|
| 100 | dispatch(new RunAt<Frame>(ThrowExceptionHandler.getInstance()) { |
---|
| 101 | @Override |
---|
| 102 | protected void runAt() { |
---|
[101] | 103 | finishJoinable(); |
---|
[97] | 104 | } |
---|
| 105 | }); |
---|
| 106 | |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | @Override |
---|
| 110 | protected void joinableFinish() { |
---|
| 111 | super.joinableFinish(); |
---|
[100] | 112 | log.debug("disposing frame {}", this); |
---|
[97] | 113 | getSwing().dispose(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | @Override |
---|
| 117 | protected void joinableJoin() throws InterruptedException { |
---|
| 118 | super.joinableJoin(); |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | @Override |
---|
| 123 | protected void initializeGui() { |
---|
| 124 | setSwing(new JFrame(title)); |
---|
| 125 | statusBar.initializeGui(); |
---|
| 126 | |
---|
| 127 | Container contentPane = getSwing().getContentPane(); |
---|
| 128 | contentPane.setLayout(new BorderLayout()); |
---|
| 129 | contentPane.add(statusBar.getSwing(), BorderLayout.SOUTH); |
---|
| 130 | |
---|
| 131 | getSwing().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
---|
| 132 | |
---|
| 133 | getSwing().addWindowListener(new WindowAdapter() { |
---|
| 134 | @Override |
---|
| 135 | public void windowClosing(WindowEvent e) { |
---|
| 136 | log.info("received closing"); |
---|
[101] | 137 | interruptJoinable(); |
---|
[97] | 138 | } |
---|
| 139 | }); |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | @Override |
---|
| 145 | public void handle(FramsticksException exception) { |
---|
| 146 | statusBar.handle(exception); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | } |
---|