/*******************************************************************************
* 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.preferences;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
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.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.EditableTableControl;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.dialogs.WGADeploymentDialog;
import de.innovationgate.eclipse.wgadesigner.dialogs.WGAEditDeploymentDialog;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeployment;
import de.innovationgate.eclipse.wgadesigner.models.WGADeploymentsModel;
import de.innovationgate.utils.TemporaryFile;
public class WGADeployments extends PreferencePage implements IWorkbenchPreferencePage {
private static final String BUTTON_UPDATE = "BUTTON_UPDATE";
private static final String BUTTON_EDIT = "BUTTON_EDIT";
private Composite _container;
private WGADeploymentsModel _model;
private EditableTableControl<WGADeployment> _tableControl;
private WGADeploymentDialog _addDialog;
private WGADeployment _selectedDeployment;
private WGADeploymentDialog _updateDialog;
private WGAEditDeploymentDialog _editDialog;
public WGADeployments() {
setDescription("Each WGA Deployment has a symbolic name which can be used in a WGA Runtime to specify the WGA Distribution to use.");
noDefaultAndApplyButton();
}
@Override
protected Control createContents(Composite parent) {
_container = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginWidth = 0;
layout.marginHeight = 0;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
_container.setLayout(layout);
_tableControl = new EditableTableControl<WGADeployment>(_container, SWT.NONE) {
@Override
protected void createButtonArea(Composite area) {
super.createButtonArea(area);
Button btnUpdate = new Button(area, SWT.PUSH);
btnUpdate.setText("update deployment");
btnUpdate.setLayoutData(GridDataFactory.copyData(_btnLayout));
btnUpdate.setEnabled(false);
_buttons.put(BUTTON_UPDATE, btnUpdate);
Button btnEdit = new Button(area, SWT.PUSH);
btnEdit.setText("edit");
btnEdit.setLayoutData(GridDataFactory.copyData(_btnLayout));
_buttons.put(BUTTON_EDIT, btnEdit);
}
};
_tableControl.setLayoutData(new GridData(GridData.FILL_BOTH));
// create columns
String[] columnNames = new String[] { "Symbolic name", "WGA Version" };
// create model
_model = new WGADeploymentsModel(WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments());
_tableControl.init(columnNames, _model);
_tableControl.getButton(EditableTableControl.BUTTON_ADD).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAdd();
}
});
_tableControl.getButton(EditableTableControl.BUTTON_REMOVE).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemove();
}
});
_tableControl.getButton(BUTTON_UPDATE).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleUpdate();
}
});
_tableControl.getButton(BUTTON_EDIT).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleEdit();
}
});
_tableControl.getTable().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
if (_tableControl.getTable().getSelectionCount() > 0) {
WGADeployment deployment = (WGADeployment) _tableControl.getTable().getSelection()[0].getData();
// if (deployment.getName().equals(WGADesignerPlugin.getDefault().getWGADeploymentManager().getDefaultDeploymentName()) ||
// WGADesignerPlugin.getDefault().getWGADeploymentManager().isProjectDeployment(deployment)) {
// _tableControl.getButton(EditableTableControl.BUTTON_REMOVE).setEnabled(false);
// } else {
// _tableControl.getButton(EditableTableControl.BUTTON_REMOVE).setEnabled(true);
// }
if (WGADesignerPlugin.getDefault().getWGADeploymentManager().isProjectDeployment(deployment)) {
_tableControl.getButton(BUTTON_UPDATE).setEnabled(false);
} else {
_tableControl.getButton(BUTTON_UPDATE).setEnabled(true);
}
} else {
_tableControl.getButton(BUTTON_UPDATE).setEnabled(false);
}
}
});
return _container;
}
private void handleAdd() {
_addDialog = new WGADeploymentDialog(getShell());
_addDialog.setTitle("Add wga deployment");
_addDialog.open();
if (_addDialog.getReturnCode() == Dialog.OK) {
// check if deployment already exists
if (WGADesignerPlugin.getDefault().getWGADeploymentManager().hasDeployment(_addDialog.getSymbolicName())) {
WorkbenchUtils.showErrorDialog(getShell(), "WGA deployment already exists.", "A WGA deployment with name '" + _addDialog.getSymbolicName() + "' already exists.");
} else {
IRunnableWithProgress runnable = null;
if (_addDialog.isDownloadRequested()) {
runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
TemporaryFile wgaWar = null;
try {
monitor.beginTask("Deploying wga", 100);
SubProgressMonitor downloadMonitor = new SubProgressMonitor(monitor, 90);
boolean isCustomDownloadLocation = false;
if (_addDialog.getLocation() != null && !_addDialog.getLocation().trim().equalsIgnoreCase("")) {
wgaWar = WGADesignerPlugin.getDefault().downloadWGA(downloadMonitor, _addDialog.getLocation());
isCustomDownloadLocation = true;
} else {
wgaWar = WGADesignerPlugin.getDefault().downloadCurrentWGARelease(downloadMonitor);
}
if (!monitor.isCanceled()) {
SubProgressMonitor deployMonitor = new SubProgressMonitor(monitor, 10);
WGADeployment deployment = WGADesignerPlugin.getDefault().getWGADeploymentManager().createDeployment(_addDialog.getSymbolicName(), wgaWar.getFile(), deployMonitor);
if (deployment != null && isCustomDownloadLocation) {
deployment.setUpdateURL(_addDialog.getLocation());
}
}
} catch (Exception e) {
throw new InterruptedException(e.getMessage());
} finally {
if (wgaWar != null) {
wgaWar.delete();
}
monitor.done();
}
}
};
} else {
final File wgaWar = new File(_addDialog.getLocation());
runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
WGADesignerPlugin.getDefault().getWGADeploymentManager().createDeployment(_addDialog.getSymbolicName(), wgaWar, monitor);
} catch (Exception e) {
throw new InterruptedException(e.getMessage());
}
}
};
}
try {
new ProgressMonitorDialog(getShell()).run(true, true, runnable);
_model.refresh(WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments());
} catch (Exception e) {
WGADesignerPlugin.getDefault().logError("Unable to create wga deployment.", e);
}
}
}
}
private void handleRemove() {
if (_tableControl.getTable().getSelectionCount() > 0) {
_selectedDeployment = (WGADeployment) _tableControl.getTable().getSelection()[0].getData();
boolean remove = MessageDialog.openConfirm(getShell(), "Remove wga deployment?", "Are you sure you want permanently remove the wga deployment '" + _selectedDeployment.toString() + "'? Depending WGA runtimes will stop working.");
if (remove) {
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
WGADesignerPlugin.getDefault().getWGADeploymentManager().removeDeployment(_selectedDeployment, monitor);
} catch (Exception e) {
throw new InterruptedException(e.getMessage());
}
}
};
try {
new ProgressMonitorDialog(getShell()).run(true, true, runnable);
} catch (Exception e) {
WGADesignerPlugin.getDefault().logError("Unable to remove wga deployment.", e);
}
_model.refresh(WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments());
}
}
}
private void handleUpdate() {
if (_tableControl.getTable().getSelectionCount() > 0) {
_selectedDeployment = (WGADeployment) _tableControl.getTable().getSelection()[0].getData();
_updateDialog = new WGADeploymentDialog(getShell(), _selectedDeployment);
_updateDialog.setTitle("Update wga deployment");
_updateDialog.open();
if (_updateDialog.getReturnCode() == Dialog.OK) {
IRunnableWithProgress runnable = null;
if (_updateDialog.isDownloadRequested()) {
runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
TemporaryFile wgaWar = null;
try {
monitor.beginTask("Deploying wga", 100);
SubProgressMonitor downloadMonitor = new SubProgressMonitor(monitor, 90);
if (_updateDialog.getLocation() != null && !_updateDialog.getLocation().trim().equalsIgnoreCase("")) {
wgaWar = WGADesignerPlugin.getDefault().downloadWGA(downloadMonitor, _updateDialog.getLocation());
} else {
wgaWar = WGADesignerPlugin.getDefault().downloadCurrentWGARelease(downloadMonitor);
}
if (!monitor.isCanceled()) {
SubProgressMonitor deployMonitor = new SubProgressMonitor(monitor, 10);
_selectedDeployment.updateWGA(wgaWar.getFile(), deployMonitor);
}
} catch (Exception e) {
throw new InterruptedException(e.getMessage());
} finally {
if (wgaWar != null) {
wgaWar.delete();
}
monitor.done();
}
}
};
} else {
final File wgaWar = new File(_updateDialog.getLocation());
runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
_selectedDeployment.updateWGA(wgaWar, monitor);
} catch (Exception e) {
throw new InterruptedException(e.getMessage());
}
}
};
}
try {
new ProgressMonitorDialog(getShell()).run(true, true, runnable);
} catch (Exception e) {
WGADesignerPlugin.getDefault().logError("Unable to update wga deployment.", e);
}
}
_model.refresh(WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments());
}
}
private void handleEdit() {
if (_tableControl.getTable().getSelectionCount() > 0) {
_selectedDeployment = (WGADeployment) _tableControl.getTable().getSelection()[0].getData();
_editDialog = new WGAEditDeploymentDialog(getShell(), _selectedDeployment);
_editDialog.setTitle("Edit wga deployment");
_editDialog.open();
}
}
@Override
public boolean performOk() {
return true;
}
public void init(IWorkbench workbench) {
// TODO Auto-generated method stub
}
}