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.VectorOfInt;
32 * Change the port label accordingly to the ipar values.
34 public class ProdPortLabeler {
36 private static final String NOT_PRINTED_LABEL = "\u00d7";
37 private static ProdPortLabeler instance;
42 public ProdPortLabeler() {
46 * @return the shared instance
48 public static ProdPortLabeler getInstance() {
49 if (instance == null) {
50 instance = new ProdPortLabeler();
56 * Change the label of the port according to the integer parameters
59 * @param source the source of the block
61 public void updateLabel(final BasicBlock source) {
63 * Get the input port children
65 final List<InputPort> ports = new ArrayList<InputPort>();
66 for (int i = 0; i < source.getChildCount(); i++) {
67 final mxICell port = source.getChildAt(i);
69 if (port instanceof InputPort) {
70 ports.add((InputPort) port);
75 * Set the ports labels
77 JavaController controller = new JavaController();
78 VectorOfInt ipar = new VectorOfInt();
79 controller.getObjectProperty(source.getUID(), Kind.BLOCK, ObjectProperties.IPAR, ipar);
81 for (int i = 0; i < ports.size(); i++) {
84 if (i < ipar.size()) {
90 ports.get(i).setValue(getLabel(gain));
94 * Check if all the values are equal to the default one.
96 if (!hasDefaultValue(ports)) {
101 * When all values are equal to the default one, set it to the block
102 * and hide the children.
104 source.setValue(NOT_PRINTED_LABEL);
105 for (InputPort port : ports) {
111 * Has all the ports have the default value ?
115 * @return true if they all have the default values
117 private boolean hasDefaultValue(final List<InputPort> ports) {
118 boolean allPortIsDefaultLabel = true;
119 for (InputPort port : ports) {
120 if (port.getValue() instanceof String) {
121 String current = port.getValue().toString();
122 if (!NOT_PRINTED_LABEL.equals(current)) {
123 allPortIsDefaultLabel = false;
128 return allPortIsDefaultLabel;
132 * Return the symbol for the gain value
136 * @return A label representing the gain
138 private String getLabel(double gain) {
140 return NOT_PRINTED_LABEL;