Changeset 5258

Show
Ignore:
Timestamp:
08/25/08 10:51:54 (3 months ago)
Author:
risto.yrjana@…
Message:

Changed DOM-structure: caption positioning broken, component growth fixed

Location:
incubator/widgets/coordinatelayout/src/com/itmill/toolkit
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • incubator/widgets/coordinatelayout/src/com/itmill/toolkit/tests/tickets/Ticket1267.java

    r5215 r5258  
    1414import com.itmill.toolkit.ui.GridLayout; 
    1515import com.itmill.toolkit.ui.Label; 
     16import com.itmill.toolkit.ui.Layout; 
    1617import com.itmill.toolkit.ui.OrderedLayout; 
    1718import com.itmill.toolkit.ui.Panel; 
     19import com.itmill.toolkit.ui.Select; 
    1820import com.itmill.toolkit.ui.Slider; 
    1921import com.itmill.toolkit.ui.Table; 
     
    2325public class Ticket1267 extends Application { 
    2426 
    25     final CoordinateLayout layout = new CoordinateLayout(); 
     27    final CoordinateLayout coordinateLayout = new CoordinateLayout(); 
    2628 
    2729    public void init() { 
     
    3234        // SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL); 
    3335 
    34         layout.setBorders(true); 
     36        coordinateLayout.setMargin(true); 
     37        coordinateLayout.setMargin(true, true, true, true); 
    3538 
    3639        // Setup table 
     
    6063        } 
    6164 
    62         layout.addComponent(table); 
    63         table.setSizeFull(); 
    64         layout.setCoordinates(table, new CoordinateLayout.Coordinates( 
    65                 "0,0,0,0,-1,-1")); 
     65        coordinateLayout.addComponent(table); 
     66        coordinateLayout.setCoordinates(table, 
     67                new CoordinateLayout.Coordinates("0,0,-1,-1")); 
    6668 
    6769        CoordinateLayout.Coordinates xy = new CoordinateLayout.Coordinates(40, 
     
    6971        xy.setUnitsPercent(true, true, true, true, false, false); 
    7072 
    71         layout.addComponent( 
    72                 new TextField("Caption", "TextField with a caption"), xy); 
     73        coordinateLayout.addComponent(new TextField("Caption", 
     74                "TextField with a caption"), xy); 
    7375 
    7476        final DateField dateField = new DateField("Removal test"); 
    75         layout.addComponent(dateField); 
     77        // coordinateLayout.addComponent(dateField); 
    7678 
    7779        final Panel panel = new Panel("TestPanel"); 
    78         layout.addComponent(panel, "-1,0,0%,-1,-1,-1%"); 
     80        // coordinateLayout.addComponent(panel, "0,-1,-1,-1,-1,0"); 
     81 
     82        final Layout ol = new OrderedLayout(); 
     83        ol.addComponent(new Select("Select inside layouts", table 
     84                .getContainerDataSource())); 
     85        ol.addComponent(new Select("Select inside layouts", table 
     86                .getContainerDataSource())); 
     87        ol.addComponent(new Select("Select inside layouts", table 
     88                .getContainerDataSource())); 
     89 
     90        // CoordinateLayout.Coordinates xy2 = new CoordinateLayout.Coordinates( 
     91        // "-1,-1,0,0,200,200"); 
     92        // coordinateLayout.addComponent(ol, xy2); 
    7993 
    8094        Button b1 = new Button("Add contents to panel"); 
     
    93107            public void buttonClick(Button.ClickEvent e) { 
    94108                if (bottom) { 
    95                     layout.sendToTop(panel); 
     109                    coordinateLayout.sendToTop(panel); 
    96110                    bottom = false; 
    97111                } else { 
    98                     layout.sendToBottom(panel); 
     112                    coordinateLayout.sendToBottom(panel); 
    99113                    bottom = true; 
    100114                } 
     
    105119        b3.addListener(new Button.ClickListener() { 
    106120            public void buttonClick(Button.ClickEvent e) { 
    107                 layout.setBorders(!layout.hasBorders()); 
     121                coordinateLayout.setBorders(!coordinateLayout.hasBorders()); 
    108122            } 
    109123        }); 
     
    113127            public void buttonClick(Button.ClickEvent e) { 
    114128                e.getButton().setEnabled(false); 
    115                 layout.removeComponent(dateField); 
     129                coordinateLayout.removeComponent(dateField); 
    116130            } 
    117131        }); 
     
    120134        b5.addListener(new Button.ClickListener() { 
    121135            public void buttonClick(Button.ClickEvent e) { 
    122                 layout.requestRepaint(); 
    123             } 
    124         }); 
    125  
    126         // Setup layout for splitpanel 
     136                coordinateLayout.requestRepaint(); 
     137            } 
     138        }); 
     139 
     140        // Setup coordinateLayout for right 
    127141        OrderedLayout right = new OrderedLayout(); 
    128142        right.addComponent(getControlPanel(table)); 
     
    134148        right.addComponent(b5); 
    135149 
    136         // sPanel.addComponent(layout); 
     150        // sPanel.addComponent(coordinateLayout); 
    137151        // sPanel.addComponent(right); 
    138152        // 
     
    143157        ExpandLayout mainLayout = new ExpandLayout( 
    144158                ExpandLayout.ORIENTATION_HORIZONTAL); 
    145         mainLayout.addComponent(layout); 
     159        mainLayout.addComponent(coordinateLayout); 
    146160        mainLayout.addComponent(right); 
    147161        main.setLayout(mainLayout); 
     
    207221                        newCoord.setUnitsPercent(true, true, true, true, true, 
    208222                                true); 
    209                         layout.setCoordinates(c, newCoord); 
     223                        coordinateLayout.setCoordinates(c, newCoord); 
    210224                    } 
    211225                } 
     
    222236                    newCoord 
    223237                            .setUnitsPercent(true, true, true, true, true, true); 
    224                     layout.setCoordinates(c, newCoord); 
     238                    coordinateLayout.setCoordinates(c, newCoord); 
    225239                    labelArray[j].setValue(new Integer(values[j])); 
    226240                } 
  • incubator/widgets/coordinatelayout/src/com/itmill/toolkit/ui/CoordinateLayout.java

    r5220 r5258  
    22 
    33import java.util.ArrayList; 
     4import java.util.Arrays; 
    45import java.util.HashMap; 
    56import java.util.Iterator; 
     
    1213/** 
    1314 *  
    14  * A layout that enables absolute positioning for its children. 
     15 * A coordinateLayout that enables absolute positioning for its children. 
    1516 *  
    1617 *  
     
    2324    protected boolean hasBorders; 
    2425 
    25     public static int TOP = 0; 
    26     public static int RIGHT = 1; 
    27     public static int BOTTOM = 2; 
    28     public static int LEFT = 3; 
    29     public static int WIDTH = 4; 
    30     public static int HEIGHT = 5; 
    31  
    32     /** 
    33      * Creates an empty coordinatelayout. 
     26    public static final int LEFT = 0; 
     27    public static final int TOP = 1; 
     28    public static final int WIDTH = 2; 
     29    public static final int HEIGHT = 3; 
     30    public static final int RIGHT = 4; 
     31    public static final int BOTTOM = 5; 
     32 
     33    /** 
     34     * Creates an empty coordinatelayout. The coordinateLayout is full size by default. 
    3435     */ 
    3536    public CoordinateLayout() { 
     
    4445    /** 
    4546     *  
    46      * Adds a component to the layout at the specified coordinates. Null 
    47      * arguments are not permitted and will throw an IllegalArgumentException. 
     47     * Adds a component to the coordinateLayout at the specified coordinates. The values 
     48     * are treated as pixel values by default and for example 
     49     * {@link com.itmill.toolkit.ui.CoordinateLayout.Coordinates#setUnitPercent(int, boolean)} 
     50     * can be used to set the values as percentage. 
     51     *  
     52     * The value should not be negative, except for -1 which is reserved for 
     53     * automatic value (terminal decides). Null argument is not permitted and 
     54     * will throw an IllegalArgumentException. 
     55     *  
     56     * Examples: 
     57     *  
     58     * <code>addComponent(c, 0, 0, -1, -1, -1, -1)</code> to attach to 
     59     * upper-left corner and let terminal decide size 
     60     *  
     61     * <code>Coordinates c = addComponent(c, 5, 5, 200, 200, -1, -1) 
     62     *       c.setUnitPercent(CoordinateLayout.LEFT, true) 
     63     *       c.setUnitPercent(CoordinateLayout.TOP, true) 
     64     * </code> to attach 5% from upper-left corner and define size to 200 x 200 
     65     * pixels 
     66     *  
     67     * <code>addComponent(c, 0, 0, -1, -1, 0, 0)</code> to stretch component to 
     68     * cover coordinateLayout area (attached to all four corners) 
     69     *  
    4870     *  
    4971     * @param c 
    5072     *            component to be added 
     73     * @param left 
     74     *            distance of component left from coordinateLayout left 
    5175     * @param top 
    52      *            distance of component top from layout top 
    53      * @param right 
    54      *            distance of component right from layout right 
    55      * @param bottom 
    56      *            distance of component bottom from layout bottom 
    57      * @param left 
    58      *            distance of component left from layout left 
     76     *            distance of component top from coordinateLayout top 
    5977     * @param width 
    6078     *            component width 
    6179     * @param height 
    6280     *            component height 
     81     * @param right 
     82     *            distance of component right from coordinateLayout right 
     83     * @param bottom 
     84     *            distance of component bottom from coordinateLayout bottom 
    6385     * @return the coordinates object for the component 
    6486     *  
    6587     * @throws IllegalArgumentException 
    6688     */ 
    67     public Coordinates addComponent(Component c, int top, int right, 
    68             int bottom, int left, int width, int height) 
    69             throws IllegalArgumentException { 
     89    public Coordinates addComponent(Component c, int left, int top, int width, 
     90            int height, int right, int bottom) throws IllegalArgumentException { 
    7091        if (c == null) { 
    7192            throw new IllegalArgumentException(); 
    7293        } 
    73         Coordinates newCoords = new Coordinates(top, right, bottom, left, 
    74                 width, height); 
     94        Coordinates newCoords = new Coordinates(left, top, width, height, 
     95                right, bottom); 
    7596        componentToCoord.put(c, newCoords); 
    7697        addComponent(c); 
     
    80101    /** 
    81102     * Add a component to the specified coordinates. The string format is 
    82      * "top[%],right[%],bottom[%],left[%],width[%],height[%]". Null arguments 
    83      * will throw an IllegalArgumentException 
     103     * "left[%], top[%] [,width[%], height[%] [,right[%], bottom[%]]]". Null 
     104     * arguments will throw an IllegalArgumentException. 
     105     *  
     106     * Examples for string format: 
     107     *  
     108     * <code>addComponent(c, "0, 0")</code> to attach to upper-left corner and 
     109     * let terminal decide size 
     110     *  
     111     * <code>addComponent(c, "5%, 5%, 200, 200")</code> to attach 5% from 
     112     * upper-left corner and define size to 200 x 200 pixels 
     113     *  
     114     * <code>addComponent(c,"0, 0, , ,0, 0")</code> to stretch component to 
     115     * cover coordinateLayout area (attached to all four corners) 
    84116     *  
    85117     * @param c 
     
    87119     * @param coordString 
    88120     *            coordinates in the specified string format 
    89      * @return the Coordinates object for the component 
     121     * @return the created Coordinates object for the component 
    90122     *  
    91123     * @throws IllegalArgumentException 
     
    103135 
    104136    /** 
    105      * Adds a component to the layout at the specified coordinates. Null 
     137     * Adds a component to the coordinateLayout at the specified coordinates. Null 
    106138     * arguments are not permitted and will throw an IllegalArgumentException. 
    107139     *  
     
    126158     * permitted and will throw an IllegalArgumentException. In addition, the 
    127159     * user of this method must make sure that the given component is a child of 
    128      * this layout. 
     160     * this coordinateLayout. 
    129161     *  
    130162     * @param c 
     
    142174        setCoordinates(c, new Coordinates(top, right, bottom, left, width, 
    143175                height)); 
    144         updateContents(); 
     176        requestRepaint(); 
    145177    } 
    146178 
     
    149181     * permitted and will throw an IllegalArgumentException. In addition, the 
    150182     * user of this method must make sure that the given component is a child of 
    151      * this layout. 
     183     * this coordinateLayout. 
    152184     *  
    153185     * @param c 
     
    164196        } 
    165197        componentToCoord.put(c, coord); 
    166         updateContents(); 
     198        requestRepaint(); 
    167199    } 
    168200 
    169201    public void setBorders(boolean bordersEnabled) { 
    170202        hasBorders = bordersEnabled; 
    171         updateContents(); 
     203        requestRepaint(); 
    172204    } 
    173205 
     
    181213        componentList.add(c); 
    182214 
    183         updateContents(); 
     215        requestRepaint(); 
    184216    } 
    185217 
     
    193225        componentList.add(0, c); 
    194226 
    195         updateContents(); 
    196     } 
    197  
    198     public void updateContents() { 
    199  
    200227        requestRepaint(); 
    201228    } 
     
    203230    /** 
    204231     * If a component is added with this method the component is added to the 
    205      * top-left corner of the layout. 
     232     * top-left corner of the coordinateLayout. 
    206233     *  
    207234     * @param c 
     
    224251        } 
    225252        super.addComponent(c); 
    226         updateContents(); 
    227     } 
    228  
    229     /** 
    230      * Removes the component from the layout. Method will throw 
     253        requestRepaint(); 
     254    } 
     255 
     256    /** 
     257     * Removes the component from the coordinateLayout. Method will throw 
    231258     * IllegalArgumentException if the given component is not a child of this 
    232      * layout. 
     259     * coordinateLayout. 
    233260     *  
    234261     * @param c 
     
    246273        componentToCoord.remove(c); 
    247274 
    248         updateContents(); 
     275        requestRepaint(); 
    249276    } 
    250277 
     
    252279     * Replaces a child component with a new one. The new component uses the 
    253280     * same coordinates as the previous component. If the arguments are null or 
    254      * the component is not a child of this layout IllegalArgumentException is 
     281     * the component is not a child of this coordinateLayout IllegalArgumentException is 
    255282     * thrown. 
    256283     *  
     
    270297        } 
    271298        componentList.set(componentList.indexOf(c1), c2); 
    272         updateContents(); 
    273     } 
    274  
    275     /** 
    276      * Get the coordinates for a given component in this layout 
     299        requestRepaint(); 
     300    } 
     301 
     302    /** 
     303     * Get the coordinates for a given component in this coordinateLayout 
    277304     *  
    278305     * @param c 
    279306     * @return the Coordinates object for the given component 
    280307     * @throws IllegalArgumentException 
    281      *             thrown if component is not in this layout 
     308     *             thrown if component is not in this coordinateLayout 
    282309     */ 
    283310    public Coordinates getCoordinates(Component c) 
     
    303330    /** 
    304331     * Returns the iterator for the coordinates of the child components in this 
    305      * layout. 
     332     * coordinateLayout. 
    306333     *  
    307334     * @return the iterator for the coordinate list 
     
    312339 
    313340    /** 
    314      * True if this layout has children 
     341     * True if this coordinateLayout has children 
    315342     *  
    316343     * @return 
     
    376403 
    377404            target.startTag("component"); 
    378  
    379             target.startTag("coords"); 
    380             target.addAttribute("top", coords.getCoord(TOP)); 
    381             target.addAttribute("right", coords.getCoord(RIGHT)); 
    382             target.addAttribute("bottom", coords.getCoord(BOTTOM)); 
    383             target.addAttribute("left", coords.getCoord(LEFT)); 
    384             target.endTag("coords"); 
    385  
    386             target.startTag("dimensions"); 
    387             target.addAttribute("width", coords.getWidth()); 
    388             target.addAttribute("height", coords.getHeight()); 
    389             target.endTag("dimensions"); 
    390  
    391             target.startTag("isValuePercent"); 
    392             target.addAttribute("top", coords.isValuePercent(TOP)); 
    393             target.addAttribute("right", coords.isValuePercent(RIGHT)); 
    394             target.addAttribute("bottom", coords.isValuePercent(BOTTOM)); 
    395             target.addAttribute("left", coords.isValuePercent(LEFT)); 
    396             target.addAttribute("width", coords.isValuePercent(WIDTH)); 
    397             target.addAttribute("height", coords.isValuePercent(HEIGHT)); 
    398             target.endTag("isValuePercent"); 
    399  
    400             target.startTag("data"); 
     405            target.addAttribute("position", coords.toString()); 
     406 
    401407            component.paint(target); 
    402             target.endTag("data"); 
    403408 
    404409            target.endTag("component"); 
     
    431436         *  
    432437         * @param top 
    433          *            distance of component top from layout top 
     438         *            distance of component top from coordinateLayout top 
    434439         * @param right 
    435          *            distance of component right from layout right 
     440         *            distance of component right from coordinateLayout right 
    436441         * @param bottom 
    437          *            distance of component bottom from layout bottom 
     442         *            distance of component bottom from coordinateLayout bottom 
    438443         * @param left 
    439          *            distance of component left from layout left 
     444         *            distance of component left from coordinateLayout left 
    440445         * @param width 
    441446         *            component width 
     
    447452         */ 
    448453        public Coordinates(int top, int right, int bottom, int left, int width, 
    449                 int height) throws NumberFormatException { 
     454                int height) throws IllegalArgumentException { 
     455 
    450456            properties = new int[6]; 
    451  
    452457            isUnitPercent = new boolean[6]; 
    453458 
     
    460465            properties[HEIGHT] = height; 
    461466 
    462             java.util.Arrays.fill(isUnitPercent, false); 
    463  
    464         } 
    465  
    466         /** 
    467          * Create a new Coordinates object with a initialization string. The 
    468          * string format is 
    469          * "top[%],right[%],bottom[%],left[%],width[%],height[%]". 
     467            for (int i = 0; i < properties.length; i++) { 
     468                if (properties[i] < -1) { 
     469                    throw new IllegalArgumentException( 
     470                            "Illegal coordinate value " + properties[i]); 
     471                } 
     472            } 
     473 
     474        } 
     475 
     476        /** 
    470477         *  
    471478         * @param coordinateString 
    472479         * @throws IllegalArgumentException 
    473          */ 
    474         public Coordinates(String coordinateString) // TODO test this 
    475                 // properly 
    476                 throws IllegalArgumentException { 
     480         * @see com.itmill.toolkit.ui.CoordinateLayout#addComponent(Component, 
     481         *      String) 
     482         */ 
     483        public Coordinates(String coordinateString) 
     484                throws IllegalArgumentException {// TODO test this properly 
     485 
    477486            properties = new int[6]; 
    478  
    479487            isUnitPercent = new boolean[6]; 
    480488 
     489            Arrays.fill(properties, -1); 
     490 
    481491            String coordStringArray[] = coordinateString.split(","); 
    482492 
    483             if (coordStringArray.length != 6) { 
    484                 throw new IllegalArgumentException("Wrong number of arguments"); 
     493            if (coordStringArray.length > 6) { 
     494                throw new IllegalArgumentException( 
     495                        "Incorrect string syntax: too many arguments"); 
    485496            } 
    486497 
     
    489500            Matcher matcher = null; 
    490501            try { 
    491                 for (int i = 0; i < 4; i++) { 
     502                for (int i = 0; i < coordStringArray.length; i++) { 
    492503 
    493504                    matcher = numberPattern.matcher(coordStringArray[i].trim()); 
    494                     matcher.find(); 
    495                     properties[i] = Integer.parseInt(matcher.group()); 
    496  
    497                     isUnitPercent[i] = coordStringArray[i] 
    498                             .matches(percentRegex); 
     505                    if (matcher.find()) { 
     506                        properties[i] = Integer.parseInt(matcher.group()); 
     507 
     508                        isUnitPercent[i] = coordStringArray[i] 
     509                                .matches(percentRegex); 
     510                    } else { 
     511                        properties[i] = -1; 
     512                    } 
    499513                } 
    500514 
    501                 matcher = numberPattern.matcher(coordStringArray[WIDTH].trim()); 
    502                 matcher.find(); 
    503                 properties[WIDTH] = Integer.parseInt(matcher.group()); 
    504                 isUnitPercent[WIDTH] = coordStringArray[WIDTH] 
    505                         .matches(percentRegex); 
    506  
    507                 matcher = numberPattern 
    508                         .matcher(coordStringArray[HEIGHT].trim()); 
    509                 matcher.find(); 
    510                 properties[HEIGHT] = Integer.parseInt(matcher.group()); 
    511                 isUnitPercent[HEIGHT] = coordStringArray[HEIGHT] 
    512                         .matches(percentRegex); 
    513  
    514515            } catch (IllegalStateException e) { 
    515                 throw new IllegalArgumentException(e.toString()); 
     516                throw new IllegalArgumentException("Incorrect string syntax " 
     517                        + e.toString()); 
    516518            } 
    517519        } 
     
    538540        } 
    539541 
     542        public void setUnitPercent(int valueId, boolean isPercent) { 
     543            isUnitPercent[valueId] = isPercent; 
     544        } 
     545 
    540546        /** 
    541547         * Set all the values to be treated as percentage. 
     
    660666                    returnString.append("%"); 
    661667                } 
    662                 returnString.append(", "); 
     668                if (i != properties.length - 1) { 
     669                    returnString.append(","); 
     670                } 
    663671            }