2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2011-2015 - Scilab Enterprises - Clement DAVID
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.xcos.port;
19 import org.scilab.modules.graph.utils.StyleMap;
20 import org.scilab.modules.types.ScilabType;
21 import org.scilab.modules.xcos.JavaController;
22 import org.scilab.modules.xcos.Kind;
23 import org.scilab.modules.xcos.ObjectProperties;
24 import org.scilab.modules.xcos.PortKind;
25 import org.scilab.modules.xcos.block.BasicBlock;
26 import org.scilab.modules.xcos.graph.model.XcosCell;
28 import com.mxgraph.model.mxGeometry;
29 import com.mxgraph.model.mxICell;
30 import com.mxgraph.util.mxConstants;
33 * Common implementation of any Port.
35 public abstract class BasicPort extends XcosCell {
36 private static final long serialVersionUID = 0L;
39 * The side-size of any port. All ports must have the same size.
41 public static final double DEFAULT_PORTSIZE = 8;
43 private Orientation orientation;
45 /** Type of any dataport */
47 /** The link direction as no impact on simulation */
49 /** The link keep its direction on simulation */
53 * @return A scicos compatible representation
55 public String getAsString() {
67 /** Type of any data on any dataport */
68 public enum DataType {
69 /** A not specific type */
71 /** Data is real (double) numbers */
73 /** Data is complex (double + i * double) numbers */
75 /** Data is int32 (32 bits) numbers */
77 /** Data is int16 (16 bits) numbers */
79 /** Data is int8 (8bits) numbers */
81 /** Data is uint32 (unsigned 32 bits) numbers */
83 /** Data is uint16 (unsigned 16 bits) numbers */
85 /** Data is uint8 (unsigned 8 bits) numbers */
89 * @return A scicos compatible representation
91 public int asScilabValue() {
92 if (this.equals(UNKNOW_TYPE)) {
96 // We assume that the types are sorted well on the enum definition
97 return this.ordinal();
102 * A scicos representation
103 * @return The java compatible representation
105 public static DataType convertScilabValue(int val) {
106 if (val <= 0 || val > DataType.values().length - 1) {
107 return DataType.UNKNOW_TYPE;
110 // We assume that the types are sorted well on the enum definition
111 return DataType.values()[val];
114 public static DataType convertScilabValue(double val) {
115 return convertScilabValue((int) val);
120 * Instantiate a port with a style (or typename).
123 * Value to be set as a Style and as TypeName
125 public BasicPort(final JavaController controller, long uid, Kind kind, Object value, String style, String id, Orientation orientation, boolean isImplicit, PortKind portKind) {
126 super(controller, uid, kind, value, new mxGeometry(0, 0, DEFAULT_PORTSIZE, DEFAULT_PORTSIZE), style, id);
130 controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.IMPLICIT, isImplicit);
131 controller.setObjectProperty(uid, Kind.PORT, ObjectProperties.PORT_KIND, portKind.ordinal());
133 this.orientation = orientation;
134 setLabelPosition(orientation);
138 * @return the type of the port (Explicit or Implicit)
140 public abstract Type getType();
142 public abstract PortKind getPortKind();
144 /** @return The default orientation of this port */
145 public final Orientation getOrientation() {
150 * @param defaultOrientation
151 * The default orientation of this port
153 public final void setOrientation(Orientation defaultOrientation) {
154 if (orientation != defaultOrientation) {
155 orientation = defaultOrientation;
156 setLabelPosition(orientation);
161 * Set the label position of the current port according to the orientation.
164 * the port orientation, if null, does nothing.
166 public final void setLabelPosition(final Orientation current) {
167 if (current != null) {
168 StyleMap style = new StyleMap(getStyle());
170 // clean up any previously set spacing values
171 style.remove(mxConstants.STYLE_LABEL_POSITION);
172 style.remove(mxConstants.STYLE_VERTICAL_LABEL_POSITION);
173 style.remove(mxConstants.STYLE_SPACING_BOTTOM);
174 style.remove(mxConstants.STYLE_SPACING_LEFT);
175 style.remove(mxConstants.STYLE_SPACING_RIGHT);
176 style.remove(mxConstants.STYLE_SPACING_TOP);
178 // set up the port position
179 style.put(mxConstants.STYLE_ALIGN, current.getLabelPosition());
180 style.put(mxConstants.STYLE_VERTICAL_ALIGN, current.getVerticalLabelPosition());
181 style.put(mxConstants.STYLE_SPACING, Double.toString(BasicPort.DEFAULT_PORTSIZE + 2.0));
183 setStyle(style.toString());
188 * Hook to update the port label from the associated block expression.
190 * The current port index may be found in the ordering data.
193 * the associated block expression.
195 public void updateLabel(ScilabType exprs) {
199 * Overriden methods from jgraphx
206 public String toString() {
207 final StringBuilder str = new StringBuilder();
209 final mxICell parent = getParent();
210 if (parent != null) {
211 if (parent instanceof BasicBlock) {
212 JavaController controller = new JavaController();
213 String[] interfaceFunctionName = new String[1];
214 controller.getObjectProperty(((BasicBlock) parent).getUID(), Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, interfaceFunctionName);
215 str.append(interfaceFunctionName[0]).append('.');
217 str.append(parent.getClass().getSimpleName()).append('.');
221 if (getChildCount() > 0) {
223 str.append(getChildAt(0).getValue());
225 str.append(getClass().getSimpleName());
227 if (parent != null) {
228 str.append('[').append(getParent().getIndex(this)).append(']');
230 if (getEdgeCount() == 1) {
231 str.append(" (connected)");
232 } else if (getEdgeCount() > 1) {
233 str.append(" - multiple links (");
234 str.append(getEdgeCount());
238 return str.toString();