/*********************************************************************************************************************** | |
* Copyright (c) 2010 Attensity Europe 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 | |
**********************************************************************************************************************/ | |
package org.eclipse.smila.processing.designer.ui.properties; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.eclipse.bpel.ui.properties.BPELPropertySection; | |
import org.eclipse.bpel.ui.util.MultiObjectAdapter; | |
import org.eclipse.emf.common.notify.Notification; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.jface.viewers.*; | |
import org.eclipse.smila.processing.designer.model.processor.InvokeService; | |
import org.eclipse.smila.processing.designer.model.processor.SetAnnotations; | |
import org.eclipse.smila.processing.designer.model.record.*; | |
import org.eclipse.smila.processing.designer.ui.Activator; | |
import org.eclipse.smila.processing.designer.ui.SMILADesignerUIConstants; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.events.*; | |
import org.eclipse.swt.graphics.Image; | |
import org.eclipse.swt.graphics.Point; | |
import org.eclipse.swt.layout.GridData; | |
import org.eclipse.swt.layout.GridLayout; | |
import org.eclipse.swt.widgets.*; | |
/** | |
* Show the SetAnnotations options of a service. | |
* | |
* Copyright (c) 2011 Attensity Europe GmbH | |
* | |
* @author Lars Kunze | |
* | |
*/ | |
public class SetAnnotationsSection extends BPELPropertySection | |
{ | |
private static final String LABEL_COLUMN_NAME = "Name"; | |
private static final String LABEL_COLUMN_VALUE = "Value"; | |
private static final String LABLE_MENU_ITEM_ADD_ANNOTATION = "Add annotation"; | |
private static final String LABLE_MENU_ITEM_ADD_ANNOTATION_VALUE = "Add annotation value"; | |
private static final String LABLE_MENU_ITEM_REMOVE = "Remove"; | |
private static enum menuActions | |
{ | |
ADD_ANNOTATION(0), ADD_ANNOTATION_VALUE(1), DELETE_ITEM(2); | |
private int index; | |
private menuActions(int index) | |
{ | |
this.index = index; | |
} | |
} | |
private Composite compositeTree; | |
private TreeViewer treeViewer; | |
// ### Override/implement methods ### | |
/** | |
* @see BPELPropertySection#shouldUseExtraSpace() | |
*/ | |
@Override | |
public boolean shouldUseExtraSpace() | |
{ | |
return true; | |
} | |
/** | |
* @see BPELPropertySection#createAdapters() | |
*/ | |
@Override | |
protected MultiObjectAdapter[] createAdapters() | |
{ | |
return new MultiObjectAdapter[] {new MultiObjectAdapter() | |
{ | |
@Override | |
public void notify(Notification n) | |
{ | |
treeViewer.setInput(getInput()); | |
} | |
}}; | |
} | |
/** | |
* @see BPELPropertySection#basicSetInput(EObject) | |
*/ | |
@Override | |
protected void basicSetInput(EObject newInput) | |
{ | |
super.basicSetInput(newInput == null ? null : ((InvokeService) newInput).getSetAnnotations()); | |
if (getInput() != null) | |
{ | |
treeViewer.setInput(getInput()); | |
} | |
} | |
/** | |
* @see BPELPropertySection#createClient() | |
*/ | |
@Override | |
protected void createClient(Composite parent) | |
{ | |
createComponents(parent); | |
} | |
// ### Utility methods ### | |
/** | |
* Creates the components of this section. | |
* | |
* @param parent | |
*/ | |
private void createComponents(Composite parent) | |
{ | |
parent.setLayout(new GridLayout(1, false)); | |
// (1) Tree | |
compositeTree = new Composite(parent, SWT.NONE); | |
compositeTree.setLayout(new GridLayout(1, false)); | |
compositeTree.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
treeViewer = new TreeViewer(compositeTree, SWT.FULL_SELECTION); | |
treeViewer.setContentProvider(new TreeViewerContentProvider()); | |
treeViewer.getTree().addMouseListener(new TreeViewerMouseListener()); | |
treeViewer.getTree().setHeaderVisible(true); | |
treeViewer.getTree().setLinesVisible(true); | |
treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); | |
treeViewer.setAutoExpandLevel(3); | |
// (2) TreeColumns | |
TreeViewerColumn treeViewerColumnName = new TreeViewerColumn(treeViewer, SWT.NONE); | |
treeViewerColumnName.getColumn().setText(LABEL_COLUMN_NAME); | |
treeViewerColumnName.getColumn().setWidth(200); | |
treeViewerColumnName.setLabelProvider(new TreeViewerLabelProviderName()); | |
treeViewerColumnName.setEditingSupport(new TreeViewerEditingSupport(treeViewer, 0)); | |
TreeViewerColumn treeViewerColumnValue = new TreeViewerColumn(treeViewer, SWT.NONE); | |
treeViewerColumnValue.getColumn().setText(LABEL_COLUMN_VALUE); | |
treeViewerColumnValue.getColumn().setWidth(200); | |
treeViewerColumnValue.setLabelProvider(new TreeViewerLabelProviderValue()); | |
treeViewerColumnValue.setEditingSupport(new TreeViewerEditingSupport(treeViewer, 1)); | |
// (3) ColumnViewerEditor | |
TreeViewerEditor.create(treeViewer, new TreeViewerColumnViewerEditorActivationStrategy(treeViewer), | |
ColumnViewerEditor.KEYBOARD_ACTIVATION); | |
// (4) TreeMenu | |
Menu menuTree = new Menu(parent); | |
MenuItem menuItemAddAnnotation = new MenuItem(menuTree, SWT.PUSH); | |
menuItemAddAnnotation.setText(LABLE_MENU_ITEM_ADD_ANNOTATION); | |
menuItemAddAnnotation.setData(menuActions.ADD_ANNOTATION); | |
menuItemAddAnnotation.setImage(Activator.getDefault().getImageDescriptor(SMILADesignerUIConstants.ANNOTATION_ICON_16) | |
.createImage()); | |
menuItemAddAnnotation.addSelectionListener(new MenuItemSelectionListener()); | |
MenuItem menuItemAddAnnotationValue = new MenuItem(menuTree, SWT.PUSH); | |
menuItemAddAnnotationValue.setText(LABLE_MENU_ITEM_ADD_ANNOTATION_VALUE); | |
menuItemAddAnnotationValue.setData(menuActions.ADD_ANNOTATION_VALUE); | |
menuItemAddAnnotationValue.setImage(Activator.getDefault() | |
.getImageDescriptor(SMILADesignerUIConstants.ANNOTATION_VALUE_ICON_16).createImage()); | |
menuItemAddAnnotationValue.addSelectionListener(new MenuItemSelectionListener()); | |
MenuItem menuItemRemove = new MenuItem(menuTree, SWT.PUSH); | |
menuItemRemove.setText(LABLE_MENU_ITEM_REMOVE); | |
menuItemRemove.setData(menuActions.DELETE_ITEM); | |
menuItemRemove.addSelectionListener(new MenuItemSelectionListener()); | |
treeViewer.getTree().setMenu(menuTree); | |
} | |
// ### Inner classes ### | |
/** | |
* Implementation of the ITreeContentProvider interface for the treeViewer. | |
*/ | |
class TreeViewerContentProvider implements ITreeContentProvider | |
{ | |
/** | |
* @see ITreeContentProvider#dispose() | |
*/ | |
@Override | |
public void dispose() | |
{ | |
// Nothing todo | |
} | |
/** | |
* @see ITreeContentProvider#inputChanged(Viewer, Object, Object) | |
*/ | |
@Override | |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) | |
{ | |
// Nothing todo | |
} | |
/** | |
* @see ITreeContentProvider#getElements(Object) | |
*/ | |
@Override | |
public Object[] getElements(Object inputElement) | |
{ | |
return getChildren(inputElement); | |
} | |
/** | |
* @see ITreeContentProvider#getChildren(Object) | |
*/ | |
@Override | |
public Object[] getChildren(Object parentElement) | |
{ | |
if (parentElement != null) | |
{ | |
List<Object> children = new ArrayList<Object>(); | |
if (parentElement instanceof SetAnnotations) | |
{ | |
children.addAll(((SetAnnotations) parentElement).getAnnotations()); | |
} | |
else if (parentElement instanceof Annotation) | |
{ | |
children.addAll(((Annotation) parentElement).getAnnotations()); | |
children.addAll(((Annotation) parentElement).getAnnotationValues()); | |
} | |
return children.toArray(); | |
} | |
return null; | |
} | |
/** | |
* @see ITreeContentProvider#getParent() | |
*/ | |
@Override | |
public Object getParent(Object element) | |
{ | |
return null; | |
} | |
/** | |
* @see ITreeContentProvider#hasChildren() | |
*/ | |
@Override | |
public boolean hasChildren(Object element) | |
{ | |
return getChildren(element).length > 0; | |
} | |
} | |
/** | |
* ColumnLabelProvider for the column "name" of the treeViewer. | |
*/ | |
class TreeViewerLabelProviderName extends ColumnLabelProvider | |
{ | |
/** | |
* @see ColumnLabelProvider#getImage(Object) | |
*/ | |
@Override | |
public Image getImage(Object element) | |
{ | |
if (element instanceof Annotation) | |
{ | |
return Activator.getDefault().getImageDescriptor(SMILADesignerUIConstants.ANNOTATION_ICON_16).createImage(); | |
} | |
else if (element instanceof AnnotationValue) | |
{ | |
return Activator.getDefault().getImageDescriptor(SMILADesignerUIConstants.ANNOTATION_VALUE_ICON_16) | |
.createImage(); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
/** | |
* @see ColumnLabelProvider#getText(Object) | |
*/ | |
@Override | |
public String getText(Object element) | |
{ | |
if (element instanceof Annotation) | |
{ | |
return ((Annotation) element).getName(); | |
} | |
else if (element instanceof AnnotationValue) | |
{ | |
return ((AnnotationValue) element).getName(); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
} | |
/** | |
* ColumnLabelProvider for the column "value" of the treeViewer. | |
*/ | |
class TreeViewerLabelProviderValue extends ColumnLabelProvider | |
{ | |
/** | |
* @see ColumnLabelProvider#getImage(Object) | |
*/ | |
@Override | |
public Image getImage(Object element) | |
{ | |
return null; | |
} | |
/** | |
* @see ColumnLabelProvider#getText(Object) | |
*/ | |
@Override | |
public String getText(Object element) | |
{ | |
if (element instanceof AnnotationValue) | |
{ | |
return ((AnnotationValue) element).getValue(); | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
} | |
/** | |
* Extension of the class EditingSupport for use in the treeViewer (to make the columns editable). | |
*/ | |
public class TreeViewerEditingSupport extends EditingSupport | |
{ | |
private final CellEditor cellEditor; | |
private int column; | |
/** | |
* Creates a new instance of {@link TreeViewerEditingSupport} | |
* | |
* @param viewer The viewer for which columns the EditorSupport should be enabled | |
* @param column The column number on which this EditorSupport implementation should be applied | |
*/ | |
public TreeViewerEditingSupport(TreeViewer viewer, int column) | |
{ | |
super(viewer); | |
this.column = column; | |
this.cellEditor = new TextCellEditor(viewer.getTree()); | |
} | |
/** | |
* @see EditingSupport#canEdit(Object) | |
*/ | |
@Override | |
protected boolean canEdit(Object element) | |
{ | |
if (element instanceof AnnotationValue || column == 0) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* @see EditingSupport#getCellEditor(Object) | |
*/ | |
@Override | |
protected CellEditor getCellEditor(Object element) | |
{ | |
return cellEditor; | |
} | |
/** | |
* @see EditingSupport#getValue(Object) | |
*/ | |
@Override | |
protected Object getValue(Object element) | |
{ | |
if (element instanceof Annotation) | |
{ | |
Annotation annotation = (Annotation) element; | |
if (column == 0) | |
{ | |
return annotation.getName(); | |
} | |
} | |
else if (element instanceof AnnotationValue) | |
{ | |
AnnotationValue annotationValue = (AnnotationValue) element; | |
if (column == 0) | |
{ | |
return annotationValue.getName(); | |
} | |
else if (column == 1) | |
{ | |
return annotationValue.getValue(); | |
} | |
} | |
return null; | |
} | |
/** | |
* @see EditingSupport#setValue(Object, Object) | |
*/ | |
@Override | |
protected void setValue(Object element, Object value) | |
{ | |
if (value instanceof String && value != null) | |
{ | |
if (element instanceof Annotation && column == 0) | |
{ | |
((Annotation) element).setName((String) value); | |
} | |
else if (element instanceof AnnotationValue) | |
{ | |
if (column == 0) | |
{ | |
((AnnotationValue) element).setName((String) value); | |
} | |
else if (column == 1) | |
{ | |
((AnnotationValue) element).setValue((String) value); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* ColumnViewerEditorActivationStrategy for the treeViewer. | |
* Defines that the columns will be editable after a doubleclick on them. | |
*/ | |
class TreeViewerColumnViewerEditorActivationStrategy extends ColumnViewerEditorActivationStrategy | |
{ | |
/** | |
* Creates a new instance of {@link TreeViewerColumnViewerEditorActivationStrategy}. | |
* | |
* @param viewer The viewer on which this strategy should be used. | |
*/ | |
public TreeViewerColumnViewerEditorActivationStrategy(ColumnViewer viewer) | |
{ | |
super(viewer); | |
} | |
/** | |
* @see ColumnViewerEditorActivationStrategy#isEditorActivationEvent(ColumnViewerEditorActivationEvent) | |
*/ | |
@Override | |
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) | |
{ | |
MouseEvent mouseEvent = (MouseEvent) event.sourceEvent; | |
boolean singleSelect = ((IStructuredSelection) treeViewer.getSelection()).size() == 1; | |
boolean isLeftMouseDoubleSelect = | |
event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION && mouseEvent.button == 1; | |
return singleSelect | |
&& (isLeftMouseDoubleSelect || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC | |
|| event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)); | |
} | |
} | |
/** | |
* SelectionListener for the menu of the treeViewer. | |
*/ | |
class MenuItemSelectionListener implements SelectionListener | |
{ | |
@Override | |
public void widgetSelected(SelectionEvent e) | |
{ | |
TreeItem treeItem = treeViewer.getTree().getSelection().length > 0 ? treeViewer.getTree().getSelection()[0] : null; | |
MenuItem menuItem = (MenuItem) e.getSource(); | |
if (treeItem != null) | |
{ | |
if (menuItem.getData() == menuActions.ADD_ANNOTATION) | |
{ | |
Annotation annotation = RecordFactory.eINSTANCE.createAnnotation(); | |
if (treeItem.getData() instanceof Annotation) | |
{ | |
((Annotation) treeItem.getData()).getAnnotations().add(annotation); | |
} | |
else if (treeItem.getData() instanceof AnnotationValue) | |
{ | |
((Annotation) treeItem.getParentItem().getData()).getAnnotations().add(annotation); | |
} | |
} | |
else if (menuItem.getData() == menuActions.ADD_ANNOTATION_VALUE) | |
{ | |
AnnotationValue annotationValue = RecordFactory.eINSTANCE.createAnnotationValue(); | |
if (treeItem.getData() instanceof Annotation) | |
{ | |
((Annotation) treeItem.getData()).getAnnotationValues().add(annotationValue); | |
} | |
else if (treeItem.getData() instanceof AnnotationValue) | |
{ | |
((Annotation) treeItem.getParentItem().getData()).getAnnotationValues().add(annotationValue); | |
} | |
} | |
else if (menuItem.getData() == menuActions.DELETE_ITEM) | |
{ | |
TreeItem treeItemParent = treeItem.getParentItem(); | |
if (treeItemParent != null) | |
{ | |
if (treeItem.getData() instanceof Annotation) | |
{ | |
if (treeItemParent.getData() instanceof Annotation) | |
{ | |
((Annotation) treeItemParent.getData()).getAnnotations().remove(treeItem.getData()); | |
} | |
} | |
else if (treeItem.getData() instanceof AnnotationValue) | |
{ | |
((Annotation) treeItemParent.getData()).getAnnotationValues().remove(treeItem.getData()); | |
} | |
} | |
else | |
{ | |
((SetAnnotations) treeItem.getParent().getData()).getAnnotations().remove(treeItem.getData()); | |
} | |
} | |
} | |
else if (menuItem.getData() == menuActions.ADD_ANNOTATION) | |
{ | |
Annotation annotation = RecordFactory.eINSTANCE.createAnnotation(); | |
((SetAnnotations) treeViewer.getTree().getData()).getAnnotations().add(annotation); | |
} | |
} | |
@Override | |
public void widgetDefaultSelected(SelectionEvent e) | |
{ | |
// Nothing to do | |
} | |
} | |
/** | |
* MouseListener for the {@link TreeViewer}. | |
*/ | |
class TreeViewerMouseListener implements MouseListener | |
{ | |
/** | |
* @see MouseListener#mouseUp(MouseEvent) | |
*/ | |
@Override | |
public void mouseUp(MouseEvent e) | |
{ | |
TreeItem treeItem = treeViewer.getTree().getItem(new Point(e.x, e.y)); | |
if (treeItem != null) | |
{ | |
if (treeItem.getData() instanceof AnnotationValue) | |
{ | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION.index).setEnabled(false); | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION_VALUE.index).setEnabled(true); | |
treeViewer.getTree().getMenu().getItem(menuActions.DELETE_ITEM.index).setEnabled(true); | |
} | |
else if (treeItem.getData() instanceof Annotation) | |
{ | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION.index).setEnabled(true); | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION_VALUE.index).setEnabled(true); | |
treeViewer.getTree().getMenu().getItem(menuActions.DELETE_ITEM.index).setEnabled(true); | |
} | |
} | |
else | |
{ | |
if (treeViewer.getTree().getItems().length == 0) | |
{ | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION.index).setEnabled(true); | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION_VALUE.index).setEnabled(false); | |
treeViewer.getTree().getMenu().getItem(menuActions.DELETE_ITEM.index).setEnabled(false); | |
} | |
else | |
{ | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION.index).setEnabled(false); | |
treeViewer.getTree().getMenu().getItem(menuActions.ADD_ANNOTATION_VALUE.index).setEnabled(false); | |
treeViewer.getTree().getMenu().getItem(menuActions.DELETE_ITEM.index).setEnabled(false); | |
} | |
} | |
} | |
/** | |
* @see MouseListener#mouseDown(MouseEvent) | |
*/ | |
@Override | |
public void mouseDown(MouseEvent e) | |
{ | |
// Nothing to do | |
} | |
/** | |
* @see MouseListener#mouseDoubleClick(MouseEvent) | |
*/ | |
@Override | |
public void mouseDoubleClick(MouseEvent e) | |
{ | |
// Nothing to do | |
} | |
} | |
} |