/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */

package org.apache.directory.studio.valueeditors.address;


import java.util.regex.Pattern;

import org.apache.commons.text.translate.CharSequenceTranslator;
import org.apache.directory.studio.common.ui.widgets.BaseWidgetUtils;
import org.apache.directory.studio.ldapbrowser.core.BrowserCoreConstants;
import org.apache.directory.studio.ldapbrowser.core.utils.Utils;
import org.apache.directory.studio.valueeditors.ValueEditorsActivator;
import org.apache.directory.studio.valueeditors.ValueEditorsConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
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.Shell;
import org.eclipse.swt.widgets.Text;


/**
 * The AddressDialog is used from the address value editor to edit an address.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class AddressDialog extends Dialog
{

    /** The initial address. */
    private String initialAddress;

    /** The return address. */
    private String returnAddress;

    /** The text widget. */
    private Text text;

    /** The postal address decoder. */
    private CharSequenceTranslator decoder;

    /** The postal address encoder. */
    private CharSequenceTranslator encoder;

    /** The checkbox to strip trailing whitespace. */
    private Button stripWhitespaceCheckbox;

    /**
     * Creates a new instance of AddressDialog.
     * 
     * @param parentShell the parent shell
     * @param initialAddress the initial address
     */
    public AddressDialog( Shell parentShell, String initialAddress )
    {
        super( parentShell );
        super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
        this.initialAddress = initialAddress;
        this.returnAddress = null;
        this.decoder = Utils.createPostalAddressDecoder( BrowserCoreConstants.LINE_SEPARATOR );
        this.encoder = Utils.createPostalAddressEncoder( BrowserCoreConstants.LINE_SEPARATOR );
    }


    /**
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    protected void configureShell( Shell shell )
    {
        super.configureShell( shell );
        shell.setText( Messages.getString( "AddressDialog.AddressEditor" ) ); //$NON-NLS-1$
        shell.setImage( ValueEditorsActivator.getDefault().getImage( ValueEditorsConstants.IMG_ADDRESSEDITOR ) );
    }


    /**
     * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
     */
    protected void createButtonsForButtonBar( Composite parent )
    {
        ((GridLayout) parent.getLayout()).numColumns = 2;
        stripWhitespaceCheckbox = BaseWidgetUtils.createCheckbox( parent, Messages.getString( "AddressDialog.StripWhitespace" ), 2 ); //$NON-NLS-1$
        stripWhitespaceCheckbox.setSelection( true );
        createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
        createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
    }


    private static final Pattern TRAILING_WHITESPACE = Pattern.compile( "\\s+$", Pattern.MULTILINE ); //$NON-NLS-1$


    /**
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    protected void okPressed()
    {
        String lines = text.getText();
        if ( stripWhitespaceCheckbox.getSelection() ) {
            lines = TRAILING_WHITESPACE.matcher(lines).replaceAll( "" ); //$NON-NLS-1$
        }
        returnAddress = encoder.translate( lines );
        super.okPressed();
    }


    /**
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    protected Control createDialogArea( Composite parent )
    {
        // create composite
        Composite composite = ( Composite ) super.createDialogArea( parent );
        GridData gd = new GridData( GridData.FILL_BOTH );
        composite.setLayoutData( gd );

        // text widget
        text = new Text( composite, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL );
        text.setText( decoder.translate( initialAddress ) );
        // GridData gd = new GridData(GridData.GRAB_HORIZONTAL |
        // GridData.HORIZONTAL_ALIGN_FILL);
        gd = new GridData( GridData.FILL_BOTH );
        gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
        gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 );
        text.setLayoutData( gd );

        applyDialogFont( composite );
        return composite;
    }


    /**
     * Gets the address.
     * 
     * @return the address
     */
    public String getAddress()
    {
        return returnAddress;
    }

}
