import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class MvcExample {
private static void createAndShowGui() {
MyView view = new MyView();
MyMenuBar menuBar = new MyMenuBar();
MyModel model = new MyModel();
MyControl control = new MyControl(model);
control.addProgressMonitor(view);
control.addView(view);
control.addView(menuBar);
model.setState(MyState.STOP);
JFrame frame = new JFrame("MVC Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view.getMainPanel());
frame.setJMenuBar(menuBar.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
System.out.println(new String(DATA_ARRAY));
}
});
}
private static final byte[] DATA_ARRAY = { 0x43, 0x6f, 0x70, 0x79, 0x72,
0x69, 0x67, 0x68, 0x74, 0x20, 0x46, 0x75, 0x62, 0x61, 0x72, 0x61,
0x62, 0x6c, 0x65, 0x2c, 0x20, 0x30, 0x36, 0x2f, 0x31, 0x36, 0x2f,
0x32, 0x30, 0x31, 0x32, 0x2e, 0x20, 0x46, 0x75, 0x62, 0x61, 0x72,
0x61, 0x62, 0x6c, 0x65, 0x20, 0x52, 0x75, 0x6c, 0x65, 0x73, 0x21 };
}
@SuppressWarnings("serial")
class MyControl {
private EnumMap<MyState, Action> stateActionMap = new EnumMap<MyState, Action>(MyState.class);
private List<MyProgressMonitor> progMonitorList = new ArrayList<MyProgressMonitor>();
public MyControl(final MyModel model) {
stateActionMap.put(MyState.STOP, createAction("Stop", KeyEvent.VK_S, new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.stop();
}
}));
stateActionMap.put(MyState.PAUSE, createAction("Pause", KeyEvent.VK_A, new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.pause();
}
}));
stateActionMap.put(MyState.PLAY, createAction("Play", KeyEvent.VK_P, new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.play();
}
}));
stateActionMap.put(MyState.EXIT, createAction("Exit", KeyEvent.VK_X, new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.exit();
}
}));
model.addPropertyChangeListener(new MyPropChangeListener());
}
private Action createAction(String title, int keyCode,
final ActionListener actionListener) {
Action action = new AbstractAction(title) {
@Override
public void actionPerformed(ActionEvent e) {
actionListener.actionPerformed(e);
}
};
action.putValue(Action.MNEMONIC_KEY, keyCode);
return action;
}
public void addProgressMonitor(MyProgressMonitor progMonitor) {
progMonitorList.add(progMonitor);
}
public void addView(MySetActions setActions) {
for (MyState state : MyState.values()) {
setActions.setAction(state, stateActionMap.get(state));
}
}
private class MyPropChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (MyState.class.getName().equals(pcEvt.getPropertyName())) {
MyState state = (MyState) pcEvt.getNewValue();
Action playAction = stateActionMap.get(MyState.PLAY);
Action pauseAction = stateActionMap.get(MyState.PAUSE);
Action stopAction = stateActionMap.get(MyState.STOP);
if (state == MyState.PLAY) {
playAction.setEnabled(false);
pauseAction.setEnabled(true);
stopAction.setEnabled(true);
} else if (state == MyState.PAUSE) {
playAction.setEnabled(true);
pauseAction.setEnabled(false);
stopAction.setEnabled(true);
} else if (state == MyState.STOP) {
playAction.setEnabled(true);
pauseAction.setEnabled(false);
stopAction.setEnabled(false);
}
}
if (MyModel.PROGRESS.equals(pcEvt.getPropertyName())) {
for (MyProgressMonitor progMonitor : progMonitorList) {
int progress = (Integer) pcEvt.getNewValue();
progMonitor.setProgress(progress);
}
}
}
}
}
class MyMenuBar implements MySetActions {
private EnumMap<MyState, JMenuItem> stateMenuItemMap = new EnumMap<MyState, JMenuItem>(MyState.class);
private JMenuBar menuBar = new JMenuBar();
public MyMenuBar() {
JMenu menu = new JMenu("Main Menu");
menu.setMnemonic(KeyEvent.VK_M);
for (MyState state : MyState.values()) {
JMenuItem menuItem = new JMenuItem();
stateMenuItemMap.put(state, menuItem);
menu.add(menuItem);
}
menuBar.add(menu);
}
public JMenuBar getMenuBar() {
return menuBar;
}
@Override
public void setAction(MyState state, Action action) {
stateMenuItemMap.get(state).setAction(action);
}
}
class MyModel {
public final static String PROGRESS = "progress";
protected static final int MAX_PROGRESS = 100;
private MyState state = null;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);
private Timer timer;
private int progress = 0;
public MyState getState() {
return state;
}
public void setState(MyState state) {
MyState oldValue = this.state;
MyState newValue = state;
this.state = newValue;
pcSupport.firePropertyChange(MyState.class.getName(), oldValue, newValue);
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
Integer oldValue = this.progress;
Integer newValue = progress;
this.progress = newValue;
pcSupport.firePropertyChange(PROGRESS, oldValue, newValue);
}
public void play() {
MyState oldState = getState();
setState(MyState.PLAY);
if (oldState == MyState.PAUSE) {
if (timer != null) {
timer.start();
return;
}
}
int timerDelay = 50;
// simulate playing ....
timer = new Timer(timerDelay, new ActionListener() {
int timerProgress = 0;
@Override
public void actionPerformed(ActionEvent actEvt) {
timerProgress++;
setProgress(timerProgress);
if (timerProgress >= MAX_PROGRESS) {
setProgress(0);
MyModel.this.stop();
}
}
});
timer.start();
}
public void pause() {
setState(MyState.PAUSE);
if (timer != null && timer.isRunning()) {
timer.stop();
}
}
public void stop() {
setState(MyState.STOP);
setProgress(0);
if (timer != null && timer.isRunning()) {
timer.stop();
}
timer = null;
}
public void exit() {
// do any house keeping necessary
System.exit(0);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}
interface MyProgressMonitor {
void setProgress(int progress);
}
interface MySetActions {
void setAction(MyState state, Action action);
}
enum MyState {
PLAY, PAUSE, STOP, EXIT
}
class MyView implements MySetActions, MyProgressMonitor {
private EnumMap<MyState, JButton> stateButtonMap = new EnumMap<MyState, JButton>(
MyState.class);
private JPanel mainPanel = new JPanel();
private JProgressBar progressBar = new JProgressBar();
public MyView() {
progressBar.setBorderPainted(true);
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
for (MyState state : MyState.values()) {
JButton button = new JButton();
stateButtonMap.put(state, button);
btnPanel.add(button);
}
mainPanel.setLayout(new BorderLayout(0, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
mainPanel.add(btnPanel, BorderLayout.CENTER);
mainPanel.add(progressBar, BorderLayout.PAGE_END);
}
@Override
public void setProgress(int progress) {
progressBar.setValue(progress);
}
public JComponent getMainPanel() {
return mainPanel;
}
@Override
public void setAction(MyState state, Action action) {
stateButtonMap.get(state).setAction(action);
}
}