/*
* 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.Iterator;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.translations.Messages;
import org.jampa.model.radio.CategoryRadioItem;
import org.jampa.model.radio.IRadioItem;
import org.jampa.model.radio.RadioItem;
import org.jampa.model.validators.RadioCategoryNameValidator;
import org.jampa.model.validators.RadioNameValidator;
public class RadioItemPropertiesDialog extends TitleAreaDialog {
private IRadioItem _item;
private Combo cbCategoryName;
private Text teCategoryName;
private Text teRadioName;
private Text teRadioUrl;
private Button ckRadioIsFavorite;
private Button btnOk;
private String _initialCategory;
private String _initialRadioName;
private String _initialRadioUrl;
private boolean _initialIsFavorite;
private boolean _isCategory;
private RadioCategoryNameValidator _categoryNameValidator;
private RadioNameValidator _radioNameValidator;
private Table radioListTable;
public RadioItemPropertiesDialog(Shell parentShell, IRadioItem item) {
super(parentShell);
_item = item;
_isCategory = (item instanceof CategoryRadioItem);
_categoryNameValidator = new RadioCategoryNameValidator();
_radioNameValidator = new RadioNameValidator();
if (_isCategory) {
_initialCategory = _item.getName();
_initialRadioName = null;
_initialRadioUrl = null;
_initialIsFavorite = false;
} else {
_initialCategory = _item.getParent().getName();
_initialRadioName = _item.getName();
_initialRadioUrl = ((RadioItem) _item).getUrl();
_initialIsFavorite = ((RadioItem) _item).isFavorite();
}
}
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
// Set the title
setTitle(Messages.getString("RadioPropertiesDialog.Title")); //$NON-NLS-1$
// Set the message
setMessage(Messages.getString("RadioPropertiesDialog.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;
if (_isCategory) {
mainGD.heightHint = 200;
panel.setLayoutData(mainGD);
Label laCategoryName = new Label(panel, SWT.NONE);
laCategoryName.setText(Messages.getString("RadioPropertiesDialog.LabelCategoryName"));
teCategoryName = new Text(panel, SWT.BORDER);
teCategoryName.setText(_item.getName());
teCategoryName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teCategoryName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateFullCategoryName();
}
});
radioListTable = new Table(panel, SWT.BORDER);
GridData radioListTableGD = new GridData(SWT.FILL, SWT.FILL, true, true);
radioListTableGD.horizontalSpan = 2;
radioListTable.setLayoutData(radioListTableGD);
TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(50, 10, true));
layout.addColumnData(new ColumnWeightData(50, 10, true));
radioListTable.setLayout(layout);
TableColumn columnRadioName = new TableColumn(radioListTable, SWT.NONE);
columnRadioName.setText(Messages.getString("RadioPropertiesDialog.ColumnRadioName")); //$NON-NLS-1$
TableColumn columnRadioUrl = new TableColumn(radioListTable, SWT.NONE);
columnRadioUrl.setText(Messages.getString("RadioPropertiesDialog.ColumnRadioUrl")); //$NON-NLS-1$
radioListTable.setHeaderVisible(true);
TableItem tableItem;
IRadioItem item;
Iterator<IRadioItem> radioIter = _item.getChildren().iterator();
while (radioIter.hasNext()) {
item = radioIter.next();
tableItem = new TableItem(radioListTable, SWT.NONE);
tableItem.setText(0, item.getName());
tableItem.setText(1, ((RadioItem) item).getUrl());
}
} else {
panel.setLayoutData(mainGD);
Label laCategoryName = new Label(panel, SWT.NONE);
laCategoryName.setText(Messages.getString("RadioPropertiesDialog.LabelCategoryName"));
cbCategoryName = new Combo(panel, SWT.BORDER);
cbCategoryName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Iterator<IRadioItem> iter = Controller.getInstance().getRadioController().getRootCategory().getChildren().iterator();
while (iter.hasNext()) {
cbCategoryName.add(iter.next().getName());
}
cbCategoryName.setText(_item.getParent().getName());
cbCategoryName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateNames();
}
});
Label laRadioName = new Label(panel, SWT.NONE);
laRadioName.setText(Messages.getString("RadioPropertiesDialog.LabelRadioName"));
teRadioName = new Text(panel, SWT.BORDER);
teRadioName.setText(_item.getName());
teRadioName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teRadioName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateNames();
}
});
Label laRadioUrl = new Label(panel, SWT.NONE);
laRadioUrl.setText(Messages.getString("RadioPropertiesDialog.LabelRadioUrl"));
teRadioUrl = new Text(panel, SWT.BORDER);
teRadioUrl.setText(((RadioItem) _item).getUrl());
teRadioUrl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
teRadioUrl.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateNames();
}
});
Label laRadioIsFavoriteLabel = new Label(panel, SWT.NONE);
laRadioIsFavoriteLabel.setText(Messages.getString("RadioPropertiesDialog.LabelRadioIsFavorite"));
ckRadioIsFavorite = new Button(panel, SWT.CHECK);
ckRadioIsFavorite.setSelection(((RadioItem) _item).isFavorite());
}
return parent;
}
private void validateNames() {
boolean categoryOk;
if ((cbCategoryName.getText() == null) ||
(cbCategoryName.getText().isEmpty())) {
this.setErrorMessage(Messages.getString("RadioCategoryNameValidator.CategoryNameEmpty"));
btnOk.setEnabled(false);
categoryOk = false;
} else {
this.setErrorMessage(null);
btnOk.setEnabled(true);
categoryOk = true;
}
if (categoryOk) {
boolean radioNameOk;
if (!teRadioName.getText().equals(_initialRadioName)) {
_radioNameValidator.setCategory(cbCategoryName.getText());
String errorMessage = _radioNameValidator.isValid(teRadioName.getText());
radioNameOk = errorMessage == null ? true : false;
this.setErrorMessage(errorMessage);
btnOk.setEnabled(radioNameOk);
} else {
this.setErrorMessage(null);
btnOk.setEnabled(true);
radioNameOk = true;
}
if (radioNameOk) {
if ((teRadioUrl.getText() == null) ||
(teRadioUrl.getText().isEmpty())) {
this.setErrorMessage(Messages.getString("RadioItemPropertiesDialog.RadioUrlEmpty"));
btnOk.setEnabled(false);
} else {
this.setErrorMessage(null);
btnOk.setEnabled(true);
}
}
}
}
private void validateFullCategoryName() {
if (!teCategoryName.getText().equals(_initialCategory)) {
String errorMessage = _categoryNameValidator.isValid(teCategoryName.getText());
this.setErrorMessage(errorMessage);
btnOk.setEnabled(errorMessage == null ? true : false);
} else {
this.setErrorMessage(null);
btnOk.setEnabled(true);
}
}
private void modifyCategory() {
boolean hasBeenModified = false;
if (!_initialCategory.equals(teCategoryName.getText())) {
_item.setName(teCategoryName.getText());
hasBeenModified = true;
}
if (hasBeenModified) {
Controller.getInstance().getEventController().fireRadioChange(EventConstants.EVT_RADIO_CHANGE, null, null);
}
}
private void modifyRadio() {
boolean hasBeenModified = false;
if (!_initialCategory.equals(cbCategoryName.getText())) {
Controller.getInstance().getRadioController().changeItemCategory(_item, cbCategoryName.getText());
hasBeenModified = true;
}
if (!_initialRadioName.equals(teRadioName.getText())) {
_item.setName(teRadioName.getText());
hasBeenModified = true;
}
if (!_initialRadioUrl.equals(teRadioUrl.getText())) {
((RadioItem) _item).setUrl(teRadioUrl.getText());
hasBeenModified = true;
}
if (_initialIsFavorite != ckRadioIsFavorite.getSelection()) {
((RadioItem) _item).setFavorite(ckRadioIsFavorite.getSelection());
hasBeenModified = true;
}
if (hasBeenModified) {
Controller.getInstance().getEventController().fireRadioChange(EventConstants.EVT_RADIO_CHANGE, null, null);
}
}
protected void createButtonsForButtonBar(Composite parent) {
btnOk = createButton(parent, -1, IDialogConstants.OK_LABEL, true);
Button closeBtn = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
btnOk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (_isCategory) {
modifyCategory();
} else {
modifyRadio();
}
close();
}
});
closeBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
}