source: trunk/src/org/apollo/gui/PlaybackControlPopup.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 8.8 KB
Line 
1package org.apollo.gui;
2
3import java.awt.Dimension;
4import java.awt.Graphics2D;
5import java.awt.GridBagConstraints;
6import java.awt.GridBagLayout;
7import java.awt.Insets;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10
11import javax.swing.JButton;
12import javax.swing.JPanel;
13import javax.swing.JSlider;
14import javax.swing.JToggleButton;
15import javax.swing.event.ChangeEvent;
16import javax.swing.event.ChangeListener;
17
18import org.apollo.io.IconRepository;
19import org.expeditee.core.Image;
20import org.expeditee.core.bounds.AxisAlignedBoxBounds;
21import org.expeditee.gio.swing.SwingConversions;
22import org.expeditee.gio.swing.SwingMiscManager;
23import org.expeditee.gui.Popup;
24import org.expeditee.gui.PopupManager.ExpandShrinkAnimator;
25
26/**
27 * A pure GUI for playback and mixing popup used for tracks and linked tracks.
28 * Does not implement beahviour - just the gui.
29 *
30 * @author Brook Novak
31 *
32 */
33public abstract class PlaybackControlPopup extends Popup implements ActionListener
34{
35 public JPanel panel;
36 public JButton playPauseButton;
37 public JButton stopButton;
38 public JButton rewindButton;
39 public JToggleButton muteButton;
40 public JToggleButton soloButton;
41 public JButton miscButton;
42 public JSlider volumeSlider;
43
44 private boolean isUpdatingGUI = false;
45
46 private static final int BUTTON_SIZE = 40;
47
48 protected PlaybackControlPopup()
49 {
50 super(new ExpandShrinkAnimator());
51
52 panel = new JPanel(new GridBagLayout());
53
54 playPauseButton = new JButton();
55 playPauseButton.setActionCommand("playpause");
56 playPauseButton.addActionListener(this);
57 SwingMiscManager.setJButtonIcon(playPauseButton, IconRepository.getIcon("play.png"));
58 playPauseButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
59 playPauseButton.setToolTipText("Play selection / Pause");
60
61 stopButton = new JButton();
62 stopButton.setEnabled(false);
63 stopButton.addActionListener(this);
64 stopButton.setActionCommand("stop");
65 SwingMiscManager.setJButtonIcon(stopButton, IconRepository.getIcon("stop.png"));
66 stopButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
67 stopButton.setToolTipText("Stop playback");
68
69 rewindButton = new JButton();
70 rewindButton.addActionListener(this);
71 rewindButton.setActionCommand("rewind");
72 SwingMiscManager.setJButtonIcon(rewindButton, IconRepository.getIcon("rewind.png"));
73 rewindButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
74 rewindButton.setToolTipText("Rewind to start");
75
76 // Icon changes
77 muteButton = new JToggleButton();
78 SwingMiscManager.setJButtonSelectedIcon(muteButton, IconRepository.getIcon("volmute.png"));
79 muteButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
80 muteButton.setToolTipText("Toggle mute");
81 muteButton.addChangeListener(new ChangeListener() {
82 public void stateChanged(ChangeEvent e) {
83 if (!PlaybackControlPopup.this.isUpdatingGUI) {
84 muteChanged();
85 }
86 }
87 });
88
89 soloButton = new JToggleButton();
90 SwingMiscManager.setJButtonIcon(soloButton, IconRepository.getIcon("solo.png"));
91 SwingMiscManager.setJButtonSelectedIcon(soloButton, IconRepository.getIcon("soloon.png"));
92 soloButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
93 soloButton.setToolTipText("Toggle solo");
94 soloButton.addChangeListener(new ChangeListener() {
95 public void stateChanged(ChangeEvent e) {
96 if (!PlaybackControlPopup.this.isUpdatingGUI) {
97 soloChanged();
98 }
99 }
100 });
101
102 miscButton = new JButton();
103 miscButton.addActionListener(this);
104 miscButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
105
106 final int VOLUME_SPACING = 6;
107 volumeSlider = new JSlider(JSlider.HORIZONTAL);
108 volumeSlider.setPreferredSize(new Dimension(2 * (BUTTON_SIZE - VOLUME_SPACING), BUTTON_SIZE));
109 volumeSlider.setMinimum(0);
110 volumeSlider.setMaximum(100);
111 volumeSlider.addChangeListener(new ChangeListener() {
112 public void stateChanged(ChangeEvent e) {
113 if (!PlaybackControlPopup.this.isUpdatingGUI) {
114 volumeChanged();
115 }
116 // Update the icons
117 updateButtonGUI();
118 }
119 });
120
121
122 // Create the toolbar
123 GridBagConstraints c = new GridBagConstraints();
124 c.gridx = 0;
125 c.gridy = 0;
126 c.fill = GridBagConstraints.BOTH;
127 panel.add(playPauseButton, c);
128
129 c = new GridBagConstraints();
130 c.gridx = 1;
131 c.gridy = 0;
132 c.fill = GridBagConstraints.BOTH;
133 panel.add(stopButton, c);
134
135 c = new GridBagConstraints();
136 c.gridx = 2;
137 c.gridy = 0;
138 c.fill = GridBagConstraints.BOTH;
139 panel.add(rewindButton, c);
140
141 c = new GridBagConstraints();
142 c.gridx = 3;
143 c.gridy = 0;
144 c.fill = GridBagConstraints.BOTH;
145 panel.add(soloButton, c);
146
147 c = new GridBagConstraints();
148 c.gridx = 4;
149 c.gridy = 0;
150 c.fill = GridBagConstraints.BOTH;
151 panel.add(muteButton, c);
152
153 c = new GridBagConstraints();
154 c.gridx = 5;
155 c.gridy = 0;
156 c.fill = GridBagConstraints.BOTH;
157 c.insets = new Insets(0,VOLUME_SPACING,0,VOLUME_SPACING);
158 panel.add(volumeSlider, c);
159
160 c = new GridBagConstraints();
161 c.gridx = 7;
162 c.gridy = 0;
163 c.fill = GridBagConstraints.BOTH;
164 panel.add(miscButton, c);
165
166//
167// GridBagConstraints c = new GridBagConstraints();
168// c.gridx = 0;
169// c.gridy = 0;
170// c.fill = GridBagConstraints.BOTH;
171// this.add(soloButton, c);
172//
173// c = new GridBagConstraints();
174// c.gridx = 1;
175// c.gridy = 0;
176// c.fill = GridBagConstraints.BOTH;
177// this.add(muteButton, c);
178//
179// c = new GridBagConstraints();
180// c.gridx = 2;
181// c.gridy = 0;
182// c.gridwidth = 2;
183// c.insets = new Insets(0,VOLUME_SPACING,0,VOLUME_SPACING);
184// c.fill = GridBagConstraints.BOTH;
185// this.add(volumeSlider, c);
186//
187// c = new GridBagConstraints();
188// c.gridx = 0;
189// c.gridy = 1;
190// c.fill = GridBagConstraints.BOTH;
191// this.add(playPauseButton, c);
192//
193// c = new GridBagConstraints();
194// c.gridx = 1;
195// c.gridy = 1;
196// c.fill = GridBagConstraints.BOTH;
197// this.add(stopButton, c);
198//
199// c = new GridBagConstraints();
200// c.gridx = 2;
201// c.gridy = 1;
202// c.fill = GridBagConstraints.BOTH;
203// this.add(rewindButton, c);
204//
205// c = new GridBagConstraints();
206// c.gridx = 3;
207// c.gridy = 1;
208// c.fill = GridBagConstraints.BOTH;
209// this.add(miscButton, c);
210//
211// this.setSize(BUTTON_SIZE * 4, BUTTON_SIZE * 2);
212
213 panel.setSize(BUTTON_SIZE * 8, BUTTON_SIZE);
214
215 panel.doLayout();
216
217 }
218
219 /**
220 * Sets the mute icon to represent the current volume value in the slider.
221 * Note: this is not the icon if mute is on.
222 */
223 private void updateButtonGUI() {
224
225 Image newIcon = null;
226 if (volumeSlider.getValue() <= 25)
227 newIcon = IconRepository.getIcon("vol25.png");
228 else if (volumeSlider.getValue() <= 50)
229 newIcon = IconRepository.getIcon("vol50.png");
230 else if (volumeSlider.getValue() <= 75)
231 newIcon = IconRepository.getIcon("vol75.png");
232 else // maxing
233 newIcon = IconRepository.getIcon("vol100.png");
234
235 SwingMiscManager.setJButtonIcon(muteButton, newIcon);
236 }
237
238 @Override
239 protected void paintInternal()
240 {
241 Graphics2D g = SwingMiscManager.getIfUsingSwingGraphicsManager().getCurrentSurface();
242 AxisAlignedBoxBounds bounds = getBounds();
243
244 g.translate(bounds.getMinX(), bounds.getMinY());
245 panel.paint(g);
246 g.translate(-bounds.getMinX(), -bounds.getMinY());
247 }
248
249 @Override
250 public AxisAlignedBoxBounds getFullBounds()
251 {
252 return SwingConversions.fromSwingRectangle(panel.getBounds());
253 }
254
255 public abstract void actionPerformed(ActionEvent e);
256
257 /**
258 * Invoke when the slider changes via the user (i.e. not from a model-changed to event)
259 */
260 protected abstract void volumeChanged();
261
262 /**
263 * Invoke when mute button toggles via the user (i.e. not from a model-changed to event)
264 */
265 protected abstract void muteChanged();
266
267 /**
268 * Invoke when solo button toggles via the user (i.e. not from a model-changed to event)
269 */
270 protected abstract void soloChanged();
271
272
273 /**
274 * Updates the volume GUI.
275 * {@link #volumeChanged()} is not raised as a result of this call.
276 *
277 * @param vol
278 * The volume ranging from 0 - 100. Clamped.
279 */
280 public void updateVolume(int vol) {
281
282 if (volumeSlider.getValue() == vol) return;
283
284 // Clamp
285 if(vol < 0) vol = 0;
286 else if (vol > 100) vol = 100;
287
288 isUpdatingGUI = true;
289
290 volumeSlider.setValue(vol);
291
292 isUpdatingGUI = false;
293 }
294
295
296 /**
297 * Updates the mute button GUI.
298 * {@link #muteChanged()} is not raised as a result of this call.
299 *
300 * @param isMuted
301 * True if set gui to muted.
302 */
303 public void updateMute(boolean isMuted) {
304
305 if (muteButton.isSelected() == isMuted) return;
306
307 isUpdatingGUI = true;
308
309 muteButton.setSelected(isMuted);
310
311 isUpdatingGUI = false;
312 }
313
314
315 /**
316 * Updates the solo button GUI.
317 * {@link #muteChanged()} is not raised as a result of this call.
318 *
319 * @param isSolo
320 * True if set gui to solo on.
321 */
322 public void updateSolo(boolean isSolo) {
323
324 if (soloButton.isSelected() == isSolo) return;
325
326 isUpdatingGUI = true;
327
328 soloButton.setSelected(isSolo);
329
330 isUpdatingGUI = false;
331 }
332
333
334}
Note: See TracBrowser for help on using the repository browser.