Package org.jampa.gui.views

Source Code of org.jampa.gui.views.resetInformations

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

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;
import org.jampa.Activator;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.dialogs.MetaDataDialog;
import org.jampa.gui.dialogs.PodcastItemPropertiesDialog;
import org.jampa.gui.dialogs.RadioItemPropertiesDialog;
import org.jampa.gui.translations.Messages;
import org.jampa.model.IAudioItem;
import org.jampa.model.playlists.AudioItem;
import org.jampa.model.podcasts.PodcastItem;
import org.jampa.model.radio.IRadioItem;
import org.jampa.model.radio.RadioItem;
import org.jampa.utils.Utils;

public class PropertyView extends ViewPart implements PropertyChangeListener {
 
  public static String ID = "Jampa.PropertyView"; //$NON-NLS-1$
 
  private Table propertiesTable; 
 
  private IAction editPropertiesAction;
 
  private Composite _parent;
 
  private Font boldFont = null;   
 
  private IAudioItem _currentAudioItem = null;

  public PropertyView() {   
    Controller.getInstance().getEventController().addAudioItemChangeListener(this);
    Controller.getInstance().getEventController().addLibraryChangeListener(this);
    Controller.getInstance().getEventController().addRadioChangeListeners(this);
  }

  @Override
  public void createPartControl(Composite parent) {
    _parent = parent;
    this.setPartName(Messages.getString("PropertyView.ViewTitle")); //$NON-NLS-1$

    boldFont = new Font(parent.getDisplay(),
        parent.getFont().getFontData()[0].getName(),
        parent.getFont().getFontData()[0].getHeight(),
        SWT.BOLD);
   
    parent.setLayout(new FillLayout());
   
    propertiesTable = new Table(parent, SWT.BORDER);
    propertiesTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
   
    TableLayout layout = new TableLayout();
    layout.addColumnData(new ColumnWeightData(25, 10, true));
    layout.addColumnData(new ColumnWeightData(75, 10, true));
    propertiesTable.setLayout(layout);
   
    propertiesTable.setHeaderVisible(true);
   
    if (!Util.isWindows()) {
      propertiesTable.setLinesVisible(true);
    }
   
    TableColumn columnPropertyName = new TableColumn(propertiesTable, SWT.NONE);
    columnPropertyName.setText(""); //$NON-NLS-1$
   
    TableColumn columnPropertyValue = new TableColumn(propertiesTable, SWT.NONE);
    columnPropertyValue.setText(""); //$NON-NLS-1$
   
    buildActions();
   
    if (Controller.getInstance().getEngine().isPlaying()) {
      updateInformations(Controller.getInstance().getPlaylistController().getCurrentAudioItem());
    }
  } 
 
  private void openEditPropertiesDialog(IAudioItem item) {
    if (item != null) {
      if (item instanceof AudioItem) {
        new MetaDataDialog(_parent.getShell(), (AudioItem) item).open();
      } else if (item instanceof PodcastItem) {
        new PodcastItemPropertiesDialog(_parent.getShell(), (PodcastItem) item).open();
      } else if (item instanceof RadioItem) {
        new RadioItemPropertiesDialog(_parent.getShell(), (IRadioItem) item).open();
      }
    }
  }
 
  private void buildActions() {
    editPropertiesAction = new Action(Messages.getString("PropertyView.EditProperties")) { //$NON-NLS-1$
      public void run() {   
        openEditPropertiesDialog(_currentAudioItem);
      }
    };
    editPropertiesAction.setImageDescriptor(Activator.getImageDescriptor("/icons/property_16.png")); //$NON-NLS-1$
    editPropertiesAction.setEnabled(false);
   
    IToolBarManager rootMenuManager = getViewSite().getActionBars().getToolBarManager();
    rootMenuManager.removeAll();
   
    rootMenuManager.add(editPropertiesAction);
  }
 
  private void resetInformations() {
    if (!Controller.getInstance().isAplicationStopping()) { 
           
      propertiesTable.removeAll();
     
      editPropertiesAction.setEnabled(false);
      _currentAudioItem = null
     
      propertiesTable.getColumn(0).pack();
      propertiesTable.getColumn(1).pack();
    }
  }
 
  private void updateInformationForRadioItem(IAudioItem item) {
    TableItem tableItem;   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, ((RadioItem) item).getPropertiesLabelList().get(RadioItem.RADIO_NAME));
    tableItem.setText(1, ((RadioItem) item).getPropertiesList().get(RadioItem.RADIO_NAME));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, ((RadioItem) item).getPropertiesLabelList().get(RadioItem.RADIO_PARENT_NAME));
    tableItem.setText(1, ((RadioItem) item).getPropertiesList().get(RadioItem.RADIO_PARENT_NAME));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, ((RadioItem) item).getPropertiesLabelList().get(RadioItem.RADIO_URL));
    tableItem.setText(1, ((RadioItem) item).getPropertiesList().get(RadioItem.RADIO_URL))
   
    if (!((RadioItem) item).getTitlesList().isEmpty()) {
      int count = 0;
      Iterator<String> iter = ((RadioItem) item).getTitlesList().iterator();
      while (iter.hasNext()) {
        tableItem = new TableItem(propertiesTable, SWT.NONE);
       
        if (count == 0) {
          tableItem.setText(0, ((RadioItem) item).getPropertiesLabelList().get(RadioItem.RADIO_CURRENTLY_PLAYING));
        } else if (count == 1) {
          tableItem.setText(0, Messages.getString("RadioView.RadioPreviousTitles"));
        } else {
          tableItem.setText(0, "");
        }
        tableItem.setText(1, iter.next());
        count++;
      }
    }
  }
 
  private void updateInformationForPodcastItem(IAudioItem item) {
    TableItem tableItem;   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(PodcastItem.PODCAST_NAME));
    tableItem.setText(1, item.getPropertiesList().get(PodcastItem.PODCAST_NAME));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(PodcastItem.PODCAST_PARENT_NAME));
    tableItem.setText(1, item.getPropertiesList().get(PodcastItem.PODCAST_PARENT_NAME));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(PodcastItem.PODCAST_URL));
    tableItem.setText(1, item.getPropertiesList().get(PodcastItem.PODCAST_URL));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, Messages.getString("PropertyView.laLength"));
    tableItem.setText(1, Utils.seconds2String(item.getTrackLength()));
  }
 
  private void updateInformationForAudioItem(IAudioItem item) {
   
    TableItem tableItem;   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.TITLE));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.TITLE));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.ARTIST));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.ARTIST));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.ALBUM));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.ALBUM));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.GENRE));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.GENRE));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.YEAR));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.YEAR));
   
    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, Messages.getString("PropertyView.laLength"));
    tableItem.setText(1, Utils.seconds2String(item.getTrackLength()));

    tableItem = new TableItem(propertiesTable, SWT.NONE);
    tableItem.setText(0, item.getPropertiesLabelList().get(AudioItem.COMMENT));
    tableItem.setText(1, item.getPropertiesList().get(AudioItem.COMMENT));
  }
 
  private void updateInformations(IAudioItem item) {
    if (!Controller.getInstance().isAplicationStopping()) {
     
      propertiesTable.removeAll();
     
      if (item instanceof RadioItem) {
        updateInformationForRadioItem(item);
      } else if (item instanceof PodcastItem) {
        updateInformationForPodcastItem(item);
      } else {           
        updateInformationForAudioItem(item);
      }
   
      editPropertiesAction.setEnabled(true);
      _currentAudioItem = item; 
     
      propertiesTable.getColumn(0).pack();
      propertiesTable.getColumn(1).pack();
    }
  }
 
  private void updateInformations() {
    updateInformations(_currentAudioItem);
  }
 
  private void updateInformationsOnNewTitle(IAudioItem item) {
    updateInformations(item);
  }
 
  private void updateInformationsOnNewStreamTitle(IAudioItem item) {   
    updateInformations(item);
  }

  @Override
  public void propertyChange(PropertyChangeEvent arg0) {
    if (arg0.getPropertyName().equals(EventConstants.EVT_PLAY_NEW_AUDIO_ITEM)) {
      class updateInformationsOnNewTitle implements Runnable {
        IAudioItem _newItem;
        public updateInformationsOnNewTitle(IAudioItem newItem) {
          _newItem = newItem;
        }
        public void run() {
          updateInformationsOnNewTitle(_newItem);
        }
      }
      Display.getDefault().asyncExec(new updateInformationsOnNewTitle((IAudioItem) arg0.getNewValue()));
    } else if(arg0.getPropertyName().equals(EventConstants.EVT_RADIO_CHANGE_STREAM_TITLE)) {
      class updateInformationsOnNewStreamTitle implements Runnable {
        IAudioItem _newItem;
        public updateInformationsOnNewStreamTitle(IAudioItem newItem) {
          _newItem = newItem;
        }
        public void run() {
          updateInformationsOnNewStreamTitle(_newItem);
        }
      }     
      Display.getDefault().asyncExec(new updateInformationsOnNewStreamTitle((IAudioItem) arg0.getNewValue()));
    } else if (arg0.getPropertyName().equals(EventConstants.EVT_STOP_PLAYBACK)) {
      class resetInformations implements Runnable {
        public void run() {
          resetInformations();
        }
      }
      Display.getDefault().asyncExec(new resetInformations());
    } else if (arg0.getPropertyName().equals(EventConstants.EVT_LIBRARY_SCAN_CHANGE)) {
      class updateInformations implements Runnable {
        public void run() {
          updateInformations();
        }
      }
      Display.getDefault().asyncExec(new updateInformations());
    }
  }
 
  public void dispose() {
    if (boldFont != null) {
      boldFont.dispose();
    }
   
    Controller.getInstance().getEventController().removeAudioItemChangeListener(this);
    Controller.getInstance().getEventController().removeLibraryChangeListener(this);
    Controller.getInstance().getEventController().removeRadioChangeListeners(this);
  }
 
  @Override
  public void setFocus() {
    // TODO Auto-generated method stub

  }

}
TOP

Related Classes of org.jampa.gui.views.resetInformations

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.