Fix UTF8 bug, create first executable JAR file
[jvcard.git] / src / com / googlecode / lanterna / screen / ScreenBuffer.java
... / ...
CommitLineData
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 */
19package com.googlecode.lanterna.screen;
20
21import com.googlecode.lanterna.TerminalPosition;
22import com.googlecode.lanterna.TextCharacter;
23import com.googlecode.lanterna.TerminalSize;
24import com.googlecode.lanterna.graphics.BasicTextImage;
25import com.googlecode.lanterna.graphics.TextGraphics;
26import com.googlecode.lanterna.graphics.TextImage;
27
28/**
29 * Defines a buffer used by AbstractScreen and its subclasses to keep its state of what's currently displayed and what
30 * the edit buffer looks like. A ScreenBuffer is essentially a two-dimensional array of TextCharacter with some utility
31 * methods to inspect and manipulate it in a safe way.
32 * @author martin
33 */
34public class ScreenBuffer implements TextImage {
35 private final BasicTextImage backend;
36
37 /**
38 * Creates a new ScreenBuffer with a given size and a TextCharacter to initially fill it with
39 * @param size Size of the buffer
40 * @param filler What character to set as the initial content of the buffer
41 */
42 public ScreenBuffer(TerminalSize size, TextCharacter filler) {
43 this(new BasicTextImage(size, filler));
44 }
45
46 private ScreenBuffer(BasicTextImage backend) {
47 this.backend = backend;
48 }
49
50 @Override
51 public ScreenBuffer resize(TerminalSize newSize, TextCharacter filler) {
52 BasicTextImage resizedBackend = backend.resize(newSize, filler);
53 return new ScreenBuffer(resizedBackend);
54 }
55
56 boolean isVeryDifferent(ScreenBuffer other, int threshold) {
57 if(!getSize().equals(other.getSize())) {
58 throw new IllegalArgumentException("Can only call isVeryDifferent comparing two ScreenBuffers of the same size!"
59 + " This is probably a bug in Lanterna.");
60 }
61 int differences = 0;
62 for(int y = 0; y < getSize().getRows(); y++) {
63 for(int x = 0; x < getSize().getColumns(); x++) {
64 if(!getCharacterAt(x, y).equals(other.getCharacterAt(x, y))) {
65 if(++differences >= threshold) {
66 return true;
67 }
68 }
69 }
70 }
71 return false;
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // Delegate all TextImage calls (except resize) to the backend BasicTextImage
76 @Override
77 public TerminalSize getSize() {
78 return backend.getSize();
79 }
80
81 @Override
82 public TextCharacter getCharacterAt(TerminalPosition position) {
83 return backend.getCharacterAt(position);
84 }
85
86 @Override
87 public TextCharacter getCharacterAt(int column, int row) {
88 return backend.getCharacterAt(column, row);
89 }
90
91 @Override
92 public void setCharacterAt(TerminalPosition position, TextCharacter character) {
93 backend.setCharacterAt(position, character);
94 }
95
96 @Override
97 public void setCharacterAt(int column, int row, TextCharacter character) {
98 backend.setCharacterAt(column, row, character);
99 }
100
101 @Override
102 public void setAll(TextCharacter character) {
103 backend.setAll(character);
104 }
105
106 @Override
107 public TextGraphics newTextGraphics() {
108 return backend.newTextGraphics();
109 }
110
111 @Override
112 public void copyTo(TextImage destination) {
113 if(destination instanceof ScreenBuffer) {
114 //This will allow the BasicTextImage's copy method to use System.arraycopy (micro-optimization?)
115 destination = ((ScreenBuffer)destination).backend;
116 }
117 backend.copyTo(destination);
118 }
119
120 @Override
121 public void copyTo(TextImage destination, int startRowIndex, int rows, int startColumnIndex, int columns, int destinationRowOffset, int destinationColumnOffset) {
122 if(destination instanceof ScreenBuffer) {
123 //This will allow the BasicTextImage's copy method to use System.arraycopy (micro-optimization?)
124 destination = ((ScreenBuffer)destination).backend;
125 }
126 backend.copyTo(destination, startRowIndex, rows, startColumnIndex, columns, destinationRowOffset, destinationColumnOffset);
127 }
128
129 /**
130 * Copies the content from a TextImage into this buffer.
131 * @param source Source to copy content from
132 * @param startRowIndex Which row in the source image to start copying from
133 * @param rows How many rows to copy
134 * @param startColumnIndex Which column in the source image to start copying from
135 * @param columns How many columns to copy
136 * @param destinationRowOffset The row offset in this buffer of where to place the copied content
137 * @param destinationColumnOffset The column offset in this buffer of where to place the copied content
138 */
139 public void copyFrom(TextImage source, int startRowIndex, int rows, int startColumnIndex, int columns, int destinationRowOffset, int destinationColumnOffset) {
140 source.copyTo(backend, startRowIndex, rows, startColumnIndex, columns, destinationRowOffset, destinationColumnOffset);
141 }
142
143 @Override
144 public void scrollLines(int firstLine, int lastLine, int distance) {
145 backend.scrollLines(firstLine, lastLine, distance);
146 }
147
148 @Override
149 public String toString() {
150 return backend.toString();
151 }
152}