/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * 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
 ********************************************************************************/
package org.eclipse.jdt.ui.internal.copyrightupdater;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.PixelConverter;

import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
import org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI;

public class CopyrightTabPage implements ICleanUpConfigurationUI {

 private PixelConverter fPixelConverter;
 private CleanUpOptions fOptions;

 public CopyrightTabPage() {
  super();
 }

 /* (non-Javadoc)
  * @see org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI#createContents(org.eclipse.swt.widgets.Composite)
  */
 public Composite createContents(Composite parent) {
  final int numColumns= 4;

  if (fPixelConverter == null) {
   fPixelConverter= new PixelConverter(parent);
  }

  final SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
  sashForm.setFont(parent.getFont());

  Composite scrollContainer = new Composite(sashForm, SWT.NONE);

  GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
  scrollContainer.setLayoutData(gridData);

  GridLayout layout= new GridLayout(2, false);
  layout.marginHeight= 0;
  layout.marginWidth= 0;
  layout.horizontalSpacing= 0;
  layout.verticalSpacing= 0;
  scrollContainer.setLayout(layout);

  ScrolledComposite scroll= new ScrolledComposite(scrollContainer, SWT.V_SCROLL | SWT.H_SCROLL);
  scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
  scroll.setExpandHorizontal(true);
  scroll.setExpandVertical(true);

  final Composite settingsContainer= new Composite(scroll, SWT.NONE);
  settingsContainer.setFont(sashForm.getFont());

  scroll.setContent(settingsContainer);

  settingsContainer.setLayout(new PageLayout(scroll, 400, 400));
  settingsContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

  Composite settingsPane= new Composite(settingsContainer, SWT.NONE);
  settingsPane.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

  layout= new GridLayout(numColumns, false);
  layout.verticalSpacing= (int)(1.5 * fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING));
  layout.horizontalSpacing= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
  layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
  layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
  settingsPane.setLayout(layout);
  doCreatePreferences(settingsPane);

  settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));

  scroll.addControlListener(new ControlListener() {

   public void controlMoved(ControlEvent e) {
   }

   public void controlResized(ControlEvent e) {
    settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
   }
  });

  Label sashHandle = new Label(scrollContainer, SWT.SEPARATOR | SWT.VERTICAL);
  gridData= new GridData(SWT.RIGHT, SWT.FILL, false, true);
  sashHandle.setLayoutData(gridData);

  return sashForm;
 }



 /**
  * Creates the preferences for the tab page.
  *
  * @param composite Composite to create in
  */
 protected void doCreatePreferences(Composite composite) {
  Group group= new Group(composite, SWT.NONE);
  group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  group.setLayout(new GridLayout(1, false));
  group.setText("Copyright Update"); //$NON-NLS-1$

  final Button updateCheckbox= new Button(group, SWT.CHECK);
  updateCheckbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
  updateCheckbox.setText("Update the Copyrights"); //$NON-NLS-1$
  updateCheckbox.setSelection(fOptions.isEnabled("cleanup.update_copyrights")); //$NON-NLS-1$
  updateCheckbox.addSelectionListener(new SelectionAdapter() {
   /*
    * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
    */
   public void widgetSelected(SelectionEvent e) {
    fOptions.setOption("cleanup.update_copyrights", updateCheckbox.getSelection() ? CleanUpOptions.TRUE : CleanUpOptions.FALSE); //$NON-NLS-1$
   }
  });
 }

 /* (non-Javadoc)
  * @see org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI#getCleanUpCount()
  */
 public int getCleanUpCount() {
  return 1;
 }

 /* (non-Javadoc)
  * @see org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI#getPreview()
  */
 public String getPreview() {
  StringBuffer buf= new StringBuffer();

  if (fOptions.isEnabled("cleanup.update_copyrights")) {//$NON-NLS-1$
   buf.append("/* Copyright (c) 2007, 2009 IBM Corporation and others.*/"); //$NON-NLS-1$
  } else {
   buf.append("/* Copyright (c) 2007, 2008 IBM Corporation and others.*/"); //$NON-NLS-1$
  }

  return buf.toString();

 }

 /* (non-Javadoc)
  * @see org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI#getSelectedCleanUpCount()
  */
 public int getSelectedCleanUpCount() {
  return fOptions.isEnabled("cleanup.update_copyrights") ? 1 : 0; //$NON-NLS-1$
 }

 /* (non-Javadoc)
  * @see org.eclipse.jdt.ui.cleanup.ICleanUpConfigurationUI#setOptions(org.eclipse.jdt.ui.cleanup.CleanUpOptions)
  */
 public void setOptions(CleanUpOptions options) {
  fOptions= options;

 }

 /**
  * Layout used for the settings part. Makes sure to show scrollbars
  * if necessary. The settings part needs to be layouted on resize.
  */
 private static class PageLayout extends Layout {

  private final ScrolledComposite fContainer;
  private final int fMinimalWidth;
  private final int fMinimalHight;

  private PageLayout(ScrolledComposite container, int minimalWidth, int minimalHight) {
   fContainer= container;
   fMinimalWidth= minimalWidth;
   fMinimalHight= minimalHight;
  }

  public Point computeSize(Composite composite, int wHint, int hHint, boolean force) {
   if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
    return new Point(wHint, hHint);
   }

   int x = fMinimalWidth;
   int y = fMinimalHight;
   Control[] children = composite.getChildren();
   for (int i = 0; i < children.length; i++) {
    Point size = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
    x = Math.max(x, size.x);
    y = Math.max(y, size.y);
   }

   Rectangle area= fContainer.getClientArea();
   if (area.width > x) {
    fContainer.setExpandHorizontal(true);
   } else {
    fContainer.setExpandHorizontal(false);
   }

   if (area.height > y) {
    fContainer.setExpandVertical(true);
   } else {
    fContainer.setExpandVertical(false);
   }

   if (wHint != SWT.DEFAULT) {
    x = wHint;
   }
   if (hHint != SWT.DEFAULT) {
    y = hHint;
   }

   return new Point(x, y);
  }

  /* (non-Javadoc)
   * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
   */
  public void layout(Composite composite, boolean force) {
   Rectangle rect = composite.getClientArea();
   Control[] children = composite.getChildren();
   for (int i = 0; i < children.length; i++) {
    children[i].setSize(rect.width, rect.height);
   }
  }
 }
}