package mpmetr.ui;
import mpmetr.core.SectorEnum;
import static mpmetr.core.SectorModel.DEF_RATE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import static javax.swing.SwingConstants.CENTER;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.util.ArrayList;
import java.util.List;
public final class MainPanel extends JPanel {
private final Ticker ticker;
private final Controller controller;
private final FeedbackLabel feedback;
private JLabel lblBpm;
private List<SectorPanel> panels;
public MainPanel() {
this("");
}
public MainPanel(String msg) {
lblBpm = new JLabel("" + DEF_RATE);
UiUtil.alignText(lblBpm, CENTER, CENTER);
feedback = new FeedbackLabel(msg);
panels = populateScreen(lblBpm, feedback);
controller = new Controller(panels, lblBpm);
ticker = new Ticker(panels);
}
public void scale() {
Scaler s = new Scaler(getWidth(), getHeight());
Dimension d = new Dimension(s.getPanelWidth(), s.getPanelHeight());
setPreferredSize(d);
UiUtil.setFontSize(lblBpm, s.getBpmSize());
UiUtil.setFontSize(feedback, s.getFeedbackSize());
for (SectorPanel p : panels) {
final JLabel lbl = p.getLabel();
UiUtil.setFontSize(lbl, s.getSectorSize());
}
}
public void launch() {
setupKeystrokes();
controller.requestBpm(DEF_RATE);
ticker.launch();
feedback.launch();
}
private void setupKeystrokes() {
KeyboardFocusManager mgr = KeyboardFocusManager.getCurrentKeyboardFocusManager();
mgr.addKeyEventDispatcher(new Dispatcher(feedback, controller));
}
private List<SectorPanel> populateScreen(JLabel lblBpm, FeedbackLabel feedback) {
List<SectorPanel> panels = new ArrayList<SectorPanel>();
JPanel pnlGrid = buildGrid(lblBpm, panels);
this.setLayout(new BorderLayout());
this.add(pnlGrid, BorderLayout.CENTER);
this.add(feedback, BorderLayout.SOUTH);
return panels;
}
private JPanel buildGrid(JLabel lblBpm, List<SectorPanel> panels) {
JPanel pnl = new JPanel(new GridLayout(3, 3));
addSectorPanel(pnl, panels, SectorEnum.SE_1030);
addSectorPanel(pnl, panels, SectorEnum.SE_1200);
addSectorPanel(pnl, panels, SectorEnum.SE_0130);
addSectorPanel(pnl, panels, SectorEnum.SE_0900);
pnl.add(lblBpm);
addSectorPanel(pnl, panels, SectorEnum.SE_0300);
addSectorPanel(pnl, panels, SectorEnum.SE_0730);
addSectorPanel(pnl, panels, SectorEnum.SE_0600);
addSectorPanel(pnl, panels, SectorEnum.SE_0430);
return pnl;
}
private void addSectorPanel(JPanel pnl, List<SectorPanel> panels, SectorEnum sector) {
SectorPanel panel = new SectorPanel(sector);
pnl.add(panel);
panels.add(panel);
}
}