/*******************************************************************************
* 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.pages;
import java.io.File;
import java.io.IOException;
import net.java.dev.genesis.annotation.ViewHandler;
import net.java.dev.genesis.ui.swt.SWTBinder;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swt.SWT;
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.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.forms.HyperlinkGroup;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.GenesisBoundFormPage;
import de.innovationgate.eclipse.utils.ui.WidgetFactory;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeploymentsListener;
import de.innovationgate.eclipse.wgadesigner.models.WGARuntimeConfigurationModel;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatServerStateListener;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
import de.innovationgate.wga.model.Model;
@ViewHandler
public class WGARuntimeConfigurationPage extends GenesisBoundFormPage implements TomcatServerStateListener, WGADeploymentsListener {
public static final String ID = "de.innovationgate.eclipse.wgadesigner.wgaruntimeeditor.pages.WGARuntimeConfigurationPage";
public static final String PAGE_TITLE = "Configuration";
private WGARuntimeConfigurationModel _model;
private Button _btnConfigureWGAInstallations;
private List _lstTomcatLibraries;
private Button _btnAddTomcatLibrary;
private Button _btnRemoveTomcatLibrary;
private Combo _comboWGADistribution;
private Button _btnStartRuntime;
private Button _btnStopRuntime;
private Label _lblState;
private Hyperlink _linkOpenAdminPage;
private Hyperlink _linkOpenContentManager;
private Text _txtMinHeap;
private Text _txtMaxHeap;
private Text _txtMinPerm;
private Text _txtMaxPerm;
private Text _txtJvmOptions;
private Combo _comboJVM;
public WGARuntimeConfigurationPage(FormEditor editor) {
super(editor, ID, PAGE_TITLE);
WGADesignerPlugin.getDefault().getWGADeploymentManager().addListener(this);
}
public void setModel(Model model) {
_model = (WGARuntimeConfigurationModel) model;
}
public Model getModel() {
return _model;
}
@Override
protected void createFormContent(IManagedForm managedForm) {
ScrolledForm form = managedForm.getForm();
FormToolkit toolkit = managedForm.getToolkit();
toolkit.decorateFormHeading(form.getForm());
WidgetFactory factory = new WidgetFactory(toolkit);
form.setText(PAGE_TITLE);
ColumnLayout layout = new ColumnLayout();
layout.maxNumColumns = 2;
form.getBody().setLayout(layout);
// general section
Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("General");
Composite sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
GridLayout sectionLayout = new GridLayout();
GridData fillHSpan = new GridData(GridData.FILL_HORIZONTAL);
fillHSpan.horizontalSpan = 2;
GridData comboLayout = new GridData(GridData.GRAB_HORIZONTAL);
comboLayout.minimumWidth = 150;
sectionLayout.numColumns = 3;
sectionClient.setLayout(sectionLayout);
_comboWGADistribution = factory.createCombo(sectionClient, "WGA Distribution:", "wgaDeployment");
_comboWGADistribution.setLayoutData(comboLayout);
// _btnConfigureWGAInstallations = new Button(sectionClient, SWT.NONE);
// _btnConfigureWGAInstallations.setText("Configure distributions ...");
// _btnConfigureWGAInstallations.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
// public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
// handleConfigureWGADistributions();
// }
// });
HyperlinkGroup group = new HyperlinkGroup(sectionClient.getDisplay());
ImageHyperlink linkConfigureDistributions = new ImageHyperlink(sectionClient, SWT.None);
linkConfigureDistributions.setText("Configure distributions ...");
linkConfigureDistributions.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
handleConfigureWGADistributions();
}
});
group.add(linkConfigureDistributions);
//factory.createCheckBox(sectionClient, "Output TML-Warnings on server console:", "enabled", "outputTMLWarningsOnConsole");
//factory.addFiller(sectionClient);
// factory.createCheckBox(sectionClient, "TML-Script debugger:", "enabled", "tmlScriptDebuggerEnabled");
// factory.addFiller(sectionClient);
//
// factory.createCheckBox(sectionClient, "Java debugging:", "enabled", "javaDebuggerEnabled");
// factory.addFiller(sectionClient);
group = new HyperlinkGroup(sectionClient.getDisplay());
ImageHyperlink linkConfigureGlobalRuntimePreferences = new ImageHyperlink(sectionClient, SWT.None);
linkConfigureGlobalRuntimePreferences.setText("Configure global runtime settings ...");
linkConfigureGlobalRuntimePreferences.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
openGeneralPreferencesPage();
}
});
group.add(linkConfigureGlobalRuntimePreferences);
// libraries section
section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.COMPACT);
section.setText("Tomcat libraries");
sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
sectionLayout = new GridLayout();
sectionLayout.numColumns = 2;
sectionClient.setLayout(sectionLayout);
GridData fillBoth = new GridData(GridData.FILL_BOTH);
fillBoth.minimumHeight = 100;
fillBoth.verticalSpan = 2;
_lstTomcatLibraries = new List(sectionClient, SWT.BORDER|SWT.MULTI);
_lstTomcatLibraries.setLayoutData(fillBoth);
_lstTomcatLibraries.setItems(_model.getTomcatLibraryNames());
_btnAddTomcatLibrary = toolkit.createButton(sectionClient, "add...", SWT.PUSH);
_btnAddTomcatLibrary.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddTomcatLibrary();
}
});
GridData btnLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, false, false);
_btnAddTomcatLibrary.setLayoutData(btnLayout);
_btnRemoveTomcatLibrary = toolkit.createButton(sectionClient, "remove...", SWT.PUSH);
_btnRemoveTomcatLibrary.setLayoutData(btnLayout);
_btnRemoveTomcatLibrary.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveTomcatLibrary();
}
});
// java section
section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Java");
sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
sectionLayout = new GridLayout();
sectionLayout.numColumns = 2;
sectionClient.setLayout(sectionLayout);
_comboJVM = factory.createCombo(sectionClient, "Execution Environment:", "executionEnvironment");
_comboJVM.setLayoutData(comboLayout);
// _txtMinHeap = factory.createText(sectionClient, "Minimum java heap size (in MB):", "minHeapSize");
// _txtMinHeap.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// //factory.addFiller(sectionClient);
// _txtMaxHeap = factory.createText(sectionClient, "Maximum java heap size (in MB):", "maxHeapSize");
// _txtMaxHeap.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// //factory.addFiller(sectionClient);
// _txtMinPerm = factory.createText(sectionClient, "Minimum java permgen size (in MB):", "minPermSize");
// _txtMinPerm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// //factory.addFiller(sectionClient);
// _txtMaxPerm = factory.createText(sectionClient, "Maximum java permgen size (in MB):", "maxPermSize");
// _txtMaxPerm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label lbl = toolkit.createLabel(sectionClient, "Additional jvm options:");
lbl.setLayoutData(new GridData(GridData.BEGINNING));
_txtJvmOptions = toolkit.createText(sectionClient, "", SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
_txtJvmOptions.setData("jvmOptions");
GridData jvmOptionLayout = new GridData(GridData.FILL_BOTH);
jvmOptionLayout.minimumHeight = WidgetFactory.computeFontHeight(_txtJvmOptions) * 5;
_txtJvmOptions.setLayoutData(jvmOptionLayout);
bind(form, SWTBinder.BINDING_STRATEGY_AS_YOU_TYPE);
}
private void handleConfigureWGADistributions() {
PreferenceDialog pref = PreferencesUtil.createPreferenceDialogOn(new Shell(), WGADesignerPlugin.PREFERENCES_PAGE_WGA_DEPLOYMENTS, null, null);
if (pref != null) {
pref.open();
}
}
private void openGeneralPreferencesPage() {
PreferenceDialog pref = PreferencesUtil.createPreferenceDialogOn(new Shell(), WGADesignerPlugin.PREFERENCES_PAGE_WGA_DEVELOPMENT_STUDIO, null, null);
if (pref != null) {
pref.open();
}
}
private void handleAddTomcatLibrary() {
Shell shell = getManagedForm().getForm().getShell();
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
dialog.setText("Select libraries to add");
dialog.setFilterExtensions(new String[] {"*.jar"});
String selectedFile = dialog.open();
if (selectedFile != null) {
String[] fileNames = dialog.getFileNames();
File dir = new File(dialog.getFilterPath());
for (int i = 0; i < fileNames.length; i++) {
String filename = fileNames[i];
boolean overwrite = true;
if (_model.hasTomcatLibrary(filename)) {
overwrite = MessageDialog.openConfirm(shell, "Overwrite?", "Library '" + filename + "' already exists, overwrite?");
}
if (overwrite) {
try {
_model.addTomcatLibrary(new File(dir, filename));
} catch (IOException e) {
WorkbenchUtils.showErrorDialog(shell, "Adding library failed", e);
}
}
}
_lstTomcatLibraries.setItems(_model.getTomcatLibraryNames());
}
}
private void handleRemoveTomcatLibrary() {
Shell shell = getManagedForm().getForm().getShell();
if (_lstTomcatLibraries.getSelectionCount() > 0) {
boolean delete = MessageDialog.openConfirm(shell, "Remove selected libraries?", "Are you sure you want to remove the selected libraries? This will also delete the selected files from filesystem.");
if (delete) {
String[] fileNames = _lstTomcatLibraries.getSelection();
for (int i=0; i < fileNames.length; i++) {
if (!_model.deleteLibrary(fileNames[i])) {
MessageDialog.openWarning(shell, "Deletion failed.", "Unable to delete library '" + fileNames[i] + "'.");
}
}
}
}
_lstTomcatLibraries.setItems(_model.getTomcatLibraryNames());
}
public void stateChanged(int state) {
if (state == TomcatServerStateListener.STATE_RUNNING && TomcatUtils.getInstance().isRunning(_model.getWgaRuntime())) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
_btnStartRuntime.setEnabled(false);
_btnStopRuntime.setEnabled(true);
_lblState.setText("running");
_lblState.pack();
_linkOpenContentManager.setEnabled(true);
_linkOpenAdminPage.setEnabled(true);
}
});
} else {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
_btnStartRuntime.setEnabled(true);
_btnStopRuntime.setEnabled(false);
_lblState.setText("stopped");
_lblState.pack();
_linkOpenContentManager.setEnabled(false);
_linkOpenAdminPage.setEnabled(false);
}
});
}
}
public void deploymentsChanged() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
_binder.invokeAction("populateWGADeployments");
_binder.refresh();
}
});
}
@Override
public void dispose() {
WGADesignerPlugin.getDefault().getWGADeploymentManager().removeListener(this);
TomcatUtils.getInstance().removeListener(this);
}
}