2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Clement DAVID
4 * Copyright (C) 2011-2016 - 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.block.custom;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.scilab.modules.xcos.block.BasicBlock;
23 import org.scilab.modules.xcos.port.input.InputPort;
25 import com.mxgraph.model.mxICell;
26 import org.scilab.modules.xcos.JavaController;
27 import org.scilab.modules.xcos.Kind;
28 import org.scilab.modules.xcos.ObjectProperties;
29 import org.scilab.modules.xcos.VectorOfDouble;
32 * Change the port label on rpar change.
34 * This listener must be installed on "integerParameters" property.
36 public class SumPortLabeler {
38 private static final String NOT_PRINTED_LABEL = "+";
39 private static SumPortLabeler instance;
44 public SumPortLabeler() {
48 * @return the shared instance
50 public static SumPortLabeler getInstance() {
51 if (instance == null) {
52 instance = new SumPortLabeler();
58 * Change the label of the port according to the integer parameters
61 * @param source the block to update
63 public void updateLabel(BasicBlock source) {
65 * Get the input port children
67 final List<InputPort> ports = new ArrayList<InputPort>();
68 for (int i = 0; i < source.getChildCount(); i++) {
69 final mxICell port = source.getChildAt(i);
71 if (port instanceof InputPort) {
72 ports.add((InputPort) port);
77 * Set the ports labels
79 JavaController controller = new JavaController();
80 VectorOfDouble rpar = new VectorOfDouble(ports.size());
81 controller.getObjectProperty(source.getUID(), Kind.BLOCK, ObjectProperties.RPAR, rpar);
83 for (int i = 0; i < ports.size(); i++) {
86 if (rpar.size() < i) {
92 ports.get(i).setValue(getLabel(gain));
96 * Check if all the values are equal to the default one.
98 if (!hasDefaultValue(ports)) {
103 * When all values are equal to the default one, set it to the block
104 * and hide the children.
106 source.setValue(NOT_PRINTED_LABEL);
107 for (InputPort port : ports) {
113 * Has all the ports have the default value ?
117 * @return true if they all have the default values
119 private boolean hasDefaultValue(final List<InputPort> ports) {
120 boolean allPortIsDefaultLabel = true;
121 for (InputPort port : ports) {
122 if (port.getValue() instanceof String) {
123 String current = port.getValue().toString();
124 if (!NOT_PRINTED_LABEL.equals(current)) {
125 allPortIsDefaultLabel = false;
130 return allPortIsDefaultLabel;
134 * Return the symbol for the gain value
138 * @return A label representing the gain
140 private String getLabel(double gain) {
142 return NOT_PRINTED_LABEL; // "+"