gt: 					IPath packagePath= pack.getPath();
context: /*******************************************************************************
   * Copyright (c) 2000, 2013 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
   *
   * Contributors:
   *     IBM Corporation - initial API and implementation
   *     Philippe Marschall <philippe.marschall@netcetera.ch> - [type wizards] Allow the creation of a compilation unit called package-info.java - https://bugs.eclipse.org/86168
   *     Michael Pellaton <michael.pellaton@netcetera.ch> - [type wizards] Allow the creation of a compilation unit called package-info.java - https://bugs.eclipse.org/86168
   *******************************************************************************/
  package org.eclipse.jdt.ui.wizards;
  
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.Reader;
  import java.io.UnsupportedEncodingException;
  import java.lang.reflect.InvocationTargetException;
  import java.net.URI;
  
  import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory;
  
  import org.eclipse.swt.SWT;
  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.eclipse.core.filesystem.EFS;
  import org.eclipse.core.filesystem.IFileStore;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IPath;
  import org.eclipse.core.runtime.IProgressMonitor;
  import org.eclipse.core.runtime.IStatus;
  import org.eclipse.core.runtime.NullProgressMonitor;
  import org.eclipse.core.runtime.Status;
  import org.eclipse.core.runtime.SubProgressMonitor;
  
  import org.eclipse.core.resources.IFile;
  import org.eclipse.core.resources.IFolder;
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.resources.IWorkspace;
  import org.eclipse.core.resources.ResourcesPlugin;
  
  import org.eclipse.jface.dialogs.Dialog;
  import org.eclipse.jface.dialogs.IDialogSettings;
  import org.eclipse.jface.operation.IRunnableWithProgress;
  import org.eclipse.jface.util.BidiUtils;
  import org.eclipse.jface.viewers.IStructuredSelection;
  
  import org.eclipse.ui.PlatformUI;
  
  import org.eclipse.jdt.core.IBuffer;
  import org.eclipse.jdt.core.ICompilationUnit;
  import org.eclipse.jdt.core.IJavaElement;
  import org.eclipse.jdt.core.IJavaProject;
  import org.eclipse.jdt.core.IPackageFragment;
  import org.eclipse.jdt.core.IPackageFragmentRoot;
  import org.eclipse.jdt.core.ISourceRange;
  import org.eclipse.jdt.core.JavaConventions;
  import org.eclipse.jdt.core.JavaCore;
  import org.eclipse.jdt.core.JavaModelException;
  import org.eclipse.jdt.core.formatter.CodeFormatter;
  
  import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
  import org.eclipse.jdt.internal.corext.javadoc.JavaDocCommentReader;
  import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
  import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
  import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
  import org.eclipse.jdt.internal.corext.util.Messages;
  import org.eclipse.jdt.internal.corext.util.Strings;
  
  import org.eclipse.jdt.ui.CodeGeneration;
  import org.eclipse.jdt.ui.JavaUI;
  
  import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
  import org.eclipse.jdt.internal.ui.JavaPlugin;
  import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
  import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
  import org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter;
  import org.eclipse.jdt.internal.ui.text.javadoc.JavaDoc2HTMLTextReader;
  import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
  import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
  import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
  import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
  import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
  import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
  
  /**
   * Wizard page to create a new package.
   *
   * <p>
   * Note: This class is not intended to be subclassed, but clients can instantiate.
   * To implement a different kind of a new package wizard page, extend <code>NewContainerWizardPage</code>.
   * </p>
   *
   * @since 2.0
   *
   * @noextend This class is not intended to be subclassed by clients.
   */
  public class NewPackageWizardPage extends NewContainerWizardPage {
  
  	private static final String PACKAGE_INFO_JAVA_FILENAME= JavaModelUtil.PACKAGE_INFO_JAVA;
  
  	private static final String PACKAGE_HTML_FILENAME= JavaModelUtil.PACKAGE_HTML;
  
  	private static final String PAGE_NAME= "NewPackageWizardPage"; //$NON-NLS-1$
  
  	private static final String PACKAGE= "NewPackageWizardPage.package"; //$NON-NLS-1$
  
  	private final static String SETTINGS_CREATEPACKAGE_INFO_JAVA= "create_package_info_java"; //$NON-NLS-1$
  
  	private StringDialogField fPackageDialogField;
  
  	private SelectionButtonDialogField fCreatePackageInfoJavaDialogField;
  
  	/*
  	 * Status of last validation of the package field
  	 */
  	private IStatus fPackageStatus;
  
  	private IPackageFragment fCreatedPackageFragment;
  
  	/**
  	 * Creates a new <code>NewPackageWizardPage</code>
  	 */
  	public NewPackageWizardPage() {
  		super(PAGE_NAME);
  
  		setTitle(NewWizardMessages.NewPackageWizardPage_title);
  		setDescription(NewWizardMessages.NewPackageWizardPage_description);
  
  		fCreatedPackageFragment= null;
  
  		PackageFieldAdapter adapter= new PackageFieldAdapter();
  
  		fPackageDialogField= new StringDialogField();
  		fPackageDialogField.setDialogFieldListener(adapter);
  		fPackageDialogField.setLabelText(NewWizardMessages.NewPackageWizardPage_package_label);
  
  		fCreatePackageInfoJavaDialogField= new SelectionButtonDialogField(SWT.CHECK);
  		fCreatePackageInfoJavaDialogField.setDialogFieldListener(adapter);
  		fCreatePackageInfoJavaDialogField.setLabelText(NewWizardMessages.NewPackageWizardPage_package_CreatePackageInfoJava);
  
  		fPackageStatus= new StatusInfo();
  	}
  
  	// -------- Initialization ---------
  
  	/**
  	 * The wizard owning this page is responsible for calling this method with the
  	 * current selection. The selection is used to initialize the fields of the wizard
  	 * page.
  	 *
  	 * @param selection used to initialize the fields
  	 */
  	public void init(IStructuredSelection selection) {
  		IJavaElement jelem= getInitialJavaElement(selection);
  
  		initContainerPage(jelem);
  		String pName= ""; //$NON-NLS-1$
  		if (jelem != null) {
  			IPackageFragment pf= (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
  			if (pf != null && !pf.isDefaultPackage()) {
  				pName= pf.getElementName();
  			} else {
  				if (jelem.getJavaProject() != null) {
  					final IPackageFragmentRoot pkgFragmentRoot= getPackageFragmentRoot();
  					if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
  						try {
  							IJavaElement[] packages= pkgFragmentRoot.getChildren();
  							if (packages.length == 1) { // only default package
  								String prName= jelem.getJavaProject().getElementName();
  								IStatus status= getPackageStatus(prName);
  								if (status.getSeverity() == IStatus.OK) {
  									pName= prName;
  								}
  							}
  						} catch (JavaModelException e) {
  							// fall through
  						}
  					}
  				}
  			}
  		}
  		setPackageText(pName, true);
  
  		IDialogSettings dialogSettings= getDialogSettings();
  		if (dialogSettings != null) {
  			IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
  			if (section != null) {
  				boolean createPackageDocumentation= section.getBoolean(SETTINGS_CREATEPACKAGE_INFO_JAVA);
  				fCreatePackageInfoJavaDialogField.setSelection(createPackageDocumentation);
  			}
  		}
  
  		updateStatus(new IStatus[] { fContainerStatus, fPackageStatus });
  	}
  
  	// -------- UI Creation ---------
  
  	/*
  	 * @see WizardPage#createControl
  	 */
  	public void createControl(Composite parent) {
  		initializeDialogUnits(parent);
  
  		Composite composite= new Composite(parent, SWT.NONE);
  		composite.setFont(parent.getFont());
  		int nColumns= 3;
  
  		GridLayout layout= new GridLayout();
  		layout.numColumns= 3;
  		composite.setLayout(layout);
  
  		Label label= new Label(composite, SWT.WRAP);
  		label.setText(NewWizardMessages.NewPackageWizardPage_info);
  		GridData gd= new GridData();
  		gd.widthHint= convertWidthInCharsToPixels(60);
  		gd.horizontalSpan= 3;
  		label.setLayoutData(gd);
  
  		createContainerControls(composite, nColumns);
  		createPackageControls(composite, nColumns);
  
  		setControl(composite);
  		Dialog.applyDialogFont(composite);
  		PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_PACKAGE_WIZARD_PAGE);
  	}
  
  	/**
  	 * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
  	 */
  	@Override
  	public void setVisible(boolean visible) {
  		super.setVisible(visible);
  		if (visible) {
  			setFocus();
  		}
  	}
  
  	/**
  	 * Sets the focus to the package name input field.
  	 */
  	protected void setFocus() {
  		fPackageDialogField.setFocus();
  	}
  
  
  	private void createPackageControls(Composite composite, int nColumns) {
  		fPackageDialogField.doFillIntoGrid(composite, nColumns - 1);
  		Text text= fPackageDialogField.getTextControl(null);
  		LayoutUtil.setWidthHint(text, getMaxFieldWidth());
  		LayoutUtil.setHorizontalGrabbing(text);
  		DialogField.createEmptySpace(composite);
  
  		TextFieldNavigationHandler.install(text);
  		
  		fCreatePackageInfoJavaDialogField.doFillIntoGrid(composite, nColumns);
  		BidiUtils.applyBidiProcessing(fPackageDialogField.getTextControl(null), StructuredTextTypeHandlerFactory.JAVA);
  	}
  
  	// -------- PackageFieldAdapter --------
  
  	private class PackageFieldAdapter implements IDialogFieldListener {
  
  		// --------- IDialogFieldListener
  
  		public void dialogFieldChanged(DialogField field) {
  			fPackageStatus= getPackageStatus(getPackageText());
  			// tell all others
  			handleFieldChanged(PACKAGE);
  		}
  	}
  
  	// -------- update message ----------------
  
  	/*
  	 * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#handleFieldChanged(String)
  	 */
  	@Override
  	protected void handleFieldChanged(String fieldName) {
  		super.handleFieldChanged(fieldName);
  		if (fieldName == CONTAINER) {
  			fPackageStatus= getPackageStatus(getPackageText());
  		}
  		// do status line update
  		updateStatus(new IStatus[] { fContainerStatus, fPackageStatus });
  	}
  
  	// ----------- validation ----------
  
  	private IStatus validatePackageName(String text) {
  		IJavaProject project= getJavaProject();
  		if (project == null || !project.exists()) {
  			return JavaConventions.validatePackageName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
  		}
  		return JavaConventionsUtil.validatePackageName(text, project);
  	}
  
  	/**
  	 * Validates the package name and returns the status of the validation.
  	 * 
  	 * @param packName the package name
  	 * 
  	 * @return the status of the validation
  	 */
  	private IStatus getPackageStatus(String packName) {
  		StatusInfo status= new StatusInfo();
  		if (packName.length() > 0) {
  			IStatus val= validatePackageName(packName);
  			if (val.getSeverity() == IStatus.ERROR) {
  				status.setError(Messages.format(NewWizardMessages.NewPackageWizardPage_error_InvalidPackageName, val.getMessage()));
  				return status;
  			} else if (val.getSeverity() == IStatus.WARNING) {
  				status.setWarning(Messages.format(NewWizardMessages.NewPackageWizardPage_warning_DiscouragedPackageName, val.getMessage()));
  			}
  		} else {
  			status.setError(NewWizardMessages.NewPackageWizardPage_error_EnterName);
  			return status;
  		}
  
  		IPackageFragmentRoot root= getPackageFragmentRoot();
  		if (root != null && root.getJavaProject().exists()) {
  			IPackageFragment pack= root.getPackageFragment(packName);
  			try {
  				IPath rootPath= root.getPath();
  				IPath outputPath= root.getJavaProject().getOutputLocation();
  				if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
  					// if the bin folder is inside of our root, don't allow to name a package
  					// like the bin folder
  