/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.postgresql.ui;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.jkiss.dbeaver.ext.postgresql.PostgreActivator;
import org.jkiss.dbeaver.ext.postgresql.PostgreConstants;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.ui.ICompositeDialogPage;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.connection.ClientHomesSelector;
import org.jkiss.dbeaver.ui.dialogs.connection.ConnectionPageAbstract;
import org.jkiss.dbeaver.ui.dialogs.connection.DriverPropertiesDialogPage;
import org.jkiss.utils.CommonUtils;
import java.util.Locale;
/**
* PostgreConnectionPage
*/
public class PostgreConnectionPage extends ConnectionPageAbstract implements ICompositeDialogPage
{
private Text hostText;
private Text portText;
private Text dbText;
private Text usernameText;
private Text passwordText;
private ClientHomesSelector homesSelector;
private Button hideNonDefault;
private boolean activated = false;
private static ImageDescriptor LOGO_IMG = PostgreActivator.getImageDescriptor("icons/postgresql_logo.png");
@Override
public void dispose()
{
super.dispose();
}
@Override
public void createControl(Composite composite)
{
//Composite group = new Composite(composite, SWT.NONE);
//group.setLayout(new GridLayout(1, true));
ModifyListener textListener = new ModifyListener()
{
@Override
public void modifyText(ModifyEvent e)
{
if (activated) {
saveSettings(site.getActiveDataSource());
site.updateButtons();
}
}
};
Composite addrGroup = UIUtils.createPlaceholder(composite, 4);
GridLayout gl = new GridLayout(4, false);
addrGroup.setLayout(gl);
GridData gd = new GridData(GridData.FILL_BOTH);
addrGroup.setLayoutData(gd);
Label hostLabel = UIUtils.createControlLabel(addrGroup, "Host");
hostLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
hostText = new Text(addrGroup, SWT.BORDER);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.grabExcessHorizontalSpace = true;
hostText.setLayoutData(gd);
hostText.addModifyListener(textListener);
Label portLabel = UIUtils.createControlLabel(addrGroup, "Port");
gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
portLabel.setLayoutData(gd);
portText = new Text(addrGroup, SWT.BORDER);
gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
gd.widthHint = 40;
portText.setLayoutData(gd);
portText.addVerifyListener(UIUtils.getIntegerVerifyListener(Locale.getDefault()));
portText.addModifyListener(textListener);
Label dbLabel = UIUtils.createControlLabel(addrGroup, "Database");
dbLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
dbText = new Text(addrGroup, SWT.BORDER);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.grabExcessHorizontalSpace = true;
gd.horizontalSpan = 3;
dbText.setLayoutData(gd);
dbText.addModifyListener(textListener);
Label usernameLabel = UIUtils.createControlLabel(addrGroup, "User");
usernameLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
usernameText = new Text(addrGroup, SWT.BORDER);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.grabExcessHorizontalSpace = true;
usernameText.setLayoutData(gd);
usernameText.addModifyListener(textListener);
Label passwordLabel = UIUtils.createControlLabel(addrGroup, "Password");
passwordLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
passwordText = new Text(addrGroup, SWT.BORDER | SWT.PASSWORD);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.grabExcessHorizontalSpace = true;
passwordText.setLayoutData(gd);
passwordText.addModifyListener(textListener);
{
Composite buttonsGroup = new Composite(addrGroup, SWT.NONE);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
buttonsGroup.setLayoutData(gd);
buttonsGroup.setLayout(new GridLayout(2, false));
homesSelector = new ClientHomesSelector(buttonsGroup, SWT.NONE, "Local Client");
gd = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING);
homesSelector.getPanel().setLayoutData(gd);
}
{
Group secureGroup = new Group(addrGroup, SWT.NONE);
secureGroup.setText("Settings");
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 4;
secureGroup.setLayoutData(gd);
secureGroup.setLayout(new GridLayout(2, false));
hideNonDefault = UIUtils.createLabelCheckbox(secureGroup, "Show non-default databases", true);
}
createDriverPanel(addrGroup);
setControl(addrGroup);
}
@Override
public boolean isComplete()
{
return hostText != null && portText != null &&
!CommonUtils.isEmpty(hostText.getText()) &&
!CommonUtils.isEmpty(portText.getText());
}
@Override
public void loadSettings()
{
super.loadSettings();
if (!activated) {
setImageDescriptor(LOGO_IMG);
}
// Load values from new connection info
DBPConnectionConfiguration connectionInfo = site.getActiveDataSource().getConnectionConfiguration();
if (hostText != null) {
if (!CommonUtils.isEmpty(connectionInfo.getHostName())) {
hostText.setText(connectionInfo.getHostName());
} else {
hostText.setText(PostgreConstants.DEFAULT_HOST);
}
}
if (portText != null) {
if (!CommonUtils.isEmpty(connectionInfo.getHostPort())) {
portText.setText(String.valueOf(connectionInfo.getHostPort()));
} else if (site.getDriver().getDefaultPort() != null) {
portText.setText(site.getDriver().getDefaultPort());
} else {
portText.setText("");
}
}
if (dbText != null) {
String databaseName = connectionInfo.getDatabaseName();
if (CommonUtils.isEmpty(databaseName) && getSite().isNew()) {
databaseName = PostgreConstants.DEFAULT_DATABASE;
}
dbText.setText(databaseName);
}
if (usernameText != null) {
usernameText.setText(CommonUtils.notEmpty(connectionInfo.getUserName()));
}
if (passwordText != null) {
passwordText.setText(CommonUtils.notEmpty(connectionInfo.getUserPassword()));
}
homesSelector.populateHomes(site.getDriver(), connectionInfo.getClientHomeId());
final boolean showNDD = CommonUtils.toBoolean(connectionInfo.getProviderProperty(PostgreConstants.PROP_SHOW_NON_DEFAULT_DB));
hideNonDefault.setSelection(showNDD);
activated = true;
}
@Override
public void saveSettings(DBPDataSourceContainer dataSource)
{
DBPConnectionConfiguration connectionInfo = dataSource.getConnectionConfiguration();
if (hostText != null) {
connectionInfo.setHostName(hostText.getText().trim());
}
if (portText != null) {
connectionInfo.setHostPort(portText.getText().trim());
}
if (dbText != null) {
connectionInfo.setDatabaseName(dbText.getText().trim());
}
if (usernameText != null) {
connectionInfo.setUserName(usernameText.getText().trim());
}
if (passwordText != null) {
connectionInfo.setUserPassword(passwordText.getText());
}
if (homesSelector != null) {
connectionInfo.setClientHomeId(homesSelector.getSelectedHome());
}
connectionInfo.setProviderProperty(PostgreConstants.PROP_SHOW_NON_DEFAULT_DB, String.valueOf(hideNonDefault.getSelection()));
super.saveSettings(dataSource);
}
@Override
public IDialogPage[] getSubPages()
{
return new IDialogPage[] {
new DriverPropertiesDialogPage(this)
};
}
}