<commit_before>/*******************************************************************************
 * Copyright (c) 2015 MEDEVIT <<EMAIL>>.
 * 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:
 *     MEDEVIT <<EMAIL>> - initial API and implementation
 ******************************************************************************/
package ch.elexis.core.ui.wizards;

import java.util.Hashtable;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
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.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.util.DBConnection;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.data.PersistentObject;
import ch.rgw.tools.StringTool;

public class DBConnectSelectionConnectionWizardPage extends DBConnectWizardPage {
	private Label lblConnection, lblUser, lblDriver, lblTyp;
	private ComboViewer cViewerConns;
	private Button btnDelStoredConn;
	
	/**
	 * @wbp.parser.constructor
	 */
	public DBConnectSelectionConnectionWizardPage(String dBConnectWizard_typeOfDB){
		super(Messages.DBConnectFirstPage_Connection);
		
		setTitle(Messages.DBConnectFirstPage_Connection);
		setMessage(Messages.DBConnectSelectionConnectionWizardPage_this_message);
	}
	
	@Override
	public void createControl(Composite parent){
		Composite area = new Composite(parent, SWT.NONE);
		area.setLayout(new GridLayout(1, false));
		
		setControl(area);
		
		Group grpStatCurrentConnection = new Group(area, SWT.NONE);
		grpStatCurrentConnection.setLayout(new GridLayout(2, true));
		grpStatCurrentConnection
			.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		grpStatCurrentConnection
			.setText(Messages.DBConnectWizardPage_grpStatCurrentConnection_text);
		createEntityArea(grpStatCurrentConnection);
		
		Composite cmpExistConnSelector = new Composite(area, SWT.BORDER);
		cmpExistConnSelector.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		cmpExistConnSelector.setLayout(new GridLayout(2, false));
		
		Label lblGespeicherteVerbindungen = new Label(cmpExistConnSelector, SWT.NONE);
		lblGespeicherteVerbindungen
			.setText(Messages.DBConnectWizardPage_lblGespeicherteVerbindungen_text);
		new Label(cmpExistConnSelector, SWT.NONE);
		cViewerConns = new ComboViewer(cmpExistConnSelector, SWT.READ_ONLY);
		Combo combo = cViewerConns.getCombo();
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cViewerConns.setContentProvider(ArrayContentProvider.getInstance());
		cViewerConns.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element){
				DBConnection dbc = (DBConnection) element;
				if (dbc.username != null && dbc.connectionString != null) {
					return dbc.username + "@" + dbc.connectionString;
				} else {
					return "Neue Verbindung erstellen";
				}
			}
		});
		cViewerConns.setInput(getDBConnectWizard().getStoredConnectionList());
		
		btnDelStoredConn = new Button(cmpExistConnSelector, SWT.FLAT);
		btnDelStoredConn.setImage(Images.IMG_DELETE.getImage());
		btnDelStoredConn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e){
				IStructuredSelection selection = (IStructuredSelection) cViewerConns.getSelection();
				if (selection.size() > 0) {
					Object firstElement = selection.getFirstElement();
					if (firstElement != null) {
						getDBConnectWizard().removeConnection((DBConnection) firstElement);
						cViewerConns.setInput(getDBConnectWizard().getStoredConnectionList());
						setCurrentSelection();
					}
				}
			}
		});
		
		cViewerConns.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event){
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				if (selection.size() > 0) {
					Object firstElement = selection.getFirstElement();
					if (firstElement != null) {
						DBConnection targetedConnection = (DBConnection) firstElement;
						getDBConnectWizard().setTargetedConnection(targetedConnection);
						btnDelStoredConn.setEnabled(!targetedConnection.equals(getDBConnectWizard()
							.getCurrentConnection()));
					}
				}
			}
		});
		setCurrentSelection();
		
		Label lblOderAufDer = new Label(cmpExistConnSelector, SWT.NONE);
		lblOderAufDer.setText(Messages.DBConnectSelectionConnectionWizardPage_lblOderAufDer_text);
		new Label(cmpExistConnSelector, SWT.NONE);
		
		tdbg = new TestDBConnectionGroup(area, SWT.NONE, getDBConnectWizard());
		tdbg.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}
	
	private void setCurrentSelection(){
		if (getDBConnectWizard().getCurrentConnection() == null)
			return;
		cViewerConns.setSelection(new StructuredSelection(getDBConnectWizard()
			.getCurrentConnection()));
	}
	
	private void createEntityArea(Group grpEntity){
		String driver = "";
		String user = "";
		String typ = "";
		String connection = "";
		
		Composite typArea = new Composite(grpEntity, SWT.NONE);
		typArea.setLayout(new GridLayout(2, false));<commit_msg>[2966] Add copy to clipboard option to DBWizard connection selection<commit_after>/*******************************************************************************
 * Copyright (c) 2015 MEDEVIT <<EMAIL>>.
 * 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:
 *     MEDEVIT <<EMAIL>> - initial API and implementation
 ******************************************************************************/
package ch.elexis.core.ui.wizards;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;

import javax.xml.bind.JAXBException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.util.DBConnection;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.data.PersistentObject;
import ch.rgw.tools.StringTool;

public class DBConnectSelectionConnectionWizardPage extends DBConnectWizardPage {
	private Label lblConnection, lblUser, lblDriver, lblTyp;
	private ComboViewer cViewerConns;
	private Button btnDelStoredConn;
	private Button btnCopyStoredConn;
	
	/**
	 * @wbp.parser.constructor
	 */
	public DBConnectSelectionConnectionWizardPage(String dBConnectWizard_typeOfDB){
		super(Messages.DBConnectFirstPage_Connection);
		
		setTitle(Messages.DBConnectFirstPage_Connection);
		setMessage(Messages.DBConnectSelectionConnectionWizardPage_this_message);
	}
	
	@Override
	public void createControl(Composite parent){
		Composite area = new Composite(parent, SWT.NONE);
		area.setLayout(new GridLayout(1, false));
		
		setControl(area);
		
		Group grpStatCurrentConnection = new Group(area, SWT.NONE);
		grpStatCurrentConnection.setLayout(new GridLayout(2, true));
		grpStatCurrentConnection
			.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		grpStatCurrentConnection
			.setText(Messages.DBConnectWizardPage_grpStatCurrentConnection_text);
		createEntityArea(grpStatCurrentConnection);
		
		Composite cmpExistConnSelector = new Composite(area, SWT.BORDER);
		cmpExistConnSelector.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		cmpExistConnSelector.setLayout(new GridLayout(3, false));
		
		Label lblGespeicherteVerbindungen = new Label(cmpExistConnSelector, SWT.NONE);
		lblGespeicherteVerbindungen.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
		lblGespeicherteVerbindungen
			.setText(Messages.DBConnectWizardPage_lblGespeicherteVerbindungen_text);

		cViewerConns = new ComboViewer(cmpExistConnSelector, SWT.READ_ONLY);
		Combo combo = cViewerConns.getCombo();
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cViewerConns.setContentProvider(ArrayContentProvider.getInstance());
		cViewerConns.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element){
				DBConnection dbc = (DBConnection) element;
				if (dbc.username != null && dbc.connectionString != null) {
					return dbc.username + "@" + dbc.connectionString;
				} else {
					return "Neue Verbindung erstellen";
				}
			}
		});
		cViewerConns.setInput(getDBConnectWizard().getStoredConnectionList());
		
		btnDelStoredConn = new Button(cmpExistConnSelector, SWT.FLAT);
		btnDelStoredConn.setImage(Images.IMG_DELETE.getImage());
		btnDelStoredConn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e){
				IStructuredSelection selection = (IStructuredSelection) cViewerConns.getSelection();
				if (selection.size() > 0) {
					Object firstElement = selection.getFirstElement();
					if (firstElement != null) {
						getDBConnectWizard().removeConnection((DBConnection) firstElement);
						cViewerConns.setInput(getDBConnectWizard().getStoredConnectionList());
						setCurrentSelection();
					}
				}
			}
		});
		
		btnCopyStoredConn = new Button(cmpExistConnSelector, SWT.FLAT);
		btnCopyStoredConn.setImage(Images.IMG_COPY.getImage());
		btnCopyStoredConn.setToolTipText("Verbindungsdaten in Zwischenablage kopieren");
		btnCopyStoredConn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e){
				IStructuredSelection selection = (IStructuredSelection) cViewerConns.getSelection();
				if (selection.size() > 0) {
					Object firstElement = selection.getFirstElement();
					if (firstElement != null) {
						DBConnection dbc = (DBConnection) firstElement;
						Clipboard cb = new Clipboard(UiDesk.getDisplay());
						TextTransfer textTransfer = TextTransfer.getInstance();
						try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
							dbc.marshall(bos);
							cb.setContents(new Object[] {
								bos.toString()
							}, new Transfer[] {
								textTransfer
							});
						} catch (JAXBException | IOException e1) {
							MessageDialog.openError(UiDesk.getTopShell(), "Error", e1.getMessage());
						}
					}
				}
			}
		});
		
		cViewerConns.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event){
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				if (selection.size() > 0) {
					Object firstElement = selection.getFirstElement();
					if (firstElement != null) {
						DBConnection targetedConnection = (DBConnection) firstElement;
						getDBConnectWizard().setTargetedConnection(targetedConnection);
						btnDelStoredConn.setEnabled(!targetedConnection.equals(getDBConnectWizard()
							.getCurrentConnection()));
					}
				}
			}
		});
		setCurrentSelection();
		
		Label lblOderAufDer = new Label(cmpExistConnSelector, SWT.NONE);
		lblOderAufDer.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
		lblOderAufDer.setText(Messages.DBConnectSelectionConnectionWizardPage_lblOderAufDer_text);
		
		tdbg = new TestDBConnectionGroup(area, SWT.NONE, getDBConnectWizard());
		tdbg.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}
	
	private void setCurrentSelection(){
		if (getDBConnectWizard().getCurrentConnection() == null)
			return;
		cViewerConns.setSelection(new StructuredSelection(getDBConnectWizard()
			.getCurrentConnection()));
	}
	
	private void createEntityArea(Group grpEntity){
		String driver = "";
		String user = "";
		String typ = "";
		String connection = "";
		
		Composite typArea = new Composite(grpEntity, SWT.NONE);
		typArea.setLayout(new GridLayout(2, false));
{'fraction_non_alphanumeric': 0.12496333235552948, 'fraction_numerical': 0.004033440891757114, 'mean_word_length': 5.432547169811321, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '4744971', 'n_tokens_mistral': 4680, 'n_tokens_neox': 4111, 'n_words': 742}