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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* SIP Communicator, 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.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.service.keybindings.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
/**
* A custom frame that remembers its size and location and could have a
* semi-transparent background.
*
* @author Yana Stamcheva
* @author Lubomir Marinov
*/
public abstract class SIPCommFrame
extends JFrame
implements Observer
{
/**
* The <tt>Logger</tt> used by the <tt>SIPCommFrame</tt> class and its
* instances for logging output.
*/
private static final Logger logger = Logger.getLogger(SIPCommFrame.class);
private static final String SIP_COMMUNICATOR_LOGO =
"service.gui.SIP_COMMUNICATOR_LOGO";
private final ActionMap amap;
private final InputMap imap;
private KeybindingSet bindings = null;
/**
* Creates a <tt>SIPCommFrame</tt>.
*/
public SIPCommFrame()
{
this.setContentPane(new MainContentPane());
Image scLogo = UtilActivator.getImage(SIP_COMMUNICATOR_LOGO);
this.setIconImage(scLogo);
// In order to have the same icon when using option panes
JOptionPane.getRootFrame().setIconImage(scLogo);
this.addWindowListener(new FrameWindowAdapter());
JRootPane rootPane = getRootPane();
amap = rootPane.getActionMap();
amap.put("close", new CloseAction());
imap =
rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
/**
* The action invoked when user presses Escape key.
*/
private class CloseAction
extends UIAction
{
public void actionPerformed(ActionEvent e)
{
saveSizeAndLocation();
close(true);
}
}
/**
* Sets the input map to utilize a given category of keybindings. The frame
* is updated to reflect the new bindings when they change. This replaces
* any previous bindings that have been added.
*
* @param category set of keybindings to be utilized
*/
protected void setKeybindingInput(KeybindingSet.Category category)
{
// Removes old binding set
if (this.bindings != null)
{
this.bindings.deleteObserver(this);
resetInputMap();
}
// Adds new bindings to input map
this.bindings =
UtilActivator.getKeybindingsService().getBindings(category);
for (Map.Entry<KeyStroke, String> key2action : this.bindings
.getBindings().entrySet())
{
imap.put(key2action.getKey(), key2action.getValue());
}
this.bindings.addObserver(this);
}
/**
* Bindings the string representation for a keybinding to the action that
* will be executed.
*
* @param binding string representation of action used by input map
* @param action the action which will be executed when user presses the
* given key combination
*/
protected void addKeybindingAction(String binding, Action action)
{
amap.put(binding, action);
}
private static class FrameWindowAdapter
extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
((SIPCommFrame) e.getWindow()).windowClosing(e);
}
}
/**
* Invoked when this window is in the process of being closed. The close
* operation can be overridden at this point.
*
* @param e
*/
protected void windowClosing(WindowEvent e)
{
/*
* Before closing the application window save the current size and
* position through the ConfigurationService.
*/
saveSizeAndLocation();
close(false);
}
/**
* Saves the size and the location of this frame through the
* <tt>ConfigurationService</tt>.
*/
private void saveSizeAndLocation()
{
try
{
saveSizeAndLocation(this);
}
catch (PropertyVetoException e)
{
logger
.error(
"Saving the size and the location properties failed",
e);
}
}
/**
* Saves the size and the location of a specific <tt>Component</tt> through
* the <tt>ConfigurationService</tt>.
*
* @param component the <tt>Component</tt> which is to have its size and
* location saved through the <tt>ConfigurationService</tt>
* @throws PropertyVetoException if the <tt>ConfigurationService</tt> does
* not accept the saving because of objections from its
* <tt>PropertyVetoListener</tt>s.
*/
static void saveSizeAndLocation(Component component)
throws PropertyVetoException
{
Map<String, Object> props = new HashMap<String, Object>();
String className
= component.getClass().getName().replaceAll("\\$", "_");
props.put(className + ".width", component.getWidth());
props.put(className + ".height", component.getHeight());
props.put(className + ".x", component.getX());
props.put(className + ".y", component.getY());
UtilActivator.getConfigurationService().setProperties(props);
}
/**
* Sets window size and position.
*/
public void setSizeAndLocation()
{
ConfigurationService configService =
UtilActivator.getConfigurationService();
String className = this.getClass().getName();
String widthString = configService.getString(className + ".width");
String heightString = configService.getString(className + ".height");
String xString = configService.getString(className + ".x");
String yString = configService.getString(className + ".y");
int width = 0;
int height = 0;
if (widthString != null && heightString != null)
{
width = Integer.parseInt(widthString);
height = Integer.parseInt(heightString);
if (width > 0 && height > 0)
{
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
if (width <= screenSize.width && height <= screenSize.height)
this.setSize(width, height);
}
}
int x = 0;
int y = 0;
if (xString != null && yString != null)
{
x = Integer.parseInt(xString);
y = Integer.parseInt(yString);
this.setLocation(x, y);
}
else
{
this.setCenterLocation();
}
}
/**
* Positions this window in the center of the screen.
*/
private void setCenterLocation()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(screenSize.width / 2 - this.getWidth() / 2,
screenSize.height / 2 - this.getHeight() / 2);
}
/**
* Checks whether the current component will exceeds the screen size and if
* it do will set a default size
*/
private void ensureOnScreenLocationAndSize()
{
int x = this.getX();
int y = this.getY();
int width = this.getWidth();
int height = this.getHeight();
Rectangle virtualBounds = ScreenInformation.getScreenBounds();
// the default distance to the screen border
final int borderDistance = 10;
// in case any of the sizes exceeds the screen size
// we set default one
// get the left upper point of the window
if (!(virtualBounds.contains(x, y)))
{
// top left exceeds screen bounds
if (x < virtualBounds.x)
{
// window is too far to the left
// move it to the right
x = virtualBounds.x + borderDistance;
}
else if (x > virtualBounds.x)
{
// window is too far to the right
// can only occour, when screen resolution is
// changed or displayed are disconnected
// move the window in the bounds to the very right
x =
virtualBounds.x + virtualBounds.width - width
- borderDistance;
if (x < virtualBounds.x + borderDistance)
{
x = virtualBounds.x + borderDistance;
}
}
// top left exceeds screen bounds
if (y < virtualBounds.y)
{
// window is too far to the top
// move it to the bottom
y = virtualBounds.y + borderDistance;
}
else if (y > virtualBounds.y)
{
// window is too far to the bottom
// can only occour, when screen resolution is
// changed or displayed are disconnected
// move the window in the bounds to the very bottom
y =
virtualBounds.y + virtualBounds.height - height
- borderDistance;
if (y < virtualBounds.y + borderDistance)
{
y = virtualBounds.y + borderDistance;
}
}
this.setLocation(x, y);
}
// check the lower right corder
if (!(virtualBounds.contains(x + width, y + height)))
{
if (x + width > virtualBounds.x + virtualBounds.width)
{
// location of window is too far to the right, its right
// border is out of bounds
// calculate a new horizontal position
// move the whole window to the left
x =
virtualBounds.x + virtualBounds.width - width
- borderDistance;
if (x < virtualBounds.x + borderDistance)
{
// window is already on left side, it is too wide.
x = virtualBounds.x + borderDistance;
// reduce the width, so it surely fits
width = virtualBounds.width - 2 * borderDistance;
}
}
if (y + height > virtualBounds.y + virtualBounds.height)
{
// location of window is too far to the bottom, its bottom
// border is out of bounds
// calculate a new vertical position
// move the whole window to the top
y =
virtualBounds.y + virtualBounds.height - height
- borderDistance;
if (y < virtualBounds.y + borderDistance)
{
// window is already on top, it is too high.
y = virtualBounds.y + borderDistance;
// reduce the width, so it surely fits
height = virtualBounds.height - 2 * borderDistance;
}
}
this.setPreferredSize(new Dimension(width, height));
this.setSize(width, height);
this.setLocation(x, y);
}
}
/**
* Overwrites the setVisible method in order to set the size and the
* position of this window before showing it.
*/
public void setVisible(boolean isVisible)
{
if (isVisible)
{
this.setSizeAndLocation();
this.ensureOnScreenLocationAndSize();
}
super.setVisible(isVisible);
}
/**
* Overwrites the setVisible method in order to set the size and the
* position of this window before showing it.
* @param isVisible indicates if this window will be made visible or will
* be hidden
* @param isPackEnabled indicates if the pack() method should be invoked
* before showing this window
*/
public void setVisible(boolean isVisible, boolean isPackEnabled)
{
if (isVisible)
{
/*
* Since setSizeAndLocation() will use the width and the height,
* pack() should be called prior to it. Otherwise, the width and the
* height may be zero or may just change after setSizeAndLocation()
* during pack().
*/
this.pack();
this.setSizeAndLocation();
this.ensureOnScreenLocationAndSize();
}
super.setVisible(isVisible);
}
/**
* Overwrites the dispose method in order to save the size and the position
* of this window before closing it.
*/
public void dispose()
{
this.saveSizeAndLocation();
/*
* The keybinding service will outlive us so don't let us retain our
* memory.
*/
if (bindings != null)
bindings.deleteObserver(this);
super.dispose();
}
private void resetInputMap()
{
imap.clear();
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
}
/**
* Listens for changes in binding sets so they can be reflected in the input
* map
*/
public void update(Observable obs, Object arg)
{
if (obs instanceof KeybindingSet)
{
KeybindingSet changedBindings = (KeybindingSet) obs;
resetInputMap();
for (Map.Entry<KeyStroke, String> key2action : changedBindings
.getBindings().entrySet())
{
imap.put(key2action.getKey(), key2action.getValue());
}
}
}
public static class MainContentPane
extends JPanel
{
private final boolean isColorBgEnabled;
private final boolean isImageBgEnabled;
private final Color bgStartColor;
private final Color bgEndColor;
private BufferedImage bgImage = null;
private TexturePaint texture = null;
public MainContentPane()
{
super(new BorderLayout());
ResourceManagementService resources =
UtilActivator.getResources();
int borderSize =
resources
.getSettingsInt("impl.gui.MAIN_WINDOW_BORDER_SIZE");
this.setBorder(BorderFactory.createEmptyBorder(borderSize,
borderSize, borderSize, borderSize));
isColorBgEnabled =
new Boolean(resources.getSettingsString(
"impl.gui.IS_WINDOW_COLOR_BACKGROUND_ENABLED"))
.booleanValue();
if (isColorBgEnabled)
{
bgStartColor =
new Color(resources.getColor("service.gui.MAIN_BACKGROUND"));
bgEndColor =
new Color(resources
.getColor("service.gui.MAIN_BACKGROUND_GRADIENT"));
}
else
{
bgStartColor = null;
bgEndColor = null;
}
isImageBgEnabled =
new Boolean(resources.getSettingsString(
"impl.gui.IS_WINDOW_IMAGE_BACKGROUND_ENABLED"))
.booleanValue();
if (isImageBgEnabled)
{
final URL bgImagePath
= resources.getImageURL("service.gui.WINDOW_TITLE_BAR_BG");
bgImage = ImageUtils.getBufferedImage(bgImagePath);
final Rectangle rect =
new Rectangle(0, 0, bgImage.getWidth(),
bgImage.getHeight());
texture = new TexturePaint(bgImage, rect);
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// If the custom color or image window background is not enabled we
// have nothing to do here.
if (isColorBgEnabled || isImageBgEnabled)
{
g = g.create();
try
{
internalPaintComponent(g);
}
finally
{
g.dispose();
}
}
}
private void internalPaintComponent(Graphics g)
{
AntialiasingManager.activateAntialiasing(g);
Graphics2D g2 = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
if (isColorBgEnabled)
{
GradientPaint bgGradientColor =
new GradientPaint(width / 2, 0, bgStartColor, width / 2, 80,
bgEndColor);
g2.setPaint(bgGradientColor);
g2.fillRect(0, 0, width, 80);
g2.setColor(bgEndColor);
g2.fillRect(0, 78, width, height);
}
if (isImageBgEnabled)
{
if (bgImage != null && texture != null)
{
g2.setPaint(texture);
g2.fillRect(0, 0, this.getWidth(), bgImage.getHeight());
}
}
}
}
/**
* All functions implemented in this method will be invoked when user
* presses the Escape key.
*/
protected abstract void close(boolean isEscaped);
}
|