/*
* 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 org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
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.translations.Messages;
import org.jampa.model.statistics.StatisticItem;
import org.jampa.model.statistics.StatisticItemType;
import org.jampa.utils.Utils;
public class StatisticView extends ViewPart implements PropertyChangeListener {
public static String ID = "Jampa.StatisticView"; //$NON-NLS-1$
private Composite _parent;
private FormToolkit toolkit;
private ScrolledForm form;
private Image _statsImage;
private Action _resetStatsAction;
private Label laLibraryNbItemsValue;
private Label laLibraryTotalTimeValue;
private Label laLibraryNbPlaylistsValue;
private TableViewer _playlistViewer;
private TableViewer _genreViewer;
private TableViewer _artistViewer;
private TableViewer _albumViewer;
private TableViewer _titleViewer;
public StatisticView() {
Activator.getDefault();
ImageDescriptor imageDescriptor = Activator.getImageDescriptor("/icons/stats_16.png"); //$NON-NLS-1$
_statsImage = imageDescriptor.createImage();
buildActions();
}
private void buildActions() {
_resetStatsAction = new Action(Messages.getString("StatisticView.ResetAction")) { //$NON-NLS-1$
public void run() {
boolean confirm = MessageDialog.openConfirm(form.getShell(),
Messages.getString("StatisticView.ResetActionTitle"), //$NON-NLS-1$
Messages.getString("StatisticView.ResetActionText")); //$NON-NLS-1$
if (confirm) {
Controller.getInstance().getStatisticsController().resetStatistics();
fillData();
}
}
};
_resetStatsAction.setImageDescriptor(Activator.getImageDescriptor("/icons/resetstats_16.png")); //$NON-NLS-1$
}
private void fillData() {
laLibraryNbItemsValue.setText(Integer.toString(Controller.getInstance().getStatisticsController().getLibraryRecordCount()));
laLibraryTotalTimeValue.setText(Utils.seconds2HourString(Controller.getInstance().getStatisticsController().getLibraryTotalTime()));
if (Controller.getInstance().getStatisticsController().isStatsActivated()) {
// -1 to remove the default playlist.
laLibraryNbPlaylistsValue.setText(Integer.toString(Controller.getInstance().getPlaylistController().getPlaylistsCount() - 1));
_playlistViewer.setInput(getViewSite());
_genreViewer.setInput(getViewSite());
_artistViewer.setInput(getViewSite());
_albumViewer.setInput(getViewSite());
_titleViewer.setInput(getViewSite());
}
}
private void buildAdvancedSections() {
// Playlists section
Section playlistStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
playlistStatSection.setText(Messages.getString("StatisticView.PlaylistStatSectionTitle")); //$NON-NLS-1$
GridData gd = new GridData(SWT.FILL, SWT.NONE, true, true);
playlistStatSection.setLayoutData(gd);
Composite playlistStatClient = toolkit.createComposite(playlistStatSection);
playlistStatClient.setLayout(new GridLayout(2, false));
toolkit.createLabel(playlistStatClient, Messages.getString("StatisticView.PlaylistNbPlaylistsTitle")); //$NON-NLS-1$);
laLibraryNbPlaylistsValue = toolkit.createLabel(playlistStatClient, ""); //$NON-NLS-1$);
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
toolkit.createLabel(playlistStatClient, Messages.getString("StatisticView.PlaylistTableTitle")).setLayoutData(gd); //$NON-NLS-1$);
final Table playlistTable = toolkit.createTable(playlistStatClient, SWT.NULL);
playlistTable.setHeaderVisible(true);
// Lines visible is ugly under Windows.
if (!Util.isWindows()) {
playlistTable.setLinesVisible(true);
}
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 100;
gd.horizontalSpan = 2;
playlistTable.setLayoutData(gd);
toolkit.paintBordersFor(playlistStatClient);
playlistStatSection.setClient(playlistStatClient);
_playlistViewer = new TableViewer(playlistTable);
final TableColumn playlistColumnName = new TableColumn(playlistTable, SWT.NONE);
playlistColumnName.setText(Messages.getString("StatisticView.TableColumnName")); //$NON-NLS-1$
final TableColumn playlistColumnCount = new TableColumn(playlistTable, SWT.NONE);
playlistColumnCount.setText(Messages.getString("StatisticView.TableColumnCount")); //$NON-NLS-1$
final TableColumn playlistColumnPercent = new TableColumn(playlistTable, SWT.NONE);
playlistColumnPercent.setText(Messages.getString("StatisticView.TableColumnPercent")); //$NON-NLS-1$
_playlistViewer.setContentProvider(new StatContentProvider(StatisticItemType.Playlist));
_playlistViewer.setLabelProvider(new StatLabelProvider(StatisticItemType.Playlist));
_playlistViewer.setInput(getViewSite());
// Genre section
Section genreStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
genreStatSection.setText(Messages.getString("StatisticView.GenreStatSectionTitle")); //$NON-NLS-1$
gd = new GridData(SWT.FILL, SWT.NONE, true, true);
genreStatSection.setLayoutData(gd);
Composite genreStatClient = toolkit.createComposite(genreStatSection);
genreStatClient.setLayout(new GridLayout(2, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
toolkit.createLabel(genreStatClient, Messages.getString("StatisticView.GenreTableTitle")).setLayoutData(gd); //$NON-NLS-1$);
final Table genreTable = toolkit.createTable(genreStatClient, SWT.NULL);
genreTable.setHeaderVisible(true);
// Lines visible is ugly under Windows.
if (!Util.isWindows()) {
genreTable.setLinesVisible(true);
}
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 100;
gd.horizontalSpan = 2;
genreTable.setLayoutData(gd);
toolkit.paintBordersFor(genreStatClient);
genreStatSection.setClient(genreStatClient);
_genreViewer = new TableViewer(genreTable);
final TableColumn genreColumnName = new TableColumn(genreTable, SWT.NONE);
genreColumnName.setText(Messages.getString("StatisticView.TableColumnName")); //$NON-NLS-1$
final TableColumn genreColumnCount = new TableColumn(genreTable, SWT.NONE);
genreColumnCount.setText(Messages.getString("StatisticView.TableColumnCount")); //$NON-NLS-1$
final TableColumn genreColumnPercent = new TableColumn(genreTable, SWT.NONE);
genreColumnPercent.setText(Messages.getString("StatisticView.TableColumnPercent")); //$NON-NLS-1$
_genreViewer.setContentProvider(new StatContentProvider(StatisticItemType.Genre));
_genreViewer.setLabelProvider(new StatLabelProvider(StatisticItemType.Genre));
_genreViewer.setInput(getViewSite());
// Artist section
Section artistStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
artistStatSection.setText(Messages.getString("StatisticView.ArtistStatSectionTitle")); //$NON-NLS-1$
gd = new GridData(SWT.FILL, SWT.NONE, true, true);
artistStatSection.setLayoutData(gd);
Composite artistStatClient = toolkit.createComposite(artistStatSection);
artistStatClient.setLayout(new GridLayout(2, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
toolkit.createLabel(artistStatClient, Messages.getString("StatisticView.ArtistTableTitle")).setLayoutData(gd); //$NON-NLS-1$);
final Table artistTable = toolkit.createTable(artistStatClient, SWT.NULL);
artistTable.setHeaderVisible(true);
// Lines visible is ugly under Windows.
if (!Util.isWindows()) {
artistTable.setLinesVisible(true);
}
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 100;
gd.horizontalSpan = 2;
artistTable.setLayoutData(gd);
toolkit.paintBordersFor(artistStatClient);
artistStatSection.setClient(artistStatClient);
_artistViewer = new TableViewer(artistTable);
final TableColumn artistColumnName = new TableColumn(artistTable, SWT.NONE);
artistColumnName.setText(Messages.getString("StatisticView.TableColumnName")); //$NON-NLS-1$
final TableColumn artistColumnCount = new TableColumn(artistTable, SWT.NONE);
artistColumnCount.setText(Messages.getString("StatisticView.TableColumnCount")); //$NON-NLS-1$
final TableColumn artistColumnPercent = new TableColumn(artistTable, SWT.NONE);
artistColumnPercent.setText(Messages.getString("StatisticView.TableColumnPercent")); //$NON-NLS-1$
_artistViewer.setContentProvider(new StatContentProvider(StatisticItemType.Artist));
_artistViewer.setLabelProvider(new StatLabelProvider(StatisticItemType.Artist));
_artistViewer.setInput(getViewSite());
// Album section
Section albumStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
albumStatSection.setText(Messages.getString("StatisticView.AlbumStatSectionTitle")); //$NON-NLS-1$
gd = new GridData(SWT.FILL, SWT.NONE, true, true);
albumStatSection.setLayoutData(gd);
Composite albumStatClient = toolkit.createComposite(albumStatSection);
albumStatClient.setLayout(new GridLayout(2, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
toolkit.createLabel(albumStatClient, Messages.getString("StatisticView.AlbumTableTitle")).setLayoutData(gd); //$NON-NLS-1$);
final Table albumTable = toolkit.createTable(albumStatClient, SWT.NULL);
albumTable.setHeaderVisible(true);
// Lines visible is ugly under Windows.
if (!Util.isWindows()) {
albumTable.setLinesVisible(true);
}
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 100;
gd.horizontalSpan = 2;
albumTable.setLayoutData(gd);
toolkit.paintBordersFor(albumStatClient);
albumStatSection.setClient(albumStatClient);
_albumViewer = new TableViewer(albumTable);
final TableColumn albumColumnName = new TableColumn(albumTable, SWT.NONE);
albumColumnName.setText(Messages.getString("StatisticView.TableColumnName")); //$NON-NLS-1$
final TableColumn albumColumnAdditional = new TableColumn(albumTable, SWT.NONE);
albumColumnAdditional.setText(Messages.getString("StatisticView.AlbumColumnAdditional")); //$NON-NLS-1$
final TableColumn albumColumnCount = new TableColumn(albumTable, SWT.NONE);
albumColumnCount.setText(Messages.getString("StatisticView.TableColumnCount")); //$NON-NLS-1$
final TableColumn albumColumnPercent = new TableColumn(albumTable, SWT.NONE);
albumColumnPercent.setText(Messages.getString("StatisticView.TableColumnPercent")); //$NON-NLS-1$
_albumViewer.setContentProvider(new StatContentProvider(StatisticItemType.Album));
_albumViewer.setLabelProvider(new StatLabelProvider(StatisticItemType.Album));
_albumViewer.setInput(getViewSite());
// Title section
Section titleStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
titleStatSection.setText(Messages.getString("StatisticView.TitleStatSectionTitle")); //$NON-NLS-1$
gd = new GridData(SWT.FILL, SWT.NONE, true, true);
titleStatSection.setLayoutData(gd);
Composite titleStatClient = toolkit.createComposite(titleStatSection);
titleStatClient.setLayout(new GridLayout(2, false));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.horizontalSpan = 2;
toolkit.createLabel(titleStatClient, Messages.getString("StatisticView.TitleTableTitle")).setLayoutData(gd); //$NON-NLS-1$);
final Table titleTable = toolkit.createTable(titleStatClient, SWT.NULL);
titleTable.setHeaderVisible(true);
// Lines visible is ugly under Windows.
if (!Util.isWindows()) {
titleTable.setLinesVisible(true);
}
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 100;
gd.horizontalSpan = 2;
titleTable.setLayoutData(gd);
toolkit.paintBordersFor(titleStatClient);
titleStatSection.setClient(titleStatClient);
_titleViewer = new TableViewer(titleTable);
final TableColumn titleColumnName = new TableColumn(titleTable, SWT.NONE);
titleColumnName.setText(Messages.getString("StatisticView.TableColumnName")); //$NON-NLS-1$
final TableColumn titleColumnAdditional = new TableColumn(titleTable, SWT.NONE);
titleColumnAdditional.setText(Messages.getString("StatisticView.TitleColumnAdditional")); //$NON-NLS-1$
final TableColumn titleColumnCount = new TableColumn(titleTable, SWT.NONE);
titleColumnCount.setText(Messages.getString("StatisticView.TableColumnCount")); //$NON-NLS-1$
final TableColumn titleColumnPercent = new TableColumn(titleTable, SWT.NONE);
titleColumnPercent.setText(Messages.getString("StatisticView.TableColumnPercent")); //$NON-NLS-1$
_titleViewer.setContentProvider(new StatContentProvider(StatisticItemType.Title));
_titleViewer.setLabelProvider(new StatLabelProvider(StatisticItemType.Title));
_titleViewer.setInput(getViewSite());
fillData();
_parent.pack();
playlistTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
int width = playlistTable.getClientArea().width - playlistTable.getVerticalBar().getSize().x;
playlistColumnName.setWidth((int) (width * 0.6));
playlistColumnCount.setWidth((int) (width * 0.2));
playlistColumnPercent.setWidth((int) (width * 0.2));
}
});
genreTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
int width = genreTable.getClientArea().width - genreTable.getVerticalBar().getSize().x;
genreColumnName.setWidth((int) (width * 0.6));
genreColumnCount.setWidth((int) (width * 0.2));
genreColumnPercent.setWidth((int) (width * 0.2));
}
});
artistTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
int width = artistTable.getClientArea().width - artistTable.getVerticalBar().getSize().x;
artistColumnName.setWidth((int) (width * 0.6));
artistColumnCount.setWidth((int) (width * 0.2));
artistColumnPercent.setWidth((int) (width * 0.2));
}
});
albumTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
int width = albumTable.getClientArea().width - albumTable.getVerticalBar().getSize().x;
albumColumnName.setWidth((int) (width * 0.5));
albumColumnAdditional.setWidth((int) (width * 0.2));
albumColumnCount.setWidth((int) (width * 0.15));
albumColumnPercent.setWidth((int) (width * 0.15));
}
});
titleTable.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
int width = titleTable.getClientArea().width - titleTable.getVerticalBar().getSize().x;
titleColumnName.setWidth((int) (width * 0.5));
titleColumnAdditional.setWidth((int) (width * 0.2));
titleColumnCount.setWidth((int) (width * 0.15));
titleColumnPercent.setWidth((int) (width * 0.15));
}
});
}
@Override
public void createPartControl(Composite parent) {
_parent = parent;
setPartName(Messages.getString("StatisticView.Title")); //$NON-NLS-1$
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText(Messages.getString("StatisticView.FormTitle")); //$NON-NLS-1$
toolkit.decorateFormHeading(form.getForm());
form.setImage(_statsImage);
if (Controller.getInstance().getStatisticsController().isStatsActivated()) {
form.getToolBarManager().add(_resetStatsAction);
form.getToolBarManager().update(true);
}
GridLayout layout = new GridLayout(1, true);
form.getBody().setLayout(layout);
// General section
Section generalStatSection = toolkit.createSection(form.getBody(),
Section.TITLE_BAR | Section.EXPANDED);
generalStatSection.setText(Messages.getString("StatisticView.GeneralStatSectionTitle")); //$NON-NLS-1$
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
generalStatSection.setLayoutData(gd);
Composite generalStatClient = toolkit.createComposite(generalStatSection);
generalStatClient.setLayout(new GridLayout(2, false));
toolkit.createLabel(generalStatClient, Messages.getString("StatisticView.LibraryNbItemsTitle")); //$NON-NLS-1$);
laLibraryNbItemsValue = toolkit.createLabel(generalStatClient, ""); //$NON-NLS-1$
toolkit.createLabel(generalStatClient, Messages.getString("StatisticView.LibraryTotalTimeTitle")); //$NON-NLS-1$);
laLibraryTotalTimeValue = toolkit.createLabel(generalStatClient, ""); //$NON-NLS-1$);
generalStatSection.setClient(generalStatClient);
if (Controller.getInstance().getStatisticsController().isStatsActivated()) {
buildAdvancedSections();
} else {
toolkit.createLabel(form.getBody(), Messages.getString("StatisticView.DisabledStats")); //$NON-NLS-1$)
Hyperlink link = toolkit.createHyperlink(form.getBody(),
Messages.getString("StatisticView.ActivateStatsForMore"), SWT.WRAP); //$NON-NLS-1$)
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
ActionFactory.PREFERENCES.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow()).run();
}
});
fillData();
_parent.pack();
}
Controller.getInstance().getEventController().addLibraryChangeListener(this);
Controller.getInstance().getEventController().addPlaylistChangeListener(this);
Controller.getInstance().getEventController().addAudioItemChangeListener(this);
}
class StatLabelProvider extends LabelProvider implements ITableLabelProvider {
private StatisticItemType _type;
public StatLabelProvider(StatisticItemType type) {
this._type = type;
}
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
String result;
if ((_type == StatisticItemType.Title) ||
(_type == StatisticItemType.Album)) {
switch (columnIndex) {
case 0 : result = ((StatisticItem) element).getDisplayValue(); break;
case 1 : result = ((StatisticItem) element).getAdditionalValue(); break;
case 2 : result = Integer.toString(((StatisticItem) element).getValue()); break;
case 3 : result = Float.toString(((StatisticItem) element).getPercentValue()); break;
default : result = ""; //$NON-NLS-1$
}
} else {
switch (columnIndex) {
case 0 : result = ((StatisticItem) element).getDisplayValue(); break;
case 1 : result = Integer.toString(((StatisticItem) element).getValue()); break;
case 2 : result = Float.toString(((StatisticItem) element).getPercentValue()); break;
default : result = ""; //$NON-NLS-1$
}
}
return result;
}
}
class StatContentProvider implements IStructuredContentProvider {
private StatisticItemType _type;
public StatContentProvider(StatisticItemType type) {
this._type = type;
}
public Object[] getElements(Object inputElement) {
return Controller.getInstance().getStatisticsController().getStats(_type);
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
@Override
public void setFocus() {
form.setFocus();
}
public void dispose() {
_statsImage.dispose();
Controller.getInstance().getEventController().removeLibraryChangeListener(this);
Controller.getInstance().getEventController().removePlaylistChangeListener(this);
Controller.getInstance().getEventController().removeAudioItemChangeListener(this);
toolkit.dispose();
super.dispose();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ((evt.getPropertyName().equals(EventConstants.EVT_ADD_PLAYLIST)) ||
(evt.getPropertyName().equals(EventConstants.EVT_REMOVE_PLAYLIST)) ||
(evt.getPropertyName().equals(EventConstants.EVT_LIBRARY_SCAN_CHANGE)) ||
(evt.getPropertyName().equals(EventConstants.EVT_PLAY_NEW_AUDIO_ITEM))) {
class updateInformations implements Runnable {
public void run() {
fillData();
}
}
Display.getDefault().asyncExec(new updateInformations());
}
}
}