/******************************************************************************* | |
* <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.math.BigDecimal; | |
import java.math.MathContext; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
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.domain.IEditingDomainProvider; | |
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.emf.transaction.TransactionalEditingDomain; | |
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil; | |
import org.eclipse.fmc.blockdiagram.editor.util.FontUtils; | |
import org.eclipse.gef.GraphicalEditPart; | |
import org.eclipse.graphiti.mm.algorithms.AbstractText; | |
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; | |
import org.eclipse.graphiti.mm.algorithms.styles.Font; | |
import org.eclipse.graphiti.mm.algorithms.styles.Style; | |
import org.eclipse.graphiti.mm.pictograms.Connection; | |
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.Diagram; | |
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.graphiti.services.Graphiti; | |
import org.eclipse.jface.viewers.ISelection; | |
import org.eclipse.jface.viewers.IStructuredSelection; | |
import org.eclipse.jface.viewers.StructuredSelection; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.custom.CCombo; | |
import org.eclipse.swt.custom.ScrolledComposite; | |
import org.eclipse.swt.events.ModifyEvent; | |
import org.eclipse.swt.events.ModifyListener; | |
import org.eclipse.swt.events.MouseAdapter; | |
import org.eclipse.swt.events.MouseEvent; | |
import org.eclipse.swt.events.SelectionAdapter; | |
import org.eclipse.swt.events.SelectionEvent; | |
import org.eclipse.swt.events.SelectionListener; | |
import org.eclipse.swt.graphics.Color; | |
import org.eclipse.swt.layout.GridData; | |
import org.eclipse.swt.layout.GridLayout; | |
import org.eclipse.swt.widgets.Button; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.swt.widgets.Label; | |
import org.eclipse.swt.widgets.Spinner; | |
import org.eclipse.ui.IWorkbenchPart; | |
import org.eclipse.ui.views.properties.IPropertySource; | |
import org.eclipse.ui.views.properties.IPropertySourceProvider; | |
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection; | |
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage; | |
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; | |
/** | |
* Standard property section that allows the user to change fonts and colors of | |
* objects in a Graphiti-based diagram editor. | |
* | |
* @author Rainer Thome | |
* | |
*/ | |
public class ColorsAndFontsPropertySection extends AbstractPropertySection | |
implements IPropertySourceProvider { | |
private static final String TOOLTIP_BG_COLOR = "Sets the background color of the selcted shape"; | |
private static final String TOOLTIP_FG_COLOR = "Sets the color of the frame for the selected shape"; | |
private static final String TOOLTIP_FONT_COLOR = "Sets the color for the font of the selected shape"; | |
private static final String TOOLTIP_TRANSPARENCY = "Sets the transparency of the selected shape."; | |
private static final String TOOLTIP_TEXT_ANGLE = "Rotates the text of the selected shape by the chosen angle."; | |
private static final String TOOLTIP_STYLE = "A predefined style for the shape can be selected. By changing the predefined style of a shape it will loose its own style information and inherits the style of predefined one. Predefined styles can be maintained in the Predefined Styles Property Tab."; | |
private static final String TOOLTIP_ITALIC = "Sets the font type to italic for the text of the selected shape(s)."; | |
private static final String TOOLTIP_BOLD = "Sets the font type to bold for the text of the selected shape(s)."; | |
private static final String TOOLTIP_FONT_SIZE = "Changes the font size for the text of the selected shape(s)."; | |
private static final String TOOLTIP_RECURSIVE_FONT = "Propagates all changes to fonts also to fonts of children shapes."; | |
private static final String TOOLTIP_FONT_TYPE = "Sets the font type for the text of the selected shape(s)."; | |
private static final String NONE = "NONE"; | |
protected boolean disposed; | |
protected CCombo fontsCombo; | |
protected CCombo sizesCombo; | |
protected CCombo stylesCombo; | |
protected Label foreLabel; | |
protected Label backLabel; | |
protected Label fontLabel; | |
protected Spinner transparency; | |
protected Spinner textAngle; | |
protected TransactionalEditingDomain editingDomain; | |
protected Button isRecursiveFont; | |
protected Button isBold; | |
protected Button isItalic; | |
protected Diagram diagram; | |
protected Composite comp; | |
protected GridData gridData; | |
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(); | |
} | |
private boolean fontSupported(String fontName) { | |
for (String supportedFontName : FontUtils.getFontNames()) { | |
if (fontName.equals(supportedFontName)) | |
return true; | |
} | |
return false; | |
} | |
protected PictogramElement getColorElement() { | |
if (getInput().isEmpty()) | |
return null; | |
else | |
return getInput().get(0); | |
} | |
@Override | |
public void createControls(Composite parent, | |
TabbedPropertySheetPage aTabbedPropertySheetPage) { | |
super.createControls(parent, aTabbedPropertySheetPage); | |
// page.setPropertySourceProvider(getPropertySourceProvider()); | |
TabbedPropertySheetWidgetFactory fac = getWidgetFactory(); | |
comp = fac.createComposite(parent, SWT.NONE); | |
GridLayout layout = new GridLayout(2, false); | |
layout.horizontalSpacing = 10; | |
comp.setLayout(layout); | |
comp.setLayoutData(gridData); | |
createFontColorControl(fac, comp); | |
createFontTypeControl(fac, comp); | |
createFontSizeControl(fac, comp); | |
createTextAngleControl(fac, comp); | |
createBackgroundColorControl(fac, comp); | |
createTransparencyControl(fac, comp); | |
createBoldControl(fac, comp); | |
createItalicControl(fac, comp); | |
createForegroundColorControl(fac, comp); | |
createStyleControl(fac, comp); | |
createRecursiveFontControl(fac, comp); | |
refresh(); | |
} | |
protected void createFontTypeControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Font Type:"); | |
label.setToolTipText(TOOLTIP_FONT_TYPE); | |
fontsCombo = fac.createCCombo(comp); | |
fontsCombo.setToolTipText(TOOLTIP_FONT_TYPE); | |
fontsCombo.setItems(FontUtils.getFontNames()); | |
fontsCombo.addSelectionListener(new SelectionAdapter() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
updateFonts(); | |
} | |
}); | |
} | |
protected void createFontSizeControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Font Size:"); | |
label.setToolTipText(TOOLTIP_FONT_SIZE); | |
sizesCombo = fac.createCCombo(comp); | |
sizesCombo.setToolTipText(TOOLTIP_FONT_SIZE); | |
sizesCombo.setItems(FontUtils.FONT_SIZES); | |
sizesCombo.addSelectionListener(new SelectionAdapter() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
updateFonts(); | |
} | |
}); | |
} | |
protected void createBoldControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Bold:"); | |
label.setToolTipText(TOOLTIP_BOLD); | |
isBold = new Button(comp, SWT.CHECK); | |
isBold.setToolTipText(TOOLTIP_FONT_SIZE); | |
isBold.addSelectionListener(new SelectionListener() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
updateFonts(); | |
} | |
@Override | |
public void widgetDefaultSelected(SelectionEvent e) { | |
} | |
}); | |
} | |
protected void createItalicControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Italic:"); | |
label.setToolTipText(TOOLTIP_ITALIC); | |
isItalic = new Button(comp, SWT.CHECK); | |
isItalic.setToolTipText(TOOLTIP_ITALIC); | |
isItalic.addSelectionListener(new SelectionListener() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
updateFonts(); | |
} | |
@Override | |
public void widgetDefaultSelected(SelectionEvent e) { | |
} | |
}); | |
} | |
protected void createFontColorControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
fontLabel = fac.createLabel(comp, "Font Color:"); | |
fontLabel.setToolTipText(TOOLTIP_FONT_COLOR); | |
Button bt3 = fac.createButton(comp, "Change Font Color...", SWT.PUSH); | |
bt3.setToolTipText(TOOLTIP_FONT_COLOR); | |
bt3.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseUp(MouseEvent e) { | |
FMCUtil.getActiveEditor() | |
.getEditingDomain() | |
.getCommandStack() | |
.execute( | |
new RecordingCommand(FMCUtil.getActiveEditor() | |
.getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
updateFontColors(FMCUtil.editColor(null, | |
diagram)); | |
} | |
}); | |
} | |
}); | |
} | |
protected void createRecursiveFontControl( | |
TabbedPropertySheetWidgetFactory fac, Composite comp) { | |
Label label = fac.createLabel(comp, "Change Fonts for Children:"); | |
label.setToolTipText(TOOLTIP_RECURSIVE_FONT); | |
isRecursiveFont = new Button(comp, SWT.CHECK); | |
isRecursiveFont.setToolTipText(TOOLTIP_RECURSIVE_FONT); | |
} | |
protected void createForegroundColorControl( | |
TabbedPropertySheetWidgetFactory fac, Composite comp) { | |
foreLabel = fac.createLabel(comp, "Foreground:"); | |
foreLabel.setToolTipText(TOOLTIP_FG_COLOR); | |
Button bt = fac.createButton(comp, "Change Foreground Color...", | |
SWT.PUSH); | |
bt.setToolTipText(TOOLTIP_FG_COLOR); | |
bt.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseUp(MouseEvent e) { | |
FMCUtil.getActiveEditor() | |
.getEditingDomain() | |
.getCommandStack() | |
.execute( | |
new RecordingCommand(FMCUtil.getActiveEditor() | |
.getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
if (!getInput().isEmpty()) | |
updateColors( | |
getInput().get(0), | |
FMCUtil.editColor( | |
getColorElement() | |
.getGraphicsAlgorithm() | |
.getForeground(), | |
diagram), true); | |
} | |
}); | |
} | |
}); | |
} | |
protected void createBackgroundColorControl( | |
TabbedPropertySheetWidgetFactory fac, Composite comp) { | |
backLabel = fac.createLabel(comp, "Background:"); | |
backLabel.setToolTipText(TOOLTIP_BG_COLOR); | |
Button bt2 = fac.createButton(comp, "Change Background Color...", | |
SWT.PUSH); | |
bt2.setToolTipText(TOOLTIP_BG_COLOR); | |
bt2.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseUp(MouseEvent e) { | |
FMCUtil.getActiveEditor() | |
.getEditingDomain() | |
.getCommandStack() | |
.execute( | |
new RecordingCommand(FMCUtil.getActiveEditor() | |
.getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
if (!getInput().isEmpty()) | |
updateColors( | |
getInput().get(0), | |
FMCUtil.editColor( | |
getColorElement() | |
.getGraphicsAlgorithm() | |
.getBackground(), | |
diagram), false); | |
} | |
}); | |
} | |
}); | |
} | |
protected void createTransparencyControl( | |
TabbedPropertySheetWidgetFactory fac, Composite comp) { | |
Label label = fac.createLabel(comp, "Transparency:"); | |
label.setToolTipText(TOOLTIP_TRANSPARENCY); | |
transparency = new Spinner(comp, SWT.BORDER); | |
transparency.setToolTipText(TOOLTIP_TRANSPARENCY); | |
transparency.addModifyListener(new ModifyListener() { | |
@Override | |
public void modifyText(ModifyEvent e) { | |
final double transparencyValue = ((Spinner) e.getSource()) | |
.getSelection(); | |
for (PictogramElement el : getInput()) { | |
updateTransparency(el, transparencyValue); | |
} | |
} | |
}); | |
} | |
protected void createTextAngleControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Text Angle:"); | |
label.setToolTipText(TOOLTIP_TEXT_ANGLE); | |
textAngle = new Spinner(comp, SWT.BORDER); | |
textAngle.setToolTipText(TOOLTIP_TEXT_ANGLE); | |
textAngle.setMaximum(360); | |
textAngle.setIncrement(90); | |
textAngle.addModifyListener(new ModifyListener() { | |
@Override | |
public void modifyText(ModifyEvent e) { | |
final int angle = ((Spinner) e.getSource()).getSelection(); | |
for (PictogramElement el : getInput()) { | |
rotateText(el, angle); | |
} | |
} | |
}); | |
} | |
protected void createStyleControl(TabbedPropertySheetWidgetFactory fac, | |
Composite comp) { | |
Label label = fac.createLabel(comp, "Style:"); | |
label.setToolTipText(TOOLTIP_STYLE); | |
stylesCombo = fac.createCCombo(comp); | |
stylesCombo.setToolTipText(TOOLTIP_STYLE); | |
stylesCombo.addSelectionListener(new SelectionAdapter() { | |
@Override | |
public void widgetSelected(SelectionEvent e) { | |
for (PictogramElement el : getInput()) { | |
updateStyle(el, stylesCombo.getItem(stylesCombo | |
.getSelectionIndex())); | |
} | |
} | |
}); | |
} | |
protected void updateStyle(final PictogramElement el, final String styleId) { | |
editingDomain.getCommandStack().execute( | |
new RecordingCommand(editingDomain) { | |
@Override | |
protected void doExecute() { | |
Style style = getStyle(styleId); | |
GraphicsAlgorithm ga = el.getGraphicsAlgorithm(); | |
if (NONE.equals(styleId)) | |
updateStyle(ga, null); | |
else { | |
updateStyle(ga, style); | |
if (el instanceof ContainerShape) { | |
AbstractText text = getTextFromShape((ContainerShape) el); | |
if (text != null) { | |
text.setForeground(null); | |
text.setFont(null); | |
text.setStyle(style); | |
} | |
} | |
} | |
} | |
}); | |
} | |
private void updateStyle(GraphicsAlgorithm ga, Style style) { | |
ga.setStyle(style); | |
ga.setTransparency(null); | |
ga.setBackground(null); | |
ga.setForeground(null); | |
for (GraphicsAlgorithm gaChild : ga.getGraphicsAlgorithmChildren()) { | |
updateStyle(gaChild, style); | |
} | |
} | |
private Style getStyle(String styleId) { | |
for (Style style : diagram.getStyles()) { | |
if (styleId.equals(style.getId())) | |
return style; | |
} | |
return null; | |
} | |
private String[] getStyleIds() { | |
String[] styles = new String[diagram.getStyles().size()]; | |
// styles[0] = NONE; | |
int i = 0; | |
for (Style style : diagram.getStyles()) { | |
styles[i] = style.getId(); | |
i++; | |
} | |
return styles; | |
} | |
protected void updateTransparency(final PictogramElement el, | |
final double transparencyValue) { | |
FMCUtil.getActiveEditor() | |
.getEditingDomain() | |
.getCommandStack() | |
.execute( | |
new RecordingCommand(FMCUtil.getActiveEditor() | |
.getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
GraphicsAlgorithm graphicsAlgorithm = el | |
.getGraphicsAlgorithm(); | |
double trans = graphicsAlgorithm | |
.getTransparency() == null ? 0 | |
: graphicsAlgorithm.getTransparency(); | |
BigDecimal decimal = new BigDecimal(trans, | |
new MathContext(2)); | |
if (Double.compare(transparencyValue / 100.0, | |
decimal.doubleValue()) != 0) { | |
graphicsAlgorithm | |
.setTransparency(transparencyValue / 100.0); | |
for (GraphicsAlgorithm childGa : graphicsAlgorithm | |
.getGraphicsAlgorithmChildren()) { | |
childGa.setTransparency(transparencyValue / 100.0); | |
} | |
} | |
} | |
}); | |
} | |
protected void rotateText(final PictogramElement el, final int angle) { | |
FMCUtil.getActiveEditor() | |
.getEditingDomain() | |
.getCommandStack() | |
.execute( | |
new RecordingCommand(FMCUtil.getActiveEditor() | |
.getEditingDomain()) { | |
@Override | |
protected void doExecute() { | |
if (el instanceof ContainerShape) { | |
ContainerShape container = (ContainerShape) el; | |
final AbstractText textFromShape = getTextFromShape(container); | |
if (textFromShape != null | |
&& textFromShape.getAngle() != angle) { | |
textFromShape.setAngle(angle); | |
refresh(); | |
} | |
if (isRecursiveFont.getSelection()) { | |
for (Shape shape : container | |
.getChildren()) { | |
rotateText(shape, angle); | |
} | |
} | |
} else if (el.getGraphicsAlgorithm() instanceof AbstractText | |
&& ((AbstractText) el | |
.getGraphicsAlgorithm()) | |
.getAngle() != angle) { | |
((AbstractText) el.getGraphicsAlgorithm()) | |
.setAngle(angle); | |
refresh(); | |
} | |
} | |
}); | |
} | |
protected void updateFonts() { | |
for (PictogramElement el : this.getInput()) { | |
updateFont(el); | |
} | |
} | |
protected void updateFont(final PictogramElement el) { | |
if (el.getGraphicsAlgorithm() instanceof AbstractText) { | |
final AbstractText text = ((AbstractText) el.getGraphicsAlgorithm()); | |
updateFont(text); | |
} else if (el instanceof ContainerShape) { | |
ContainerShape container = (ContainerShape) el; | |
AbstractText text = getTextFromShape(container); | |
if (text != null) | |
updateFont(text); | |
if (isRecursiveFont.getSelection()) | |
for (Shape shape : container.getChildren()) { | |
updateFont(shape); | |
} | |
} else if (el instanceof Connection) { | |
Connection con = (Connection) el; | |
for (ConnectionDecorator decorator : con.getConnectionDecorators()) { | |
if (decorator.getGraphicsAlgorithm() instanceof AbstractText) | |
updateFont(((AbstractText) decorator.getGraphicsAlgorithm())); | |
} | |
} | |
} | |
protected void updateFont(final AbstractText text) { | |
editingDomain.getCommandStack().execute( | |
new RecordingCommand(editingDomain) { | |
@Override | |
protected void doExecute() { | |
text.setFont(Graphiti.getGaService().manageFont( | |
diagram, | |
fontsCombo.getItem(fontsCombo | |
.getSelectionIndex()), | |
Integer.parseInt(sizesCombo.getItem(sizesCombo | |
.getSelectionIndex())), | |
isItalic.getSelection(), isBold.getSelection())); | |
} | |
}); | |
} | |
protected void updateColors(final PictogramElement el, | |
final org.eclipse.graphiti.mm.algorithms.styles.Color c, | |
final boolean isForeground) { | |
editingDomain.getCommandStack().execute( | |
new RecordingCommand(editingDomain) { | |
@Override | |
protected void doExecute() { | |
updateColors(el.getGraphicsAlgorithm(), c, isForeground); | |
} | |
}); | |
refresh(); | |
} | |
private void updateColors(GraphicsAlgorithm ga, | |
org.eclipse.graphiti.mm.algorithms.styles.Color c, | |
boolean isForeground) { | |
if (isForeground) { | |
ga.setForeground(c); | |
} else { | |
if (ga.getStyle() != null) | |
ga.getStyle().setRenderingStyle(null); | |
ga.setBackground(c); | |
} | |
for (GraphicsAlgorithm gaChild : ga.getGraphicsAlgorithmChildren()) { | |
updateColors(gaChild, c, isForeground); | |
} | |
} | |
protected void updateFontColors( | |
final org.eclipse.graphiti.mm.algorithms.styles.Color c) { | |
for (PictogramElement el : this.getInput()) { | |
updateFontColor(c, el); | |
} | |
} | |
protected void updateFontColor( | |
final org.eclipse.graphiti.mm.algorithms.styles.Color c, | |
PictogramElement el) { | |
if (el instanceof ContainerShape) { | |
ContainerShape container = (ContainerShape) el; | |
final AbstractText textFromShape = getTextFromShape(container); | |
if (textFromShape != null) | |
updateFontColor(c, textFromShape); | |
if (isRecursiveFont.getSelection()) { | |
for (Shape shape : container.getChildren()) { | |
updateFontColor(c, shape); | |
} | |
} | |
refresh(); | |
} else if (el.getGraphicsAlgorithm() instanceof AbstractText) { | |
updateFontColor(c, (AbstractText) el.getGraphicsAlgorithm()); | |
} | |
} | |
protected void updateFontColor( | |
final org.eclipse.graphiti.mm.algorithms.styles.Color c, | |
final AbstractText ga) { | |
editingDomain.getCommandStack().execute( | |
new RecordingCommand(editingDomain) { | |
@Override | |
protected void doExecute() { | |
ga.setForeground(c); | |
} | |
}); | |
refresh(); | |
} | |
protected void updateLines() { | |
} | |
public void dispose() { | |
super.dispose(); | |
disposed = true; | |
} | |
@Override | |
public void refresh() { | |
if (!this.isDisposed()) { | |
PictogramElement el = getColorElement(); | |
if (el != null && el instanceof Shape) { | |
GraphicsAlgorithm ga = el.getGraphicsAlgorithm(); | |
if (ga == null) | |
return; | |
stylesCombo.setItems(getStyleIds()); | |
if (ga.getStyle() != null) | |
stylesCombo.setText(ga.getStyle().getId()); | |
else | |
stylesCombo.setText(NONE); | |
if (ga instanceof AbstractText) { | |
fontsCombo.setEnabled(true); | |
if ((((AbstractText) ga).getFont() == null)) { | |
defaultCombos(); | |
} else { | |
Font font = ((AbstractText) ga).getFont(); | |
fontsCombo.setText(font.getName()); | |
sizesCombo.setEnabled(true); | |
sizesCombo.setText(String.valueOf(font.getSize())); | |
isBold.setSelection(font.isBold()); | |
isItalic.setSelection(font.isItalic()); | |
textAngle.setSelection(((AbstractText) ga).getAngle()); | |
} | |
} else { | |
if (el instanceof ContainerShape) { | |
ContainerShape container = (ContainerShape) el; | |
if (ga.getTransparency() != null) | |
transparency.setSelection((int) (ga | |
.getTransparency().doubleValue() * 100)); | |
else | |
transparency.setSelection(0); | |
AbstractText text = getTextFromShape(container); | |
if (text != null) { | |
textAngle.setSelection(text.getAngle()); | |
extractFontFromText(text); | |
setColorOfFCLabel(text.getForeground()); | |
} | |
} else { | |
disableCombos(); | |
} | |
org.eclipse.graphiti.mm.algorithms.styles.Color back = ga | |
.getBackground(); | |
setColorOfBGLabel(back); | |
org.eclipse.graphiti.mm.algorithms.styles.Color fore = ga | |
.getForeground(); | |
setColorOfFGLabel(fore); | |
} | |
} else if (el instanceof Connection) { | |
Connection con = (Connection) el; | |
for (ConnectionDecorator decorator : con | |
.getConnectionDecorators()) { | |
if (decorator.getGraphicsAlgorithm() instanceof AbstractText) | |
extractFontFromText((AbstractText) decorator | |
.getGraphicsAlgorithm()); | |
} | |
} | |
refreshGridLayout(); | |
} | |
} | |
protected void refreshGridLayout() { | |
ScrolledComposite tabbedPropertyComposite = findTabbedPropertyComposite(comp); | |
if (tabbedPropertyComposite != null) { | |
int cols = tabbedPropertyComposite.getSize().x / 200 * 2; | |
cols = cols < 2 ? 2 : cols; | |
comp.setLayout(new GridLayout(cols > 8 ? 8 : cols, false)); | |
comp.layout(); | |
} | |
} | |
protected void setColorOfFCLabel( | |
org.eclipse.graphiti.mm.algorithms.styles.Color fore) { | |
if (fore != null) { | |
fontLabel.setBackground(new Color(Display.getDefault(), fore | |
.getRed(), fore.getGreen(), fore.getBlue())); | |
fontLabel.setForeground(new Color(Display.getDefault(), 255 - fore | |
.getRed(), 255 - fore.getGreen(), 255 - fore.getBlue())); | |
} else { | |
fontLabel.setBackground(new Color(Display.getDefault(), 255, 255, | |
255)); | |
fontLabel.setForeground(new Color(Display.getDefault(), 0, 0, 0)); | |
} | |
} | |
protected void setColorOfFGLabel( | |
org.eclipse.graphiti.mm.algorithms.styles.Color fore) { | |
if (fore != null) { | |
foreLabel.setBackground(new Color(Display.getDefault(), fore | |
.getRed(), fore.getGreen(), fore.getBlue())); | |
foreLabel.setForeground(new Color(Display.getDefault(), 255 - fore | |
.getRed(), 255 - fore.getGreen(), 255 - fore.getBlue())); | |
} else { | |
foreLabel.setBackground(new Color(Display.getDefault(), 255, 255, | |
255)); | |
foreLabel.setForeground(new Color(Display.getDefault(), 0, 0, 0)); | |
} | |
} | |
protected void setColorOfBGLabel( | |
org.eclipse.graphiti.mm.algorithms.styles.Color back) { | |
if (back != null) { | |
backLabel.setBackground(new Color(Display.getDefault(), back | |
.getRed(), back.getGreen(), back.getBlue())); | |
backLabel.setForeground(new Color(Display.getDefault(), 255 - back | |
.getRed(), 255 - back.getGreen(), 255 - back.getBlue())); | |
} else { | |
backLabel.setBackground(new Color(Display.getDefault(), 255, 255, | |
255)); | |
backLabel.setForeground(new Color(Display.getDefault(), 0, 0, 0)); | |
} | |
} | |
protected void extractFontFromText(AbstractText text) { | |
if (text != null && text.getFont() != null) { | |
fontsCombo.setEnabled(true); | |
fontsCombo.setText(text.getFont().getName()); | |
sizesCombo.setEnabled(true); | |
sizesCombo.setText(String.valueOf(text.getFont().getSize())); | |
isBold.setSelection(text.getFont().isBold()); | |
isItalic.setSelection(text.getFont().isItalic()); | |
} else if (text != null && text.getStyle() != null | |
&& text.getStyle().getFont() != null) { | |
fontsCombo.setEnabled(true); | |
fontsCombo.setText(text.getStyle().getFont().getName()); | |
sizesCombo.setEnabled(true); | |
sizesCombo.setText(String.valueOf(text.getStyle().getFont() | |
.getSize())); | |
isBold.setSelection(text.getStyle().getFont().isBold()); | |
isItalic.setSelection(text.getStyle().getFont().isItalic()); | |
} else { | |
// disableCombos(); | |
defaultCombos(); | |
} | |
} | |
protected AbstractText getTextFromShape(ContainerShape container) { | |
AbstractText text = null; | |
for (Shape shape : container.getChildren()) { | |
if (shape.getGraphicsAlgorithm() instanceof AbstractText) { | |
text = (AbstractText) shape.getGraphicsAlgorithm(); | |
return text; | |
} | |
} | |
for (Shape shape : container.getChildren()) { | |
if (shape instanceof ContainerShape) { | |
AbstractText textFromShape = getTextFromShape((ContainerShape) shape); | |
if (textFromShape != null) | |
return textFromShape; | |
} | |
} | |
for (GraphicsAlgorithm ga : container.getGraphicsAlgorithm() | |
.getGraphicsAlgorithmChildren()) { | |
if (ga instanceof AbstractText) { | |
return (AbstractText) ga; | |
} | |
} | |
return text; | |
} | |
protected void disableCombos() { | |
fontsCombo.clearSelection(); | |
fontsCombo.setText(""); | |
fontsCombo.setEnabled(false); | |
sizesCombo.clearSelection(); | |
sizesCombo.setText(""); | |
sizesCombo.setEnabled(false); | |
} | |
protected void defaultCombos() { | |
if (fontSupported("Arial")) | |
fontsCombo.setText("Arial"); | |
else if (FontUtils.getFontNames().length > 0) | |
fontsCombo.setText(FontUtils.getFontNames()[0]); | |
fontsCombo.setEnabled(true); | |
sizesCombo.setText("8"); | |
sizesCombo.setEnabled(true); | |
isBold.setSelection(false); | |
isItalic.setSelection(false); | |
} | |
@SuppressWarnings({ "unchecked", "rawtypes" }) | |
protected List<PictogramElement> getInput() { | |
ISelection sel = getSelection(); | |
if (sel instanceof IStructuredSelection) { | |
Object[] fe = ((IStructuredSelection) sel).toArray(); | |
for (Object object : fe) { | |
if (!(object instanceof PictogramElement)) | |
return Collections.EMPTY_LIST; | |
} | |
return new ArrayList(Arrays.asList(fe)); | |
// if (fe != null && fe instanceof PictogramElement) | |
// return (PictogramElement) fe; | |
} | |
return Collections.EMPTY_LIST; | |
} | |
public boolean isDisposed() { | |
return disposed; | |
} | |
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 setInput(IWorkbenchPart part, ISelection selection) { | |
// set the current editing domain | |
IEditingDomainProvider provider = (IEditingDomainProvider) part | |
.getAdapter(IEditingDomainProvider.class); | |
assert provider.getEditingDomain() instanceof TransactionalEditingDomain; | |
editingDomain = (TransactionalEditingDomain) provider | |
.getEditingDomain(); | |
diagram = FMCUtil.getActiveEditor().getDiagramTypeProvider().getDiagram(); | |
// transform the selection | |
List<Object> objects = new ArrayList<Object>(); | |
if (selection instanceof IStructuredSelection) { | |
Iterator<?> it = ((IStructuredSelection) selection).iterator(); | |
while (it.hasNext()) { | |
GraphicalEditPart editPart = (GraphicalEditPart) it.next(); | |
objects.add((PictogramElement) editPart.getModel()); | |
} | |
} | |
StructuredSelection newSelection = new StructuredSelection(objects); | |
super.setInput(part, newSelection); | |
refresh(); | |
} | |
protected ScrolledComposite findTabbedPropertyComposite(Composite comp) { | |
if (comp.getParent() == null) { | |
return null; | |
} else if (comp.getParent() instanceof ScrolledComposite) { | |
return (ScrolledComposite) comp.getParent(); | |
} else | |
return findTabbedPropertyComposite(comp.getParent()); | |
} | |
} |