1 // **********************************************************************
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
8 // **********************************************************************
10 package IceGridGUI
.LiveDeployment
;
12 import java
.awt
.Cursor
;
13 import java
.awt
.BorderLayout
;
14 import java
.awt
.Container
;
15 import java
.awt
.Dimension
;
16 import java
.awt
.Frame
;
18 import java
.awt
.event
.ActionEvent
;
19 import java
.awt
.event
.ActionListener
;
20 import java
.awt
.event
.KeyEvent
;
22 import javax
.swing
.AbstractAction
;
23 import javax
.swing
.Action
;
24 import javax
.swing
.ButtonGroup
;
25 import javax
.swing
.JComponent
;
26 import javax
.swing
.JDialog
;
27 import javax
.swing
.JFileChooser
;
28 import javax
.swing
.JMenu
;
29 import javax
.swing
.JMenuBar
;
30 import javax
.swing
.JOptionPane
;
31 import javax
.swing
.JPanel
;
32 import javax
.swing
.JRadioButtonMenuItem
;
33 import javax
.swing
.JScrollPane
;
34 import javax
.swing
.JTextArea
;
35 import javax
.swing
.JToggleButton
;
36 import javax
.swing
.JToolBar
;
37 import javax
.swing
.KeyStroke
;
38 import javax
.swing
.SwingUtilities
;
40 import com
.jgoodies
.looks
.BorderStyle
;
41 import com
.jgoodies
.looks
.HeaderStyle
;
42 import com
.jgoodies
.looks
.Options
;
43 import com
.jgoodies
.looks
.plastic
.PlasticLookAndFeel
;
48 class ShowLogDialog
extends JDialog
50 static interface FileIteratorFactory
52 FileIteratorPrx
open(int count
)
53 throws Ice
.UserException
;
57 String
getDefaultFilename();
60 private class FIFOTextArea
extends JTextArea
62 FIFOTextArea(int rows
, int colums
)
69 public void appendLines(final String
[] lines
, final int maxLines
, final int maxSize
)
71 SwingUtilities
.invokeLater(new Runnable()
75 for(int i
= 0; i
< lines
.length
; ++i
)
78 // The last line is always incomplete
80 if(i
+ 1 != lines
.length
)
82 append(lines
[i
] + "\n");
88 removeLines(maxLines
, maxSize
);
94 public void removeLines(int maxLines
, int maxSize
)
96 javax
.swing
.text
.Document doc
= getDocument();
97 javax
.swing
.text
.Element rootElt
= doc
.getDefaultRootElement();
100 // We keep at least one line, no matter its length
102 int lineCount
= getLineCount();
103 while(lineCount
> 1 && (doc
.getLength() > maxSize
|| (lineCount
> maxLines
)))
105 javax
.swing
.text
.Element firstLine
= rootElt
.getElement(0);
108 doc
.remove(0, firstLine
.getEndOffset());
110 catch(javax
.swing
.text
.BadLocationException ble
)
116 setCaretPosition(doc
.getLength());
120 private class ReaderThread
extends Thread
124 _threadMaxLines
= _maxLines
;
125 _threadMaxSize
= _maxSize
;
126 _threadInitialLines
= _initialLines
;
127 _threadMaxReadSize
= _maxReadSize
;
128 _threadPeriod
= _period
;
130 _playButton
.setSelected(true);
131 _playItem
.setSelected(true);
132 _pause
.setEnabled(true);
135 private void openError(final String message
)
137 SwingUtilities
.invokeLater(
142 if(_textArea
.getText() == null || _textArea
.getText().length() == 0)
151 JOptionPane
.showMessageDialog(
154 _factory
.getTitle() + ": cannot open file",
155 JOptionPane
.ERROR_MESSAGE
);
169 initialLines
= _threadInitialLines
;
174 _p
= _factory
.open(initialLines
);
176 catch(Ice
.UserException e
)
178 openError(e
.toString());
181 catch(Ice
.LocalException e
)
183 openError(e
.toString());
187 SwingUtilities
.invokeLater(
194 _textArea
.setText(null);
203 boolean firstRun
= true;
212 while(_paused
&& !_done
)
218 catch(InterruptedException e
)
229 catch(InterruptedException e
)
246 boolean eofEncountered
= false;
248 while(!eofEncountered
)
258 break; // while(!eofEncountered)
261 maxLines
= _threadMaxLines
;
262 maxSize
= _threadMaxSize
;
263 maxReadSize
= _threadMaxReadSize
;
266 Ice
.StringSeqHolder linesHolder
= new Ice
.StringSeqHolder();
270 eofEncountered
= _p
.read(maxReadSize
, linesHolder
);
272 catch(IceGrid
.FileNotAvailableException e
)
274 _textArea
.appendLines(new String
[]
276 "---------------------------",
277 "IceGridAdmin caught: " + e
.toString(),
278 "---------------------------"
279 }, maxLines
, maxSize
);
280 SwingUtilities
.invokeLater(
291 catch(Ice
.LocalException e
)
293 _textArea
.appendLines(new String
[]
295 "---------------------------",
296 "IceGridAdmin caught: " + e
.toString(),
297 "---------------------------"
298 }, maxLines
, maxSize
);
299 SwingUtilities
.invokeLater(
310 _textArea
.appendLines(linesHolder
.value
, maxLines
, maxSize
);
315 private void cleanupIterator()
321 catch(Ice
.LocalException e
)
323 // Ignored, maybe should log warning
327 synchronized void pause()
336 synchronized void terminate()
345 synchronized void play()
354 synchronized void setPrefs()
356 _threadMaxLines
= _maxLines
;
357 _threadMaxSize
= _maxSize
;
358 _threadInitialLines
= _initialLines
;
359 _threadMaxReadSize
= _maxReadSize
;
360 _threadPeriod
= _period
;
363 private FileIteratorPrx _p
;
364 private boolean _done
= false;
365 private boolean _paused
= false;
367 private int _threadMaxLines
;
368 private int _threadMaxSize
;
369 private int _threadInitialLines
;
370 private int _threadMaxReadSize
;
371 private int _threadPeriod
;
374 private class MenuBar
extends JMenuBar
378 putClientProperty(Options
.HEADER_STYLE_KEY
, HeaderStyle
.BOTH
);
379 putClientProperty(PlasticLookAndFeel
.BORDER_STYLE_KEY
, BorderStyle
.SEPARATOR
);
381 final int MENU_MASK
= java
.awt
.Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask();
386 JMenu fileMenu
= new JMenu("File");
387 fileMenu
.setMnemonic(java
.awt
.event
.KeyEvent
.VK_F
);
390 ButtonGroup bg
= new ButtonGroup();
392 _pauseItem
= new JRadioButtonMenuItem(_pause
);
393 fileMenu
.add(_pauseItem
);
395 _playItem
= new JRadioButtonMenuItem(_play
);
396 fileMenu
.add(_playItem
);
398 _stopItem
= new JRadioButtonMenuItem(_stop
);
399 fileMenu
.add(_stopItem
);
401 fileMenu
.addSeparator();
403 Action save
= new AbstractAction("Save As...")
405 public void actionPerformed(ActionEvent e
)
407 JFileChooser fileChooser
= _root
.getCoordinator().getSaveLogChooser();
409 fileChooser
.setSelectedFile(new java
.io
.File(fileChooser
.getCurrentDirectory(),
410 _factory
.getDefaultFilename()));
412 java
.io
.File file
= null;
416 int result
= fileChooser
.showSaveDialog(ShowLogDialog
.this);
417 if(result
== JFileChooser
.APPROVE_OPTION
)
419 file
= fileChooser
.getSelectedFile();
423 if(!file
.exists() && file
.getName().indexOf('.') == -1)
425 file
= new java
.io
.File(file
.getAbsolutePath() + ".log");
428 java
.io
.OutputStreamWriter os
= null;
432 os
= new java
.io
.OutputStreamWriter(new java
.io
.FileOutputStream(file
));
433 String txt
= _textArea
.getText();
438 os
.write(txt
, 0, txt
.length());
440 catch(java
.io
.IOException io
)
442 JOptionPane
.showMessageDialog(
446 JOptionPane
.ERROR_MESSAGE
);
456 catch(java
.io
.IOException io
)
470 save
.putValue(Action
.ACCELERATOR_KEY
, KeyStroke
.getKeyStroke(KeyEvent
.VK_S
, MENU_MASK
));
471 save
.putValue(Action
.SHORT_DESCRIPTION
, "Save As...");
473 fileMenu
.addSeparator();
475 fileMenu
.add(new AbstractAction("Close")
477 public void actionPerformed(ActionEvent e
)
482 JMenu editMenu
= new JMenu("Edit");
483 editMenu
.setMnemonic(java
.awt
.event
.KeyEvent
.VK_E
);
486 Action copy
= new AbstractAction("Copy")
488 public void actionPerformed(ActionEvent e
)
494 copy
.putValue(Action
.ACCELERATOR_KEY
, KeyStroke
.getKeyStroke(KeyEvent
.VK_C
, MENU_MASK
));
495 copy
.putValue(Action
.SHORT_DESCRIPTION
, "Copy");
498 editMenu
.addSeparator();
499 Action selectAll
= new AbstractAction("Select All")
501 public void actionPerformed(ActionEvent e
)
503 _textArea
.grabFocus();
504 _textArea
.selectAll();
507 selectAll
.putValue(Action
.ACCELERATOR_KEY
, KeyStroke
.getKeyStroke(KeyEvent
.VK_A
, MENU_MASK
));
508 selectAll
.putValue(Action
.SHORT_DESCRIPTION
, "Select All");
510 editMenu
.add(selectAll
);
511 editMenu
.addSeparator();
512 editMenu
.add(new AbstractAction("Preferences...")
514 public void actionPerformed(ActionEvent e
)
516 new ShowLogPrefsDialog(ShowLogDialog
.this);
522 private class ToolBar
extends JToolBar
526 putClientProperty(Options
.HEADER_STYLE_KEY
, HeaderStyle
.BOTH
);
527 putClientProperty(PlasticLookAndFeel
.BORDER_STYLE_KEY
, BorderStyle
.SEPARATOR
);
529 putClientProperty("JToolBar.isRollover", Boolean
.TRUE
);
531 _pauseButton
= new JToggleButton(_pause
);
532 _pauseButton
.setText(null);
533 _pauseButton
.setIcon(Utils
.getIcon("/icons/16x16/pause.png"));
535 _playButton
= new JToggleButton(_play
);
536 _playButton
.setText(null);
537 _playButton
.setIcon(Utils
.getIcon("/icons/16x16/start.png"));
539 _stopButton
= new JToggleButton(_stop
);
540 _stopButton
.setText(null);
541 _stopButton
.setIcon(Utils
.getIcon("/icons/16x16/stop.png"));
544 ButtonGroup bg
= new ButtonGroup();
545 bg
.add(_pauseButton
);
551 ShowLogDialog(Root root
, FileIteratorFactory factory
, int maxLines
, int maxSize
, int initialLines
, int maxReadSize
,
554 super(root
.getCoordinator().getMainFrame(), factory
.getTitle() + " - IceGrid Admin", false);
556 _maxLines
= maxLines
;
558 _initialLines
= initialLines
;
559 _maxReadSize
= maxReadSize
;
564 setDefaultCloseOperation(JDialog
.DO_NOTHING_ON_CLOSE
);
565 addWindowListener(new java
.awt
.event
.WindowAdapter()
567 public void windowClosing(java
.awt
.event
.WindowEvent e
)
573 _pause
= new AbstractAction("Pause")
575 public void actionPerformed(ActionEvent e
)
581 _play
= new AbstractAction("Play")
583 public void actionPerformed(ActionEvent e
)
589 _stop
= new AbstractAction("Stop")
591 public void actionPerformed(ActionEvent e
)
597 setJMenuBar(new MenuBar());
598 getContentPane().add(new ToolBar(), BorderLayout
.PAGE_START
);
600 JScrollPane scrollPane
= new JScrollPane(_textArea
,
601 JScrollPane
.VERTICAL_SCROLLBAR_AS_NEEDED
,
602 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
604 getContentPane().add(scrollPane
);
609 setLocationRelativeTo(root
.getCoordinator().getMainFrame());
616 _pauseItem
.setSelected(true);
617 _pauseButton
.setSelected(true);
630 catch(InterruptedException e
)
635 _stopItem
.setSelected(true);
636 _stopButton
.setSelected(true);
637 _pause
.setEnabled(false);
646 _playItem
.setSelected(true);
647 _playButton
.setSelected(true);
648 _pause
.setEnabled(true);
652 _thread
= new ReaderThread();
667 int getInitialLines()
669 return _initialLines
;
682 void setPrefs(int maxLines
, int maxSize
, int initialLines
, int maxReadSize
, int period
)
688 _maxLines
= maxLines
;
696 _initialLines
= initialLines
;
698 if(maxReadSize
< 100)
702 else if(maxReadSize
+ 512 > _root
.getMessageSizeMax())
704 maxReadSize
= _root
.getMessageSizeMax() - 512;
706 _maxReadSize
= maxReadSize
;
712 else if(period
> 5000)
723 _root
.setLogPrefs(_maxLines
, _maxSize
, _initialLines
, _maxReadSize
, _period
);
726 void close(boolean notifyRoot
)
732 _root
.removeShowLogDialog(_factory
.getTitle());
737 private final Root _root
;
738 private final FileIteratorFactory _factory
;
740 private int _maxLines
;
741 private int _maxSize
;
742 private int _initialLines
;
743 private int _maxReadSize
;
746 private Action _play
;
747 private Action _pause
;
748 private Action _stop
;
750 private JRadioButtonMenuItem _playItem
;
751 private JRadioButtonMenuItem _pauseItem
;
752 private JRadioButtonMenuItem _stopItem
;
754 private JToggleButton _playButton
;
755 private JToggleButton _pauseButton
;
756 private JToggleButton _stopButton
;
758 private FIFOTextArea _textArea
= new FIFOTextArea(20, 45);
759 private ReaderThread _thread
;