blob: a37c60c3ba2f544565f37a63755be4458807bde9 [file] [log] [blame]
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2013, 2013 SAP AG.
* 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:
* SAP AG - initial API, implementation and documentation
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.fmc.blockdiagram.editor.property;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.ui.provider.PropertySource;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.graphiti.mm.algorithms.AbstractText;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.mm.pictograms.util.PictogramsAdapterFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
/**
*
* @author Benjamin Schmeling
*
*/
public class AlignmentPropertySection extends GraphitiPropertySection {
protected CCombo horCombo, verCombo;
@Override
public void createControls(Composite parent,
TabbedPropertySheetPage aTabbedPropertySheetPage) {
super.createControls(parent, aTabbedPropertySheetPage);
TabbedPropertySheetWidgetFactory fac = getWidgetFactory();
Composite comp = fac.createComposite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
fac.createLabel(comp, "Horizontal Orientation:");
horCombo = fac.createCCombo(comp);
horCombo.setItems(new String[] { Orientation.ALIGNMENT_LEFT.getName(),
Orientation.ALIGNMENT_CENTER.getName(),
Orientation.ALIGNMENT_RIGHT.getName() });
horCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
changeHorizontalAlignment();
}
});
fac.createLabel(comp, "Vertical Orientation:");
verCombo = fac.createCCombo(comp);
verCombo.setItems(new String[] { Orientation.ALIGNMENT_TOP.getName(),
Orientation.ALIGNMENT_CENTER.getName(),
Orientation.ALIGNMENT_BOTTOM.getName() });
verCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
changeVerticalAlignment();
}
});
refresh();
}
private void changeHorizontalAlignment() {
editingDomain.getCommandStack().execute(
new RecordingCommand(editingDomain) {
@Override
protected void doExecute() {
List<PictogramElement> inputs = getInput();
for (PictogramElement singleInput : inputs) {
if (singleInput instanceof ContainerShape) {
for (Shape s : ((ContainerShape) singleInput)
.getChildren()) {
setHorizontalAlignment(s);
}
} else if (singleInput.getGraphicsAlgorithm() instanceof AbstractText) {
setHorizontalAlignment(singleInput);
}
}
}
});
}
private void changeVerticalAlignment() {
editingDomain.getCommandStack().execute(
new RecordingCommand(editingDomain) {
@Override
protected void doExecute() {
List<PictogramElement> inputs = getInput();
for (PictogramElement singleInput : inputs) {
if (singleInput instanceof ContainerShape) {
for (Shape s : ((ContainerShape) singleInput)
.getChildren()) {
setVerticalAlignment(s);
}
} else if (singleInput.getGraphicsAlgorithm() instanceof AbstractText) {
setVerticalAlignment(singleInput);
}
}
}
});
}
public IPropertySource getPropertySource(Object object) {
if (object instanceof IPropertySource) {
return (IPropertySource) object;
}
AdapterFactory adapterFactory = getAdapterFactory(object);
if (adapterFactory != null) {
IItemPropertySource itemPropertySource = (IItemPropertySource) adapterFactory
.adapt(object, IItemPropertySource.class);
if (itemPropertySource != null) {
return new PropertySource(object, itemPropertySource);
}
}
if (object instanceof IAdaptable) {
return (IPropertySource) ((IAdaptable) object)
.getAdapter(IPropertySource.class);
}
return null;
}
protected AdapterFactory getAdapterFactory(Object object) {
return new PictogramsAdapterFactory();
}
public NotificationFilter getFilter() {
return NotificationFilter
.createEventTypeFilter(Notification.SET)
.or(NotificationFilter
.createEventTypeFilter(Notification.UNSET))
.and(NotificationFilter.createNotifierTypeFilter(EObject.class));
}
public void update(final Notification notification) {
final EObject eObject = (EObject) notification.getNotifier();
if (!isDisposed() && isSelection(eObject) && !isDeleted(eObject)) {
getDisplay().asyncExec(new Runnable() {
public void run() {
if (!isDisposed() && isSelection(eObject)
&& !isDeleted(eObject))
refresh();
}
});
}
}
protected boolean isSelection(EObject eObject) {
ISelection selection = getSelection();
if (selection == null) {
return false;
}
IStructuredSelection s = (IStructuredSelection) selection;
if (s.size() > 1) {
return false;
}
for (Iterator<?> it = s.iterator(); it.hasNext();) {
if (eObject == it.next()) {
return true;
}
}
return false;
}
protected boolean isDeleted(EObject eObject) {
return eObject.eResource() == null;
}
protected Display getDisplay() {
return getPart().getSite().getShell().getDisplay();
}
@Override
public void refresh() {
if (!this.isDisposed()) {
PictogramElement el = this.getSingleInput();
if (el != null && el instanceof ContainerShape) {
for (Shape s : ((ContainerShape) el).getChildren()) {
if (s.getGraphicsAlgorithm() instanceof AbstractText) {
// && !s.isActive()) {
refreshText(s);
}
}
} else if (el != null
&& el.getGraphicsAlgorithm() instanceof AbstractText) {
refreshText((Shape) el);
} else {
verCombo.setEnabled(false);
horCombo.setEnabled(false);
}
}
}
private void refreshText(Shape s) {
verCombo.setEnabled(true);
horCombo.setEnabled(true);
horCombo.setText(((AbstractText) s.getGraphicsAlgorithm())
.getHorizontalAlignment().getName());
verCombo.setText(((AbstractText) s.getGraphicsAlgorithm())
.getVerticalAlignment().getName());
}
private void setHorizontalAlignment(PictogramElement s) {
if (s.getGraphicsAlgorithm() instanceof AbstractText) {
((AbstractText) s.getGraphicsAlgorithm())
.setHorizontalAlignment(Orientation.getByName(horCombo
.getText()));
}
}
private void setVerticalAlignment(PictogramElement s) {
if (s.getGraphicsAlgorithm() instanceof AbstractText) {
((AbstractText) s.getGraphicsAlgorithm())
.setVerticalAlignment(Orientation.getByName(verCombo
.getText()));
}
}
}