Package org.jampa.controllers.ui

Source Code of org.jampa.controllers.ui.onPlaymodeChange

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.controllers.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.factories.ToolbarImageFactory;
import org.jampa.gui.actions.ChangePlaymodeAction;
import org.jampa.gui.actions.PauseAction;
import org.jampa.gui.actions.PlayNextAction;
import org.jampa.gui.actions.PlayPreviousAction;
import org.jampa.gui.actions.SeekAction;
import org.jampa.gui.actions.SetVolumeAction;
import org.jampa.gui.actions.StopAction;
import org.jampa.gui.actions.VolumeMuteAction;
import org.jampa.gui.controlcontributions.TimeToolbarContribution;
import org.jampa.gui.controlcontributions.VolumeBarToolbarContribution;
import org.jampa.gui.translations.Messages;
import org.jampa.model.IAudioItem;
import org.jampa.model.radio.RadioItem;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.utils.Constants;

public class ToolbarController implements PropertyChangeListener {
 
  public enum ToolBarSize {
    LARGE("0"),
    MEDIUM("1"),
    SMALL("2");   
   
    protected String _value;
   
    ToolBarSize(String value) {
      this._value = value;
    }
   
    public String getValue() {
      return this._value;
    }
   
    public static ToolBarSize getValueOf(String mode) {
      if (mode.equals("0")) { //$NON-NLS-1$
        return LARGE;
      } else if (mode.equals("1")) { //$NON-NLS-1$
        return MEDIUM;
      } else if (mode.equals("2")) { //$NON-NLS-1$
        return SMALL;
      }
      return LARGE;
    }
  }
 
  private IAction stopAction;
  private IAction pauseAction;
  private IAction seekMinusAction;
  private IAction previousAction;
  private IAction nextAction;
  private IAction seekPlusAction;
 
  private IAction volumeMuteAction;
  private IAction volumeUpAction;
  private IAction volumeDownAction;
 
  private IAction normalPlayModeAction;
  private IAction repeatPlayModeAction;
  private IAction randomPlayModeAction;
 
  private VolumeBarToolbarContribution volumeBar;
 
  private List<Image> _imageToDispose;
 
  private ToolBarSize _toolbarSize;
 
  public ToolbarController() {
    _imageToDispose = new ArrayList<Image>();
    _toolbarSize = ToolBarSize.getValueOf(Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.PLAYERVIEW_TOOLBAR_SIZE));
   
    Controller.getInstance().getEventController().addGeneralEventsListener(this);
    Controller.getInstance().getEventController().addAudioItemChangeListener(this);
  }

  public void buildActions() {
   
    /**
     * Controls actions.
     */
    int seekStep = Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP);
   
    stopAction = new Action(Messages.getString("PlayerView.BtnStop"), SWT.NONE) {
      public void run() {
        new StopAction().run();
      }
    };
    stopAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("stop", _toolbarSize));
    stopAction.setEnabled(false);
   
    pauseAction = new Action(Messages.getString("PlayerView.BtnPause"), SWT.TOGGLE) {
      public void run() {
        new PauseAction().run();
      }
    };
    pauseAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("pause", _toolbarSize));
    pauseAction.setEnabled(false);   
   
    seekMinusAction = new Action("-" + Integer.toString(seekStep) + " " + Messages.getString("PlayerView.Seconds"), SWT.NONE) {
      public void run() {
        new SeekAction(-Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP)).run();
      }
    };
    seekMinusAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("seekminus", _toolbarSize));
    seekMinusAction.setEnabled(false);
   
    previousAction = new Action(Messages.getString("PlayerView.BtnPrevious"), SWT.NONE) {
      public void run() {
        new PlayPreviousAction().run();
      }
    };
    previousAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("previous", _toolbarSize));
    previousAction.setEnabled(false);
   
    nextAction = new Action(Messages.getString("PlayerView.BtnNext"), SWT.NONE) {
      public void run() {
        new PlayNextAction().run();
      }
    };
    nextAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("next", _toolbarSize));
    nextAction.setEnabled(false);
   
    seekPlusAction = new Action("+" + Integer.toString(seekStep) + " " + Messages.getString("PlayerView.Seconds"), SWT.NONE) {
      public void run() {
        new SeekAction(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP)).run();
      }
    };
    seekPlusAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("seekplus", _toolbarSize));
    seekPlusAction.setEnabled(false);
   
    /**
     * Volume actions.
     */
    volumeMuteAction = new Action(Messages.getString("PlayerView.VolumeMute"), SWT.TOGGLE) {
      public void run() {
        new VolumeMuteAction().run();
      }
    };
    volumeMuteAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("volumemute", _toolbarSize));
    volumeMuteAction.setEnabled(false);
   
    volumeUpAction = new Action(Messages.getString("PlayerView.VolumeUp")) {
      public void run() {
        new SetVolumeAction(volumeBar.getSelection() + 10).run();
      }
    };
    volumeUpAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("volumeup", _toolbarSize));
   
    volumeDownAction = new Action(Messages.getString("PlayerView.VolumeDown")) {
      public void run() {
        new SetVolumeAction(volumeBar.getSelection() - 10).run();
      }
    };
    volumeDownAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("volumedown", _toolbarSize));
   
    /**
     * Play mode actions.
     */
    normalPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeNormal")) { //$NON-NLS-1$
      public void run() {
        changePlayMode(normalPlayModeAction);
      }
    };
    normalPlayModeAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("normal", _toolbarSize));
   
    repeatPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeRepeat")) { //$NON-NLS-1$
      public void run() {
        changePlayMode(repeatPlayModeAction);
      }
    };
    repeatPlayModeAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("repeat", _toolbarSize));
   
    randomPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeRandom")) { //$NON-NLS-1$
      public void run() {
        changePlayMode(randomPlayModeAction);
      }
    };
    randomPlayModeAction.setImageDescriptor(ToolbarImageFactory.getInstance().getImageDescriptor("random", _toolbarSize));
   
    setPlaymodeUI();
  }
 
  private void setPlaymodeUI() {
    switch (Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAY_MODE)) {
      case 0 : normalPlayModeAction.setChecked(true); repeatPlayModeAction.setChecked(false); randomPlayModeAction.setChecked(false); break;
      case 1 : normalPlayModeAction.setChecked(false); repeatPlayModeAction.setChecked(true); randomPlayModeAction.setChecked(false); break;
      case 2 : normalPlayModeAction.setChecked(false); repeatPlayModeAction.setChecked(false); randomPlayModeAction.setChecked(true); break;
      default : normalPlayModeAction.setChecked(true); repeatPlayModeAction.setChecked(false); randomPlayModeAction.setChecked(false); new ChangePlaymodeAction(0).run();
    }
  }
 
  public void fillToolbar(ICoolBarManager coolBar) {     
    IToolBarManager controlToolbar1 = new ToolBarManager(coolBar.getStyle());
    coolBar.add(controlToolbar1);
   
    controlToolbar1.add(stopAction);
    controlToolbar1.add(pauseAction);

    controlToolbar1.update(true);
   
    IToolBarManager controlToolbar2 = new ToolBarManager(coolBar.getStyle());
    coolBar.add(controlToolbar2);

    controlToolbar2.add(seekMinusAction);
    controlToolbar2.add(previousAction);
    controlToolbar2.add(nextAction);
    controlToolbar2.add(seekPlusAction);

    /*
    IToolBarManager timeToolbar = new ToolBarManager(coolBar.getStyle());
    coolBar.add(timeToolbar);
    */
   
    TimeToolbarContribution timeToolbarContribution = new TimeToolbarContribution();
    controlToolbar2.add(timeToolbarContribution);

    IToolBarManager volumeToolbar = new ToolBarManager(coolBar.getStyle());
    coolBar.add(volumeToolbar);

    volumeBar = new VolumeBarToolbarContribution();

    volumeToolbar.add(volumeMuteAction);     
    volumeToolbar.add(volumeDownAction);
    volumeToolbar.add(volumeUpAction);
    volumeToolbar.add(volumeBar);

    IToolBarManager playmodeToolbar = new ToolBarManager(coolBar.getStyle());
    coolBar.add(playmodeToolbar);

    playmodeToolbar.add(normalPlayModeAction);
    playmodeToolbar.add(repeatPlayModeAction);
    playmodeToolbar.add(randomPlayModeAction);
  }
 
  private void changePlayMode(IAction action) {
    if (action == normalPlayModeAction) {
      if (normalPlayModeAction.isChecked()) {
        repeatPlayModeAction.setChecked(false);
        randomPlayModeAction.setChecked(false);
        new ChangePlaymodeAction(0).run();
      } else {
        repeatPlayModeAction.setChecked(true);
        randomPlayModeAction.setChecked(false);
        new ChangePlaymodeAction(1).run();       
      }
    } else if (action == repeatPlayModeAction) {
      if (repeatPlayModeAction.isChecked()) {
        normalPlayModeAction.setChecked(false);
        randomPlayModeAction.setChecked(false);
        new ChangePlaymodeAction(1).run();       
      } else {
        normalPlayModeAction.setChecked(false);
        randomPlayModeAction.setChecked(true);
        new ChangePlaymodeAction(2).run();   
      }
    } else if (action == randomPlayModeAction) {
      if (randomPlayModeAction.isChecked()) {
        normalPlayModeAction.setChecked(false);
        repeatPlayModeAction.setChecked(false);
        new ChangePlaymodeAction(2).run();       
      } else {
        normalPlayModeAction.setChecked(true);
        repeatPlayModeAction.setChecked(false);
        new ChangePlaymodeAction(0).run();       
      }
    }   
  }
 
  private void enableControlActions(boolean newState) {
    stopAction.setEnabled(newState);
    pauseAction.setEnabled(newState);
    seekMinusAction.setEnabled(newState);
    previousAction.setEnabled(newState);
    nextAction.setEnabled(newState);
    seekPlusAction.setEnabled(newState);
  }
 
  private void disableControlActionsForRadioItem() {
    seekMinusAction.setEnabled(false);
    seekPlusAction.setEnabled(false);
  }
 
  private void onPlayNewItem(IAudioItem newItem) {
    volumeMuteAction.setChecked(false);
    volumeMuteAction.setEnabled(true);
   
    enableControlActions(true);
   
    if (newItem instanceof RadioItem) {
      disableControlActionsForRadioItem();
    }
   
    pauseAction.setChecked(false);
  }
 
  private void onStopPlayback() {
    volumeMuteAction.setChecked(false);
    volumeMuteAction.setEnabled(false);
   
    enableControlActions(false);
   
    pauseAction.setChecked(false);
  }
 
  private void onPausePlayback(IAudioItem item) {
    boolean paused = Controller.getInstance().getEngine().isPaused();
    pauseAction.setChecked(paused);
    seekMinusAction.setEnabled(!paused);
    seekPlusAction.setEnabled(!paused);
   
    if (item instanceof RadioItem) {
      disableControlActionsForRadioItem();
    }
  }
 
  private void onVolumeChange() {
    volumeMuteAction.setChecked(Controller.getInstance().getEngine().isMuted());
  }

  @Override
  public void propertyChange(PropertyChangeEvent arg0) {
    if (arg0.getPropertyName().equals(EventConstants.EVT_PLAY_NEW_AUDIO_ITEM)) {
      class onPlayNewItem implements Runnable {
        IAudioItem _newItem;
        public onPlayNewItem(IAudioItem newItem) {
          _newItem = newItem;
        }
        public void run() {
          onPlayNewItem(_newItem);
        }
      }
      Display.getDefault().asyncExec(new onPlayNewItem((IAudioItem) arg0.getNewValue()));
    }
    if (arg0.getPropertyName().equals(EventConstants.EVT_STOP_PLAYBACK)) {
      class onStopPlayback implements Runnable {       
        public void run() {
          onStopPlayback();
        }
      }
      Display.getDefault().asyncExec(new onStopPlayback());
    }   
    if (arg0.getPropertyName().equals(EventConstants.EVT_PAUSE_PLAYBACK)) {
      class onPausePlayback implements Runnable {
        IAudioItem _newItem;
        public onPausePlayback(IAudioItem newItem) {
          _newItem = newItem;
        }
        public void run() {
          onPausePlayback(_newItem);
        }
      }
      Display.getDefault().asyncExec(new onPausePlayback((IAudioItem) arg0.getNewValue()));
    }
    if (arg0.getPropertyName().equals(EventConstants.EVT_VOLUME_CHANGE)) {
      class onVolumeChange implements Runnable {       
        public void run() {
          onVolumeChange();
        }
      }
      Display.getDefault().asyncExec(new onVolumeChange());
    }
    if (arg0.getPropertyName().equals(Constants.EVT_PLAY_MODE_CHANGE)) {
      class onPlaymodeChange implements Runnable {       
        public void run() {
          setPlaymodeUI();
        }
      }
      Display.getDefault().asyncExec(new onPlaymodeChange());
    }
  }
 
  public void dispose() {
    Iterator<Image> imageIter = _imageToDispose.iterator();
    while (imageIter.hasNext()) {
      imageIter.next().dispose();
    }
   
    Controller.getInstance().getEventController().removeGeneralEventsListener(this);
    Controller.getInstance().getEventController().removeAudioItemChangeListener(this);
  }
 
}
TOP

Related Classes of org.jampa.controllers.ui.onPlaymodeChange

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.