001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.text.ui;
021    
022    import org.crsh.text.LineRenderer;
023    import org.crsh.text.Style;
024    import org.crsh.util.CharSlicer;
025    import org.crsh.util.Pair;
026    
027    public class LabelElement extends Element {
028    
029      /** . */
030      final String value;
031    
032      /** . */
033      final int minWidth;
034    
035      /** . */
036      final int actualWidth;
037    
038      /** . */
039      final int actualHeight;
040    
041      /** . */
042      final CharSlicer slicer;
043    
044      /**
045       * Create a new label element
046       *
047       * @param value the label value
048       * @param minWidth the label minimum width
049       * @throws IllegalArgumentException if the minimum width is negative
050       */
051      public LabelElement(Object value, int minWidth) throws IllegalArgumentException {
052        if (minWidth < 0) {
053          throw new IllegalArgumentException("No negative min size allowed");
054        }
055    
056        //
057        String s = String.valueOf(value);
058    
059        // Determine size
060        CharSlicer slicer = new CharSlicer(s);
061        Pair<Integer, Integer> size = slicer.size();
062    
063        //
064        this.value = s;
065        this.minWidth = Math.min(size.getFirst(), minWidth);
066        this.actualWidth = size.getFirst();
067        this.actualHeight = size.getSecond();
068        this.slicer = slicer;
069      }
070    
071      public LabelElement(String value) {
072        this((Object)value);
073      }
074    
075      public LabelElement(String value, int minWidth) {
076        this((Object)value, minWidth);
077      }
078    
079      public LabelElement(Object value) {
080        this(value, 1);
081      }
082    
083      public String getValue() {
084        return value;
085      }
086    
087      public LineRenderer renderer() {
088        return new LabelLineRenderer(this);
089      }
090    
091      @Override
092      public String toString() {
093        return "Label[" + value + "]";
094      }
095    
096      @Override
097      public LabelElement style(Style.Composite style) {
098        return (LabelElement)super.style(style);
099      }
100    }