001package headfirst.combined.djview; 002 003import java.awt.BorderLayout; 004import java.awt.Dimension; 005import java.awt.GridLayout; 006import java.awt.event.ActionEvent; 007import java.awt.event.ActionListener; 008 009import javax.swing.BorderFactory; 010import javax.swing.JButton; 011import javax.swing.JFrame; 012import javax.swing.JLabel; 013import javax.swing.JMenu; 014import javax.swing.JMenuBar; 015import javax.swing.JMenuItem; 016import javax.swing.JPanel; 017import javax.swing.JTextField; 018import javax.swing.SwingConstants; 019 020public class DJView implements ActionListener, BeatObserver, BPMObserver { 021 BeatModelInterface model; 022 ControllerInterface controller; 023 JFrame viewFrame; 024 JPanel viewPanel; 025 BeatBar beatBar; 026 JLabel bpmOutputLabel; 027 JFrame controlFrame; 028 JPanel controlPanel; 029 JLabel bpmLabel; 030 JTextField bpmTextField; 031 JButton setBPMButton; 032 JButton increaseBPMButton; 033 JButton decreaseBPMButton; 034 JMenuBar menuBar; 035 JMenu menu; 036 JMenuItem startMenuItem; 037 JMenuItem stopMenuItem; 038 039 public DJView(ControllerInterface controller, BeatModelInterface model) { 040 this.controller = controller; 041 this.model = model; 042 model.registerObserver((BeatObserver)this); 043 model.registerObserver((BPMObserver)this); 044 } 045 046 public void createView() { 047 // Create all Swing components here 048 viewPanel = new JPanel(new GridLayout(1, 2)); 049 viewFrame = new JFrame("View"); 050 viewFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 051 viewFrame.setSize(new Dimension(100, 80)); 052 bpmOutputLabel = new JLabel("offline", SwingConstants.CENTER); 053 beatBar = new BeatBar(); 054 beatBar.setValue(0); 055 JPanel bpmPanel = new JPanel(new GridLayout(2, 1)); 056 bpmPanel.add(beatBar); 057 bpmPanel.add(bpmOutputLabel); 058 viewPanel.add(bpmPanel); 059 viewFrame.getContentPane().add(viewPanel, BorderLayout.CENTER); 060 viewFrame.pack(); 061 viewFrame.setVisible(true); 062 } 063 064 065 public void createControls() { 066 // Create all Swing components here 067 JFrame.setDefaultLookAndFeelDecorated(true); 068 controlFrame = new JFrame("Control"); 069 controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 070 controlFrame.setSize(new Dimension(100, 80)); 071 072 controlPanel = new JPanel(new GridLayout(1, 2)); 073 074 menuBar = new JMenuBar(); 075 menu = new JMenu("DJ Control"); 076 startMenuItem = new JMenuItem("Start"); 077 menu.add(startMenuItem); 078 startMenuItem.addActionListener(event -> controller.start()); 079 stopMenuItem = new JMenuItem("Stop"); 080 menu.add(stopMenuItem); 081 stopMenuItem.addActionListener(event -> controller.stop()); 082 JMenuItem exit = new JMenuItem("Quit"); 083 exit.addActionListener(event -> System.exit(0)); 084 085 menu.add(exit); 086 menuBar.add(menu); 087 controlFrame.setJMenuBar(menuBar); 088 089 bpmTextField = new JTextField(2); 090 bpmLabel = new JLabel("Enter BPM:", SwingConstants.RIGHT); 091 setBPMButton = new JButton("Set"); 092 setBPMButton.setSize(new Dimension(10,40)); 093 increaseBPMButton = new JButton(">>"); 094 decreaseBPMButton = new JButton("<<"); 095 setBPMButton.addActionListener(this); 096 increaseBPMButton.addActionListener(this); 097 decreaseBPMButton.addActionListener(this); 098 099 JPanel buttonPanel = new JPanel(new GridLayout(1, 2)); 100 101 buttonPanel.add(decreaseBPMButton); 102 buttonPanel.add(increaseBPMButton); 103 104 JPanel enterPanel = new JPanel(new GridLayout(1, 2)); 105 enterPanel.add(bpmLabel); 106 enterPanel.add(bpmTextField); 107 JPanel insideControlPanel = new JPanel(new GridLayout(3, 1)); 108 insideControlPanel.add(enterPanel); 109 insideControlPanel.add(setBPMButton); 110 insideControlPanel.add(buttonPanel); 111 controlPanel.add(insideControlPanel); 112 113 bpmLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 114 bpmOutputLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 115 116 controlFrame.getRootPane().setDefaultButton(setBPMButton); 117 controlFrame.getContentPane().add(controlPanel, BorderLayout.CENTER); 118 119 controlFrame.pack(); 120 controlFrame.setVisible(true); 121 } 122 123 public void enableStopMenuItem() { 124 stopMenuItem.setEnabled(true); 125 } 126 127 public void disableStopMenuItem() { 128 stopMenuItem.setEnabled(false); 129 } 130 131 public void enableStartMenuItem() { 132 startMenuItem.setEnabled(true); 133 } 134 135 public void disableStartMenuItem() { 136 startMenuItem.setEnabled(false); 137 } 138 139 public void actionPerformed(ActionEvent event) { 140 if (event.getSource() == setBPMButton) { 141 int bpm = Integer.parseInt(bpmTextField.getText()); 142 controller.setBPM(bpm); 143 } else if (event.getSource() == increaseBPMButton) { 144 controller.increaseBPM(); 145 } else if (event.getSource() == decreaseBPMButton) { 146 controller.decreaseBPM(); 147 } 148 } 149 150 public void updateBPM() { 151 if (model != null) { 152 int bpm = model.getBPM(); 153 if (bpm == 0) { 154 if (bpmOutputLabel != null) { 155 bpmOutputLabel.setText("offline"); 156 } 157 } else { 158 if (bpmOutputLabel != null) { 159 bpmOutputLabel.setText("Current BPM: " + model.getBPM()); 160 } 161 } 162 } 163 } 164 165 public void updateBeat() { 166 if (beatBar != null) { 167 beatBar.setValue(100); 168 } 169 } 170}