From 9eba26b6adb4ad4c1671661c07ec0a01faa48dac Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 11 May 2011 12:00:33 +0000 Subject: [PATCH] New decoration for modules & Enumeration copy+paste (not working properly, core consumes 100% cpu and nothing happens) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20863 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 48 +++++++++ .../elements2/HoverTextElementNoBounds.java | 22 ++-- .../sysdyn/ui/elements2/HoverTextNode.java | 5 +- .../sysdyn/ui/elements2/ModuleFactory.java | 25 ++++- .../sysdyn/ui/elements2/ModuleNode.java | 100 ++++++++++++++++++ .../ui/handlers/EnumerationPasteHandler.java | 29 +++++ .../sysdyn/ui/properties/EnumerationTab.java | 23 +++- .../sysdyn/ui/utils/ExpressionUtils.java | 5 +- 8 files changed, 240 insertions(+), 17 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleNode.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationPasteHandler.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 49de4a69..563a7dd3 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -870,6 +870,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java index 2b583a4a..1495974b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -33,9 +33,9 @@ import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.sysdyn.ui.utils.VariableNameUtils; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintListener; import org.simantics.utils.datastructures.hints.IHintObservable; -import org.simantics.utils.datastructures.hints.IHintContext.Key; /** * ElementHandler for text elements @@ -67,11 +67,14 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable); } - @Override - public void init(final IElement e, G2DParentNode parent) { - HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback() { + protected HoverTextNode getTextNode(IElement e, G2DParentNode parent) { + return ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, getCallback(e, parent, HoverTextNode.class)); + } + + protected Callback getCallback(final IElement e, G2DParentNode parent, Class nodeClass) { + return new Callback() { @Override - public void run(HoverTextNode node) { + public void run(T node) { node.setTextListener(new ITextListener() { String textBeforeEdit; @@ -149,7 +152,12 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { } }); } - }); + }; + } + + @Override + public void init(final IElement e, G2DParentNode parent) { + HoverTextNode node = getTextNode(e, parent); //Font font = new Font("Tahoma", 0, 12); Font font = ElementUtils.getTextFont(e); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java index f3d3e0d7..7b4f2463 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -18,7 +18,6 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import org.simantics.diagram.elements.TextNode; -import org.simantics.g2d.utils.Alignment; import org.simantics.scenegraph.ISelectionPainterNode; import org.simantics.scenegraph.utils.NodeUtil; @@ -26,7 +25,7 @@ public class HoverTextNode extends TextNode implements ISelectionPainterNode { private static final long serialVersionUID = 3539499125943249895L; - private static transient ThreadLocal tempBounds = new ThreadLocal() { + protected static transient ThreadLocal tempBounds = new ThreadLocal() { @Override protected Rectangle2D initialValue() { return new Rectangle2D.Double(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java index e7321734..678f63e8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -18,6 +18,9 @@ import java.util.Collection; import org.simantics.db.Resource; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.InternalSize; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.ObjectTerminal; @@ -32,10 +35,11 @@ import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; import org.simantics.g2d.utils.Alignment; +import org.simantics.scenegraph.g2d.G2DParentNode; public class ModuleFactory extends SysdynElementFactory { - private static final BasicStroke STROKE = new BasicStroke(1f); + private static final BasicStroke STROKE = new BasicStroke(2.0f); private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true); @Override @@ -50,10 +54,25 @@ public class ModuleFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, - new HoverTextElementHandler(0, 0, Alignment.LEADING, 1f , 7, 7, true), + new ModuleSceneGraph(0, 0, Alignment.LEADING, 1f , 4, 4, true), BoundsOutline.INSTANCE, new WholeElementTerminals(terminals) ).setId(ModuleFactory.class.getSimpleName()); } + + + public static class ModuleSceneGraph extends HoverTextElementHandler implements InternalSize { + private static final long serialVersionUID = 2367230056477661273L; + + public ModuleSceneGraph(double originX, double originY, Alignment horizontalAlignment, double borderWidth, + double paddingX, double paddingY, boolean editable) { + super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable); + } + + protected HoverTextNode getTextNode(IElement e, G2DParentNode parent) { + return ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", ModuleNode.class, getCallback(e, parent, ModuleNode.class)); + } + + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleNode.java new file mode 100644 index 00000000..6695543d --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleNode.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.AlphaComposite; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Composite; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.scenegraph.utils.GeometryUtils; +import org.simantics.scenegraph.utils.NodeUtil; + +public class ModuleNode extends HoverTextNode { + + private static final long serialVersionUID = 8535695797227320496L; + private static double CORNER_LENGTH = 2.0; + private static double CORNER_PADDING = 0.8; + + + @Override + public void render(Graphics2D g) { + super.render(g); + + AffineTransform ot = g.getTransform(); + BasicStroke oldStroke = (BasicStroke)g.getStroke(); + Color oldColor = g.getColor(); + + + g.transform(transform); + + Rectangle2D bounds = expandBounds( alignBounds( getTightUnalignedBoundsInLocal( tempBounds.get() ) ) ); + + Path2D path = new Path2D.Double(); + // LEFT TOP + path.moveTo(bounds.getMinX() + CORNER_LENGTH, bounds.getMinY() - CORNER_PADDING); + path.lineTo(bounds.getMinX() - CORNER_PADDING, bounds.getMinY() - CORNER_PADDING); + path.lineTo(bounds.getMinX() - CORNER_PADDING, bounds.getMinY() + CORNER_LENGTH); + + // LEFT BOTTOM + path.moveTo(bounds.getMinX() + CORNER_LENGTH, bounds.getMaxY() + CORNER_PADDING); + path.lineTo(bounds.getMinX() - CORNER_PADDING, bounds.getMaxY() + CORNER_PADDING); + path.lineTo(bounds.getMinX() - CORNER_PADDING, bounds.getMaxY() - CORNER_LENGTH); + + // RIGHT TOP + path.moveTo(bounds.getMaxX() - CORNER_LENGTH, bounds.getMinY() - CORNER_PADDING); + path.lineTo(bounds.getMaxX() + CORNER_PADDING, bounds.getMinY() - CORNER_PADDING); + path.lineTo(bounds.getMaxX() + CORNER_PADDING, bounds.getMinY() + CORNER_LENGTH); + + // RIGHT BOTTOM + path.moveTo(bounds.getMaxX() - CORNER_LENGTH, bounds.getMaxY() + CORNER_PADDING); + path.lineTo(bounds.getMaxX() + CORNER_PADDING, bounds.getMaxY() + CORNER_PADDING); + path.lineTo(bounds.getMaxX() + CORNER_PADDING, bounds.getMaxY() - CORNER_LENGTH); + + g.translate(x, y); + g.setStroke(new BasicStroke((float) (scale*borderWidth))); + g.setColor(Color.GRAY); + g.draw(path); + + + boolean selected = NodeUtil.isSelected(this, 1); + if (selected && showSelection()) { + Composite oc = g.getComposite(); + g.setComposite(AlphaComposite.SrcAtop.derive(0.5f)); + + g.setColor(Color.RED); + float bw = borderWidth; + double s = GeometryUtils.getScale(g.getTransform()); + if (bw <= 0f) { + bw = (float) (1f / s); + } else { + bw *= 3f * scale; + } + g.setStroke(new BasicStroke(bw)); + g.draw(path); + //g.draw(GeometryUtils.expandRectangle(r, 1.0)); + + g.setComposite(oc); + } + + g.setColor(oldColor); + g.setStroke(oldStroke); + g.translate(-x, -y); + + g.setTransform(ot); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationPasteHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationPasteHandler.java new file mode 100644 index 00000000..a37cb37c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationPasteHandler.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; + +public class EnumerationPasteHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + System.out.println("PASTE " + sel); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java index 3d88d849..589f0c36 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java @@ -1,5 +1,17 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.properties; +import java.util.HashSet; import java.util.List; import org.eclipse.jface.layout.GridDataFactory; @@ -31,6 +43,7 @@ import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; @@ -164,11 +177,15 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge public void perform(WriteGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); - - + + HashSet names = new HashSet(); + for(Resource r : OrderedSetUtils.toList(graph, enumerationIndexes)) { + names.add(NameUtils.getSafeName(graph, r)); + } + Resource ei = GraphUtils.create2(graph, sr.EnumerationIndex, - l0.HasName, "index"); + l0.HasName, NameUtils.findFreshName(graph, "index", names, "")); OrderedSetUtils.add(graph, enumerationIndexes, ei); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java index 5297a2f5..904378ba 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -164,6 +164,9 @@ public class ExpressionUtils { } // VARIABLE NAMES + + if(variables.contains("time")) + variables.remove("time"); for(String v : variables) { if(!modelVariables.keySet().contains(v)) { -- 2.47.1