]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java
Prevent NPEs in ViewUtils layout loading code
[simantics/platform.git] / bundles / org.simantics.views / src / org / simantics / views / ViewUtils.java
index c0a6072875046719f6b7b79864ad3df3272f41c6..9e6e8b6ff0ab759171b1a60e80820485f37fff81 100644 (file)
@@ -88,9 +88,21 @@ public class ViewUtils {
                public int weight;
                
        }
-       
 
-       
+       private static void readMargins(ExtendedMargins extendedMargins, LayoutBean layout) {
+               if (extendedMargins != null) {
+                       layout.marginLeft = extendedMargins.left;
+                       layout.marginRight = extendedMargins.right;
+                       layout.marginTop = extendedMargins.top;
+                       layout.marginBottom = extendedMargins.bottom;
+               } else {
+                       layout.marginLeft = 3; 
+                       layout.marginRight = 3;
+                       layout.marginTop = 3;
+                       layout.marginBottom = 3;
+               }
+       }
+
        public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
 
                return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
@@ -106,13 +118,10 @@ public class ViewUtils {
                                ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
                                
                                GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
-                               layout.numColumns = columns;
-                               layout.horizontalSpacing = horizontalSpacing;
-                               layout.verticalSpacing = verticalSpacing;
-                               layout.marginLeft = extendedMargins.left;
-                               layout.marginRight = extendedMargins.right;
-                               layout.marginTop = extendedMargins.top;
-                               layout.marginBottom = extendedMargins.bottom;
+                               layout.numColumns = columns != null ? columns : 1;
+                               layout.horizontalSpacing = horizontalSpacing != null ? horizontalSpacing : 5;
+                               layout.verticalSpacing = verticalSpacing != null ? verticalSpacing : 5;
+                               readMargins(extendedMargins, layout);
 
                                return layout;
 
@@ -163,17 +172,14 @@ public class ViewUtils {
                    ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
 
                    RowLayoutBean layout = new RowLayoutBean();
-                   layout.type = type;
-                   layout.spacing = spacing;
-                layout.center = center;
-                layout.fill = fill;
-                layout.justify = justify;
-                   layout.pack = pack;
-                   layout.wrap = wrap;
-                   layout.marginLeft = extendedMargins.left;
-                   layout.marginRight = extendedMargins.right;
-                   layout.marginTop = extendedMargins.top;
-                   layout.marginBottom = extendedMargins.bottom;
+                   layout.type = type != null ? type : 256;
+                   layout.spacing = spacing != null ? spacing : 3;
+                layout.center = center != null ? center : false;
+                layout.fill = fill != null ? fill : false;
+                layout.justify = justify != null ? justify : false;
+                   layout.pack = pack != null ? pack : true;
+                   layout.wrap = wrap != null ? wrap : true;
+                   readMargins(extendedMargins, layout);
                    return layout;
                }
            });
@@ -185,8 +191,10 @@ public class ViewUtils {
                public RowDataBean perform(ReadGraph graph) throws DatabaseException {
                    ViewsResources VIEW = ViewsResources.getInstance(graph);
                    RowDataBean data = new RowDataBean();
-                   data.width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
-                   data.height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
+                   Integer width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
+                   Integer height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
+                   data.width = width != null ? width : -1;
+                   data.height = height != null ? height : -1;
                    return data;
                }
            });
@@ -235,10 +243,10 @@ public class ViewUtils {
                                bean.key = key;
                                bean.label = label;
                                bean.alignment = alignment;
-                               bean.width = width;
+                               bean.width = width != null ? width : -1;
                                bean.tooltip = tooltip;
-                               bean.grab = grab;
-                               bean.weight = weight;
+                               bean.grab = grab != null ? grab : false;
+                               bean.weight = weight != null ? weight : 0;
                                
                                return bean;