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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGridGUI.LiveDeployment;
import java.awt.Cursor;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import com.jgoodies.looks.BorderStyle;
import com.jgoodies.looks.HeaderStyle;
import com.jgoodies.looks.Options;
import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import IceGrid.*;
import IceGridGUI.*;
class ShowLogDialog extends JDialog
{
static interface FileIteratorFactory
{
FileIteratorPrx open(int count)
throws Ice.UserException;
String getTitle();
String getDefaultFilename();
}
private class FIFOTextArea extends JTextArea
{
FIFOTextArea(int rows, int colums)
{
super(rows, colums);
setEditable(false);
setLineWrap(true);
}
public void appendLines(final String[] lines, final int maxLines, final int maxSize)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
for(int i = 0; i < lines.length; ++i)
{
//
// The last line is always incomplete
//
if(i + 1 != lines.length)
{
append(lines[i] + "\n");
}
else
{
append(lines[i]);
}
removeLines(maxLines, maxSize);
}
}
});
}
public void removeLines(int maxLines, int maxSize)
{
javax.swing.text.Document doc = getDocument();
javax.swing.text.Element rootElt = doc.getDefaultRootElement();
//
// We keep at least one line, no matter its length
//
int lineCount = getLineCount();
while(lineCount > 1 && (doc.getLength() > maxSize || (lineCount > maxLines)))
{
javax.swing.text.Element firstLine = rootElt.getElement(0);
try
{
doc.remove(0, firstLine.getEndOffset());
}
catch(javax.swing.text.BadLocationException ble)
{
assert false;
}
lineCount--;
}
setCaretPosition(doc.getLength());
}
}
private class ReaderThread extends Thread
{
ReaderThread()
{
_threadMaxLines = _maxLines;
_threadMaxSize = _maxSize;
_threadInitialLines = _initialLines;
_threadMaxReadSize = _maxReadSize;
_threadPeriod = _period;
_playButton.setSelected(true);
_playItem.setSelected(true);
_pause.setEnabled(true);
}
private void openError(final String message)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
if(_textArea.getText() == null || _textArea.getText().length() == 0)
{
close(true);
}
else
{
stopReading();
}
JOptionPane.showMessageDialog(
ShowLogDialog.this,
message,
_factory.getTitle() + ": cannot open file",
JOptionPane.ERROR_MESSAGE);
}
});
}
public void run()
{
//
// Open file
//
int initialLines;
synchronized(this)
{
initialLines = _threadInitialLines;
}
try
{
_p = _factory.open(initialLines);
}
catch(Ice.UserException e)
{
openError(e.toString());
return;
}
catch(Ice.LocalException e)
{
openError(e.toString());
return;
}
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
if(isVisible())
{
_textArea.setText(null);
}
else
{
setVisible(true);
}
}
});
boolean firstRun = true;
for(;;)
{
synchronized(this)
{
if(!_done)
{
if(_paused)
{
while(_paused && !_done)
{
try
{
wait();
}
catch(InterruptedException e)
{
}
}
}
else if(!firstRun)
{
try
{
wait(_threadPeriod);
}
catch(InterruptedException e)
{
}
}
else
{
firstRun = false;
}
}
if(_done)
{
cleanupIterator();
return;
}
}
boolean eofEncountered = false;
while(!eofEncountered)
{
int maxLines;
int maxSize;
int maxReadSize;
synchronized(this)
{
if(_done || _paused)
{
break; // while(!eofEncountered)
}
maxLines = _threadMaxLines;
maxSize = _threadMaxSize;
maxReadSize = _threadMaxReadSize;
}
Ice.StringSeqHolder linesHolder = new Ice.StringSeqHolder();
try
{
eofEncountered = _p.read(maxReadSize, linesHolder);
}
catch(IceGrid.FileNotAvailableException e)
{
_textArea.appendLines(new String[]
{
"---------------------------",
"IceGridAdmin caught: " + e.toString(),
"---------------------------"
}, maxLines, maxSize);
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
stopReading();
}
});
cleanupIterator();
return;
}
catch(Ice.LocalException e)
{
_textArea.appendLines(new String[]
{
"---------------------------",
"IceGridAdmin caught: " + e.toString(),
"---------------------------"
}, maxLines, maxSize);
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
stopReading();
}
});
return;
}
_textArea.appendLines(linesHolder.value, maxLines, maxSize);
}
}
}
private void cleanupIterator()
{
try
{
_p.destroy();
}
catch(Ice.LocalException e)
{
// Ignored, maybe should log warning
}
}
synchronized void pause()
{
if(!_paused)
{
_paused = true;
notify();
}
}
synchronized void terminate()
{
if(!_done)
{
_done = true;
notify();
}
}
synchronized void play()
{
if(_paused)
{
_paused = false;
notify();
}
}
synchronized void setPrefs()
{
_threadMaxLines = _maxLines;
_threadMaxSize = _maxSize;
_threadInitialLines = _initialLines;
_threadMaxReadSize = _maxReadSize;
_threadPeriod = _period;
}
private FileIteratorPrx _p;
private boolean _done = false;
private boolean _paused = false;
private int _threadMaxLines;
private int _threadMaxSize;
private int _threadInitialLines;
private int _threadMaxReadSize;
private int _threadPeriod;
}
private class MenuBar extends JMenuBar
{
private MenuBar()
{
putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH);
putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY, BorderStyle.SEPARATOR);
final int MENU_MASK = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
//
// File menu
//
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(java.awt.event.KeyEvent.VK_F);
add(fileMenu);
ButtonGroup bg = new ButtonGroup();
_pauseItem = new JRadioButtonMenuItem(_pause);
fileMenu.add(_pauseItem);
bg.add(_pauseItem);
_playItem = new JRadioButtonMenuItem(_play);
fileMenu.add(_playItem);
bg.add(_playItem);
_stopItem = new JRadioButtonMenuItem(_stop);
fileMenu.add(_stopItem);
bg.add(_stopItem);
fileMenu.addSeparator();
Action save = new AbstractAction("Save As...")
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = _root.getCoordinator().getSaveLogChooser();
fileChooser.setSelectedFile(new java.io.File(fileChooser.getCurrentDirectory(),
_factory.getDefaultFilename()));
java.io.File file = null;
while(file == null)
{
int result = fileChooser.showSaveDialog(ShowLogDialog.this);
if(result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
if(file != null)
{
if(!file.exists() && file.getName().indexOf('.') == -1)
{
file = new java.io.File(file.getAbsolutePath() + ".log");
}
java.io.OutputStreamWriter os = null;
try
{
os = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file));
String txt = _textArea.getText();
if(txt == null)
{
txt = "";
}
os.write(txt, 0, txt.length());
}
catch(java.io.IOException io)
{
JOptionPane.showMessageDialog(
ShowLogDialog.this,
io.toString(),
"Cannot write file",
JOptionPane.ERROR_MESSAGE);
}
finally
{
if(os != null)
{
try
{
os.close();
}
catch(java.io.IOException io)
{
}
}
}
}
}
else
{
break; // while
}
}
}
};
save.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, MENU_MASK));
save.putValue(Action.SHORT_DESCRIPTION, "Save As...");
fileMenu.add(save);
fileMenu.addSeparator();
fileMenu.add(new AbstractAction("Close")
{
public void actionPerformed(ActionEvent e)
{
close(true);
}
});
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(java.awt.event.KeyEvent.VK_E);
add(editMenu);
Action copy = new AbstractAction("Copy")
{
public void actionPerformed(ActionEvent e)
{
_textArea.copy();
}
};
copy.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, MENU_MASK));
copy.putValue(Action.SHORT_DESCRIPTION, "Copy");
editMenu.add(copy);
editMenu.addSeparator();
Action selectAll = new AbstractAction("Select All")
{
public void actionPerformed(ActionEvent e)
{
_textArea.grabFocus();
_textArea.selectAll();
}
};
selectAll.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, MENU_MASK));
selectAll.putValue(Action.SHORT_DESCRIPTION, "Select All");
editMenu.add(selectAll);
editMenu.addSeparator();
editMenu.add(new AbstractAction("Preferences...")
{
public void actionPerformed(ActionEvent e)
{
new ShowLogPrefsDialog(ShowLogDialog.this);
}
});
}
}
private class ToolBar extends JToolBar
{
private ToolBar()
{
putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH);
putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY, BorderStyle.SEPARATOR);
setFloatable(false);
putClientProperty("JToolBar.isRollover", Boolean.TRUE);
_pauseButton = new JToggleButton(_pause);
_pauseButton.setText(null);
_pauseButton.setIcon(Utils.getIcon("/icons/16x16/pause.png"));
add(_pauseButton);
_playButton = new JToggleButton(_play);
_playButton.setText(null);
_playButton.setIcon(Utils.getIcon("/icons/16x16/start.png"));
add(_playButton);
_stopButton = new JToggleButton(_stop);
_stopButton.setText(null);
_stopButton.setIcon(Utils.getIcon("/icons/16x16/stop.png"));
add(_stopButton);
ButtonGroup bg = new ButtonGroup();
bg.add(_pauseButton);
bg.add(_playButton);
bg.add(_stopButton);
}
}
ShowLogDialog(Root root, FileIteratorFactory factory, int maxLines, int maxSize, int initialLines, int maxReadSize,
int period)
{
super(root.getCoordinator().getMainFrame(), factory.getTitle() + " - IceGrid Admin", false);
_maxLines = maxLines;
_maxSize = maxSize;
_initialLines = initialLines;
_maxReadSize = maxReadSize;
_period = period;
_factory = factory;
_root = root;
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
close(true);
}
});
_pause = new AbstractAction("Pause")
{
public void actionPerformed(ActionEvent e)
{
pause();
}
};
_play = new AbstractAction("Play")
{
public void actionPerformed(ActionEvent e)
{
play();
}
};
_stop = new AbstractAction("Stop")
{
public void actionPerformed(ActionEvent e)
{
stopReading();
}
};
setJMenuBar(new MenuBar());
getContentPane().add(new ToolBar(), BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(_textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(scrollPane);
pack();
setResizable(true);
setLocationRelativeTo(root.getCoordinator().getMainFrame());
play();
}
void pause()
{
_thread.pause();
_pauseItem.setSelected(true);
_pauseButton.setSelected(true);
}
void stopReading()
{
if(_thread != null)
{
_thread.terminate();
try
{
_thread.join();
}
catch(InterruptedException e)
{
}
_thread = null;
_stopItem.setSelected(true);
_stopButton.setSelected(true);
_pause.setEnabled(false);
}
}
void play()
{
if(_thread != null)
{
_thread.play();
_playItem.setSelected(true);
_playButton.setSelected(true);
_pause.setEnabled(true);
}
else
{
_thread = new ReaderThread();
_thread.start();
}
}
int getMaxLines()
{
return _maxLines;
}
int getMaxSize()
{
return _maxSize;
}
int getInitialLines()
{
return _initialLines;
}
int getMaxReadSize()
{
return _maxReadSize;
}
int getPeriod()
{
return _period;
}
void setPrefs(int maxLines, int maxSize, int initialLines, int maxReadSize, int period)
{
if(maxLines < 50)
{
maxLines = 50;
}
_maxLines = maxLines;
if(maxSize < 1000)
{
maxSize = 1000;
}
_maxSize = maxSize;
_initialLines = initialLines;
if(maxReadSize < 100)
{
maxReadSize = 100;
}
else if(maxReadSize + 512 > _root.getMessageSizeMax())
{
maxReadSize = _root.getMessageSizeMax() - 512;
}
_maxReadSize = maxReadSize;
if(period < 200)
{
period = 200;
}
else if(period > 5000)
{
period = 5000;
}
_period = period;
if(_thread != null)
{
_thread.setPrefs();
}
_root.setLogPrefs(_maxLines, _maxSize, _initialLines, _maxReadSize, _period);
}
void close(boolean notifyRoot)
{
stopReading();
if(notifyRoot)
{
_root.removeShowLogDialog(_factory.getTitle());
}
dispose();
}
private final Root _root;
private final FileIteratorFactory _factory;
private int _maxLines;
private int _maxSize;
private int _initialLines;
private int _maxReadSize;
private int _period;
private Action _play;
private Action _pause;
private Action _stop;
private JRadioButtonMenuItem _playItem;
private JRadioButtonMenuItem _pauseItem;
private JRadioButtonMenuItem _stopItem;
private JToggleButton _playButton;
private JToggleButton _pauseButton;
private JToggleButton _stopButton;
private FIFOTextArea _textArea = new FIFOTextArea(20, 45);
private ReaderThread _thread;
}
|