/*
 * 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.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
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.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.patrikdufresne.util.Localized;
import com.planimod.ui.theme.Resources;

/**
 * This dialog show general information about the application.
 * 
 * @author Patrik Dufresne
 * 
 */
public class AboutDialog extends Dialog {

	/**
	 * Create a new About dialog.
	 * 
	 * @param shell
	 *            the parent of this dialog.
	 */
	public AboutDialog(Shell shell) {
		super(shell);
	}

	/**
	 * This implementation close the dialog.
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	@Override
	protected void buttonPressed(int buttonId) {
		cancelPressed();
	}

	/**
	 * This implementation create a close button.
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.CLOSE_ID,
				IDialogConstants.CLOSE_LABEL, true);
	}

	/**
	 * This implementation set the windows title
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Localized.get(AboutDialog.class, "AboutDialog.title")); //$NON-NLS-1$
	}

	/**
	 * This implementation create the content of the about dialog.
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		// Create the composite to hold all the controls
		Composite comp = new Composite(parent, SWT.NONE);

		// Configure parent layout
		GridLayout gridLayout = new GridLayout(1, false);
		comp.setLayout(gridLayout);

		// Image
		Label imageLabel = new Label(comp, SWT.NONE);
		imageLabel.setImage(Resources.getImage(Resources.APP_ICON_128));
		imageLabel
				.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));

		// App name
		Text appNameText = new Text(comp, SWT.CENTER | SWT.READ_ONLY
				| SWT.MULTI | SWT.WRAP);
		appNameText.setText(Localized
				.get(AboutDialog.class, "AboutDialog.name")); //$NON-NLS-1$
		appNameText.setFont(Resources.getApplicationNameFont());
		appNameText
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		appNameText.setBackground(this.getShell().getBackground());

		// App version
		String version = this.getClass().getPackage()
				.getImplementationVersion();
		if (version == null) {
			version = ""; //$NON-NLS-1$
		} else {
			version = Localized.format(AboutDialog.class,
					"AboutDialog.version", version); //$NON-NLS-1$
		}
		Text appVersion = new Text(comp, SWT.CENTER | SWT.READ_ONLY | SWT.MULTI
				| SWT.WRAP);
		appVersion.setText(version);
		appVersion.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		appVersion.setBackground(this.getShell().getBackground());

		// App description
		Text appDescText = new Text(comp, SWT.CENTER | SWT.READ_ONLY
				| SWT.MULTI | SWT.WRAP);
		appDescText.setText(Localized.get(AboutDialog.class,
				"AboutDialog.description")); //$NON-NLS-1$
		appDescText
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		appDescText.setBackground(this.getShell().getBackground());

		// App copyright
		Text appCopyrightText = new Text(comp, SWT.CENTER | SWT.READ_ONLY
				| SWT.MULTI);
		appCopyrightText.setText(Localized.get(AboutDialog.class,
				"AboutDialog.copyright")); //$NON-NLS-1$
		appCopyrightText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false));
		appCopyrightText.setBackground(this.getShell().getBackground());

		// Application web site
		Link appWebSite = new Link(comp, SWT.NONE);
		appWebSite.setText(Localized.get(AboutDialog.class,
				"AboutDialog.link.text")); //$NON-NLS-1$
		appWebSite
				.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false));
		appWebSite.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				Program.launch(Localized.get(AboutDialog.class,
						"AboutDialog.link")); //$NON-NLS-1$
			}
		});

		return imageLabel;

	}
}