1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.text.util;
22
23 import java.awt.Component;
24 import java.awt.Dimension;
25 import java.awt.GraphicsEnvironment;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.HeadlessException;
29 import java.awt.Insets;
30 import java.beans.PropertyChangeEvent;
31 import java.beans.PropertyChangeListener;
32 import java.lang.reflect.InvocationTargetException;
33 import java.util.Locale;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.SwingConstants;
39 import javax.swing.SwingUtilities;
40 import javax.swing.plaf.basic.BasicHTML;
41 import javax.swing.text.View;
42 import org.jdtaus.core.container.ContainerFactory;
43 import org.jdtaus.core.logging.spi.Logger;
44 import org.jdtaus.core.text.MessageEvent;
45 import org.jdtaus.core.text.MessageListener;
46
47
48
49
50
51
52
53
54
55
56
57
58 public final class SwingMessagePane implements MessageListener
59 {
60
61
62
63
64
65
66
67
68
69
70 private Logger getLogger()
71 {
72 return (Logger) ContainerFactory.getContainer().
73 getDependency( this, "Logger" );
74
75 }
76
77
78
79
80
81
82 private Locale getLocale()
83 {
84 return (Locale) ContainerFactory.getContainer().
85 getDependency( this, "Locale" );
86
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 private java.lang.Boolean isDefaultResizable()
103 {
104 return (java.lang.Boolean) ContainerFactory.getContainer().
105 getProperty( this, "defaultResizable" );
106
107 }
108
109
110
111
112
113
114 private java.lang.Integer getDefaultMaximumMessages()
115 {
116 return (java.lang.Integer) ContainerFactory.getContainer().
117 getProperty( this, "defaultMaximumMessages" );
118
119 }
120
121
122
123
124
125
126 private java.lang.Integer getDefaultColumns()
127 {
128 return (java.lang.Integer) ContainerFactory.getContainer().
129 getProperty( this, "defaultColumns" );
130
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public void onMessage( final MessageEvent event )
146 {
147 if ( event != null )
148 {
149 try
150 {
151 final Runnable r = new Runnable()
152 {
153
154 public void run()
155 {
156 final JPanel panel = new JPanel();
157 panel.setLayout( new GridBagLayout() );
158
159 for ( int i = 0, s0 = event.getMessages().length; i < s0 && i < getMaximumMessages(); i++ )
160 {
161 String text = event.getMessages()[i].getText( getLocale() );
162
163 if ( !BasicHTML.isHTMLString( text ) )
164 {
165 text = "<html>" + HtmlEntities.escapeHtml( text ) + "</html>";
166 }
167
168 final JLabel label = new HtmlLabel( getColumns() );
169 label.setText( text );
170
171 final GridBagConstraints c = new GridBagConstraints();
172 c.anchor = GridBagConstraints.NORTHWEST;
173 c.fill = GridBagConstraints.BOTH;
174 c.gridheight = 1;
175 c.gridwidth = 1;
176 c.gridx = 0;
177 c.gridy = i;
178 c.weightx = 1.0D;
179
180 panel.add( label, c );
181
182 if ( !( i + 1 < s0 && i + 1 < getMaximumMessages() ) )
183 {
184 final JPanel p = new JPanel();
185 c.anchor = GridBagConstraints.SOUTH;
186 c.weighty = 1.0D;
187 c.gridy = i + 1;
188 panel.add( p, c );
189 }
190 }
191
192 JOptionPane optionPane = null;
193 JDialog dialog = null;
194
195 switch ( event.getType() )
196 {
197 case MessageEvent.INFORMATION:
198 optionPane = new JOptionPane();
199 optionPane.setMessage( panel );
200 optionPane.setMessageType( JOptionPane.INFORMATION_MESSAGE );
201 dialog = optionPane.createDialog( getParent(), getInformationMessage( getLocale() ) );
202 dialog.setResizable( isResizable() );
203 dialog.setVisible( true );
204 dialog.dispose();
205 break;
206
207 case MessageEvent.NOTIFICATION:
208 optionPane = new JOptionPane();
209 optionPane.setMessage( panel );
210 optionPane.setMessageType( JOptionPane.INFORMATION_MESSAGE );
211 dialog = optionPane.createDialog( getParent(), getNotificationMessage( getLocale() ) );
212 dialog.setResizable( isResizable() );
213 dialog.setVisible( true );
214 dialog.dispose();
215 break;
216
217 case MessageEvent.WARNING:
218 optionPane = new JOptionPane();
219 optionPane.setMessage( panel );
220 optionPane.setMessageType( JOptionPane.WARNING_MESSAGE );
221 dialog = optionPane.createDialog( getParent(), getWarningMessage( getLocale() ) );
222 dialog.setResizable( isResizable() );
223 dialog.setVisible( true );
224 dialog.dispose();
225 break;
226
227 case MessageEvent.ERROR:
228 optionPane = new JOptionPane();
229 optionPane.setMessage( panel );
230 optionPane.setMessageType( JOptionPane.ERROR_MESSAGE );
231 dialog = optionPane.createDialog( getParent(), getErrorMessage( getLocale() ) );
232 dialog.setResizable( isResizable() );
233 dialog.setVisible( true );
234 dialog.dispose();
235 break;
236
237 default:
238 getLogger().warn( getUnknownMessageEventTypeMessage(
239 getLocale(), new Integer( event.getType() ) ) );
240
241 }
242 }
243
244 };
245
246 if ( SwingUtilities.isEventDispatchThread() )
247 {
248 r.run();
249 }
250 else
251 {
252 SwingUtilities.invokeAndWait( r );
253 }
254 }
255 catch ( final InterruptedException e )
256 {
257 this.getLogger().error( e );
258 }
259 catch ( final InvocationTargetException e )
260 {
261 this.getLogger().error( e );
262 }
263 }
264 }
265
266
267
268
269
270 private Component parent;
271
272
273 private Integer maximumMessages;
274
275
276 private Integer columns;
277
278
279 private Boolean resizable;
280
281
282
283
284
285
286
287
288
289 public SwingMessagePane( final Component parent )
290 {
291 this( parent, Integer.MIN_VALUE, Integer.MIN_VALUE, false );
292 }
293
294
295
296
297
298
299
300
301
302
303
304 public SwingMessagePane( final Component parent, final int maximumMessages )
305 {
306 this( parent, maximumMessages, Integer.MIN_VALUE, false );
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 public SwingMessagePane( final Component parent, final int maximumMessages, final int columns )
324 {
325 this( parent, maximumMessages, columns, false );
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 public SwingMessagePane( final Component parent, final int maximumMessages, final int columns,
345 final boolean resizable )
346 {
347 if ( parent == null )
348 {
349 throw new NullPointerException( "parent" );
350 }
351
352 if ( GraphicsEnvironment.isHeadless() )
353 {
354 throw new HeadlessException();
355 }
356
357 this.parent = parent;
358
359 if ( maximumMessages > 0 )
360 {
361 this.maximumMessages = new Integer( maximumMessages );
362 }
363 if ( columns > 0 )
364 {
365 this.columns = new Integer( columns );
366 }
367
368 this.resizable = Boolean.valueOf( resizable );
369 }
370
371
372
373
374
375
376 public Component getParent()
377 {
378 return this.parent;
379 }
380
381
382
383
384
385
386
387
388 public void setParent( final Component parent )
389 {
390 if ( parent == null )
391 {
392 throw new NullPointerException( "parent" );
393 }
394
395 this.parent = parent;
396 }
397
398
399
400
401
402
403 public int getMaximumMessages()
404 {
405 if ( this.maximumMessages == null )
406 {
407 this.maximumMessages = this.getDefaultMaximumMessages();
408 }
409
410 return this.maximumMessages.intValue();
411 }
412
413
414
415
416
417
418
419
420 public int getColumns()
421 {
422 if ( this.columns == null )
423 {
424 this.columns = this.getDefaultColumns();
425 }
426
427 return this.columns.intValue();
428 }
429
430
431
432
433
434
435
436
437 public boolean isResizable()
438 {
439 if ( this.resizable == null )
440 {
441 this.resizable = this.isDefaultResizable();
442 }
443
444 return this.resizable.booleanValue();
445 }
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 private String getUnknownMessageEventTypeMessage( final Locale locale,
464 final java.lang.Number unknownEventType )
465 {
466 return ContainerFactory.getContainer().
467 getMessage( this, "unknownMessageEventType", locale,
468 new Object[]
469 {
470 unknownEventType
471 });
472
473 }
474
475
476
477
478
479
480
481
482
483
484 private String getInformationMessage( final Locale locale )
485 {
486 return ContainerFactory.getContainer().
487 getMessage( this, "information", locale, null );
488
489 }
490
491
492
493
494
495
496
497
498
499
500 private String getNotificationMessage( final Locale locale )
501 {
502 return ContainerFactory.getContainer().
503 getMessage( this, "notification", locale, null );
504
505 }
506
507
508
509
510
511
512
513
514
515
516 private String getWarningMessage( final Locale locale )
517 {
518 return ContainerFactory.getContainer().
519 getMessage( this, "warning", locale, null );
520
521 }
522
523
524
525
526
527
528
529
530
531
532 private String getErrorMessage( final Locale locale )
533 {
534 return ContainerFactory.getContainer().
535 getMessage( this, "error", locale, null );
536
537 }
538
539
540
541
542 }
543
544
545
546
547
548
549
550 class HtmlLabel extends JLabel
551 {
552
553
554 private static final long serialVersionUID = -3022796716512336911L;
555
556
557 private int columns;
558
559
560
561
562
563
564
565 HtmlLabel( final int columns )
566 {
567 super();
568 this.columns = columns;
569 this.setVerticalAlignment( SwingConstants.TOP );
570 this.setVerticalTextPosition( SwingConstants.TOP );
571
572 if ( columns > 0 )
573 {
574 this.addPropertyChangeListener( new PropertyChangeListener()
575 {
576
577 public void propertyChange( final PropertyChangeEvent evt )
578 {
579 if ( ( "text".equals( evt.getPropertyName() )
580 || "font".equals( evt.getPropertyName() )
581 || "border".equals( evt.getPropertyName() ) )
582 && getText() != null
583 && BasicHTML.isHTMLString( getText() ) )
584 {
585 setPreferredSize( computePreferredSize() );
586 }
587 }
588
589 } );
590 }
591 }
592
593 private Dimension computePreferredSize()
594 {
595 Dimension preferredSize = null;
596 final Insets insets = this.getInsets();
597 final int width = this.columns * this.getFontMetrics( this.getFont() ).charWidth( 'W' )
598 + ( insets != null ? insets.left + insets.right : 0 );
599
600 final JLabel label = new JLabel( this.getText() );
601 final Object html = label.getClientProperty( BasicHTML.propertyKey );
602
603 if ( html instanceof View )
604 {
605 final View view = (View) html;
606 view.setSize( width, 0 );
607 preferredSize = new Dimension( (int) view.getPreferredSpan( View.X_AXIS ),
608 (int) view.getPreferredSpan( View.Y_AXIS ) );
609
610 }
611
612 return preferredSize;
613 }
614
615 }