2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
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.
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.
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/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.gui2
;
21 import com
.googlecode
.lanterna
.TerminalPosition
;
22 import com
.googlecode
.lanterna
.input
.KeyStroke
;
24 import java
.util
.Collection
;
25 import java
.util
.Collections
;
28 * This abstract implementation contains common code for the different {@code Composite} implementations. A
29 * {@code Composite} component is one that encapsulates a single component, like borders. Because of this, a
30 * {@code Composite} can be seen as a special case of a {@code Container} and indeed this abstract class does in fact
31 * implement the {@code Container} interface as well, to make the composites easier to work with internally.
33 * @param <T> Should always be itself, see {@code AbstractComponent}
35 public abstract class AbstractComposite
<T
extends Container
> extends AbstractComponent
<T
> implements Composite
, Container
{
37 private Component component
;
42 public AbstractComposite() {
47 public void setComponent(Component component
) {
48 Component oldComponent
= this.component
;
49 if(oldComponent
== component
) {
52 if(oldComponent
!= null) {
53 removeComponent(oldComponent
);
55 if(component
!= null) {
56 this.component
= component
;
57 component
.onAdded(this);
58 component
.setPosition(TerminalPosition
.TOP_LEFT_CORNER
);
64 public Component
getComponent() {
69 public int getChildCount() {
70 return component
!= null ?
1 : 0;
74 public Collection
<Component
> getChildren() {
75 if(component
!= null) {
76 return Collections
.singletonList(component
);
79 return Collections
.emptyList();
84 public boolean containsComponent(Component component
) {
85 return component
!= null && component
.hasParent(this);
89 public boolean removeComponent(Component component
) {
90 if(this.component
== component
) {
91 this.component
= null;
92 component
.onRemoved(this);
100 public boolean isInvalid() {
101 return component
!= null && component
.isInvalid();
105 public void invalidate() {
109 if(component
!= null) {
110 component
.invalidate();
115 public Interactable
nextFocus(Interactable fromThis
) {
116 if(fromThis
== null && getComponent() instanceof Interactable
) {
117 return (Interactable
)getComponent();
119 else if(getComponent() instanceof Container
) {
120 return ((Container
)getComponent()).nextFocus(fromThis
);
126 public Interactable
previousFocus(Interactable fromThis
) {
127 if(fromThis
== null && getComponent() instanceof Interactable
) {
128 return (Interactable
)getComponent();
130 else if(getComponent() instanceof Container
) {
131 return ((Container
)getComponent()).previousFocus(fromThis
);
137 public boolean handleInput(KeyStroke key
) {
142 public void updateLookupMap(InteractableLookupMap interactableLookupMap
) {
143 if(getComponent() instanceof Container
) {
144 ((Container
)getComponent()).updateLookupMap(interactableLookupMap
);
146 else if(getComponent() instanceof Interactable
) {
147 interactableLookupMap
.add((Interactable
)getComponent());