| 1 | /* |
| 2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). |
| 3 | * |
| 4 | * lanterna is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright (C) 2010-2015 Martin |
| 18 | */ |
| 19 | package com.googlecode.lanterna.gui2; |
| 20 | |
| 21 | import com.googlecode.lanterna.Symbols; |
| 22 | import com.googlecode.lanterna.TerminalSize; |
| 23 | import com.googlecode.lanterna.graphics.ThemeDefinition; |
| 24 | |
| 25 | /** |
| 26 | * Static non-interactive component that is typically rendered as a single line. Normally this component is used to |
| 27 | * separate component from each other in situations where a bordered panel isn't ideal. By default the separator will |
| 28 | * ask for a size of 1x1 so you'll need to make it bigger, either through the layout manager or by overriding the |
| 29 | * preferred size. |
| 30 | * @author Martin |
| 31 | */ |
| 32 | public class Separator extends AbstractComponent<Separator> { |
| 33 | |
| 34 | private final Direction direction; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new {@code Separator} for a specific direction, which will decide whether to draw a horizontal line or |
| 38 | * a vertical line |
| 39 | * |
| 40 | * @param direction Direction of the line to draw within the separator |
| 41 | */ |
| 42 | public Separator(Direction direction) { |
| 43 | if(direction == null) { |
| 44 | throw new IllegalArgumentException("Cannot create a separator with a null direction"); |
| 45 | } |
| 46 | this.direction = direction; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the direction of the line drawn for this separator |
| 51 | * @return Direction of the line drawn for this separator |
| 52 | */ |
| 53 | public Direction getDirection() { |
| 54 | return direction; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected DefaultSeparatorRenderer createDefaultRenderer() { |
| 59 | return new DefaultSeparatorRenderer(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Helper interface that doesn't add any new methods but makes coding new button renderers a little bit more clear |
| 64 | */ |
| 65 | public static abstract class SeparatorRenderer implements ComponentRenderer<Separator> { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This is the default separator renderer that is used if you don't override anything. With this renderer, the |
| 70 | * separator has a preferred size of one but will take up the whole area it is given and fill that space with either |
| 71 | * horizontal or vertical lines, depending on the direction of the {@code Separator} |
| 72 | */ |
| 73 | public static class DefaultSeparatorRenderer extends SeparatorRenderer { |
| 74 | @Override |
| 75 | public TerminalSize getPreferredSize(Separator component) { |
| 76 | return TerminalSize.ONE; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void drawComponent(TextGUIGraphics graphics, Separator component) { |
| 81 | ThemeDefinition themeDefinition = graphics.getThemeDefinition(Separator.class); |
| 82 | graphics.applyThemeStyle(themeDefinition.getNormal()); |
| 83 | char character = themeDefinition.getCharacter(component.getDirection().name().toUpperCase(), |
| 84 | component.getDirection() == Direction.HORIZONTAL ? Symbols.SINGLE_LINE_HORIZONTAL : Symbols.SINGLE_LINE_VERTICAL); |
| 85 | graphics.fill(character); |
| 86 | } |
| 87 | } |
| 88 | } |