1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util.swing;
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
/**
* Implements the <tt>LayoutManager</tt> which lays out the local and remote
* videos in a video <tt>Call</tt>.
*
* @author Lyubomir Marinov
* @author Yana Stamcheva
*/
public class VideoLayout extends FitLayout
{
/**
* The video canvas constraint.
*/
public static final String CANVAS = "CANVAS";
/**
* The center remote video constraint.
*/
public static final String CENTER_REMOTE = "CENTER_REMOTE";
/**
* The close local video constraint.
*/
public static final String CLOSE_LOCAL_BUTTON = "CLOSE_LOCAL_BUTTON";
/**
* The east remote video constraint.
*/
public static final String EAST_REMOTE = "EAST_REMOTE";
/**
* The local video constraint.
*/
public static final String LOCAL = "LOCAL";
/**
* The ration between the local and the remote video.
*/
private static final float LOCAL_TO_REMOTE_RATIO = 0.30f;
/**
* The video canvas.
*/
private Component canvas;
/**
* The close local video button component.
*/
private Component closeButton;
/**
* The map of component constraints.
*/
private final HashMap<Component, Object> constraints
= new HashMap<Component, Object>();
/**
* The component containing the local video.
*/
private Component local;
/**
* The list of <tt>Component</tt>s depicting remote videos.
*/
private final List<Component> remotes = new LinkedList<Component>();
/**
* The x coordinate alignment of the remote video.
*/
private float remoteAlignmentX = Component.CENTER_ALIGNMENT;
/**
* Adds the given component in this layout on the specified by name
* position.
*
* @param name the constraint giving the position of the component in this
* layout
* @param comp the component to add
*/
@Override
public void addLayoutComponent(String name, Component comp)
{
super.addLayoutComponent(name, comp);
synchronized (constraints)
{
this.constraints.put(comp, name);
}
if ((name == null) || name.equals(CENTER_REMOTE))
{
remotes.add(comp);
remoteAlignmentX = Component.CENTER_ALIGNMENT;
}
else if (name.equals(EAST_REMOTE))
{
remotes.add(comp);
remoteAlignmentX = Component.RIGHT_ALIGNMENT;
}
else if (name.equals(LOCAL))
local = comp;
else if (name.equals(CLOSE_LOCAL_BUTTON))
closeButton = comp;
else if (name.equals(CANVAS))
canvas = comp;
}
/**
* Determines how may columns to use for the grid display of specific remote
* visual/video <tt>Component</tt>s.
*
* @param remotes the remote visual/video <tt>Component</tt>s to be
* displayed in a grid
* @return the number of columns to use for the grid display of the
* specified remote visual/video <tt>Component</tt>s
*/
private int calculateColumnCount(List<Component> remotes)
{
int remotesCount = remotes.size();
if (remotesCount == 1)
return 1;
else if (remotesCount == 2 || remotesCount == 4)
return 2;
else
return 3;
}
/**
* Returns the remote video component.
*
* @return the remote video component
*/
@Override
protected Component getComponent(Container parent)
{
return (remotes.size() == 1) ? remotes.get(0) : null;
}
/**
* Returns the constraints for the given component.
*
* @param c the component for which constraints we're looking for
* @return the constraints for the given component
*/
public Object getComponentConstraints(Component c)
{
synchronized (constraints)
{
return constraints.get(c);
}
}
/**
* Returns the local video component.
*
* @return the local video component
*/
public Component getLocal()
{
return local;
}
/**
* Returns the local video close button.
*
* @return the local video close button
*/
public Component getLocalCloseButton()
{
return closeButton;
}
/**
* Lays out this given container.
*
* @param parent the container to lay out
*/
@Override
public void layoutContainer(Container parent)
{
List<Component> remotes;
Component local = getLocal();
/*
* When there are multiple remote visual/video Components, the local one
* will be displayed as if it is a remote one i.e. in the same grid, not
* on top of a remote one.
*/
if ((this.remotes.size() > 1) && (local != null))
{
remotes = new ArrayList<Component>();
remotes.addAll(this.remotes);
remotes.add(local);
}
else
remotes = this.remotes;
int remoteCount = remotes.size();
// We need to reduce parent size in order to fit also the space we've
// left between videos.
Dimension parentSize = parent.getSize();
parentSize.width -= remoteCount*10;
if (remoteCount == 1)
{
super.layoutContainer(parent,
(local == null)
? Component.CENTER_ALIGNMENT
: remoteAlignmentX);
}
else if (remoteCount > 0)
{
int columns = calculateColumnCount(remotes);
int rows = (remoteCount + columns - 1) / columns;
Rectangle bounds
= new Rectangle(
0,
0,
parentSize.width / columns,
parentSize.height / rows);
int columnsMinus1 = columns - 1;
int i = 0;
for (Component remote : remotes)
{
/*
* We want the remote videos ordered from right to left so that
* the local video does not cover a remote video when possible.
*/
bounds.x = (columnsMinus1 - (i % columns)) * bounds.width;
bounds.y = (i / columns) * bounds.height;
if (bounds.x > 0)
bounds.x += ((columns - (i%columns))*10);
super.layoutComponent(
remote,
bounds,
Component.CENTER_ALIGNMENT,
Component.CENTER_ALIGNMENT);
i++;
if (i >= remoteCount)
break;
}
}
if (local != null)
{
/*
* If the local visual/video Component is not displayed as if it is
* a remote one, it will be placed on top of a remote one.
*/
if (!remotes.contains(local))
{
Component remote0 = remotes.isEmpty() ? null : remotes.get(0);
int localX;
int localY;
int height
= Math.round(parentSize.height * LOCAL_TO_REMOTE_RATIO);
int width
= Math.round(parentSize.width * LOCAL_TO_REMOTE_RATIO);
/*
* XXX The remote Component being a JLabel is meant to signal
* that there is no remote video and the remote is the
* photoLabel.
*/
if ((remotes.size() == 1) && (remote0 instanceof JLabel))
{
localX = parentSize.width/2 - width/2;
localY = parentSize.height - height;
super.layoutComponent(
local,
new Rectangle(localX, localY, width, height),
Component.CENTER_ALIGNMENT,
Component.BOTTOM_ALIGNMENT);
}
else
{
localX = ((remote0 == null) ? 0 : remote0.getX()) + 5;
localY = parentSize.height - height - 5;
super.layoutComponent(
local,
new Rectangle(localX, localY, width, height),
Component.LEFT_ALIGNMENT,
Component.BOTTOM_ALIGNMENT);
}
}
if (closeButton != null)
{
super.layoutComponent(
closeButton,
new Rectangle(
local.getX() + local.getWidth()
- closeButton.getWidth(),
local.getY(),
closeButton.getWidth(),
closeButton.getHeight()),
Component.CENTER_ALIGNMENT,
Component.CENTER_ALIGNMENT);
}
}
if (canvas != null)
{
/*
* The video canvas will get the locations of the other components
* to paint so it has to cover the parent completely.
*/
canvas.setBounds(0, 0, parentSize.width, parentSize.height);
}
}
/**
* Returns the minimum layout size for the given container.
*
* @param parent the container which minimum layout size we're looking for
* @return a Dimension containing, the minimum layout size for the given
* container
*/
@Override
public Dimension minimumLayoutSize(Container parent)
{
// TODO Auto-generated method stub
return super.minimumLayoutSize(parent);
}
/**
* Returns the preferred layout size for the given container.
*
* @param parent the container which preferred layout size we're looking for
* @return a Dimension containing, the preferred layout size for the given
* container
*/
@Override
public Dimension preferredLayoutSize(Container parent)
{
List<Component> remotes;
Component local = getLocal();
/*
* When there are multiple remote visual/video Components, the local one
* will be displayed as if it is a remote one i.e. in the same grid, not
* on top of a remote one.
*/
if ((this.remotes.size() > 1) && (local != null))
{
remotes = new ArrayList<Component>();
remotes.addAll(this.remotes);
remotes.add(local);
}
else
remotes = this.remotes;
int remoteCount = remotes.size();
if (remoteCount == 0)
{
/*
* If there is no remote visual/video Component, the local one will
* serve the preferredSize of the Container.
*/
if (local != null)
{
Dimension preferredSize = local.getPreferredSize();
if (preferredSize != null)
return preferredSize;
}
}
else if (remoteCount == 1)
{
/*
* If there is a single remote visual/video Component, the local one
* will be on top of it so the remote one will serve the
* preferredSize of the Container.
*/
Dimension preferredSize = remotes.get(0).getPreferredSize();
if (preferredSize != null)
return preferredSize;
}
else if (remoteCount > 1)
{
int maxWidth = 0;
int maxHeight = 0;
for (Component remote : remotes)
{
Dimension preferredSize = remote.getPreferredSize();
if (preferredSize != null)
{
if (maxWidth < preferredSize.width)
maxWidth = preferredSize.width;
if (maxHeight < preferredSize.height)
maxHeight = preferredSize.height;
}
}
if ((maxWidth > 0) && (maxHeight > 0))
{
int columns = calculateColumnCount(remotes);
int rows = (remoteCount + columns - 1) / columns;
System.out.println("EHIIIIII WIDTH=========" + maxWidth * columns
+ "EHIIIIIIIII HEIGHT========" + maxHeight * rows);
return new Dimension(maxWidth * columns, maxHeight * rows);
}
}
return super.preferredLayoutSize(parent);
}
/**
* Removes the given component from this layout.
*
* @param comp the component to remove from the layout
*/
@Override
public void removeLayoutComponent(Component comp)
{
super.removeLayoutComponent(comp);
if (local == comp)
local = null;
else if (closeButton == comp)
closeButton = null;
else if (canvas == comp)
canvas = null;
else
remotes.remove(comp);
}
}
|