/*
* 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.dialogs;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
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.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.jampa.controllers.Controller;
import org.jampa.gui.runnables.TagUpdater;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.disk.DirectoryItem;
import org.jampa.model.disk.FileItem;
import org.jampa.model.disk.IDiskItem;
import org.jampa.model.library.ILibraryItem;
import org.jampa.model.library.TitleItem;
import org.jampa.model.playlists.AudioItem;
public class MetaDataDialog extends TitleAreaDialog {
private Button ckModifyTrackNumber;
private Button ckModifyTitle;
private Button ckModifyArtist;
private Button ckModifyAlbum;
private Button ckModifyGenre;
private Button ckModifyYear;
private Button ckModifyComment;
private Label laTrackNumber;
private Label laTitle;
private Label laArtist;
private Label laAlbum;
private Label laGenre;
private Label laYear;
private Label laComment;
private Text teTrackNumber;
private Text teTitle;
private Combo cbArtist;
private Combo cbAlbum;
private Combo cbGenre;
private Text teYear;
private Text teComment;
private boolean boSameTrackNumber = true;
private boolean boSameTitle = true;
private boolean boSameArtist = true;
private boolean boSameAlbum = true;
private boolean boSameGenre = true;
private boolean boSameYear = true;
private boolean boSameComment = true;
private String stTrackNumberValue = ""; //$NON-NLS-1$
private String stTitleValue = ""; //$NON-NLS-1$
private String stArtistValue = ""; //$NON-NLS-1$
private String stAlbumValue = ""; //$NON-NLS-1$
private String stGenreValue = ""; //$NON-NLS-1$
private String stYearValue = ""; //$NON-NLS-1$
private String stCommentValue = ""; //$NON-NLS-1$
private Font italicFont;
private List<AudioItem> _itemList;
public MetaDataDialog(Shell parentShell, ILibraryItem itemList) {
this(parentShell);
_itemList = new ArrayList<AudioItem>();
createList(itemList);
}
public MetaDataDialog(Shell parentShell, List<IDiskItem> itemList) {
this(parentShell);
_itemList = new ArrayList<AudioItem>();
createList(itemList);
}
public MetaDataDialog(Shell parentShell, ArrayList<AudioItem> itemList) {
this(parentShell);
_itemList = itemList;
}
public MetaDataDialog(Shell parentShell, AudioItem item) {
this(parentShell);
_itemList = new ArrayList<AudioItem>();
_itemList.add(item);
}
private MetaDataDialog(Shell parentShell) {
super(parentShell);
italicFont = new Font(parentShell.getDisplay(),
parentShell.getFont().getFontData()[0].getName(),
parentShell.getFont().getFontData()[0].getHeight(),
SWT.ITALIC);
}
private void createList(List<IDiskItem> itemList) {
IDiskItem item;
AudioItem audioItem;
Iterator<IDiskItem> iter = itemList.iterator();
while (iter.hasNext()) {
item = iter.next();
if (item instanceof FileItem) {
audioItem = new AudioItem(((FileItem) item).getFileName(), true, false);
_itemList.add(audioItem);
} else if (item instanceof DirectoryItem) {
createList(((DirectoryItem) item).getFileListAsDiskItem());
createList(((DirectoryItem) item).getDirectoryListAsDiskItem());
}
}
}
private void createList(ILibraryItem itemList) {
ILibraryItem item;
AudioItem audioItem;
Iterator<ILibraryItem> iter = itemList.getChildrenList().iterator();
while (iter.hasNext()) {
item = iter.next();
if (item instanceof TitleItem) {
audioItem = new AudioItem(((TitleItem) item).getFilePath(), true, false);
_itemList.add(audioItem);
} else {
createList(item);
}
}
}
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
// Set the title
setTitle(Messages.getString("MetaDataDialog.Title")); //$NON-NLS-1$
// Set the message
setMessage(Messages.getString("MetaDataDialog.TitleArea"), IMessageProvider.INFORMATION); //$NON-NLS-1$
return contents;
}
private void updateTextState(Button button, Control control) {
control.setEnabled(button.getSelection());
}
private void fillCombos() {
List<String> keyList;
Iterator<String> iter;
keyList = Controller.getInstance().getLibrary().getArtistList();
iter = keyList.iterator();
while (iter.hasNext()) {
cbArtist.add(iter.next());
}
keyList = Controller.getInstance().getLibrary().getAlbumList();
iter = keyList.iterator();
while (iter.hasNext()) {
cbAlbum.add(iter.next());
}
keyList = Controller.getInstance().getLibrary().getGenreList();
iter = keyList.iterator();
while (iter.hasNext()) {
cbGenre.add(iter.next());
}
}
private void fillFields() {
boolean firstItem = true;
AudioItem audioItem;
Iterator<AudioItem> iter = _itemList.iterator();
while (iter.hasNext()) {
audioItem = iter.next();
if (firstItem) {
stTrackNumberValue = audioItem.getTrackNumber();
stTitleValue = audioItem.getPropertiesList().get(AudioItem.TITLE);
stArtistValue = audioItem.getPropertiesList().get(AudioItem.ARTIST);
stAlbumValue = audioItem.getPropertiesList().get(AudioItem.ALBUM);
stGenreValue = audioItem.getPropertiesList().get(AudioItem.GENRE);
stYearValue = audioItem.getPropertiesList().get(AudioItem.YEAR);
stCommentValue = audioItem.getPropertiesList().get(AudioItem.COMMENT);
firstItem = false;
} else {
if (!audioItem.getTrackNumber().equals(stTrackNumberValue)) {
boSameTrackNumber = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.TITLE).equals(stTitleValue)) {
boSameTitle = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.ARTIST).equals(stArtistValue)) {
boSameArtist = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.ALBUM).equals(stAlbumValue)) {
boSameAlbum = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.GENRE).equals(stGenreValue)) {
boSameGenre = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.YEAR).equals(stYearValue)) {
boSameYear = false;
}
if (!audioItem.getPropertiesList().get(AudioItem.COMMENT).equals(stCommentValue)) {
boSameComment = false;
}
}
}
if (boSameTrackNumber)
teTrackNumber.setText(stTrackNumberValue);
else
laTrackNumber.setFont(italicFont);
if (boSameTitle)
teTitle.setText(stTitleValue);
else
laTitle.setFont(italicFont);
if (boSameArtist)
cbArtist.setText(stArtistValue);
else
laArtist.setFont(italicFont);
if (boSameAlbum)
cbAlbum.setText(stAlbumValue);
else
laAlbum.setFont(italicFont);
if (boSameGenre)
cbGenre.setText(stGenreValue);
else
laGenre.setFont(italicFont);
if (boSameYear)
teYear.setText(stYearValue);
else
laYear.setFont(italicFont);
if (boSameComment)
teComment.setText(stCommentValue);
else
laComment.setFont(italicFont);
}
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
Composite panel = new Composite(composite, SWT.NONE);
GridLayout gl = new GridLayout(3, false);
panel.setLayout(gl);
panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
laTrackNumber = new Label(panel, SWT.NONE);
laTrackNumber.setText(Messages.getString("MetaDataDialog.LaTrackNumber")); //$NON-NLS-1$
teTrackNumber = new Text(panel, SWT.BORDER);
teTrackNumber.setEnabled(false);
teTrackNumber.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyTrackNumber = new Button(panel, SWT.CHECK);
ckModifyTrackNumber.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyTrackNumber.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyTrackNumber, teTrackNumber);
}
});
laTitle = new Label(panel, SWT.NONE);
laTitle.setText(Messages.getString("MetaDataDialog.LaTitle")); //$NON-NLS-1$
teTitle = new Text(panel, SWT.BORDER);
teTitle.setEnabled(false);
teTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyTitle = new Button(panel, SWT.CHECK);
ckModifyTitle.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyTitle.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyTitle, teTitle);
}
});
laArtist = new Label(panel, SWT.NONE);
laArtist.setText(Messages.getString("MetaDataDialog.LaArtist")); //$NON-NLS-1$
cbArtist = new Combo(panel, SWT.BORDER);
cbArtist.setEnabled(false);
cbArtist.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyArtist = new Button(panel, SWT.CHECK);
ckModifyArtist.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyArtist.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyArtist, cbArtist);
}
});
laAlbum = new Label(panel, SWT.NONE);
laAlbum.setText(Messages.getString("MetaDataDialog.LaAlbum")); //$NON-NLS-1$
cbAlbum = new Combo(panel, SWT.BORDER);
cbAlbum.setEnabled(false);
cbAlbum.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyAlbum = new Button(panel, SWT.CHECK);
ckModifyAlbum.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyAlbum.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyAlbum, cbAlbum);
}
});
laGenre = new Label(panel, SWT.NONE);
laGenre.setText(Messages.getString("MetaDataDialog.LaGenre")); //$NON-NLS-1$
cbGenre = new Combo(panel, SWT.BORDER);
cbGenre.setEnabled(false);
cbGenre.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyGenre = new Button(panel, SWT.CHECK);
ckModifyGenre.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyGenre.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyGenre, cbGenre);
}
});
laYear = new Label(panel, SWT.NONE);
laYear.setText(Messages.getString("MetaDataDialog.LaYear")); //$NON-NLS-1$
teYear = new Text(panel, SWT.BORDER);
teYear.setEnabled(false);
teYear.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ckModifyYear = new Button(panel, SWT.CHECK);
ckModifyYear.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyYear.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyYear, teYear);
}
});
laComment = new Label(panel, SWT.NONE);
laComment.setText(Messages.getString("MetadataDialog.LaComment")); //$NON-NLS-1$
teComment = new Text(panel, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
teComment.setEnabled(false);
GridData commentGD = new GridData(SWT.FILL, SWT.FILL, true, true);
commentGD.verticalSpan = 10;
teComment.setLayoutData(commentGD);
ckModifyComment = new Button(panel, SWT.CHECK);
ckModifyComment.setText(Messages.getString("MetaDataDialog.CkModify")); //$NON-NLS-1$
ckModifyComment.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
updateTextState(ckModifyComment, teComment);
}
});
fillCombos();
fillFields();
return parent;
}
private void modifyMetadata() {
boolean isModified = ckModifyTrackNumber.getSelection() ||
ckModifyTitle.getSelection() ||
ckModifyArtist.getSelection() ||
ckModifyAlbum.getSelection() ||
ckModifyGenre.getSelection() ||
ckModifyYear.getSelection() ||
ckModifyComment.getSelection();
if (isModified) {
TagUpdater tagUpdater = new TagUpdater(_itemList,
ckModifyTrackNumber.getSelection(), teTrackNumber.getText(),
ckModifyTitle.getSelection(), teTitle.getText(),
ckModifyArtist.getSelection(), cbArtist.getText(),
ckModifyAlbum.getSelection(), cbAlbum.getText(),
ckModifyGenre.getSelection(), cbGenre.getText(),
ckModifyYear.getSelection(), teYear.getText(),
ckModifyComment.getSelection(), teComment.getText());
ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
try {
dialog.run(true, true, tagUpdater);
} catch (InvocationTargetException e) {
Log.getInstance(MetaDataDialog.class).error(e.getMessage());
} catch (InterruptedException e) {
Log.getInstance(MetaDataDialog.class).error(e.getMessage());
}
Controller.getInstance().getHSQLController().doCommitDatabase();
if (tagUpdater.getProblemsList().size() > 0) {
Log.getInstance(MetaDataDialog.class).warn("Unable to update some files tags: " + tagUpdater.getProblemsList().size()); //$NON-NLS-1$
TagUpdateProblemDialog problemDialog = new TagUpdateProblemDialog(Display.getDefault().getActiveShell(),
tagUpdater.getProblemsList());
problemDialog.open();
}
// Rebuild Library, and notify Library change.
Controller.getInstance().notifyMetadataChange();
// Invalidate AudioItem's, so they will reload next time they are asked for data.
// Notify openned playlists to reload.
Controller.getInstance().getPlaylistController().invalidateAllLoadedTags();
}
}
protected void createButtonsForButtonBar(Composite parent) {
Button okBtn = createButton(parent, -1, IDialogConstants.OK_LABEL, false);//IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); //$NON-NLS-1$
Button closeBtn = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true); //$NON-NLS-1$
okBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
modifyMetadata();
close();
}
});
closeBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
public void dispose() {
italicFont.dispose();
}
protected Point getInitialSize() {
Point pt = super.getInitialSize();
return new Point(700, pt.y);
}
}