Fix diagram profiles to work with latest DB changes
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / TextGridStyle.java
index 54972155d8d69579294e4d6e49718a453e14e66d..997ace1d63f12a87db7fc6fcaaec5f630fc92be3 100644 (file)
@@ -38,17 +38,15 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
        private final Color BACKGROUND_COLOR = new Color(255, 255, 255, 192);
        private static final Rectangle2D EMPTY_BOUNDS = new Rectangle2D.Double(0, 0, 0, 0);
 
-       // NOTE: this is a hack
-       String id;
-
        protected double xOffset;
        protected double yOffset;
 
-       public TextGridStyle() {
-               this(0.0, 2.1);
+       public TextGridStyle(Resource r) {
+               this(r, 0.0, 2.1);
        }
 
-       public TextGridStyle(double xOffset, double yOffset) {
+       public TextGridStyle(Resource r, double xOffset, double yOffset) {
+           super(r);
                this.xOffset = xOffset;
                this.yOffset = yOffset;
        }
@@ -175,29 +173,32 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
 
                        // This assumes that this TextGridStyle instance will be devoted to
                        // this row ID until the end of its life.
-                       String id = result.getRowId();
-                       //System.out.println(this + " ID: " + id);
-                       if (!id.equals(this.id)) {
-                               //System.out.println(this + " SET ID: " + this.id + " -> " + id);
-                               this.id = id;
-                       }
+//                     String id = result.getRowId();
+//                     System.out.println(this + " ID: " + id);
+//                     if (!id.equals(this.id)) {
+//                             System.out.println(this + " SET ID: " + this.id + " -> " + id);
+//                             this.id = id;
+//                     }
 
                        Integer newRow = observer.getTemporaryProperty(_node, "location");
                        if (newRow == null)
                                newRow = 1;
 
                        // Remove from existing row to add to another row if necessary.
-                       Integer row = observer.getProperty(_node, id);
+                       Integer row = getCurrentRowNumber(observer, _node);
                        if (row != null && row != newRow) {
                                String actualId = node.getRowId(row);
+                               String id = observer.getProperty(_node, rowIdKey());
                                if (id.equals(actualId)) {
                                        node.removeRow(row);
                                }
                        }
                        row = newRow;
 
-                       node.setRowId(row, id);
-                       observer.setProperty(_node, id, row);
+                       node.setRowId(row, result.getRowId());
+
+                       setCurrentRowNumber(observer, _node, result.getRowId(), row);
+
                        observer.setTemporaryProperty(_node, "location", row + 1);
 
                        node.setText(2, row, value2);
@@ -361,18 +362,45 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
 
        @Override
        protected void cleanupStyleForNode(EvaluationContext observer, INode _node) {
-               Integer row = observer.getProperty(_node, id);
+               Integer row = getCurrentRowNumber(observer, _node);
                //System.out.println(this + " cleanup(" + id + ", " + row + ")");
                //System.out.println(element);
                if (row == null)
                        return;
-               observer.setProperty(_node, id, null);
+               clearCurrentRowNumber(observer, _node);
                TextGridNode node = ProfileVariables.browseChild(_node, "TextGridStyle");
                if (node != null)
                        node.removeRow(row);
        }
 
+       private Integer getCurrentRowNumber(EvaluationContext observer, INode _node) {
+               String rowId = observer.getProperty(_node, rowIdKey());
+               return observer.getProperty(_node, rowId);
+       }
+
+       private void setCurrentRowNumber(EvaluationContext observer, INode _node, String rowId, int row) {
+               // Mapping style identity -> rowId (resourceId)
+               observer.setProperty(_node, rowIdKey(), rowId);
+               // Mapping rowId (resourceId) -> row number
+               observer.setProperty(_node, rowId, row);
+       }
+
+       private void clearCurrentRowNumber(EvaluationContext observer, INode _node) {
+               String rowId = observer.getProperty(_node, rowIdKey());
+               if(rowId != null) {
+                       observer.setProperty(_node, rowIdKey(), null);
+                       Integer row = observer.getProperty(_node, rowId);
+                       if(row != null) {
+                               observer.setProperty(_node, rowId, null);
+                       }
+               }
+       }
+
        protected void postProcessNode(TextGridNode node, int row) {
        }
 
+       private String rowIdKey() {
+               return "style" + getIdentity().toString();
+       }
+
 }