/** * Create a new label element * * @param value the label value * @param minWidth the label minimum width * @throws IllegalArgumentException if the minimum width is negative */ public LabelElement(Object value, int minWidth) throws IllegalArgumentException { if (minWidth < 0) { throw new IllegalArgumentException("No negative min size allowed"); } // String s = String.valueOf(value); // Determine size CharSlicer slicer = new CharSlicer(s); Pair<Integer, Integer> size = slicer.size(); // this.value = s; this.minWidth = Math.min(size.getFirst(), minWidth); this.actualWidth = size.getFirst(); this.actualHeight = size.getSecond(); this.slicer = slicer; }
public Pair<Integer, Integer>[] lines(final int width) { return lines(linesIterator(width), 0); }
@Override public int getActualHeight(int width) { return element.slicer.lines(width).length; }
public Pair<Integer, Integer> size() { if (size == null) { size = size(value, 0, 1); } return size; }
private Pair<Integer, Integer>[] lines(Iterator<Pair<Integer, Integer>> i, int count) { Pair<Integer, Integer>[] lines; if (i.hasNext()) { Pair<Integer, Integer> n = i.next(); lines = lines(i, count + 1); lines[count] = n; } else { lines = new Pair[count]; } return lines; }
public CharSlicer(String value) { this.value = value; this.size = size(); }
public Pair<Integer, Integer>[] lines(final int width) { return lines(linesIterator(width), 0); }
public LabelElement(String value, int minWidth) { if (minWidth < 0) { throw new IllegalArgumentException("No negative min size allowed"); } // Determine size CharSlicer slicer = new CharSlicer(value); Pair<Integer, Integer> size = slicer.size(); // this.value = value; this.minWidth = Math.min(size.getFirst(), minWidth); this.actualWidth = size.getFirst(); this.actualHeight = size.getSecond(); this.slicer = slicer; }
@Override public LineReader reader(final int width, int height) { if (width == 0) { return null; } else { Pair<Integer, Integer>[] lines = element.slicer.lines(width); if (height == -1) { height = lines.length; } if (lines.length > height) { return null; } else { return new LabelReader(element, lines, width, height); } } } }
private static Pair<Integer, Integer> size(String s, int index, int height) { if (height < 1) { throw new IllegalArgumentException("A non positive height=" + height + " cannot be accepted"); } if (index < s.length()) { int pos = s.indexOf('\n', index); if (pos == -1) { return Pair.of(s.length() - index, height); } else { Pair<Integer, Integer> ret = size(s, pos + 1, height + 1); return new Pair<Integer, Integer>(Math.max(pos - index, ret.getFirst()), ret.getSecond()); } } else { return Pair.of(0, height); } }
public Pair<Integer, Integer>[] lines(final int width) { return lines(linesIterator(width), 0); }
/** * Create a new label element * * @param value the label value * @param minWidth the label minimum width * @throws IllegalArgumentException if the minimum width is negative */ public LabelElement(Object value, int minWidth) throws IllegalArgumentException { if (minWidth < 0) { throw new IllegalArgumentException("No negative min size allowed"); } // String s = String.valueOf(value); // Determine size CharSlicer slicer = new CharSlicer(s); Pair<Integer, Integer> size = slicer.size(); // this.value = s; this.minWidth = Math.min(size.getFirst(), minWidth); this.actualWidth = size.getFirst(); this.actualHeight = size.getSecond(); this.slicer = slicer; }
@Override public int getActualHeight(int width) { return element.slicer.lines(width).length; }
public Pair<Integer, Integer> size() { if (size == null) { size = size(value, 0, 1); } return size; }
public Pair<Integer, Integer>[] lines(final int width) { return lines(linesIterator(width), 0); }
/** * Create a new label element * * @param value the label value * @param minWidth the label minimum width * @throws IllegalArgumentException if the minimum width is negative */ public LabelElement(Object value, int minWidth) throws IllegalArgumentException { if (minWidth < 0) { throw new IllegalArgumentException("No negative min size allowed"); } // String s = String.valueOf(value); // Determine size CharSlicer slicer = new CharSlicer(s); Pair<Integer, Integer> size = slicer.size(); // this.value = s; this.minWidth = Math.min(size.getFirst(), minWidth); this.actualWidth = size.getFirst(); this.actualHeight = size.getSecond(); this.slicer = slicer; }
@Override public int getActualHeight(int width) { return element.slicer.lines(width).length; }
public Pair<Integer, Integer> size() { if (size == null) { size = size(value, 0, 1); } return size; }
@Override public int getActualHeight(int width) { return element.slicer.lines(width).length; }
public CharSlicer(String value) { this.value = value; this.size = size(); }