/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.wizards.export.webapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.eclipse.compare.internal.ListContentProvider;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.markers.internal.TableViewLabelProvider;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.wga.config.ContentStore;
import de.innovationgate.wga.config.DatabaseServer;
import de.innovationgate.wga.config.WGAConfiguration;
import de.innovationgate.wga.model.BeanListTableModel;
import de.innovationgate.wgaservices.types.DatabaseServerInfo;
public class ChooseDBServerPage extends ValidatedWizardPage {
private WGAConfiguration _config;
private TableViewer _tblDatabaseServers;
private List<DatabaseServerInfo> _dbServers;
private Text _txtDatabaseName;
private Button _chkCopyLocalContent;
private Button _chkIncludeACL;
private static class DBServerLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
DatabaseServerInfo server = (DatabaseServerInfo)element;
if (columnIndex == 0) {
return server.getTitle();
} else if (columnIndex == 1) {
return server.getDescription();
}
return null;
}
}
private static class DBServerContentProvider implements IStructuredContentProvider {
private List<DatabaseServerInfo> _dbServers;
public Object[] getElements(Object inputElement) {
return _dbServers.toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
_dbServers = (List<DatabaseServerInfo>) newInput;
}
}
public ChooseDBServerPage() {
super("ChooseDatabaseServerPage");
setTitle("Select a database server for export");
}
@Override
public void computeResponse() {
ExportWGAApplication wizard = (ExportWGAApplication)getWizard();
ISelection selection = _tblDatabaseServers.getSelection();
if (selection instanceof IStructuredSelection) {
wizard.setRemoteDatabaseServer((DatabaseServerInfo)((IStructuredSelection)selection).getFirstElement());
}
wizard.setRemoteDatabaseName(_txtDatabaseName.getText());
wizard.setCopyContent(_chkCopyLocalContent.getSelection());
wizard.setIncludeACL(_chkIncludeACL.getSelection());
}
@Override
public List<IStatus> validate() {
List<IStatus> errors = new ArrayList<IStatus>();
if (_tblDatabaseServers.getTable().getSelectionCount() <= 0) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "Please select a database server for the export."));
}
if (_txtDatabaseName.getText() == null || _txtDatabaseName.getText().trim().equals("")) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "Please define a name for the new database which will be created."));
}
return errors;
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
container.setLayout(layout);
Label label = new Label(container, SWT.NONE);
label.setText("Available database servers on remote location:");
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
label.setLayoutData(gd);
_tblDatabaseServers = new TableViewer(container);
_tblDatabaseServers.setLabelProvider(new DBServerLabelProvider());
_tblDatabaseServers.setContentProvider(new DBServerContentProvider());
_tblDatabaseServers.setInput(_config);
_tblDatabaseServers.addSelectionChangedListener(this);
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
_tblDatabaseServers.getTable().setLayoutData(gd);
label = new Label(container, SWT.NONE);
label.setText("Database name:");
_txtDatabaseName = new Text(container, SWT.BORDER);
_txtDatabaseName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
_txtDatabaseName.addModifyListener(this);
label = new Label(container, SWT.NONE);
label.setText("Options:");
GridData chkGridData = new GridData(GridData.FILL_HORIZONTAL);
_chkCopyLocalContent = new Button(container, SWT.CHECK);
_chkCopyLocalContent.setText("copy local content");
_chkCopyLocalContent.setLayoutData(chkGridData);
_chkCopyLocalContent.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
if (_chkCopyLocalContent.getSelection()) {
_chkIncludeACL.setEnabled(true);
} else {
_chkIncludeACL.setEnabled(false);
}
performValidation();
}
public void widgetDefaultSelected(SelectionEvent e) {
if (_chkCopyLocalContent.getSelection()) {
_chkIncludeACL.setEnabled(true);
} else {
_chkIncludeACL.setEnabled(false);
}
performValidation();
}
});
Label filler = new Label(container, SWT.NONE);
_chkIncludeACL = new Button(container, SWT.CHECK);
_chkIncludeACL.setText("include ACL on content copy");
_chkIncludeACL.setLayoutData(GridDataFactory.copyData(chkGridData));
_chkIncludeACL.addSelectionListener(this);
_chkCopyLocalContent.setSelection(true);
_chkIncludeACL.setSelection(true);
setControl(container);
performValidation();
}
public void init(List<DatabaseServerInfo> dbServers) {
_dbServers = dbServers;
_tblDatabaseServers.setInput(_dbServers);
ExportWGAApplication wizard = (ExportWGAApplication)getWizard();
if (_txtDatabaseName != null) {
try {
WGAConfiguration config = wizard.getRuntime().retrieveWGAConfig(false);
ContentStore cs = config.getContentStore(wizard.getWebApplication());
String path = cs.getDatabaseOptions().get(ExportWGAApplication.CS_PATH_OPTION_NAME);
if (path != null) {
_txtDatabaseName.setText(path);
}
}
catch (IncompatibleWGAConfigVersion e) {
WGADesignerPlugin.getDefault().logError(e.getMessage(), e);
}
catch (IOException e) {
}
}
}
}