Graphics: avoid useless operations 35/10635/2
Calixte DENIZET [Thu, 28 Feb 2013 12:18:42 +0000 (13:18 +0100)]
Change-Id: Id4e4a7f1c96c5539b6934220f85bf3a128690389

scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/textObject/TextObject.java
scilab/modules/renderer/src/java/org/scilab/modules/renderer/JoGLView/DrawerVisitor.java

index e80e41d..9c00907 100644 (file)
@@ -28,290 +28,304 @@ import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProp
  * @author Manuel JULIACHS
  */
 public abstract class TextObject extends ContouredObject {
-       /** TextObject properties names */
-       protected enum TextObjectProperty { TEXT, TEXT_ARRAY_DIMENSIONS };
-
-       /**
-        * Formatted text objects array, row-major order.
-        * For now, elements are supposed to have identical font properties values,
-        * hence the current associated get/set methods implementation.
-        */
-       protected FormattedText[] text;
-
-       /** Text array dimensions (number of rows, number of columns) */
-       private int[] dimensions;
-
-       /** Constructor */
-       public TextObject() {
-               super();
-
-               dimensions = new int[2];
-               dimensions[0] = 1;
-               dimensions[1] = 1;
-
-               text = new FormattedText[1];
-               text[0] = new FormattedText();
-       }
-
-       /**
-        * Clone method
-        */
-        public TextObject clone() {
-               TextObject copy = (TextObject) super.clone();
-
-               copy.dimensions = new int[2];
-               copy.dimensions[0] = dimensions[0];
-               copy.dimensions[1] = dimensions[1];
-               copy.text = new FormattedText[dimensions[0]*dimensions[1]];
-
-               /* Actually copies the FormattedText objects */
-               copy.setText(text);
-
-               return copy;
-       }
-
-       /**
-        * Returns the enum associated to a property name
-        * @param propertyName the property name
-        * @return the property enum
-        */
-       public Object getPropertyFromName(int propertyName) {
-               switch (propertyName) {
-               case __GO_FORMATTED_TEXT__ :
-                       return TextObjectProperty.TEXT;
-               case __GO_TEXT_ARRAY_DIMENSIONS__ :
-                       return TextObjectProperty.TEXT_ARRAY_DIMENSIONS;
-               case __GO_TEXT_STRINGS__ :
-                       return FormattedText.FormattedTextProperty.TEXT;
-               case __GO_FONT_STYLE__ :
-                       return Font.FontProperty.STYLE;
-               case __GO_FONT_SIZE__ :
-                       return Font.FontProperty.SIZE;
-               case __GO_FONT_COLOR__ :
-                       return Font.FontProperty.COLOR;
-               case __GO_FONT_FRACTIONAL__ :
-                       return Font.FontProperty.FRACTIONAL;
-               default :
-                       return super.getPropertyFromName(propertyName);
-               }
-       }
-
-       /**
-        * Fast property get method
-        * @param property the property to get
-        * @return the property value
-        */
-       public Object getProperty(Object property) {
-               if (property == TextObjectProperty.TEXT) {
-                       return getText();
-               } else if (property == TextObjectProperty.TEXT_ARRAY_DIMENSIONS) {
-                       return getTextArrayDimensions();
-               } else if (property == FormattedText.FormattedTextProperty.TEXT) {
-                       return getTextStrings();
-               } else if (property == Font.FontProperty.STYLE) {
-                       return getFontStyle();
-               } else if (property == Font.FontProperty.SIZE) {
-                       return getFontSize();
-               } else if (property == Font.FontProperty.COLOR) {
-                       return getFontColor();
-               } else if (property == Font.FontProperty.FRACTIONAL) {
-                       return getFontFractional();
-               } else {
-                       return super.getProperty(property);     
-               }
-       }
-       
-       /**
-        * Fast property set method
-        * @param property the property to set
-        * @param value the property value
-        * @return true if the property has been set, false otherwise
-        */
-       public UpdateStatus setProperty(Object property, Object value) {
-               if (property == TextObjectProperty.TEXT) {
-                       setText((FormattedText[]) value);
-               } else if (property == TextObjectProperty.TEXT_ARRAY_DIMENSIONS) {
-                       setTextArrayDimensions((Integer[]) value);
-               } else if (property == FormattedText.FormattedTextProperty.TEXT) {
-                       setTextStrings((String[]) value);
-               } else if (property == Font.FontProperty.STYLE) {
-                       setFontStyle((Integer) value);
-               } else if (property == Font.FontProperty.SIZE) {
-                       setFontSize((Double) value);
-               } else if (property == Font.FontProperty.COLOR) {
-                       setFontColor((Integer) value);
-               } else if (property == Font.FontProperty.FRACTIONAL) {
-                       setFontFractional((Boolean) value);
-               } else {
-                       return super.setProperty(property, value);
-               }
-
-               return UpdateStatus.Success;
-       }
-
-       /**
-        * @return the text array dimensions
-        */
-       public Integer[] getTextArrayDimensions() {
-               Integer retDimensions[] = new Integer[2];
-               retDimensions[0] = dimensions[0];
-               retDimensions[1] = dimensions[1];
-
-               return retDimensions;
-       }
-
-       /**
-        * Sets the text array dimensions
-        * Recreates an array of formatted text objects.
-        * If there was an existing array, the new array's objects' font properties
-        * are set to those of the existing array's first element, as all the array's objects
-        * are supposed to have the same font properties for now.
-        *
-        * @param dimensions the text array dimensions to set
-        */
-       public void setTextArrayDimensions(Integer[] dimensions) {
-               int currentSize = this.dimensions[0]*this.dimensions[1];
-
-               if (dimensions[0]*dimensions[1] != currentSize) {
-                       FormattedText[] newText = new FormattedText[dimensions[0]*dimensions[1]];
-
-                       for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                               newText[i] = new FormattedText();
-
-                               if(currentSize >= 1) {
-                                       newText[i].setFont(new Font(text[0].getFont()));
-                               }
-                       }
-
-                       text = newText;
-               }
-
-               this.dimensions[0] = dimensions[0];
-               this.dimensions[1] = dimensions[1];
-       }
-
-       /**
-        * @return the text
-        */
-       public FormattedText[] getText() {
-               FormattedText[] retText = new FormattedText[dimensions[0]*dimensions[1]];
-
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       retText[i] = new FormattedText(text[i]);
-               }
-
-               return retText;
-       }
-
-       /**
-        * @param textArray the textArray to set
-        */
-       public void setText(FormattedText[] textArray) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i] = new FormattedText(textArray[i]);
-               }
-       }
-
-       /**
-        * @return the text strings
-        */
-       public String[] getTextStrings() {
-               String[] textStrings = new String[dimensions[0]*dimensions[1]];
-
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       textStrings[i] = new String(text[i].getText());
-               }
-
-               return textStrings;
-       }
-
-       /**
-        * @param textStrings the text strings array to set
-        */
-       public void setTextStrings(String[] textStrings) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].setText(textStrings[i]);
-               }
-       }
-
-       /**
-        * @return the font
-        */
-       public Font getFont() {
-               return text[0].getFont();
-       }
-
-       /**
-        * @param font the font to set
-        */
-       public void setFont(Font font) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].setFont(font);
-               }
-       }
-
-       /**
-        * @return the font style
-        */
-       public Integer getFontStyle() {
-               return text[0].getFont().getStyle();
-       }
-
-       /**
-        * @param style the font style to set
-        */
-       public void setFontStyle(Integer style) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].getFont().setStyle(style);
-               }
-       }
-
-       /**
-        * @return the font color
-        */
-       public Integer getFontColor() {
-               return text[0].getFont().getColor();
-       }
-
-       /**
-        * @param color the font color to set 
-        */
-       public void setFontColor(Integer color) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].getFont().setColor(color);
-               }
-       }
-
-       /**
-        * @return the font size
-        */
-       public Double getFontSize() {
-               return text[0].getFont().getSize();
-       }
-
-       /**
-        * @param size the font size to set
-        */
-       public void setFontSize(Double size) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].getFont().setSize(size);
-               }
-       }
-
-       /**
-        * @return the font fractional
-        */
-       public Boolean getFontFractional() {
-               return text[0].getFont().getFractional();
-       }
-
-       /**
-        * @param fractional the font fractional to set
-        */
-       public void setFontFractional(Boolean fractional) {
-               for (int i = 0; i < dimensions[0]*dimensions[1]; i++) {
-                       text[i].getFont().setFractional(fractional);
-               }
-       }
+    /** TextObject properties names */
+    protected enum TextObjectProperty { TEXT, TEXT_ARRAY_DIMENSIONS };
+
+    /**
+     * Formatted text objects array, row-major order.
+     * For now, elements are supposed to have identical font properties values,
+     * hence the current associated get/set methods implementation.
+     */
+    protected FormattedText[] text;
+
+    /** Text array dimensions (number of rows, number of columns) */
+    private int[] dimensions;
+
+    /** Constructor */
+    public TextObject() {
+        super();
+
+        dimensions = new int[2];
+        dimensions[0] = 1;
+        dimensions[1] = 1;
+
+        text = new FormattedText[1];
+        text[0] = new FormattedText();
+    }
+
+    /**
+     * Clone method
+     */
+    public TextObject clone() {
+        TextObject copy = (TextObject) super.clone();
+
+        copy.dimensions = new int[2];
+        copy.dimensions[0] = dimensions[0];
+        copy.dimensions[1] = dimensions[1];
+        copy.text = new FormattedText[dimensions[0] * dimensions[1]];
+
+        /* Actually copies the FormattedText objects */
+        copy.setText(text);
+
+        return copy;
+    }
+
+    /**
+     * @return true if we have an empty label
+     */
+    public boolean isEmpty() {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            String s = text[i].getText();
+            if (s != null && !s.isEmpty()) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * Returns the enum associated to a property name
+     * @param propertyName the property name
+     * @return the property enum
+     */
+    public Object getPropertyFromName(int propertyName) {
+        switch (propertyName) {
+            case __GO_FORMATTED_TEXT__ :
+                return TextObjectProperty.TEXT;
+            case __GO_TEXT_ARRAY_DIMENSIONS__ :
+                return TextObjectProperty.TEXT_ARRAY_DIMENSIONS;
+            case __GO_TEXT_STRINGS__ :
+                return FormattedText.FormattedTextProperty.TEXT;
+            case __GO_FONT_STYLE__ :
+                return Font.FontProperty.STYLE;
+            case __GO_FONT_SIZE__ :
+                return Font.FontProperty.SIZE;
+            case __GO_FONT_COLOR__ :
+                return Font.FontProperty.COLOR;
+            case __GO_FONT_FRACTIONAL__ :
+                return Font.FontProperty.FRACTIONAL;
+            default :
+                return super.getPropertyFromName(propertyName);
+        }
+    }
+
+    /**
+     * Fast property get method
+     * @param property the property to get
+     * @return the property value
+     */
+    public Object getProperty(Object property) {
+        if (property == TextObjectProperty.TEXT) {
+            return getText();
+        } else if (property == TextObjectProperty.TEXT_ARRAY_DIMENSIONS) {
+            return getTextArrayDimensions();
+        } else if (property == FormattedText.FormattedTextProperty.TEXT) {
+            return getTextStrings();
+        } else if (property == Font.FontProperty.STYLE) {
+            return getFontStyle();
+        } else if (property == Font.FontProperty.SIZE) {
+            return getFontSize();
+        } else if (property == Font.FontProperty.COLOR) {
+            return getFontColor();
+        } else if (property == Font.FontProperty.FRACTIONAL) {
+            return getFontFractional();
+        } else {
+            return super.getProperty(property);
+        }
+    }
+
+    /**
+     * Fast property set method
+     * @param property the property to set
+     * @param value the property value
+     * @return true if the property has been set, false otherwise
+     */
+    public UpdateStatus setProperty(Object property, Object value) {
+        if (property == TextObjectProperty.TEXT) {
+            setText((FormattedText[]) value);
+        } else if (property == TextObjectProperty.TEXT_ARRAY_DIMENSIONS) {
+            setTextArrayDimensions((Integer[]) value);
+        } else if (property == FormattedText.FormattedTextProperty.TEXT) {
+            setTextStrings((String[]) value);
+        } else if (property == Font.FontProperty.STYLE) {
+            setFontStyle((Integer) value);
+        } else if (property == Font.FontProperty.SIZE) {
+            setFontSize((Double) value);
+        } else if (property == Font.FontProperty.COLOR) {
+            setFontColor((Integer) value);
+        } else if (property == Font.FontProperty.FRACTIONAL) {
+            setFontFractional((Boolean) value);
+        } else {
+            return super.setProperty(property, value);
+        }
+
+        return UpdateStatus.Success;
+    }
+
+    /**
+     * @return the text array dimensions
+     */
+    public Integer[] getTextArrayDimensions() {
+        Integer retDimensions[] = new Integer[2];
+        retDimensions[0] = dimensions[0];
+        retDimensions[1] = dimensions[1];
+
+        return retDimensions;
+    }
+
+    /**
+     * Sets the text array dimensions
+     * Recreates an array of formatted text objects.
+     * If there was an existing array, the new array's objects' font properties
+     * are set to those of the existing array's first element, as all the array's objects
+     * are supposed to have the same font properties for now.
+     *
+     * @param dimensions the text array dimensions to set
+     */
+    public void setTextArrayDimensions(Integer[] dimensions) {
+        int currentSize = this.dimensions[0] * this.dimensions[1];
+
+        if (dimensions[0] * dimensions[1] != currentSize) {
+            FormattedText[] newText = new FormattedText[dimensions[0] * dimensions[1]];
+
+            for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+                newText[i] = new FormattedText();
+
+                if (currentSize >= 1) {
+                    newText[i].setFont(new Font(text[0].getFont()));
+                }
+            }
+
+            text = newText;
+        }
+
+        this.dimensions[0] = dimensions[0];
+        this.dimensions[1] = dimensions[1];
+    }
+
+    /**
+     * @return the text
+     */
+    public FormattedText[] getText() {
+        FormattedText[] retText = new FormattedText[dimensions[0] * dimensions[1]];
+
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            retText[i] = new FormattedText(text[i]);
+        }
+
+        return retText;
+    }
+
+    /**
+     * @param textArray the textArray to set
+     */
+    public void setText(FormattedText[] textArray) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i] = new FormattedText(textArray[i]);
+        }
+    }
+
+    /**
+     * @return the text strings
+     */
+    public String[] getTextStrings() {
+        String[] textStrings = new String[dimensions[0] * dimensions[1]];
+
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            textStrings[i] = new String(text[i].getText());
+        }
+
+        return textStrings;
+    }
+
+    /**
+     * @param textStrings the text strings array to set
+     */
+    public void setTextStrings(String[] textStrings) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].setText(textStrings[i]);
+        }
+    }
+
+    /**
+     * @return the font
+     */
+    public Font getFont() {
+        return text[0].getFont();
+    }
+
+    /**
+     * @param font the font to set
+     */
+    public void setFont(Font font) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].setFont(font);
+        }
+    }
+
+    /**
+     * @return the font style
+     */
+    public Integer getFontStyle() {
+        return text[0].getFont().getStyle();
+    }
+
+    /**
+     * @param style the font style to set
+     */
+    public void setFontStyle(Integer style) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].getFont().setStyle(style);
+        }
+    }
+
+    /**
+     * @return the font color
+     */
+    public Integer getFontColor() {
+        return text[0].getFont().getColor();
+    }
+
+    /**
+     * @param color the font color to set
+     */
+    public void setFontColor(Integer color) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].getFont().setColor(color);
+        }
+    }
+
+    /**
+     * @return the font size
+     */
+    public Double getFontSize() {
+        return text[0].getFont().getSize();
+    }
+
+    /**
+     * @param size the font size to set
+     */
+    public void setFontSize(Double size) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].getFont().setSize(size);
+        }
+    }
+
+    /**
+     * @return the font fractional
+     */
+    public Boolean getFontFractional() {
+        return text[0].getFont().getFractional();
+    }
+
+    /**
+     * @param fractional the font fractional to set
+     */
+    public void setFontFractional(Boolean fractional) {
+        for (int i = 0; i < dimensions[0] * dimensions[1]; i++) {
+            text[i].getFont().setFractional(fractional);
+        }
+    }
 
 }
index 795b3f1..e418cfa 100644 (file)
@@ -85,35 +85,35 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
 
     /** Set of properties changed during a draw if auto-ticks is on for X axis. */
     private static final Set<Integer> X_AXIS_TICKS_PROPERTIES = new HashSet<Integer>(Arrays.asList(
-                                                                                         GraphicObjectProperties.__GO_X_AXIS_TICKS_LOCATIONS__,
-                                                                                         GraphicObjectProperties.__GO_X_AXIS_TICKS_LABELS__,
-                                                                                         GraphicObjectProperties.__GO_X_AXIS_SUBTICKS__
-                                                                                         ));
+                GraphicObjectProperties.__GO_X_AXIS_TICKS_LOCATIONS__,
+                GraphicObjectProperties.__GO_X_AXIS_TICKS_LABELS__,
+                GraphicObjectProperties.__GO_X_AXIS_SUBTICKS__
+            ));
 
     /** Set of properties changed during a draw if auto-ticks is on for Y axis. */
     private static final Set<Integer> Y_AXIS_TICKS_PROPERTIES = new HashSet<Integer>(Arrays.asList(
-                                                                                         GraphicObjectProperties.__GO_Y_AXIS_TICKS_LOCATIONS__,
-                                                                                         GraphicObjectProperties.__GO_Y_AXIS_TICKS_LABELS__,
-                                                                                         GraphicObjectProperties.__GO_Y_AXIS_SUBTICKS__
-                                                                                         ));
+                GraphicObjectProperties.__GO_Y_AXIS_TICKS_LOCATIONS__,
+                GraphicObjectProperties.__GO_Y_AXIS_TICKS_LABELS__,
+                GraphicObjectProperties.__GO_Y_AXIS_SUBTICKS__
+            ));
 
     /** Set of properties changed during a draw if auto-ticks is on for Z axis. */
     private static final Set<Integer> Z_AXIS_TICKS_PROPERTIES = new HashSet<Integer>(Arrays.asList(
-                                                                                         GraphicObjectProperties.__GO_Z_AXIS_TICKS_LOCATIONS__,
-                                                                                         GraphicObjectProperties.__GO_Z_AXIS_TICKS_LABELS__,
-                                                                                         GraphicObjectProperties.__GO_Z_AXIS_SUBTICKS__
-                                                                                         ));
+                GraphicObjectProperties.__GO_Z_AXIS_TICKS_LOCATIONS__,
+                GraphicObjectProperties.__GO_Z_AXIS_TICKS_LABELS__,
+                GraphicObjectProperties.__GO_Z_AXIS_SUBTICKS__
+            ));
 
     /** Set of figure properties for witch a change doesn't lead to a redraw */
     private static final Set<Integer> SILENT_FIGURE_PROPERTIES = new HashSet<Integer>(Arrays.asList(
-                                                                                          GraphicObjectProperties.__GO_ROTATION_TYPE__,
-                                                                                          GraphicObjectProperties.__GO_INFO_MESSAGE__,
-                                                                                          GraphicObjectProperties.__GO_FIGURE_NAME__,
-                                                                                          GraphicObjectProperties.__GO_AUTORESIZE__,
-                                                                                          GraphicObjectProperties.__GO_POSITION__,
-                                                                                          GraphicObjectProperties.__GO_SIZE__,
-                                                                                          GraphicObjectProperties.__GO_ID__
-                                                                                          ));
+                GraphicObjectProperties.__GO_ROTATION_TYPE__,
+                GraphicObjectProperties.__GO_INFO_MESSAGE__,
+                GraphicObjectProperties.__GO_FIGURE_NAME__,
+                GraphicObjectProperties.__GO_AUTORESIZE__,
+                GraphicObjectProperties.__GO_POSITION__,
+                GraphicObjectProperties.__GO_SIZE__,
+                GraphicObjectProperties.__GO_ID__
+            ));
 
     private static final boolean DEBUG_MODE = false;
 
@@ -444,7 +444,7 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
 
     @Override
     public void visit(Label label) {
-        if (label.isValid() && label.getVisible()) {
+        if (label.isValid() && label.getVisible() && !label.isEmpty()) {
             try {
                 labelManager.draw(drawingTools, colorMap, label, axesDrawer);
             } catch (SciRendererException e) {
@@ -826,7 +826,6 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
     public void updateObject(String id, int property) {
         try {
             if (needUpdate(id, property)) {
-                GraphicController.getController().setProperty(id, GraphicObjectProperties.__GO_VALID__, true);
                 if (GraphicObjectProperties.__GO_COLORMAP__ == property && figure.getIdentifier().equals(id)) {
                     labelManager.disposeAll();
                     dataManager.disposeAllColorBuffers();
@@ -881,8 +880,8 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
         GraphicObject object = GraphicController.getController().getObjectFromId(id);
         int objectType = (Integer) GraphicController.getController().getProperty(id, GraphicObjectProperties.__GO_TYPE__);
         if ((object != null) && isFigureChild(id)
-            && objectType != GraphicObjectProperties.__GO_UICONTROL__
-            && objectType !=GraphicObjectProperties.__GO_UIMENU__) {
+                && objectType != GraphicObjectProperties.__GO_UICONTROL__
+                && objectType != GraphicObjectProperties.__GO_UIMENU__) {
 
             if (GraphicObjectProperties.__GO_VALID__ == property) {
                 return false;
@@ -909,6 +908,10 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
                 }
             }
 
+            if (!object.isValid()) {
+                GraphicController.getController().setProperty(id, GraphicObjectProperties.__GO_VALID__, true);
+            }
+
             return true;
         } else {
             return false;
@@ -958,10 +961,10 @@ public class DrawerVisitor implements Visitor, Drawer, GraphicView {
         } else {
             try {
                 SwingUtilities.invokeAndWait(new Runnable() {
-                        public void run() {
-                            canvas.destroy();
-                        }
-                    });
+                    public void run() {
+                        canvas.destroy();
+                    }
+                });
             } catch (Exception e) { }
         }
     }