diff --git a/src/main/java/ui/syscams/SysCAMSPrimitiveComponent.java b/src/main/java/ui/syscams/SysCAMSPrimitiveComponent.java new file mode 100644 index 0000000000000000000000000000000000000000..e4cbf3fe2fc3694bb3c00f77efb9e3ee8c523c6d --- /dev/null +++ b/src/main/java/ui/syscams/SysCAMSPrimitiveComponent.java @@ -0,0 +1,534 @@ +/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille + * + * ludovic.apvrille AT enst.fr + * + * This software is a computer program whose purpose is to allow the + * edition of TURTLE analysis, design and deployment diagrams, to + * allow the generation of RT-LOTOS or Java code from this diagram, + * and at last to allow the analysis of formal validation traces + * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP + * from INRIA Rhone-Alpes. + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + +package ui.syscams; + +import myutil.GraphicLib; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import ui.*; +import ui.util.IconManager; +import ui.window.JDialogSysCAMSBlockDE; +import ui.window.JDialogSysCAMSBlockTDF; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Vector; + +/** + * Class SysCAMSPrimitiveComponent + * Primitive Component. To be used in SystemC-AMSdiagrams + * Creation: 12/05/2018 + * @version 1.0 12/05/2018 + * @author Irina Kit Yan LEE + */ + +public class SysCAMSPrimitiveComponent extends TGCScalableWithInternalComponent implements SwallowTGComponent, SwallowedTGComponent, WithAttributes { + private int maxFontSize = 14; + private int minFontSize = 4; + private int currentFontSize = -1; + private boolean displayText = true; + private Color myColor; + + private boolean isAttacker=false; + // Icon + private int iconSize = 15; +// private boolean iconIsDrawn = false; + + // Attributes + public HashMap<String, Integer> attrMap = new HashMap<String, Integer>(); + public String mappingName; + protected LinkedList<TAttribute> myAttributes; + private int textX = 15; // border for ports + private double dtextX = 0.0; + + public String oldValue; + + public SysCAMSPrimitiveComponent(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); + + initScaling(200, 150); + + oldScaleFactor = tdp.getZoom(); + dtextX = textX * oldScaleFactor; + textX = (int)dtextX; + dtextX = dtextX - textX; + + minWidth = 1; + minHeight = 1; + + nbConnectingPoint = 0; + + addTGConnectingPointsComment(); + + nbInternalTGComponent = 0; + + moveable = true; + multieditable = true; + editable = true; + removable = true; + userResizable = true; + + if (this instanceof SysCAMSBlockTDF) { + value = "TDF_Block"; + } else if (this instanceof SysCAMSBlockDE) { + value = "DE_Block"; + } + + name = "Primitive component"; + + myImageIcon = IconManager.imgic1202; + + myAttributes = new LinkedList<TAttribute> (); + + actionOnAdd(); + } + + public void internalDrawing(Graphics g) { + int w; + Font f = g.getFont(); + Font fold = f; + + if (myColor == null) { + if (ColorManager.TML_COMPOSITE_COMPONENT == Color.white) { + myColor = Color.white; + } else { + if (this instanceof SysCAMSBlockTDF) { + myColor = Color.lightGray; + } else { + myColor = Color.white; + } + } + } + if ((rescaled) && (!tdp.isScaled())) { + if (currentFontSize == -1) { + currentFontSize = f.getSize(); + } + rescaled = false; + // Must set the font size .. + // Find the biggest font not greater than max_font size + // By Increment of 1 + // Or decrement of 1 + // If font is less than 4, no text is displayed + + int maxCurrentFontSize = Math.max(0, Math.min(height-(2*textX), maxFontSize)); + f = f.deriveFont((float)maxCurrentFontSize); + g.setFont(f); + while(maxCurrentFontSize > (minFontSize-1)) { + if (g.getFontMetrics().stringWidth(value) < (width - iconSize - (2 * textX))) { + break; + } + maxCurrentFontSize --; + f = f.deriveFont((float)maxCurrentFontSize); + g.setFont(f); + } + currentFontSize = maxCurrentFontSize; + if(currentFontSize <minFontSize) { + displayText = false; + } else { + displayText = true; + f = f.deriveFont((float)currentFontSize); + g.setFont(f); + } + } + + // Zoom is assumed to be computed + Color c = g.getColor(); + g.drawRect(x, y, width, height); + if ((width > 2) && (height > 2)) { + g.setColor(myColor); + g.fillRect(x+1, y+1, width-1, height-1); + g.setColor(c); + } + + // Font size + if (displayText) { + f = f.deriveFont((float)currentFontSize); + g.setFont(f); + w = g.getFontMetrics().stringWidth(value); + if (w > (width - 2 * (iconSize + textX))) { + g.drawString(value, x + textX + 1, y + currentFontSize + textX); + + if (this instanceof SysCAMSBlockTDF) { + String s = "Tm = " + ((SysCAMSBlockTDF) this).getPeriod(); + g.drawString(s, x + textX + 1, y + height - currentFontSize - textX); + } else if (this instanceof SysCAMSBlockDE) { + String s = "Tm = " + ((SysCAMSBlockDE) this).getPeriod(); + g.drawString(s, x + textX + 1, y + height - currentFontSize - textX); + } + } else { + g.drawString(value, x + (width - w)/2, y + currentFontSize + textX); + if (this instanceof SysCAMSBlockTDF) { + String s = "Tm = " + ((SysCAMSBlockTDF) this).getPeriod(); + w = g.getFontMetrics().stringWidth(s); + g.drawString(s, x + (width - w)/2, y + height - currentFontSize - textX); + } else if (this instanceof SysCAMSBlockDE) { + String s = "Tm = " + ((SysCAMSBlockDE) this).getPeriod(); + w = g.getFontMetrics().stringWidth(s); + g.drawString(s, x + (width - w)/2, y + height - currentFontSize - textX); + } + } + } + + // Attributes + if (tdp.areAttributesVisible()) { + int index = 0; + int cpt = currentFontSize + 2 * textX; + String attr; + + TAttribute a; + + int si = Math.min(12, (int)((float)currentFontSize - 2)); + + f = g.getFont(); + f = f.deriveFont((float)si); + g.setFont(f); + int step = si + 2; + + while(index < myAttributes.size()) { + cpt += step; + if (cpt >= (height - textX)) { + break; + } + a = myAttributes.get (index); + attr = a.toString(); + w = g.getFontMetrics().stringWidth(attr); + if ((w + (2 * textX) + 1) < width) { + g.drawString(attr, x + textX, y + cpt); + drawVerification(g, x + textX, y+ cpt, a.getConfidentialityVerification()); + } else { + attr = "..."; + w = g.getFontMetrics().stringWidth(attr); + if ((w + textX + 2) < width) { + g.drawString(attr, x + textX + 1, y + cpt); + } else { + // skip attribute + cpt -= step; + } + } + index ++; + } + } + g.setFont(fold); + } + public void drawVerification(Graphics g, int x, int y, int checkConfStatus){ + Color c = g.getColor(); + Color c1; + switch(checkConfStatus) { + case TAttribute.CONFIDENTIALITY_OK: + c1 = Color.green; + break; + case TAttribute.CONFIDENTIALITY_KO: + c1 = Color.red; + break; + default: + return; + } + g.drawOval(x-10, y-10, 6, 9); + g.setColor(c1); + g.fillRect(x-12, y-5, 9, 7); + g.setColor(c); + g.drawRect(x-12, y-5, 9, 7); + } + + public void rescale(double scaleFactor){ + dtextX = (textX + dtextX) / oldScaleFactor * scaleFactor; + textX = (int)(dtextX); + dtextX = dtextX - textX; + super.rescale(scaleFactor); + } + + public TGComponent isOnOnlyMe(int _x, int _y) { + if (GraphicLib.isInRectangle(_x, _y, x, y, width, height)) { + return this; + } + return null; + } + + + public boolean isAttacker(){ + return isAttacker; + } + + public boolean editOndoubleClick(JFrame frame, int _x, int _y) { + if (this instanceof SysCAMSBlockTDF) { + JDialogSysCAMSBlockTDF jtdf = new JDialogSysCAMSBlockTDF((SysCAMSBlockTDF) this); + jtdf.setVisible(true); + } else { + JDialogSysCAMSBlockDE jde = new JDialogSysCAMSBlockDE((SysCAMSBlockDE) this); + jde.setVisible(true); + } + rescaled = true; + return true; + } + + public Vector<String> getAllRecords() { + return ((SysCAMSComponentTaskDiagramPanel)(tdp)).getAllRecords(this); + } + + public SysCAMSRecordComponent getRecordNamed(String _nameOfRecord) { + return ((SysCAMSComponentTaskDiagramPanel)(tdp)).getRecordNamed(this, _nameOfRecord); + } + + public int getType() { + return TGComponentManager.TMLCTD_PCOMPONENT; + } + + public void wasSwallowed() { + myColor = null; + } + + public void wasUnswallowed() { + myColor = null; + setFather(null); + TDiagramPanel tdp = getTDiagramPanel(); + setCdRectangle(tdp.getMinX(), tdp.getMaxX(), tdp.getMinY(), tdp.getMaxY()); + } + + public boolean acceptSwallowedTGComponent(TGComponent tgc) { + return tgc instanceof SysCAMSPrimitivePort; + } + + public boolean addSwallowedTGComponent(TGComponent tgc, int x, int y) { + //TraceManager.addDev("Add swallow component"); + // Choose its position + // Make it an internal component + // It's one of my son + //Set its coordinates + if (tgc instanceof SysCAMSPrimitivePort) { + tgc.setFather(this); + tgc.setDrawingZone(true); + tgc.resizeWithFather(); + addInternalComponent(tgc, 0); + return true; + } + return false; + } + + public void removeSwallowedTGComponent(TGComponent tgc) { + removeInternalComponent(tgc); + } + + public void hasBeenResized() { + rescaled = true; + for(int i=0; i<nbInternalTGComponent; i++) { + if (tgcomponent[i] instanceof SysCAMSPrimitivePort) { + tgcomponent[i].resizeWithFather(); + } + } + if (getFather() != null) { + resizeWithFather(); + } + } + + public void resizeWithFather() { + if ((father != null) && (father instanceof SysCAMSCompositeComponent)) { + // Too large to fit in the father? -> resize it! + resizeToFatherSize(); + + setCdRectangle(0, father.getWidth() - getWidth(), 0, father.getHeight() - getHeight()); + setMoveCd(x, y); + } + } + + public int getChildCount() { + return myAttributes.size() + nbInternalTGComponent + 1; + } + + public Object getChild(int index) { + if (index == 0) { + return value; + } else { + if (index <= myAttributes.size()) { + return myAttributes.get (index-1); + } else { + return tgcomponent[index-1-myAttributes.size()]; + } + } + } + + public int getIndexOfChild(Object child) { + if (child instanceof String) { + return 0; + } else { + if (myAttributes.indexOf(child) > -1) { + return myAttributes.indexOf(child) + 1; + } else { + for(int i=0; i<nbInternalTGComponent; i++) { + if (tgcomponent[i] == child) { + return myAttributes.size() + 1 + i; + } + } + } + } + return -1; + } + + protected String translateExtraParam() { + TAttribute a; + StringBuffer sb = new StringBuffer("<extraparam>\n"); + sb.append("<Data isAttacker=\""); + sb.append(isAttacker() ? "Yes": "No"); + sb.append("\" />\n"); + for(int i=0; i<myAttributes.size(); i++) { + a = myAttributes.get (i); + sb.append("<Attribute access=\""); + sb.append(a.getAccess()); + sb.append("\" id=\""); + sb.append(a.getId()); + sb.append("\" value=\""); + sb.append(a.getInitialValue()); + sb.append("\" type=\""); + sb.append(a.getType()); + sb.append("\" typeOther=\""); + sb.append(a.getTypeOther()); + sb.append("\" />\n"); + } + sb.append("</extraparam>\n"); + return new String(sb); + } + + public void loadExtraParam(NodeList nl, int decX, int decY, int decId) throws MalformedModelingException{ + try { + NodeList nli; + Node n1, n2; + Element elt; + int access, type; + String typeOther; + String id, valueAtt; + + for(int i=0; i<nl.getLength(); i++) { + n1 = nl.item(i); + if (n1.getNodeType() == Node.ELEMENT_NODE) { + nli = n1.getChildNodes(); + for(int j=0; j<nli.getLength(); j++) { + n2 = nli.item(j); + if (n2.getNodeType() == Node.ELEMENT_NODE) { + elt = (Element) n2; + if (elt.getTagName().equals("Data")) { + isAttacker = elt.getAttribute("isAttacker").equals("Yes"); + } + if (elt.getTagName().equals("Attribute")) { + access = Integer.decode(elt.getAttribute("access")).intValue(); + type = Integer.decode(elt.getAttribute("type")).intValue(); + try { + typeOther = elt.getAttribute("typeOther"); + } catch (Exception e) { + typeOther = ""; + } + id = elt.getAttribute("id"); + valueAtt = elt.getAttribute("value"); + + if (valueAtt.equals("null")) { + valueAtt = ""; + } + if ((TAttribute.isAValidId(id, false, false)) && (TAttribute.isAValidInitialValue(type, valueAtt))) { + TAttribute ta = new TAttribute(access, id, valueAtt, type, typeOther); + myAttributes.add (ta); + } + } + } + } + } + } + } catch (Exception e) { + throw new MalformedModelingException(); + } + } + + public int getCurrentFontSize() { + return currentFontSize; + } + + public java.util.List<TAttribute> getAttributeList() { + return myAttributes; + } + + public java.util.List<SysCAMSPrimitivePort> getAllTDFOriginPorts() { + return getAllPorts(0, true); + } + + public java.util.List<SysCAMSPrimitivePort> getAllTDFDestinationPorts() { + return getAllPorts(0, false); + } + + public java.util.List<SysCAMSPrimitivePort> getAllDEOriginPorts() { + return getAllPorts(1, true); + } + + public java.util.List<SysCAMSPrimitivePort> getAllDEDestinationPorts() { + return getAllPorts(1, false); + } + + public java.util.List<SysCAMSPrimitivePort> getAllPorts(int _type, boolean _isOrigin) { + java.util.List<SysCAMSPrimitivePort> ret = new LinkedList<SysCAMSPrimitivePort>(); + SysCAMSPrimitivePort port; + + for(int i=0; i<nbInternalTGComponent; i++) { + if (tgcomponent[i] instanceof SysCAMSPrimitivePort) { + port = (SysCAMSPrimitivePort)tgcomponent[i]; + if ((port.getPortType() == _type) && (port.isOrigin() == _isOrigin)) { + ret.add(port); + } + } + } + return ret; + } + + public java.util.List<SysCAMSPrimitivePort> getAllInternalPrimitivePorts() { + java.util.List<SysCAMSPrimitivePort> list = new ArrayList<SysCAMSPrimitivePort>(); + for(int i=0; i<nbInternalTGComponent; i++) { + if (tgcomponent[i] instanceof SysCAMSPrimitivePort) { + list.add((SysCAMSPrimitivePort)(tgcomponent[i])); + } + } + return list; + } + + public String getAttributes() { + String attr = ""; + for (TAttribute a: myAttributes) { + attr += a.toAvatarString() + "\n"; + } + return attr; + } +} \ No newline at end of file