/**
* Copyright 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/package org.safehaus.penrose.studio.mapping.editor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.jface.wizard.WizardDialog;
import org.safehaus.penrose.studio.dialog.ErrorDialog;
import org.safehaus.penrose.studio.mapping.wizard.MappingPostScriptWizard;
import org.safehaus.penrose.studio.mapping.wizard.MappingPreScriptWizard;
/**
* @author Endi S. Dewata
*/
public class MappingScriptsPage extends MappingEditorPage {
Text preScriptText;
Text postScriptText;
public MappingScriptsPage(MappingEditor editor) {
super(editor, "SCRIPTS", "Scripts");
}
public void createFormContent(IManagedForm managedForm) {
super.createFormContent(managedForm);
ScrolledForm form = managedForm.getForm();
Composite body = form.getBody();
body.setLayout(new GridLayout());
Section preScriptSection = toolkit.createSection(body, Section.TITLE_BAR | Section.EXPANDED);
preScriptSection.setText("Pre-Script");
preScriptSection.setLayoutData(new GridData(GridData.FILL_BOTH));
Control preScriptControl = createPreScriptSection(preScriptSection);
preScriptSection.setClient(preScriptControl);
Section postScriptSection = toolkit.createSection(body, Section.TITLE_BAR | Section.EXPANDED);
postScriptSection.setText("Post-Script");
postScriptSection.setLayoutData(new GridData(GridData.FILL_BOTH));
Control postScriptControl = createPostScriptSection(postScriptSection);
postScriptSection.setClient(postScriptControl);
}
public Composite createPreScriptSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
composite.setLayout(new GridLayout(2, false));
Composite leftControl = createPreScriptLeftSection(composite);
leftControl.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite rightControl = createPreScriptRightSection(composite);
GridData gd = new GridData(GridData.FILL_VERTICAL);
gd.widthHint = 100;
rightControl.setLayoutData(gd);
return composite;
}
public Composite createPreScriptLeftSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
composite.setLayout(new GridLayout());
preScriptText = toolkit.createText(composite, "", SWT.READ_ONLY | SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
preScriptText.setLayoutData(new GridData(GridData.FILL_BOTH));
return composite;
}
public Composite createPreScriptRightSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout(layout);
Button editButton = new Button(composite, SWT.PUSH);
editButton.setText("Edit");
editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
editButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
MappingPreScriptWizard wizard = new MappingPreScriptWizard();
wizard.setMappingConfig(mappingConfig);
WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
dialog.setPageSize(600, 300);
int rc = dialog.open();
if (rc == WizardDialog.CANCEL) return;
editor.store();
refresh();
} catch (Exception e) {
log.error(e.getMessage(), e);
ErrorDialog.open(e);
}
}
});
return composite;
}
public Composite createPostScriptSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
composite.setLayout(new GridLayout(2, false));
Composite leftControl = createPostScriptLeftSection(composite);
leftControl.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite rightControl = createPostScriptRightSection(composite);
GridData gd = new GridData(GridData.FILL_VERTICAL);
gd.widthHint = 100;
rightControl.setLayoutData(gd);
return composite;
}
public Composite createPostScriptLeftSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
composite.setLayout(new GridLayout());
postScriptText = toolkit.createText(composite, "", SWT.READ_ONLY | SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
postScriptText.setLayoutData(new GridData(GridData.FILL_BOTH));
return composite;
}
public Composite createPostScriptRightSection(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout(layout);
Button editButton = new Button(composite, SWT.PUSH);
editButton.setText("Edit");
editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
editButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
MappingPostScriptWizard wizard = new MappingPostScriptWizard();
wizard.setMappingConfig(mappingConfig);
WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
dialog.setPageSize(600, 300);
int rc = dialog.open();
if (rc == WizardDialog.CANCEL) return;
editor.store();
refresh();
} catch (Exception e) {
log.error(e.getMessage(), e);
ErrorDialog.open(e);
}
}
});
return composite;
}
public void refresh() {
String preScript = mappingConfig.getPreScript();
preScriptText.setText(preScript == null ? "" : preScript);
String postScript = mappingConfig.getPostScript();
postScriptText.setText(postScript == null ? "" : postScript);
}
public void checkDirty() {
editor.checkDirty();
}
}