*******************************************************************************/
package org.simantics.diagram.adapter;
+import java.util.List;
+
import org.simantics.db.AsyncReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.primitiverequest.Adapter;
import org.simantics.diagram.synchronization.ErrorHandler;
import org.simantics.g2d.canvas.ICanvasContext;
import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.handler.SubstituteElementClass;
import org.simantics.g2d.element.ElementClass;
import org.simantics.g2d.element.IElement;
import org.slf4j.Logger;
}
@Override
- public void execute(AsyncReadGraph graph, final ElementClass clazz) {
-
+ public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
+ List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
+ for (SubstituteElementClass subs : substitutes) {
+ mutableClazz = subs.substitute(diagram, mutableClazz);
+ }
+ final ElementClass clazz = mutableClazz;
graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
@Override
graph.forHasStatement(element, gl.getVisible(), element, new AsyncProcedureAdapter<Boolean>() {
@Override
public void execute(AsyncReadGraph graph, Boolean result) {
- synchronized (visible) {
- visible.add(l);
+ if (result) {
+ synchronized (visible) {
+ visible.add(l);
+ }
}
if (DEBUG_LAYERS)
System.out.println(" Visible on layer '" + gl.getName() + "'");
graph.forHasStatement(element, gl.getFocusable(), element, new AsyncProcedureAdapter<Boolean>() {
@Override
public void execute(AsyncReadGraph graph, Boolean result) {
- synchronized (focusable) {
- focusable.add(l);
+ if (result) {
+ synchronized (focusable) {
+ focusable.add(l);
+ }
}
if (DEBUG_LAYERS)
System.out.println(" Focusable on layer '" + gl.getName() + "'");
BufferedImage bi;
Rectangle2D rect;
+ float alpha;
- public AWTImage(BufferedImage bi) {
+ public AWTImage(BufferedImage bi, float alpha) {
assert(bi!=null);
this.bi = bi;
+ this.alpha = alpha;
rect = new Rectangle2D.Double(bi.getMinX(),bi.getMinY(),bi.getWidth(), bi.getHeight());
}
+
+ public AWTImage(BufferedImage bi) {
+ this(bi, 1.0f);
+ }
@Override
public Rectangle2D getBounds() {
public Node init(G2DParentNode parent) {
ImageNode node = parent.getOrCreateNode("image", ImageNode.class);
node.setImage(bi);
+ node.setAlpha(alpha);
+ node.setZIndex(-100);
return node;
}
@SGInit
public void initSG(G2DParentNode parent) {
- node = parent.addNode("ruler", RulerNode.class);
+ node = parent.addNode("ruler", getNodeClass());
node.setZIndex(PAINT_PRIORITY);
updateNode();
}
+ protected Class<? extends RulerNode> getNodeClass() {
+ return RulerNode.class;
+ }
+
@SGCleanup
public void cleanupSG() {
node.remove();
import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.request.Read;
+import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
+import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
import org.simantics.g2d.participant.MouseUtil;
import org.simantics.g2d.participant.MouseUtil.MouseInfo;
import org.simantics.modeling.ModelingResources;
IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
Resource composite = input.getResource();
- addSVG(mpos.getX(), mpos.getY(), composite);
+ IDiagram idiagram = viewer.getAdapter(IDiagram.class);
+
+ addSVG(mpos.getX(), mpos.getY(), composite, idiagram);
}
- public static void addSVG(final double mposX, final double mposY, final Resource composite) {
+ public static void addSVG(final double mposX, final double mposY, final Resource composite, IDiagram idiagram) {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
@Override
public void perform(WriteGraph g) throws DatabaseException {
- Commands.get(g, "Simantics/Diagram/createSVGElement")
+ Object svg = Commands.get(g, "Simantics/Diagram/createSVGElementR")
.execute(g, g.syncRequest(new IndexRoot(composite)),
composite, suffix(filename), data, mposX, mposY);
+
+ if (svg != null && svg instanceof Resource) {
+ Resource resource = (Resource) svg;
+ // 7. Put the element on all the currently active layers if possible.
+ IModifiableSynchronizationContext context = idiagram.getHint(SynchronizationHints.CONTEXT);
+ GraphLayerManager glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
+ if (glm != null) {
+ glm.removeFromAllLayers(g, resource);
+ glm.putElementOnVisibleLayers(idiagram, g, resource);
+ }
+ }
}
});
addKeyBindingParticipants(ctx);
// Grid & Ruler & Background
- ctx.add(new GridPainter());
- ctx.add(new RulerPainter());
- ctx.add(new BackgroundPainter());
+ addGridRulerBackgroundParticipants(ctx);
h.setHint(Hints.KEY_DISPLAY_PAGE, diagramPreferences.get(DiagramPreferences.P_DISPLAY_PAGE_SIZE));
h.setHint(Hints.KEY_DISPLAY_MARGINS, diagramPreferences.get(DiagramPreferences.P_DISPLAY_MARGINS));
ctx.setLocked(false);
}
+ protected void addGridRulerBackgroundParticipants(CanvasContext ctx) {
+ ctx.add(new GridPainter());
+ ctx.add(new RulerPainter());
+ ctx.add(new BackgroundPainter());
+ }
+
protected void loadPageSettings(ICanvasContext ctx) {
DiagramDesc diagramDesc = null;
importJava "org.simantics.modeling.svg.CreateSVGElement" where
createSVGElement :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> ()
+ createSVGElementR :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> Resource
importSVGElement :: Resource -> File -> Double -> Double -> <WriteGraph> ()
+ importSVGElementR :: Resource -> File -> Double -> Double -> <WriteGraph> Resource
importJava "org.simantics.diagram.synchronization.graph.RemoveElement" where
removeElement :: Resource -> Resource -> <WriteGraph> ()
public class CreateSVGElement {
public static void createSVGElement(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException {
+ createSVGElement(g, diagram, suffix, data, mposX, mposY);
+ }
+
+ public static Resource createSVGElementR(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(g);
DiagramResource DIA = DiagramResource.getInstance(g);
throw new DatabaseException("Unknown image format " + suffix);
OrderedSetUtils.addFirst(g, diagram, element);
g.claim(diagram, L0.ConsistsOf, element);
-
+ return element;
}
public static void importSVGElement(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException {
+ importSVGElementR(graph, diagram, file, posX, posY);
+ }
+
+ public static Resource importSVGElementR(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException {
final byte[] data = FileUtils.readFile(file);
- createSVGElement(graph, diagram, suffix(file.getName()), data, posX, posY);
+ return createSVGElementR(graph, diagram, suffix(file.getName()), data, posX, posY);
}
private static String suffix(String fileName) {
//VIEWS.SharedLibraryContribution2 : SWT.TypedVariableTabContribution
// SEL.AbstractVariableTabContribution.HasPriority 1
-// SEL.AbstractTypedVariableTabContribution.HasType L0.SharedOntology
+// SEL.AbstractTypedTabContribution.HasType L0.SharedOntology
// SWT.TypedVariableTabContribution.HasView SharedLibraries
// L0.HasLabel "Shared Libraries"
*******************************************************************************/
package org.simantics.scenegraph.g2d.nodes;
+import java.awt.AlphaComposite;
+import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
protected Boolean visible = Boolean.TRUE;
protected BufferedImage img = null;
+ protected float alpha = 1.0f;
@SyncField("visible")
public void setVisible(Boolean visible) {
public void setImage(BufferedImage src) {
img = src;
}
+
+ public void setAlpha(float alpha) {
+ this.alpha = alpha;
+ }
@Override
public void render(Graphics2D g) {
// Rectangle2D b = parent.getBoundsInLocal();
// g.drawImage(img, (int)b.getMinX(), (int)b.getMinY(), (int)b.getWidth()+(int)b.getMinX(), (int)b.getHeight()+(int)b.getMinY(), 0, 0, img.getWidth(), img.getHeight(), null);
// }
+
+
+ Composite old = g.getComposite();
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
+
g.drawImage(img, 0, 0, null);
+ if (old != null)
+ g.setComposite(old);
if (ot != null)
g.setTransform(ot);
}
// Vertical ruler
for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) {
if(x > 20) {
- String str = formatValue((x-offsetX)/scaleX);
+ double val = (x-offsetX)/scaleX;
+ double modifiedValue = modifyHorizontalValue(val);
+ String str = formatValue(modifiedValue);
FontMetrics fm = g.getFontMetrics();
Rectangle2D r = fm.getStringBounds(str, g);
if((x-r.getWidth()/2) > previousText) {
for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) {
if(y > 20) {
double val = (y-offsetY)/scaleY;
- if (MAP_Y_SCALING)
- val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
- String str = formatValue(val);
+ double modifiedValue = modifyVerticalValue(val);
+ String str = formatValue(modifiedValue);
FontMetrics fm = g.getFontMetrics();
Rectangle2D r = fm.getStringBounds(str, g);
if(y-1+r.getHeight()/2 > previousText) {
g.setTransform(tr);
}
+ /**
+ * A method for subclasses to alter the actual X-value of the ruler
+ *
+ * @param value
+ * @return possibly modified X-value
+ */
+ protected double modifyHorizontalValue(double value) {
+ return value;
+ }
+
+ /**
+ * A method for subclasses to alter the actual Y-value of the ruler
+ *
+ * @param value
+ * @return possibly modified Y-value
+ */
+ protected double modifyVerticalValue(double value) {
+ return value;
+ }
+
private static final transient int MAX_DIGITS = 5;
private static final transient double EPSILON = 0.01;
private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
+import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
import org.simantics.db.Resource;
import org.simantics.utils.datastructures.ArrayMap;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
/**
* @author Tuukka Lehtonen
part = partService.createPart(partId);
return part;
}
+
+ public static MCommand getMCommandById(String id) {
+ IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+ MApplication application = context.get(MApplication.class);
+ for (MCommand command : application.getCommands()) {
+ if (id.equals(command.getElementId())) {
+ return command;
+ }
+ }
+ return null;
+ }
+ @SuppressWarnings("restriction")
+ public static IEditorPart getActiveIEditorPart(MPart mActiveEditorPart) {
+ // TODO: Fix this when we get rid of CompatibilityEditors
+ IEditorPart activeEditor = null;
+ if (mActiveEditorPart != null) {
+ Object editor = mActiveEditorPart.getObject();
+ if (editor instanceof CompatibilityEditor) {
+ CompatibilityEditor compEditor = (CompatibilityEditor) editor;
+ activeEditor = compEditor.getEditor();
+ } else {
+ // TODO: This is not good practice with E4 but an OK fallback for now
+ activeEditor = WorkbenchUtils.getActiveEditor();
+ }
+ }
+ return activeEditor;
+ }
}