<commit_before>/*******************************************************************************
  * Copyright (c) 2012 <NAME> 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:
  *    <NAME> - initial API and implementation and/or initial documentation
  *    Actuate Corporation - added support of relative file path
  *    Actuate Corporation - support defining an Excel input file path or URI as part of the data source definition
  *******************************************************************************/
package org.eclipse.birt.report.data.oda.excel.ui.wizards;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

import org.eclipse.birt.report.data.oda.excel.ExcelODAConstants;
import org.eclipse.birt.report.data.oda.excel.impl.util.ResourceLocatorUtil;
import org.eclipse.birt.report.data.oda.excel.ui.i18n.Messages;
import org.eclipse.birt.report.data.oda.excel.ui.util.IHelpConstants;
import org.eclipse.birt.report.data.oda.excel.ui.util.Utility;
import org.eclipse.datatools.connectivity.IConnection;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.design.ui.designsession.DesignSessionUtil;
import org.eclipse.datatools.connectivity.oda.design.ui.nls.TextProcessorWrapper;
import org.eclipse.datatools.connectivity.oda.util.ResourceIdentifiers;
import org.eclipse.datatools.connectivity.ui.PingJob;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;

public class ExcelDataSourcePageHelper {

	static final String DEFAULT_MESSAGE = Messages
			.getString("wizard.defaultMessage.selectExcelFile"); //$NON-NLS-1$

	private WizardPage wizardPage;
	private PreferencePage propertyPage;
    private String errorMsg = Messages.getString( "connection_CANNOT_OPEN_EXCEL_FILE_DB_DIR" ); //$NON-NLS-1$

	private transient Text folderLocation = null;
	private transient Button typeLineCheckBox = null;
	private transient MenuButton browseFolderButton = null;
	private transient Button columnNameLineCheckBox = null;
	private transient Composite parent = null;

	private static final int CORRECT_FOLDER = 0;
	private static final int ERROR_FOLDER = 1;
	private static final int ERROR_EMPTY_PATH = 2;
	private static final String EMPTY_STRING = ""; //$NON-NLS-1$
    private static final String CURRENT_DIRECTORY = "."; //$NON-NLS-1$

	private static final Integer SELECT_RELATIVE_PATH = 1;
	private static final Integer SELECT_ABSOLUTE_PATH = 2;

	private static final String ALL_XLSX_EXTENSION = "*.xlsx"; //$NON-NLS-1$
	private static final String ALL_XLS_EXTENSION = "*.xls"; //$NON-NLS-1$
	private static final String ALL_EXTENSION = "*.*";

	private ResourceIdentifiers ri;

	private static final String[] fileExtensions = new String[]{
		ALL_XLS_EXTENSION, ALL_XLSX_EXTENSION, ALL_EXTENSION};

	public ExcelDataSourcePageHelper(ExcelDataSourceWizardPage page) {
		wizardPage = page;
	}

	public ExcelDataSourcePageHelper(
			ExcelDataSourcePropertyPage excelDataSourcePropertyPage) {
		propertyPage = excelDataSourcePropertyPage;
	}

	/**
	 *
	 * @param parent
	 */
	void createCustomControl(Composite parent) {
		this.parent = parent;
		Composite content = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(3, false);
		content.setLayout(layout);

		// GridData data;
		setupFolderLocation(content);

		setupColumnNameLineCheckBox(content);

		setupTypeLineCheckBox(content);

		Utility.setSystemHelp(getControl(),
				IHelpConstants.CONEXT_ID_DATASOURCE_EXCEL);
	}

	private void setupFolderLocation(Composite composite) {
		Label label = new Label(composite, SWT.NONE);
		label.setText(Messages.getString("label.selectFile")); //$NON-NLS-1$

		GridData data = new GridData(GridData.FILL_HORIZONTAL);

		folderLocation = new Text(composite, SWT.BORDER);
		folderLocation.setLayoutData(data);
		setPageComplete(false);
		folderLocation.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				verifyFileLocation();
			}

		});

		browseFolderButton = new MenuButton( composite,
				SWT.NONE,
				Messages.getString( "button.selectFolder.browse" ) ); //$NON-NLS-1$
		Menu menu = new Menu( composite.getShell( ), SWT.POP_UP );
		SelectionAdapter action = new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				if ( e.widget instanceof MenuItem )
				{
					MenuItem item = (MenuItem) e.widget;
					Integer type = (Integer) item.getData( );
					handleFileSelection( type );
				}
				else if ( e.widget instanceof MenuButton )
				{
					handleFileSelection( SELECT_RELATIVE_PATH );
				}
			}
		};

		MenuItem item;
		if ( ri != null )
		{
			item = new MenuItem( menu, SWT.PUSH );
			item.setText( Messages.getString( "button.selectFileURI.menuItem.relativePath" ) ); //$NON-NLS-1$
			item.setData( SELECT_RELATIVE_PATH );
			item.addSelectionListener( action );
		}

		item = new MenuItem( menu, SWT.PUSH );
		item.setText( Messages.getString( "button.selectFileURI.menuItem.absolutePath" ) ); //$NON-NLS-1$
		item.setData( SELECT_ABSOLUTE_PATH );
		item.addSelectionListener( action );

		// Add relative path selection support while having resource identifier
		browseFolderButton.setDropDownMenu( menu );
		browseFolderButton.addSelectionListener( action );

	}


	public void setResourceIdentifiers( org.eclipse.datatools.connectivity.oda.design.ResourceIdentifiers resourceIdentifiers )
	{
		if ( resourceIdentifiers != null )<commit_msg>fix 49749 No response by clicking on Browse button in New Excel Data Source profile dialog in Data Source Explorer view.<commit_after>/*******************************************************************************
  * Copyright (c) 2012 <NAME> 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:
  *    <NAME> - initial API and implementation and/or initial documentation
  *    Actuate Corporation - added support of relative file path
  *    Actuate Corporation - support defining an Excel input file path or URI as part of the data source definition
  *******************************************************************************/
package org.eclipse.birt.report.data.oda.excel.ui.wizards;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

import org.eclipse.birt.report.data.oda.excel.ExcelODAConstants;
import org.eclipse.birt.report.data.oda.excel.impl.util.ResourceLocatorUtil;
import org.eclipse.birt.report.data.oda.excel.ui.i18n.Messages;
import org.eclipse.birt.report.data.oda.excel.ui.util.IHelpConstants;
import org.eclipse.birt.report.data.oda.excel.ui.util.Utility;
import org.eclipse.datatools.connectivity.IConnection;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.design.ui.designsession.DesignSessionUtil;
import org.eclipse.datatools.connectivity.oda.design.ui.nls.TextProcessorWrapper;
import org.eclipse.datatools.connectivity.oda.util.ResourceIdentifiers;
import org.eclipse.datatools.connectivity.ui.PingJob;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;

public class ExcelDataSourcePageHelper {

	static final String DEFAULT_MESSAGE = Messages
			.getString("wizard.defaultMessage.selectExcelFile"); //$NON-NLS-1$

	private WizardPage wizardPage;
	private PreferencePage propertyPage;
    private String errorMsg = Messages.getString( "connection_CANNOT_OPEN_EXCEL_FILE_DB_DIR" ); //$NON-NLS-1$

	private transient Text folderLocation = null;
	private transient Button typeLineCheckBox = null;
	private transient MenuButton browseFolderButton = null;
	private transient Button columnNameLineCheckBox = null;
	private transient Composite parent = null;

	private static final int CORRECT_FOLDER = 0;
	private static final int ERROR_FOLDER = 1;
	private static final int ERROR_EMPTY_PATH = 2;
	private static final String EMPTY_STRING = ""; //$NON-NLS-1$
    private static final String CURRENT_DIRECTORY = "."; //$NON-NLS-1$

	private static final Integer SELECT_RELATIVE_PATH = 1;
	private static final Integer SELECT_ABSOLUTE_PATH = 2;

	private static final String ALL_XLSX_EXTENSION = "*.xlsx"; //$NON-NLS-1$
	private static final String ALL_XLS_EXTENSION = "*.xls"; //$NON-NLS-1$
	private static final String ALL_EXTENSION = "*.*";

	private ResourceIdentifiers ri;

	private static final String[] fileExtensions = new String[]{
		ALL_XLS_EXTENSION, ALL_XLSX_EXTENSION, ALL_EXTENSION};

	public ExcelDataSourcePageHelper(ExcelDataSourceWizardPage page) {
		wizardPage = page;
	}

	public ExcelDataSourcePageHelper(
			ExcelDataSourcePropertyPage excelDataSourcePropertyPage) {
		propertyPage = excelDataSourcePropertyPage;
	}

	/**
	 *
	 * @param parent
	 */
	void createCustomControl(Composite parent) {
		this.parent = parent;
		Composite content = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(3, false);
		content.setLayout(layout);

		// GridData data;
		setupFolderLocation(content);

		setupColumnNameLineCheckBox(content);

		setupTypeLineCheckBox(content);

		Utility.setSystemHelp(getControl(),
				IHelpConstants.CONEXT_ID_DATASOURCE_EXCEL);
	}

	private void setupFolderLocation(Composite composite) {
		Label label = new Label(composite, SWT.NONE);
		label.setText(Messages.getString("label.selectFile")); //$NON-NLS-1$

		GridData data = new GridData(GridData.FILL_HORIZONTAL);

		folderLocation = new Text(composite, SWT.BORDER);
		folderLocation.setLayoutData(data);
		setPageComplete(false);
		folderLocation.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				verifyFileLocation();
			}

		});

		browseFolderButton = new MenuButton( composite,
				SWT.NONE,
				Messages.getString( "button.selectFolder.browse" ) ); //$NON-NLS-1$
		Menu menu = new Menu( composite.getShell( ), SWT.POP_UP );
		SelectionAdapter action = new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				if ( e.widget instanceof MenuItem )
				{
					MenuItem item = (MenuItem) e.widget;
					Integer type = (Integer) item.getData( );
					handleFileSelection( type );
				}
				else if ( e.widget instanceof MenuButton )
				{
					if ( ri == null)
					{
						handleFileSelection( SELECT_ABSOLUTE_PATH );
					}
					else 
					{
						handleFileSelection( SELECT_RELATIVE_PATH );
					}
				}
			}
		};

		MenuItem item;
		if ( ri != null )
		{
			item = new MenuItem( menu, SWT.PUSH );
			item.setText( Messages.getString( "button.selectFileURI.menuItem.relativePath" ) ); //$NON-NLS-1$
			item.setData( SELECT_RELATIVE_PATH );
			item.addSelectionListener( action );
		}

		item = new MenuItem( menu, SWT.PUSH );
		item.setText( Messages.getString( "button.selectFileURI.menuItem.absolutePath" ) ); //$NON-NLS-1$
		item.setData( SELECT_ABSOLUTE_PATH );
		item.addSelectionListener( action );

		// Add relative path selection support while having resource identifier
		browseFolderButton.setDropDownMenu( menu );
		browseFolderButton.addSelectionListener( action );

	}


	public void setResourceIdentifiers( org.eclipse.datatools.connectivity.oda.design.ResourceIdentifiers resourceIdentifiers )
	{
		if ( resourceIdentifiers != null )
{'fraction_non_alphanumeric': 0.122371804568144, 'fraction_numerical': 0.0043109968234760244, 'mean_word_length': 4.817421909370876, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5273407', 'n_tokens_mistral': 4383, 'n_tokens_neox': 3958, 'n_words': 876}