package gui.dialogs;
import gui.EasyBotAppException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
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 resources.digesters.ClassManager;
import resources.digesters.Plugin;
import utils.ErrorMessage;
import utils.defines.Defines;
import com.cloudgarden.resource.SWTResourceManager;
import components.ComponentGUIException;
import components.robot.Robot;
import components.robot.RobotGUI;
import core.simulation.Simulation;
/**
* Esta clase implementa el di�logo para configuraci�n de un robot.
* Este di�logo tiene la estructura base o "esqueleto" sobre el que
* se muestran los panes de configuraci�n de los componentes que lo
* conforman.
*/
public class RobotConfigDialog extends org.eclipse.swt.widgets.Dialog
{
private Shell dialogShell;
private Robot robot;
private Composite customSpace;
private StyledText commentText;
private Composite backComposite;
private CLabel titleLabel;
private Label titleSeparator;
private Composite header;
private Label nameLabel;
private Text nameEdit;
private Control headSeparator;
private Label footSeparator;
private Composite footer;
private Label commentLabel;
private Button addButton;
private Button saveButton;
private Composite customPannel;
private RobotGUI robotGUI;
private boolean newRobot;
public RobotConfigDialog(Shell parent, int style, String robotName) throws EasyBotAppException
{
super(parent, style);
try
{
Plugin newRobotPlugin = ClassManager.getInstance().getPluginByName(robotName);
if ( newRobotPlugin != null )
{
robot = (Robot)newRobotPlugin.makeIntance();
newRobot = true;
}
}
catch(Exception e)
{
throw new EasyBotAppException( e.getMessage() );
}
}
public RobotConfigDialog(Shell parent, int style, Robot robot)
{
super(parent,style);
this.robot = robot;
}
public void open()
{
try
{
if ( robot == null )
{
// Error grave! No se pudo crear la instancia
// a partir de la especificaci�n del plug-in
ErrorMessage.errorMessage(Defines.ERROR_NEW_PLUGIN_INSTANCE, getParent());
return;
}
Shell parent = getParent();
dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
GridLayout dialogShellLayout = new GridLayout();
dialogShell.setLayout(dialogShellLayout);
dialogShellLayout.marginHeight = 0;
dialogShellLayout.horizontalSpacing = 0;
dialogShellLayout.verticalSpacing = 0;
dialogShellLayout.marginWidth = 0;
{
backComposite = new Composite(dialogShell, SWT.NONE);
GridLayout backCompositeLayout = new GridLayout();
backCompositeLayout.makeColumnsEqualWidth = true;
backCompositeLayout.horizontalSpacing = 0;
backCompositeLayout.marginHeight = 0;
backCompositeLayout.marginWidth = 0;
GridData backCompositeLData = new GridData();
backCompositeLData.horizontalAlignment = GridData.FILL;
backCompositeLData.verticalAlignment = GridData.FILL;
backCompositeLData.grabExcessHorizontalSpace = true;
backCompositeLData.grabExcessVerticalSpace = true;
backComposite.setLayoutData(backCompositeLData);
backComposite.setLayout(backCompositeLayout);
{
titleLabel = new CLabel(backComposite, SWT.NONE);
titleLabel.setText("Configuraci�n del Robot");
GridData titleLabelLData = new GridData();
titleLabelLData.horizontalAlignment = GridData.FILL;
titleLabelLData.grabExcessHorizontalSpace = true;
titleLabel.setLayoutData(titleLabelLData);
titleLabel.setImage(SWTResourceManager.getImage("resources/icons/icon32x32/enable/AIBO210.png"));
titleLabel.setFont(SWTResourceManager.getFont("Tahoma", 9, 1, false, false));
titleLabel.setForeground(SWTResourceManager.getColor(91, 91, 91));
}
{
titleSeparator = new Label(backComposite, SWT.SEPARATOR
| SWT.HORIZONTAL);
GridData titleSeparatorLData = new GridData();
titleSeparatorLData.horizontalAlignment = GridData.FILL;
titleSeparatorLData.grabExcessHorizontalSpace = true;
titleSeparator.setLayoutData(titleSeparatorLData);
}
{
header = new Composite(backComposite, SWT.NONE);
GridLayout headerLayout = new GridLayout();
headerLayout.numColumns = 2;
headerLayout.marginWidth = 0;
headerLayout.marginHeight = 0;
headerLayout.horizontalSpacing = 0;
GridData headerLData = new GridData();
headerLData.horizontalAlignment = GridData.FILL;
header.setLayoutData(headerLData);
header.setLayout(headerLayout);
{
nameLabel = new Label(header, SWT.NONE);
GridData nameLabelLData = new GridData();
nameLabelLData.horizontalAlignment = GridData.END;
nameLabel.setLayoutData(nameLabelLData);
nameLabel.setText("Nombre: ");
}
{
GridData nameEditLData = new GridData();
nameEditLData.widthHint = 304;
nameEditLData.heightHint = 13;
nameEdit = new Text(header, SWT.NONE);
if (robot.getName()!= null)
nameEdit.setText(robot.getName());
nameEdit.setLayoutData(nameEditLData);
nameEdit.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
chageDialog(evt);
}
});
}
{
commentLabel = new Label(header, SWT.NONE);
GridData commentLabelLData = new GridData();
commentLabelLData.horizontalAlignment = GridData.END;
commentLabel.setLayoutData(commentLabelLData);
commentLabelLData.grabExcessHorizontalSpace = true;
commentLabel.setText("Comentario: ");
}
{
GridData commentTextLData = new GridData();
commentTextLData.widthHint = 310;
commentTextLData.heightHint = 30;
commentTextLData.grabExcessHorizontalSpace = true;
commentText = new StyledText(header, SWT.NONE);
commentText.setLayoutData(commentTextLData);
if (robot.getDescription() != null)
commentText.setText(robot.getDescription());
commentText.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
chageDialog(evt);
}
});
}
}
{
GridData headSeparatorLData = new GridData();
headSeparatorLData.grabExcessHorizontalSpace = true;
headSeparatorLData.horizontalSpan = 2;
headSeparatorLData.horizontalAlignment = GridData.FILL;
headSeparator = new Label(backComposite, SWT.SEPARATOR
| SWT.HORIZONTAL);
headSeparator.setLayoutData(headSeparatorLData);
}
{
customSpace = new Composite(backComposite, SWT.NONE);
GridLayout customSpaceLayout = new GridLayout();
customSpaceLayout.numColumns = 1;
customSpaceLayout.marginWidth = 0;
customSpaceLayout.verticalSpacing = 0;
customSpaceLayout.marginHeight = 0;
customSpaceLayout.horizontalSpacing = 0;
GridData customSpaceLData = new GridData();
customSpaceLData.grabExcessHorizontalSpace = true;
customSpaceLData.horizontalAlignment = GridData.FILL;
customSpaceLData.verticalAlignment = GridData.FILL;
customSpaceLData.grabExcessVerticalSpace = true;
customSpace.setLayoutData(customSpaceLData);
customSpace.setLayout(customSpaceLayout);
{
Plugin robotPlugin = ClassManager.getInstance().getPluginByName(robot.getClass().getCanonicalName());
if (robotPlugin != null)
{
robotGUI= (RobotGUI)robotPlugin.makeGUInstance(robot);
customPannel = robotGUI.getConfigPannel(customSpace, customSpace.getStyle());
if (customPannel != null)
{
//Fuerzo el layout que traia a que se ajuste al del dialogo
GridData customPannelLData = new GridData();
customPannelLData.horizontalAlignment = GridData.FILL;
customPannelLData.verticalAlignment = GridData.FILL;
customPannelLData.grabExcessHorizontalSpace = true;
customPannelLData.grabExcessVerticalSpace = true;
customPannel.setLayoutData(customPannelLData);
}
}
}
//customSpace.layout(true);
}
{
GridData footSeparatorLData = new GridData();
footSeparatorLData.horizontalSpan = 2;
footSeparatorLData.horizontalAlignment = GridData.FILL;
footSeparator = new Label(backComposite, SWT.SEPARATOR
| SWT.HORIZONTAL);
footSeparator.setLayoutData(footSeparatorLData);
}
{
footer = new Composite(backComposite, SWT.NONE);
GridLayout footerLayout = new GridLayout();
footerLayout.makeColumnsEqualWidth = true;
footerLayout.numColumns = 2;
GridData footerLData = new GridData();
footerLData.horizontalAlignment = GridData.FILL;
footerLData.grabExcessHorizontalSpace = true;
footer.setLayoutData(footerLData);
footer.setLayout(footerLayout);
{
addButton = new Button(footer, SWT.PUSH | SWT.CENTER);
GridData addButtonLData = new GridData();
addButtonLData.widthHint = 65;
addButtonLData.heightHint = 23;
addButtonLData.grabExcessHorizontalSpace = true;
addButtonLData.horizontalAlignment = GridData.CENTER;
addButton.setLayoutData(addButtonLData);
addButton.setText("Agregar");
addButton.setEnabled(false);
addButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt) {
saveConfig();
}
});
}
{
saveButton = new Button(footer, SWT.PUSH | SWT.CENTER);
GridData saveButtonLData = new GridData();
saveButtonLData.widthHint = 65;
saveButtonLData.heightHint = 23;
saveButtonLData.horizontalAlignment = GridData.CENTER;
saveButtonLData.grabExcessHorizontalSpace = true;
saveButton.setLayoutData(saveButtonLData);
saveButton.setText("Aplicar");
saveButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt) {
saveButtonWidgetSelected(evt);
}
});
}
}
}
dialogShell.layout();
dialogShell.pack();
dialogShell.setSize(546, 410);
dialogShell.open();
Display display = dialogShell.getDisplay();
while (!dialogShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
catch (Exception e)
{
ErrorMessage.customMessage( e.getMessage(), ErrorMessage.ERROR_MESSAGE, new Shell(Display.getDefault()) );
}
}
private void chageDialog(KeyEvent evt)
{
addButton.setEnabled(false);
saveButton.setEnabled(true);
}
public void applyChanges() throws ComponentGUIException
{
if (robot != null)
{
robot.setName(nameEdit.getText());
robot.setDescription(commentText.getText());
robotGUI.applyChanges();
}
}
private void saveButtonWidgetSelected(SelectionEvent evt)
{
try
{
applyChanges();
}
catch (ComponentGUIException e)
{
ErrorMessage.customMessage(e.getMessage(), ErrorMessage.ERROR_MESSAGE, getParent() );
}
if (robot.isAllOK())
{
addButton.setEnabled(true);
saveButton.setEnabled(false);
}
}
public void saveConfig()
{
if (robot.isAllOK())
{
//Si el robot no existe se lo agrega
if (newRobot)
Simulation.getCurrent().addRobot(robot);
robot.setXMLConfigFile(robot.getName() + ".xml");
dialogShell.close();
}
}
}