/*******************************************************************************
 * Copyright (c) 2013 Pivotal Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Pivotal Software, Inc. - initial API and implementation
 *******************************************************************************/
package org.springsource.ide.eclipse.commons.livexp.ui;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.springsource.ide.eclipse.commons.livexp.core.FieldModel;
import org.springsource.ide.eclipse.commons.livexp.core.LiveExpression;
import org.springsource.ide.eclipse.commons.livexp.core.LiveVariable;
import org.springsource.ide.eclipse.commons.livexp.core.ValidationResult;
import org.springsource.ide.eclipse.commons.livexp.core.Validator;
import org.springsource.ide.eclipse.commons.livexp.core.ValueListener;

public class StringFieldSection extends WizardPageSection {

	/// options
	private String labelText; //what text to use for the label of the field
	private String tooltip = null; // what text to use for tooltip

	/// model elements
	private final LiveVariable<String> variable;
	private final LiveExpression<ValidationResult> validator;

	/// UI elements
	private Text text;


	//////////////////////////////

	public StringFieldSection(IPageWithSections owner,
			String labelText,
			LiveVariable<String> variable,
			LiveExpression<ValidationResult> validator) {
		super(owner);
		this.labelText = labelText;
		this.variable = variable;
		this.validator = validator;
	}

	public StringFieldSection(IPageWithSections owner, FieldModel<String> f) {
		this(owner, f.getLabel(), f.getVariable(), f.getValidator());
	}

	public StringFieldSection(IPageWithSections owner, String label, LiveVariable<String> f) {
		this(owner, label, f, Validator.OK);
	}

	@Override
	public LiveExpression<ValidationResult> getValidator() {
		return validator;
	}

	@Override
	public void createContents(Composite page) {
        // project specification group
        Composite projectGroup = new Composite(page, SWT.NONE);
        GridLayout layout = GridLayoutFactory.fillDefaults().numColumns(2).margins(0,2).create();
        projectGroup.setLayout(layout);
        projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Label label = new Label(projectGroup, SWT.NONE);
        label.setText(labelText);
        GridDataFactory.fillDefaults()
        	.hint(UIConstants.fieldLabelWidthHint(label), SWT.DEFAULT)
        	.align(SWT.BEGINNING, SWT.CENTER)
        	.applyTo(label);

        text = new Text(projectGroup, SWT.BORDER);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.widthHint = UIConstants.FIELD_TEXT_AREA_WIDTH;
        text.setLayoutData(data);
        text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				variable.setValue(text.getText());
			}
		});
        variable.addListener(new ValueListener<String>() {
			public void gotValue(LiveExpression<String> exp, String value) {
				if (text!=null && !text.isDisposed()) {
					if (value==null) {
						value = "";
					}
					String oldText = text.getText();
					if (!oldText.equals(value)) {
						text.setText(value);
					}
				}
			}
		});

        if (tooltip!=null) {
        	label.setToolTipText(tooltip);
        	text.setToolTipText(tooltip);
        }
	}

	public StringFieldSection tooltip(String string) {
		this.tooltip = string;
		return this;
	}

}

@@ -1799,16 +1799,43 @@
 t text;%0A
+%09private boolean password;%0A
 %0A%0A%09/////
@@ -2013,16 +2013,42 @@
 alidator
+,%0A%09%09%09boolean passwordField
 ) %7B%0A%09%09su
@@ -2143,24 +2143,271 @@
  validator;%0A
+%09%09this.password = passwordField;%0A%09%7D%0A%0A%09public StringFieldSection(IPageWithSections owner,%0A%09%09%09String labelText,%0A%09%09%09LiveVariable%3CString%3E variable,%0A%09%09%09LiveExpression%3CValidationResult%3E validator) %7B%0A%09%09this(owner, labelText, variable, validator, false);%0A
 %09%7D%0A%0A%09public 
@@ -2626,32 +2626,183 @@
 le%3CString%3E f) %7B%0A
+%09%09this(owner, label, f, false);%0A%09%7D%0A%0A%09public StringFieldSection(IPageWithSections owner, String label, LiveVariable%3CString%3E f, boolean passwordField) %7B%0A
 %09%09this(owner, la
@@ -2821,16 +2821,31 @@
 dator.OK
+, passwordField
 );%0A%09%7D%0A%0A%09
@@ -3575,24 +3575,24 @@
 To(label);%0A%0A
-
         text
@@ -3627,16 +3627,48 @@
 T.BORDER
+ %7C (password ? SWT.PASSWORD : 0)
 );%0A     

allow string field to be a password field