2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Clement DAVID
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.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;
30 import org.scilab.modules.xcos.VectorOfInt;
33 * Change the port label accordingly to the ipar values.
35 public class ProdPortLabeler {
37 private static final String NOT_PRINTED_LABEL = "\u00d7";
38 private static ProdPortLabeler instance;
43 public ProdPortLabeler() {
47 * @return the shared instance
49 public static ProdPortLabeler getInstance() {
50 if (instance == null) {
51 instance = new ProdPortLabeler();
57 * Change the label of the port according to the integer parameters
60 * @param source the source of the block
62 public void updateLabel(final BasicBlock source) {
64 * Get the input port children
66 final List<InputPort> ports = new ArrayList<InputPort>();
67 for (int i = 0; i < source.getChildCount(); i++) {
68 final mxICell port = source.getChildAt(i);
70 if (port instanceof InputPort) {
71 ports.add((InputPort) port);
76 * Set the ports labels
78 JavaController controller = new JavaController();
79 VectorOfInt ipar = new VectorOfInt(ports.size());
80 controller.getObjectProperty(source.getUID(), Kind.BLOCK, ObjectProperties.IPAR, ipar);
82 for (int i = 0; i < ports.size(); i++) {
85 if (ipar.size() < i) {
91 ports.get(i).setValue(getLabel(gain));
95 * Check if all the values are equal to the default one.
97 if (!hasDefaultValue(ports)) {
102 * When all values are equal to the default one, set it to the block
103 * and hide the children.
105 source.setValue(NOT_PRINTED_LABEL);
106 for (InputPort port : ports) {
112 * Has all the ports have the default value ?
116 * @return true if they all have the default values
118 private boolean hasDefaultValue(final List<InputPort> ports) {
119 boolean allPortIsDefaultLabel = true;
120 for (InputPort port : ports) {
121 if (port.getValue() instanceof String) {
122 String current = port.getValue().toString();
123 if (!NOT_PRINTED_LABEL.equals(current)) {
124 allPortIsDefaultLabel = false;
129 return allPortIsDefaultLabel;
133 * Return the symbol for the gain value
137 * @return A label representing the gain
139 private String getLabel(double gain) {
141 return NOT_PRINTED_LABEL;