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.Renderer;
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      public LabelElement(String value, int minWidth) {
045        if (minWidth < 0) {
046          throw new IllegalArgumentException("No negative min size allowed");
047        }
048    
049        // Determine size
050        CharSlicer slicer = new CharSlicer(value);
051        Pair<Integer, Integer> size = slicer.size();
052    
053        //
054        this.value = value;
055        this.minWidth = Math.min(size.getFirst(), minWidth);
056        this.actualWidth = size.getFirst();
057        this.actualHeight = size.getSecond();
058        this.slicer = slicer;
059      }
060    
061      public LabelElement(String value) {
062        this(value, 1);
063      }
064    
065      public LabelElement(Object value, int minWidth) {
066        this(String.valueOf(value), minWidth);
067      }
068    
069      public LabelElement(Object value) {
070        this(String.valueOf(value));
071      }
072    
073      public String getValue() {
074        return value;
075      }
076    
077      public Renderer renderer() {
078        return new LabelRenderer(this);
079      }
080    
081      @Override
082      public String toString() {
083        return "Label[" + value + "]";
084      }
085    
086      @Override
087      public LabelElement style(Style.Composite style) {
088        return (LabelElement)super.style(style);
089      }
090    }