/*
* 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.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
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.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.translations.Messages;
import org.jampa.model.podcasts.Podcast;
import org.jampa.model.validators.PodcastNameValidator;
import org.jampa.net.podcast.PodcastUpdaterJob;
public class PodcastPropertiesDialog extends TitleAreaDialog {
private Podcast _item;
private String _initialName;
private String _initialUrl;
private Text teName;
private Text teUrl;
private Button btnOk;
private PodcastNameValidator _nameValidator;
public PodcastPropertiesDialog(Shell parentShell, Podcast item) {
super(parentShell);
_item = item;
_initialName = _item.getName();
_initialUrl = _item.getUrl();
_nameValidator = new PodcastNameValidator();
}
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
// Set the title
setTitle(Messages.getString("PodcastPropertiesDialog.Title")); //$NON-NLS-1$
// Set the message
setMessage(Messages.getString("PodcastPropertiesDialog.TitleArea"), IMessageProvider.INFORMATION); //$NON-NLS-1$
return contents;
}
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
Composite panel = new Composite(composite, SWT.NONE);
GridLayout gl = new GridLayout(2, false);
panel.setLayout(gl);
GridData mainGD = new GridData(SWT.FILL, SWT.FILL, true, true);
mainGD.widthHint = 400;
panel.setLayoutData(mainGD);
Label laNameTitle = new Label(panel, SWT.NONE);
laNameTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelName"));
teName = new Text(panel, SWT.BORDER);
teName.setText(_item.getName());
teName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateData();
}
});
Label laTitleTitle = new Label(panel, SWT.NONE);
laTitleTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelTitle"));
Text teTitle = new Text(panel, SWT.BORDER);
teTitle.setText(_item.getTitle());
teTitle.setEditable(false);
teTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teTitle.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
Label laDescriptionTitle = new Label(panel, SWT.NONE);
laDescriptionTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelDescription"));
Text teDescription = new Text(panel, SWT.BORDER);
teDescription.setText(_item.getDescription());
teDescription.setEditable(false);
teDescription.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teDescription.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
Label laUrlTitle = new Label(panel, SWT.NONE);
laUrlTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelUrl"));
teUrl = new Text(panel, SWT.BORDER);
teUrl.setText(_item.getUrl());
teUrl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teUrl.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateData();
}
});
Label laNbItemsTitle = new Label(panel, SWT.NONE);
laNbItemsTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelNbItems"));
Text teNbItems = new Text(panel, SWT.BORDER);
teNbItems.setText(Integer.toString(_item.getItemsCount()));
teNbItems.setEditable(false);
teNbItems.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teNbItems.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
Label laNbUnreadItemsTitle = new Label(panel, SWT.NONE);
laNbUnreadItemsTitle.setText(Messages.getString("PodcastPropertiesDialog.LabelNbUnreadItems"));
Text teNbUnreadItems = new Text(panel, SWT.BORDER);
teNbUnreadItems.setText(Integer.toString(_item.getUnreadItemsCount()));
teNbUnreadItems.setEditable(false);
teNbUnreadItems.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teNbUnreadItems.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
return parent;
}
private void validateData() {
boolean nameOk;
if (!teName.getText().equals(_initialName)) {
String errorMessage = _nameValidator.isValid(teName.getText());
nameOk = errorMessage == null ? true : false;
setErrorMessage(errorMessage);
btnOk.setEnabled(nameOk);
} else {
setErrorMessage(null);
btnOk.setEnabled(true);
nameOk = true;
}
if (nameOk) {
if ((teUrl.getText() == null) ||
(teUrl.getText().isEmpty())) {
setErrorMessage(Messages.getString("PodcastPropertiesDialog.EmptyUrl"));
btnOk.setEnabled(false);
} else {
setErrorMessage(null);
btnOk.setEnabled(true);
}
}
}
private void modify() {
if (!_initialName.equals(teName.getText())) {
Controller.getInstance().getPlaylistController().renamePodcast(teName.getText(), _initialName);
_item = Controller.getInstance().getPlaylistController().getPodcastByName(teName.getText());
}
if (!_initialUrl.equals(teUrl.getText())) {
_item.setUrl(teUrl.getText());
List<Podcast> tmpList = new ArrayList<Podcast>();
tmpList.add(_item);
new PodcastUpdaterJob(tmpList).schedule();
}
}
protected void createButtonsForButtonBar(Composite parent) {
btnOk = createButton(parent, -1, IDialogConstants.OK_LABEL, true);
btnOk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
modify();
close();
}
});
Button closeBtn = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
closeBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
closeBtn.setFocus();
}
}