1 /*
2  * This file is part of libbluray
3  * Copyright (C) 2010  William Hahne
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 package org.havi.ui;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import org.dvb.ui.TestOpacity;
25 
26 //https://www.jinahya.com/mvn/site/com.googlecode.jinahya/ocap-api/1.3.1/apidocs/org/havi/ui/HContainer.html
27 
28 public class HContainer extends Container implements HMatteLayer,
29         HComponentOrdering, TestOpacity {
HContainer()30     public HContainer() {
31         this(0,0,0,0);
32     }
33 
HContainer(int x, int y, int width, int height)34     public HContainer(int x, int y, int width, int height) {
35         setBounds(x,y,width,height);
36     }
37 
setMatte(HMatte m)38     public void setMatte(HMatte m) throws HMatteException {
39         org.videolan.Logger.unimplemented("HContainer", "setMatte");
40         throw new HMatteException("Matte is not supported");
41     }
42 
getMatte()43     public HMatte getMatte() {
44         return null;
45     }
46 
isDoubleBuffered()47     public boolean isDoubleBuffered() {
48         return false;   // can this be true ?
49     }
50 
isOpaque()51     public boolean isOpaque() {
52         return false;   // can this be true ?
53     }
54 
getOffset(java.awt.Component c)55     private int getOffset(java.awt.Component c) throws ArrayIndexOutOfBoundsException {
56         Component cs[] = getComponents();
57         for (int i = 0; i < cs.length; ++i)
58             if (cs[i] == c) return i;
59 
60         throw new ArrayIndexOutOfBoundsException("Component not contained within");
61     }
62 
checkLineage(java.awt.Component c)63     private void checkLineage(java.awt.Component c) {
64         if (c.getParent() != this) throw new ArrayIndexOutOfBoundsException("Component not contained within");
65     }
66 
addBefore(Component component, Component behind)67     public Component addBefore(Component component, Component behind) {
68         // check to see if behind is an element of this container
69         try {
70             getOffset(behind);
71         } catch (Exception e) {
72             return null;
73         }
74 
75         if (component == behind) return component;
76         synchronized (getTreeLock()) {
77             try {
78                 // Explicitly remove component if in this container.
79                 // Should have no effect if not in this container.
80                 // This must be done so that problems don't occur
81                 // when componentIndex < frontIndex.
82                 remove(component);
83 
84                 int offset = getOffset(behind);
85 
86                 return add(component, offset);
87             } catch (Exception e) {
88                 return null;
89             }
90         }
91     }
92 
addAfter(Component component, Component front)93     public Component addAfter(Component component, Component front) {
94         // check to see if front is an element of this container
95         try {
96             getOffset(front);
97         } catch (Exception e) {
98             return null;
99         }
100 
101         if (component == front) return component;
102         synchronized (getTreeLock()) {
103             try {
104                 // Explicitly remove component if in this container.
105                 // Should have no effect if not in this container.
106                 // This must be done so that problems don't occur
107                 // when componentIndex < frontIndex.
108                 remove(component);
109 
110                 int offset = getOffset(front);
111 
112                 return add(component, offset + 1);
113             } catch (Exception e) {
114                 return null;
115             }
116         }
117     }
118 
popToFront(Component component)119     public boolean popToFront(Component component) {
120         synchronized (getTreeLock()) {
121             try {
122                 // Ensure it is there
123                 checkLineage(component);
124 
125                 // explicitly remove component
126                 // (even if reparenting is implicit)
127                 remove(component);
128                 add(component, 0);
129                 return true;
130             } catch (Exception e) {
131                 return false;
132             }
133         }
134     }
135 
pushToBack(Component component)136     public boolean pushToBack(Component component) {
137         synchronized (getTreeLock()) {
138             try {
139                 // Ensure it is there
140                 checkLineage(component);
141 
142                 // explicitly remove component
143                 // (even if reparenting is implicit)
144                 remove(component);
145                 add(component, -1);
146                 return true;
147             } catch (Exception e) {
148                 return false;
149             }
150         }
151     }
152 
pop(Component component)153     public boolean pop(Component component) {
154         synchronized (getTreeLock()) {
155             try {
156                 int offset = getOffset(component);
157 
158                 if (offset > 0) {
159                     // explicitly remove component
160                     // (even if reparenting is implicit)
161                     remove(component);
162                     add(component, offset - 1);
163                     return true;
164                 }
165             } catch (Exception e) {
166             }
167             return false;
168         }
169     }
170 
push(Component component)171     public boolean push(Component component) {
172         synchronized (getTreeLock()) {
173             try {
174                 int offset = getOffset(component);
175                 int count = getComponentCount();
176 
177                 if (offset == (count - 1)) {
178                     return true;
179                 }
180 
181                 if (offset < (count - 1)) {
182                     // explicitly remove component
183                     // (even if reparenting is implicit)
184                     remove(component);
185                     add(component, offset + 1);
186                     return true;
187                 }
188             } catch (Exception e) {
189             }
190 
191             return false;
192         }
193     }
194 
popInFrontOf(Component move, Component behind)195     public boolean popInFrontOf(Component move, Component behind) {
196         synchronized (getTreeLock()) {
197             try {
198                 if (move != behind) {
199                     // Ensure they are present
200                     checkLineage(move);
201                     checkLineage(behind);
202 
203                     // explicitly remove component
204                     // (even if reparenting is implicit)
205                     remove(move);
206                     // Re-add
207                     addBefore(move, behind);
208                 }
209                 return true;
210             } catch (Exception e) {
211                 return false;
212             }
213         }
214     }
215 
pushBehind(Component move, Component front)216     public boolean pushBehind(Component move, Component front) {
217         synchronized (getTreeLock()) {
218             try {
219                 if (move != front) {
220                     // Ensure they are present
221                     checkLineage(move);
222                     checkLineage(front);
223 
224                     // explicitly remove component
225                     // (even if reparenting is implicit)
226                     remove(move);
227                     // re-add in proper location
228                     addAfter(move, front);
229                 }
230                 return true;
231             } catch (Exception e) {
232                 return false;
233             }
234         }
235     }
236 
group()237     public void group() {
238         grouped = true;
239     }
240 
ungroup()241     public void ungroup() {
242         grouped = false;
243     }
244 
isGrouped()245     public boolean isGrouped() {
246        return grouped;
247     }
248 
249     private boolean grouped = false;
250 
251     private static final long serialVersionUID = 263606166411114032L;
252 }
253