Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / terminal / AbstractTerminal.java
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.terminal;
20
21 import com.googlecode.lanterna.TerminalSize;
22 import com.googlecode.lanterna.graphics.TextGraphics;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * Containing a some very fundamental functionality that should be common (and usable) to all terminal implementations.
30 * All the Terminal implementers within Lanterna extends from this class.
31 *
32 * @author Martin
33 */
34 public abstract class AbstractTerminal implements Terminal {
35
36 private final List<ResizeListener> resizeListeners;
37 private TerminalSize lastKnownSize;
38
39 protected AbstractTerminal() {
40 this.resizeListeners = new ArrayList<ResizeListener>();
41 this.lastKnownSize = null;
42 }
43
44 @Override
45 public void addResizeListener(ResizeListener listener) {
46 if (listener != null) {
47 resizeListeners.add(listener);
48 }
49 }
50
51 @Override
52 public void removeResizeListener(ResizeListener listener) {
53 if (listener != null) {
54 resizeListeners.remove(listener);
55 }
56 }
57
58 /**
59 * Call this method when the terminal has been resized or the initial size of the terminal has been discovered. It
60 * will trigger all resize listeners, but only if the size has changed from before.
61 *
62 * @param columns Number of columns in the new size
63 * @param rows Number of rows in the new size
64 */
65 protected synchronized void onResized(int columns, int rows) {
66 TerminalSize newSize = new TerminalSize(columns, rows);
67 if (lastKnownSize == null || !lastKnownSize.equals(newSize)) {
68 lastKnownSize = newSize;
69 for (ResizeListener resizeListener : resizeListeners) {
70 resizeListener.onResized(this, lastKnownSize);
71 }
72 }
73 }
74
75 @Override
76 public TextGraphics newTextGraphics() throws IOException {
77 return new TerminalTextGraphics(this);
78 }
79 }