public void provide(Object element) throws IOException { Renderer current = Renderer.getRenderable(element.getClass()); if (current == null) { current = Renderer.ANY; } if (current != null) { if (renderable != null && !current.equals(renderable)) { flush(); } buffer.addLast(element); renderable = current; } }
public static <I> Renderer<? super I> getRenderable(Class<I> itemType) { for (Renderer<?> formatter : renderables) { try { if (formatter.getType().isAssignableFrom(itemType)) { return (Renderer<I>)formatter; } } catch (Exception e) { } catch (NoClassDefFoundError e) { } } return null; }
public void flush() throws IOException { // We don't really flush, we just compute renderables from the buffer if (buffer.size() > 0) { LineRenderer i = renderable.renderer(buffer.iterator()); buffer.clear(); renderers.add(i); } }
for (int i = 0;i < row.getCols().size();i++) { Renderer renderable = row.getCols().get(i); eltWidths[i] = Math.max(eltWidths[i], renderable.getActualWidth() + row.row.leftCellPadding + row.row.rightCellPadding); eltMinWidths[i] = Math.max(eltMinWidths[i], renderable.getMinWidth() + row.row.leftCellPadding + row.row.rightCellPadding); return Renderer.NULL.reader(width);
private Composite(Iterable<? extends Renderer> renderers) { int actualWidth = 0; int minWidth = 0; for (Renderer renderer : renderers) { actualWidth = Math.max(actualWidth, renderer.getActualWidth()); minWidth = Math.max(minWidth, renderer.getMinWidth()); } this.actualWidth = actualWidth; this.minWidth = minWidth; this.renderers = renderers; }
/** * Create a renderer for the specified width and height or return null if the element does not provide any output * for the specified dimensions. The default implementation delegates to the {@link #reader(int)} method when the * <code>height</code> argument is not positive otherwise it returns null. Subclasses should override this method * when they want to provide content that can adapts to the specified height. * * @param width the width * @param height the height * @return the renderer */ public LineReader reader(int width, int height) { if (height > 0) { return null; } else { return reader(width); } }
@Override public Renderer renderer(Iterator<MemoryUsage> stream) { ArrayList<MemoryUsageRenderer> renderers = new ArrayList<MemoryUsageRenderer>(); while (stream.hasNext()) { MemoryUsage usage = stream.next(); renderers.add(new MemoryUsageRenderer(usage)); } return Renderer.vertical(renderers); } }
@Override public int getActualHeight(int width) { int actualHeight = 0; for (Renderer renderer : renderers) { actualHeight += renderer.getActualHeight(width); } return actualHeight; }
public void send() throws IOException { if (buffer.size() > 0) { Renderer renderer = renderable.renderer(buffer.iterator()); renderer.render(out); buffer.clear(); renderable = null; } } }
@Override public int getMinHeight(int width) { int minHeight = 0; for (Renderer col : cols) { minHeight = Math.max(minHeight, col.getMinHeight(width)); } return minHeight; }
@Override public int getActualWidth() { int width = value != null ? value.getActualWidth() : 0; for (Renderer child : children) { width = Math.max(width, 2 + child.getActualWidth()); } return width; }
@Override public int getMinWidth() { int width = value != null ? value.getMinWidth() : 0; for (Renderer child : children) { width = Math.max(width, 2 + child.getMinWidth()); } return width; }
@Override public LineReader reader(int width) { int[] widths = new int[cols.size()]; int[] minWidths = new int[cols.size()]; for (int i = 0;i < cols.size();i++) { Renderer renderable = cols.get(i); widths[i] = Math.max(widths[i], renderable.getActualWidth()); minWidths[i] = Math.max(minWidths[i], renderable.getMinWidth()); } widths = Layout.flow().compute(false, width, widths, minWidths); if (widths == null) { return null; } else { // Size could be smaller and lead to ArrayIndexOutOfBounds later widths = Arrays.copyOf(widths, minWidths.length); return renderer(widths, -1); } } }
@Override public Renderer renderer(Iterator<UIBuilder> stream) { LinkedList<Renderer> renderers = new LinkedList<Renderer>(); while (stream.hasNext()) { for (Element element : stream.next().getElements()) { renderers.add(element.renderer()); } } return Renderer.vertical(renderers); } }
@Override public int getActualHeight(int width) { int actualHeight = 0; for (Renderer col : cols) { actualHeight = Math.max(actualHeight, col.getActualHeight(width)); } return actualHeight; }
@Override public void print(Object obj) { if (obj instanceof UIBuilder) { RenderAppendable out = new RenderAppendable(this.out); new UIBuilderRenderable().renderer(Collections.singleton((UIBuilder)obj).iterator()).render(out); } else if (obj instanceof Element) { RenderAppendable out = new RenderAppendable(this.out); ((Element)obj).renderer().render(out); } else { super.print(obj); } } }
int getMinHeight(int[] widths) { int minHeight; switch (table.overflow) { case HIDDEN: minHeight = 1; break; case WRAP: minHeight = 0; for (int i = 0;i < widths.length;i++) { Renderer col = row.getCols().get(i); minHeight = Math.max(minHeight, col.getMinHeight(widths[i])); } break; default: throw new AssertionError(); } if (hasTop()) { minHeight++; } if (hasBottom()) { minHeight++; } return minHeight; }
@Override public int getActualWidth() { int actualWidth = 0; for (int i = 0;i < cols.size();i++) { Renderer col = cols.get(i); actualWidth += col.getActualWidth(); actualWidth += leftCellPadding; actualWidth += rightCellPadding; if (separator != null && i > 0) { actualWidth++; } } return actualWidth; }
@Override public int getMinWidth() { int minWidth = 0; for (int i = 0;i < cols.size();i++) { Renderer col = cols.get(i); minWidth += col.getMinWidth(); minWidth += leftCellPadding; minWidth += rightCellPadding; if (separator != null && i > 0) { minWidth++; } } return minWidth; }