package ru.rsdn.patterns.mvc.samples;

import org.eclipse.swt.SWT;
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.Label;

import ru.rsdn.patterns.mvc.model.Model;
import ru.rsdn.patterns.mvc.view.View;

/**
 * Представление переключателя
 */
public class SwitchView extends View<Switch> {
	private final Label label;

	/**
	 * Конструктор
	 * @param parent композит
	 */
	public SwitchView(Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER |
				GridData.VERTICAL_ALIGN_CENTER));
		final Button onButton = new Button(composite, SWT.PUSH);
		onButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		onButton.setText("Включить");
		onButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edit(new Switch(true));
			}
		});
		final Button offButton = new Button(composite, SWT.PUSH);
		offButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		offButton.setText("Выключить");
		offButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				edit(new Switch(false));
			}
		});
		final Button undoButton = new Button(composite, SWT.PUSH);
		undoButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		undoButton.setText("Отмена");
		undoButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				undo();
			}
		});
		final Button redoButton = new Button(composite, SWT.PUSH);
		redoButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		redoButton.setText("Повтор");
		redoButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				redo();
			}
		});
	}

	public void modelChanged(Model<Switch> model) {
		label.setText(model.toString());
	}
}
