package tool.editors.method;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import tool.ToolPlugin;
import tool.editors.ColorManager;
import tool.editors.ToolEditor;
import tool.editors.ToolMethodContentOutlinePage;
import tool.editors.ToolSourceViewerConfiguration;
import tool.editors.contentassist.ToolTemplateAssistProcessor;
import tool.editors.text.ToolDocumentProvider;
import tool.model.ToolClass;
import tool.model.ToolInterface;
import tool.model.ToolMethod;
import tool.model.ToolParameter;
import tool.model.ToolType;
import tool.search.SelectType;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.core.databinding.UpdateValueStrategy;
import tool.model.validation.ToolNameAllowBlankValidator;
import tool.model.validation.ToolNameValidator;
/**
* This class sub classes ToolEditor
* so we can set the isEditable flag
*
* This is a kludge until we work out how to do it properly
*
* @author Peter
*
*/
public class MethodEditor extends ToolEditor implements IResourceChangeListener, PropertyChangeListener{
private DataBindingContext m_bindingContext;
public static final String EDITOR_ID = "tool.editors.method.MethodEditor";
public static final String EDITOR_CONTEXT = EDITOR_ID + ".context";
public static final String RULER_CONTEXT = EDITOR_CONTEXT + ".ruler";
private ColorManager colorManager;
private ToolMethod method;
private Text nameText;
private Text returnTypeText;
private Text returnEventText;
private Text exceptionEventText;
private TableViewerColumn tableViewerColumn_3;
private Button publicCheckButton;
private Button btnCheckButton;
private Table table;
private TableViewerColumn defaultColumn;
private TableViewerColumn parsingColumn;
private TableViewerColumn copyColumn;
private TableViewerColumn nameColumn;
private TableViewerColumn typeColumn;
private TableViewer tableViewer;
private boolean showSignature = false;
private MethodContentOutlinePage outlinePage;
public MethodEditor(){
super();
//setSourceViewerConfiguration(new MethodSourceViewerConfiguration(colorManager));
setDocumentProvider(new ToolMethodDocumentProvider());
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
this.showSignature = ToolPlugin.isSignatureShown();
}
public void dispose() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
if (this.method != null)
this.method.removePropertyChangeListener(this);
super.dispose();
}
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
super.init(site, input);
MethodEditorInput mInput = (MethodEditorInput)input;
setMethod(mInput.getMethod());
}
public void setVisibleRegion(IRegion methodSourceRegion) {
if (methodSourceRegion != null && getSourceViewer() != null){
getSourceViewer().setVisibleRegion(methodSourceRegion.getOffset(),
methodSourceRegion.getLength());
}
}
// public void doSave(IProgressMonitor monitor) {
// String declaration = this.method.toSource();
// String implementation = this.method.toImplamentationSource();
// //TODO write this to cdf and cex
// this.method.setDirty(false);
// }
public void gotoMarker(IMarker marker) {
IDE.gotoMarker(this, marker);
}
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void createPartControl(Composite parent) {
if (showSignature){
createHeader(parent);
}
super.createPartControl(parent);
if (showSignature){
parent.getChildren()[1].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
}
if (this.method != null){
IRegion region = this.method.getMethodSourceRegion(!showSignature);
setVisibleRegion(region);
if (showSignature)
m_bindingContext = initDataBindings();
}
}
protected void createHeader(Composite parent){
parent.setLayout(new GridLayout(1, true));
TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
TabItem propertiesTab = new TabItem(tabFolder, SWT.NONE);
propertiesTab.setText("Properties");
Composite propertiesPane = new Composite(tabFolder, SWT.NONE);
propertiesTab.setControl(propertiesPane);
GridLayout gl_propertiesPane = new GridLayout(3, false);
gl_propertiesPane.horizontalSpacing = 8;
propertiesPane.setLayout(gl_propertiesPane);
Label lblMethodName = new Label(propertiesPane, SWT.NONE);
lblMethodName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblMethodName.setText("Method Name:");
nameText = new Text(propertiesPane, SWT.BORDER);
nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
publicCheckButton = new Button(propertiesPane, SWT.CHECK);
publicCheckButton.setText("Public");
Label lblReturnType = new Label(propertiesPane, SWT.NONE);
lblReturnType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblReturnType.setText("Return Type:");
returnTypeText = new Text(propertiesPane, SWT.BORDER);
returnTypeText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
Composite composite = new Composite(propertiesPane, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
GridLayout gl_composite = new GridLayout(2, false);
gl_composite.verticalSpacing = 0;
gl_composite.marginWidth = 0;
gl_composite.marginHeight = 0;
composite.setLayout(gl_composite);
Button lookupTypeButton = new Button(composite, SWT.NONE);
lookupTypeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String returnType = method.getReturnType();
ToolType owningClass = (ToolType)method.getParent();
if (returnType != null && !returnType.isEmpty()){
ToolType cls = ToolClass.fetch(owningClass.getProject(), returnType);
if (cls == null){
ToolInterface inter = ToolInterface.fetch(owningClass.getProject(), returnType);
SelectType.select(owningClass, inter);
} else {
SelectType.select(owningClass, cls);
}
} else {
SelectType.select(owningClass, null);
}
}
});
lookupTypeButton.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("Tool", "icons/look_up.gif"));
btnCheckButton = new Button(composite, SWT.CHECK);
btnCheckButton.setText("Copy");
Label lblReturnEvent = new Label(propertiesPane, SWT.NONE);
lblReturnEvent.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblReturnEvent.setText("Return Event:");
returnEventText = new Text(propertiesPane, SWT.BORDER);
returnEventText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(propertiesPane, SWT.NONE);
Label lblExceptionEvent = new Label(propertiesPane, SWT.NONE);
lblExceptionEvent.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblExceptionEvent.setText("Exception Event:");
exceptionEventText = new Text(propertiesPane, SWT.BORDER);
exceptionEventText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new Label(propertiesPane, SWT.NONE);
TabItem parametersTab = new TabItem(tabFolder, SWT.NONE);
parametersTab.setText("Parameters");
tableViewer = new TableViewer(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setLinesVisible(true);
parametersTab.setControl(table);
table.setHeaderVisible(true);
nameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn = nameColumn.getColumn();
tableColumn.setWidth(197);
tableColumn.setText("Name");
typeColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_1 = typeColumn.getColumn();
tableColumn_1.setWidth(198);
tableColumn_1.setText("Type");
copyColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_2 = copyColumn.getColumn();
tableColumn_2.setWidth(42);
tableColumn_2.setText("Copy");
parsingColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_3 = parsingColumn.getColumn();
tableColumn_3.setWidth(66);
tableColumn_3.setText("Parsing");
defaultColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn_4 = defaultColumn.getColumn();
tableColumn_4.setWidth(100);
tableColumn_4.setText("Default");
}
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equalsIgnoreCase("dirty")){
firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
}
}
@Override
public void resourceChanged(final IResourceChangeEvent event){
if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
Display.getDefault().asyncExec(new Runnable(){
public void run(){
IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
for (int i = 0; i<pages.length; i++){
if(((FileEditorInput)getEditorInput()).getFile().getProject().equals(event.getResource())){
IEditorPart editorPart = pages[i].findEditor(getEditorInput());
pages[i].closeEditor(editorPart,true);
}
}
}
});
}
}
public ToolMethod getMethod() {
return method;
}
public void setMethod(ToolMethod method) {
this.method = method;
if (this.method != null){
setPartName(this.method.getName());
//IRegion region = this.method.getMethodSourceRegion(true);
//setVisibleRegion(region);
this.method.setDirty(false);
this.method.addPropertyChangeListener("dirty", this);
//m_bindingContext = initDataBindings();
//TODO scope templates
//ToolTemplateAssistProcessor assistProcessor = (ToolTemplateAssistProcessor) getSourceViewerConfiguration().getContentAssistant(null);
//assistProcessor.setMethod(this.method);
}
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue nameTextObserveTextObserveWidget = SWTObservables.observeText(nameText, SWT.Modify);
IObservableValue methodNameObserveValue = BeansObservables.observeValue(method, "name");
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setAfterConvertValidator(new ToolNameValidator(this.nameText));
bindingContext.bindValue(nameTextObserveTextObserveWidget, methodNameObserveValue, strategy, null);
//
IObservableValue returnTypeTextObserveTextObserveWidget = SWTObservables.observeText(returnTypeText, SWT.Modify);
IObservableValue methodReturnTypeObserveValue = BeansObservables.observeValue(method, "returnType");
bindingContext.bindValue(returnTypeTextObserveTextObserveWidget, methodReturnTypeObserveValue, null, null);
//
IObservableValue publicCheckButtonObserveSelectionObserveWidget = SWTObservables.observeSelection(publicCheckButton);
IObservableValue methodPublicObserveValue = BeansObservables.observeValue(method, "public");
bindingContext.bindValue(publicCheckButtonObserveSelectionObserveWidget, methodPublicObserveValue, null, null);
//
IObservableValue btnCheckButtonObserveSelectionObserveWidget = SWTObservables.observeSelection(btnCheckButton);
IObservableValue methodCopyReturnObserveValue = BeansObservables.observeValue(method, "copyReturn");
bindingContext.bindValue(btnCheckButtonObserveSelectionObserveWidget, methodCopyReturnObserveValue, null, null);
//
IObservableValue returnEventTextObserveTextObserveWidget = SWTObservables.observeText(returnEventText, SWT.Modify);
IObservableValue methodReturnEventObserveValue = BeansObservables.observeValue(method, "returnEvent");
UpdateValueStrategy strategy_2 = new UpdateValueStrategy();
strategy_2.setAfterConvertValidator(new ToolNameAllowBlankValidator(returnEventText));
bindingContext.bindValue(returnEventTextObserveTextObserveWidget, methodReturnEventObserveValue, strategy_2, null);
//
IObservableValue exceptionEventTextObserveTextObserveWidget = SWTObservables.observeText(exceptionEventText, SWT.Modify);
IObservableValue methodExceptionEventObserveValue = BeansObservables.observeValue(method, "exceptionEvent");
UpdateValueStrategy strategy_3 = new UpdateValueStrategy();
strategy_3.setAfterConvertValidator(new ToolNameAllowBlankValidator(exceptionEventText));
bindingContext.bindValue(exceptionEventTextObserveTextObserveWidget, methodExceptionEventObserveValue, strategy_3, null);
//
//
ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
tableViewer.setContentProvider(listContentProvider);
//
IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), ToolParameter.class, new String[]{"name", "type", "copy", "parsingType", "defaultValue"});
tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
//
IObservableList methodEditorgetMethodParametersObserveList = BeansObservables.observeList(Realm.getDefault(), method, "parameters");
tableViewer.setInput(methodEditorgetMethodParametersObserveList);
//
CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
IValueProperty cellEditorProperty = BeanProperties.value("value");
IBeanValueProperty valueProperty = BeanProperties.value("name");
nameColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
//
CellEditor cellEditor_1 = new TextCellEditor(tableViewer.getTable());
IValueProperty cellEditorProperty_1 = BeanProperties.value("value");
IBeanValueProperty valueProperty_1 = BeanProperties.value("type");
typeColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
//
CellEditor cellEditor_2 = new CheckboxCellEditor(tableViewer.getTable());
IValueProperty cellEditorProperty_2 = BeanProperties.value("value");
IBeanValueProperty valueProperty_2 = BeanProperties.value("copy");
copyColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_2, cellEditorProperty_2, valueProperty_2));
//
CellEditor parsingCombo = new ComboBoxCellEditor(tableViewer.getTable(), ToolParameter.PT_VALUES);
IValueProperty cellEditorProperty_3 = BeanProperties.value("value");
IBeanValueProperty valueProperty_3 = BeanProperties.value("parsingType");
parsingColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, parsingCombo, cellEditorProperty_3, valueProperty_3));
//
CellEditor cellEditor_4 = new TextCellEditor(tableViewer.getTable());
IValueProperty cellEditorProperty_4 = BeanProperties.value("value");
IBeanValueProperty valueProperty_4 = BeanProperties.value("defaultValue");
defaultColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_4, cellEditorProperty_4, valueProperty_4));
//
return bindingContext;
}
@Override
public Object getAdapter(Class required) {
if (IContentOutlinePage.class.equals(required)) {
if (outlinePage == null) {
outlinePage = new MethodContentOutlinePage();
if (getEditorInput() != null)
outlinePage.setInput(getEditorInput());
}
return outlinePage;
} else {
Object result = super.getAdapter(required);
return result;
}
}
protected void initializeEditor() {
super.initializeEditor();
setEditorContextMenuId(MethodEditor.EDITOR_CONTEXT);
setRulerContextMenuId(MethodEditor.RULER_CONTEXT);
}
}