Package org.jampa.gui.controlcontributions

Source Code of org.jampa.gui.controlcontributions.setPauseMode

/*
* 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.gui.controlcontributions;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.actions.ChangePositionAction;
import org.jampa.gui.components.sliders.SliderWrapper;
import org.jampa.gui.translations.Messages;
import org.jampa.model.IAudioItem;
import org.jampa.utils.Utils;

public class TimeToolbarContribution extends WorkbenchWindowControlContribution implements PropertyChangeListener {

  private int _totalTime = 0;
  private boolean _boSliderMove = false;
 
  private Label laEllapsedTime;
  private Label laTotalTime;
  private SliderWrapper scSlider;
 
  public TimeToolbarContribution(String id) {
    super(id);
    Controller.getInstance().getEventController().addAudioItemChangeListener(this);
  }
 
  public TimeToolbarContribution() {
    super();
    Controller.getInstance().getEventController().addAudioItemChangeListener(this);
  }
 
  @Override
  protected Control createControl(Composite parent) {   
   
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout containerGL = new GridLayout(3, false);
    containerGL.verticalSpacing = 0;
    containerGL.marginTop = -5;
    containerGL.marginBottom = -5;
    container.setLayout(containerGL);
    GridData containerGD = new GridData(SWT.NONE, SWT.CENTER, false, true);
    container.setLayoutData(containerGD);
   
    laEllapsedTime = new Label(container, SWT.NONE);
    laEllapsedTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    laEllapsedTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
   
    GridData sliderGD = new GridData();
    sliderGD.minimumWidth = 250;
    sliderGD.grabExcessHorizontalSpace = true;
    sliderGD.horizontalAlignment = SWT.FILL;   
    sliderGD.verticalAlignment = SWT.CENTER;
    scSlider = new SliderWrapper(container, SWT.HORIZONTAL);
    scSlider.setLayoutData(sliderGD);

    laTotalTime = new Label(container, SWT.NONE);
    laTotalTime.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    laTotalTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
   
    scSlider.addMouseListener(new MouseListener() {
      public void mouseDown(MouseEvent e) {
        _boSliderMove = true;
      }
     
      public void mouseUp(MouseEvent e) {
        _boSliderMove = false;
        new ChangePositionAction(scSlider.getSelection()).run();
      }
     
      public void mouseDoubleClick(MouseEvent e) {
      }
     
    });
   
    scSlider.addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent e) {
        if (_boSliderMove) {
          laEllapsedTime.setText(Utils.milliseconds2String(scSlider.getSelection()));
          laTotalTime.setText(Utils.seconds2String(_totalTime));
        }
      }
    });
   
    enableComponents(false);
   
    return container;
  }
 
  private void enableComponents(boolean value) {
    scSlider.setEnabled(value);
    laEllapsedTime.setEnabled(value);
    laTotalTime.setEnabled(value);
  }

  private void setTime(int time) {
    if (!Controller.getInstance().isAplicationStopping()) {
      if (!_boSliderMove) {       
        laEllapsedTime.setText(Utils.milliseconds2String(time));
        laTotalTime.setText(Utils.seconds2String(_totalTime));
        scSlider.setSelection(time);
      }
    }
  }
 
  private void setTotalTime(int time) {
    if (!Controller.getInstance().isAplicationStopping()) {
      if (!_boSliderMove) {
        _totalTime = time / 1000;       
        scSlider.setMaximum(time);
      }
      if (_totalTime == 0) {
        scSlider.setEnabled(false);
      }
    }
  }
 
  private void updateInformations(IAudioItem item) {
    if (!Controller.getInstance().isAplicationStopping()) {     
      enableComponents(true);
      scSlider.setMinimum(0);
      scSlider.setSelection(0);
    }
  }
 
  private void enableComponentsForPauseMode(boolean value) {
    scSlider.setEnabled(value);
  }
 
 
  private void resetInformations() {
    if (!Controller.getInstance().isAplicationStopping()) {     
      enableComponents(false);
      _totalTime = 0;
      scSlider.setMinimum(0);
      scSlider.setMaximum(0);
      scSlider.setSelection(0);

      laEllapsedTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
      laTotalTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
    }
  }
 
  @Override
  public void propertyChange(PropertyChangeEvent arg0) {
    if (arg0.getPropertyName().equals(EventConstants.EVT_PLAY_NEW_AUDIO_ITEM)) {
      class updateInformations implements Runnable {
        IAudioItem _newItem;
        public updateInformations(IAudioItem newItem) {
          _newItem = newItem;
        }
        public void run() {
          updateInformations(_newItem);
        }
      }
      Display.getDefault().asyncExec(new updateInformations((IAudioItem) arg0.getNewValue()));
    }
    if (arg0.getPropertyName().equals(EventConstants.EVT_ELAPSED_TIME_CHANGE)) {
      class setTime implements Runnable {
        int newTime = 0;
        public setTime(int newTime){
          this.newTime = newTime;
        }
        public void run() {
          setTime(newTime);
        }
      }
      Display.getDefault().asyncExec(new setTime(((Integer) arg0.getNewValue()).intValue()));
    }
    if (arg0.getPropertyName().equals(EventConstants.EVT_TOTAL_TIME_CHANGE)) {
      class setTotalTime implements Runnable {
        int newTime = 0;
        public setTotalTime(int newTime) {
          this.newTime = newTime;
        }
        public void run() {
          setTotalTime(newTime);
        }
      }
      Display.getDefault().asyncExec(new setTotalTime(((Integer) arg0.getNewValue()).intValue()));
   
    if (arg0.getPropertyName().equals(EventConstants.EVT_PAUSE_PLAYBACK)) {
      class setPauseMode implements Runnable {
        public void run() {
          enableComponentsForPauseMode(!Controller.getInstance().getEngine().isPaused());
        }
      }
      Display.getDefault().asyncExec(new setPauseMode());
    }
    if (arg0.getPropertyName().equals(EventConstants.EVT_STOP_PLAYBACK)) {
      class resetInformations implements Runnable {
        public void run() {
          resetInformations();
        }
      }
      Display.getDefault().asyncExec(new resetInformations());
    }
  }
 
  public void dispose() {
    Controller.getInstance().getEventController().removeAudioItemChangeListener(this)
    super.dispose();
  }

}
TOP

Related Classes of org.jampa.gui.controlcontributions.setPauseMode

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.