From 96cdc6dc3a2d2c0650035694b499790586f635b1 Mon Sep 17 00:00:00 2001
From: Minh Hiep Pham <minh.pham@telecom-paristech.fr>
Date: Thu, 7 Feb 2019 13:57:36 +0100
Subject: [PATCH] Update methods for checking valid port names 2

---
 .../java/tmltranslator/TMLSyntaxChecking.java | 15 +++++++++
 .../java/ui/tmlcompd/TMLCPrimitivePort.java   | 33 ++++++++++++++-----
 2 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/src/main/java/tmltranslator/TMLSyntaxChecking.java b/src/main/java/tmltranslator/TMLSyntaxChecking.java
index 354bd16f92..ee763a623f 100755
--- a/src/main/java/tmltranslator/TMLSyntaxChecking.java
+++ b/src/main/java/tmltranslator/TMLSyntaxChecking.java
@@ -45,6 +45,9 @@ import compiler.tmlparser.TMLExprParser;
 import compiler.tmlparser.TokenMgrError;
 import myutil.Conversion;
 import myutil.TraceManager;
+import ui.TAttribute;
+import ui.tmlcompd.TMLCPrimitiveComponent;
+import ui.tmlcompd.TMLCPrimitivePort;
 
 import java.io.StringReader;
 import java.util.ArrayList;
@@ -387,6 +390,18 @@ public class TMLSyntaxChecking {
                 }
             }
 
+            // Checking valid port names
+            // Minh Hiep
+            if (t.getReferenceObject() instanceof TMLCPrimitiveComponent) {
+                TMLCPrimitiveComponent tmlcpc = (TMLCPrimitiveComponent) t.getReferenceObject();
+                for (TMLCPrimitivePort tmlcpp : tmlcpc.getAllInternalPrimitivePorts()) {
+                    String s = tmlcpp.getPortName();
+                    if(!TAttribute.isAValidId(s, false, true, false)) {
+                        addError(t, null, WRONG_VARIABLE_IDENTIFIER + ": invalid port name (" + s + ")", TMLError.ERROR_STRUCTURE);
+                    }
+                }
+            }
+
 
             int n = tactivity.nElements();
             //TraceManager.addDev("Task" + t.getName());
diff --git a/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java b/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
index aa5ddce25c..8ea7bf579e 100755
--- a/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
+++ b/src/main/java/ui/tmlcompd/TMLCPrimitivePort.java
@@ -73,6 +73,8 @@ import ui.interactivesimulation.JFrameSimulationSDPanel;
 
 import javax.swing.*;
 import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Vector;
 
 /**
@@ -159,6 +161,7 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
 
 
     public int verification;
+    public String oldName;
     
     public TMLCPrimitivePort(int _x, int _y, int _minX, int _maxX, int _minY, int _maxY, boolean _pos, TGComponent _father, TDiagramPanel _tdp)  {
         super(_x, _y, _minX, _maxX, _minY, _maxY, _pos, _father, _tdp);
@@ -610,7 +613,7 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
         TraceManager.addDev( "The Data flow type is: " + dataFlowType );
         TraceManager.addDev( "The Associated event is: " + associatedEvent );
 
-        String oldName = getPortName();
+        oldName = getPortName();
         //TraceManager.addDev("old port name : " + oldName);
 
         if (jda.hasNewData()) {
@@ -637,16 +640,16 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
 
                     if (!TAttribute.isAValidId(s, false, true, false)) {
                         JOptionPane.showMessageDialog(frame,
-                                "Could not change the name of the port: the new name is not a valid name",
+                                "Could not change the name of the port: the new name (" + s + ")  is not a valid name",
                                 "Error",
                                 JOptionPane.INFORMATION_MESSAGE);
                         return false;
                     }
 
                     if (oldName.compareTo(s) != 0) {
-                        if (((TMLComponentTaskDiagramPanel) (tdp)).namePrimitivePortInUse(this, s)) {
+                        if (((TMLComponentTaskDiagramPanel) tdp).namePrimitivePortInUse(this, s)) {
                             JOptionPane.showMessageDialog(frame,
-                                    "Error: the name is already in use",
+                                    "Error: the name (" + s + ")  is already in use",
                                     "Name modification",
                                     JOptionPane.ERROR_MESSAGE);
                             return false;
@@ -841,7 +844,24 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
                             }
 
                             if (elt.getTagName().equals("Prop")) {
-                                commName = elt.getAttribute("commName");
+                                // checking the valid port name
+                                oldName = getPortName();
+                                String s = elt.getAttribute("commName");
+                                //TraceManager.addDev("port name : " + s);
+
+                                if ((s != null) && (s.length() > 0)) {
+                                    // Check whether this name is already in use, or not
+
+                                    if (!TAttribute.isAValidId(s, false, false, false)) {
+                                        JOptionPane.showMessageDialog(null,
+                                                "Could not change the name of the port: the new name (" + s + ") is not a valid name",
+                                                "Error : " + s,
+                                                JOptionPane.INFORMATION_MESSAGE);
+                                        setPortName(s);
+                                        makeValue();
+                                    }
+                                    commName = s;
+                                }
                                 try {
                                     //
                                     typep = Integer.decode(elt.getAttribute("commType")).intValue();
@@ -1183,7 +1203,4 @@ public abstract class TMLCPrimitivePort extends TGCScalableWithInternalComponent
             typePort = 2;
         return typePort;
     }
-
-
-
 }
-- 
GitLab