/*
* 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.wizard.playlistimport;
import java.io.File;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.jampa.gui.translations.Messages;
import org.jampa.model.validators.PlaylistNameValidator;
import org.jampa.utils.SystemUtils;
public class PlaylistImportPage extends WizardPage {
private Composite container;
private Text tePlaylistName;
private Text teFilename;
private PlaylistNameValidator nameValidator;
private boolean boPlaylistNameOk = false;
public PlaylistImportPage() {
super("PlaylistImportWizard"); //$NON-NLS-1$
setTitle(Messages.getString("PlaylistImportWizard.Title")); //$NON-NLS-1$
setDescription(Messages.getString("PlaylistImportWizard.IntroText")); //$NON-NLS-1$
nameValidator = new PlaylistNameValidator();
}
@Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NULL);
GridLayout gl = new GridLayout(3, false);
container.setLayout(gl);
Label laPlaylistName = new Label(container, SWT.NONE);
laPlaylistName.setText(Messages.getString("PlaylistImportWizard.PlaylistName")); //$NON-NLS-1$
tePlaylistName = new Text(container, SWT.BORDER);
tePlaylistName.setText(Messages.getString("NewPlaylistAction.DefaultName")); //$NON-NLS-1$
GridData gd = new GridData(SWT.FILL, SWT.NONE, true, false);
gd.horizontalSpan = 2;
tePlaylistName.setLayoutData(gd);
tePlaylistName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validatePage();
}
});
Label laFilename = new Label(container, SWT.NONE);
laFilename.setText(Messages.getString("PlaylistImportWizard.FileName")); //$NON-NLS-1$
teFilename = new Text(container, SWT.BORDER);
teFilename.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
teFilename.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
validatePage();
}
});
Button btnBrowse = new Button(container, SWT.NONE);
btnBrowse.setText(Messages.getString("PlaylistImportWizard.Browse")); //$NON-NLS-1$
btnBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
FileDialog fileDialog = new FileDialog(getShell());
fileDialog.setText(Messages.getString("PlaylistImportWizard.SelectPlaylist")); //$NON-NLS-1$
fileDialog.setFilterPath(SystemUtils.userHome);
String[] filters = { "*" + SystemUtils.playlistXSPFExtension + ";*" + SystemUtils.playlistM3UExtension, "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
fileDialog.setFilterExtensions(filters);
fileDialog.setFilterNames( new String[] {Messages.getString("PlaylistImportWizard.FilerPlaylists"), Messages.getString("PlaylistImportWizard.FilterAll")} ); //$NON-NLS-1$ //$NON-NLS-2$
fileDialog.open();
teFilename.setText(fileDialog.getFilterPath() + SystemUtils.fileSeparator + fileDialog.getFileName());
validatePage();
}
});
tePlaylistName.setSelection(0, tePlaylistName.getText().length());
// Required to avoid an error in the system
setControl(container);
validatePage();
checkPlaylistName();
}
public void checkPlaylistName() {
String result = nameValidator.isValid(tePlaylistName.getText());
if (result != null) {
setErrorMessage(result);
boPlaylistNameOk = false;
} else {
setErrorMessage(null);
boPlaylistNameOk = true;
}
if (tePlaylistName.getText().isEmpty()) {
boPlaylistNameOk = false;
}
}
private boolean validateFile(String value) {
File file = new File(value);
if (file.isFile())
return true;
else
return false;
}
private void validatePage() {
checkPlaylistName();
setPageComplete(boPlaylistNameOk && validateFile(teFilename.getText()));
}
public String getPlaylistName() {
return tePlaylistName.getText();
}
public String getFileName() {
return teFilename.getText();
}
public Control getControl() {
return container;
}
}