/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui.views.admin;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.ObservablesManager;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
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.Label;

import com.patrikdufresne.util.Localized;
import com.planimod.ui.preference.PlanimodPreferences;

/**
 * This dialog is used to change the Planimod parameters.
 * 
 * @author Patrik Dufresne
 * 
 */
public class AppearancePreferencePage extends PreferencePage {

	private WritableValue showLines = new WritableValue(null, Boolean.class);

	private Button showLinesButton;

	private DataBindingContext dbc;

	private Localized localized = Localized
			.load(AppearancePreferencePage.class);

	private ObservablesManager om;

	/**
	 * Create a new preference page.
	 */
	public AppearancePreferencePage() {
		super();
		setTitle(this.localized.get("AppearancePreferencePage.title")); //$NON-NLS-1$
	}

	/**
	 * Used to bind the widgets
	 */
	protected void bindValues() {

		this.showLines.setValue(Boolean.valueOf(getPreferenceStore()
				.getBoolean(PlanimodPreferences.SHOW_LINES)));

		// Bind the widgets
		this.dbc.bindValue(
				WidgetProperties.selection().observe(this.showLinesButton),
				this.showLines);

	}

	@Override
	protected Control createContents(Composite parent) {

		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(2, false));
		comp.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Show lines
		Label label = new Label(comp, SWT.WRAP);
		label.setText(this.localized
				.get("AppearancePreferencePage.showLines.notice")); //$NON-NLS-1$
		GridData data = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
		data.widthHint = 150;
		label.setLayoutData(data);
		this.showLinesButton = new Button(comp, SWT.CHECK);
		this.showLinesButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, false, 2, 1));
		this.showLinesButton.setText(this.localized
				.get("AppearancePreferencePage.showLines")); //$NON-NLS-1$

		/*
		 * Bind widgets
		 */
		this.dbc = new DataBindingContext();
		this.om = new ObservablesManager();
		this.om.runAndCollect(new Runnable() {
			@Override
			public void run() {
				bindValues();
			}
		});
		this.om.addObservablesFromContext(this.dbc, true, true);

		return comp;
	}

	/**
	 * This implementation save the parameters to the managers.
	 */
	@Override
	public boolean performOk() {
		Boolean value = (Boolean) (this.showLines.getValue() instanceof Boolean ? this.showLines
				.getValue() : Boolean.FALSE);
		getPreferenceStore().setValue(PlanimodPreferences.SHOW_LINES,
				value.booleanValue());
		return true;
	}

}
