View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.layout;
21  
22  import java.util.StringTokenizer;
23  
24  /**
25   * Basic helper type for the CSS3 Grid property.
26   * Limitations: "auto" is not supported by IE 10/11.
27   */
28  public class GridSpan {
29  
30    private Integer start;
31    private Integer span;
32  
33    private GridSpan() {
34    }
35  
36    public static GridSpan valueOf(final String string) {
37      final GridSpan item = new GridSpan();
38      final StringTokenizer tokenizer = new StringTokenizer(string, " /");
39      if (tokenizer.hasMoreElements()) {
40        item.start = Integer.parseInt(tokenizer.nextToken());
41      }
42      if (tokenizer.hasMoreElements()) {
43        final String next = tokenizer.nextToken();
44        if (next.equals("span")) {
45          item.span = Integer.parseInt(tokenizer.nextToken());
46        } else {
47          item.span = Integer.parseInt(next) - item.start;
48        }
49      }
50      return item;
51    }
52  
53    public static GridSpan valueOf(final Integer start, final Integer span) {
54      final GridSpan item = new GridSpan();
55      item.start = start;
56      item.span = span;
57      return item;
58    }
59  
60    public String encode() {
61      if (start != null) {
62        if (span != null && span != 1) {
63          return start + "/span " + span;
64        } else {
65          return start.toString();
66        }
67      } else {
68        if (span != null && span != 1) { // XXX "auto" not supported by MS IE
69          return "auto/span " + span;
70        } else {
71          return "auto";
72        }
73      }
74    }
75  
76    public Integer getStart() {
77      return start;
78    }
79  
80    public Integer getSpan() {
81      return span;
82    }
83  }