ICE 3.4.2
[php5-ice-freebsdport.git] / java / src / IceGridGUI / LiveDeployment / ShowLogDialog.java
blobf681425ea116e5898743f3b61c09c9b94fe6e3c1
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
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;
45 import IceGrid.*;
46 import IceGridGUI.*;
48 class ShowLogDialog extends JDialog
50 static interface FileIteratorFactory
52 FileIteratorPrx open(int count)
53 throws Ice.UserException;
55 String getTitle();
57 String getDefaultFilename();
60 private class FIFOTextArea extends JTextArea
62 FIFOTextArea(int rows, int colums)
64 super(rows, colums);
65 setEditable(false);
66 setLineWrap(true);
69 public void appendLines(final String[] lines, final int maxLines, final int maxSize)
71 SwingUtilities.invokeLater(new Runnable()
73 public void run()
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");
84 else
86 append(lines[i]);
88 removeLines(maxLines, maxSize);
91 });
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)
112 assert false;
114 lineCount--;
116 setCaretPosition(doc.getLength());
120 private class ReaderThread extends Thread
122 ReaderThread()
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(
138 new Runnable()
140 public void run()
142 if(_textArea.getText() == null || _textArea.getText().length() == 0)
144 close(true);
146 else
148 stopReading();
151 JOptionPane.showMessageDialog(
152 ShowLogDialog.this,
153 message,
154 _factory.getTitle() + ": cannot open file",
155 JOptionPane.ERROR_MESSAGE);
160 public void run()
163 // Open file
165 int initialLines;
167 synchronized(this)
169 initialLines = _threadInitialLines;
174 _p = _factory.open(initialLines);
176 catch(Ice.UserException e)
178 openError(e.toString());
179 return;
181 catch(Ice.LocalException e)
183 openError(e.toString());
184 return;
187 SwingUtilities.invokeLater(
188 new Runnable()
190 public void run()
192 if(isVisible())
194 _textArea.setText(null);
196 else
198 setVisible(true);
203 boolean firstRun = true;
204 for(;;)
206 synchronized(this)
208 if(!_done)
210 if(_paused)
212 while(_paused && !_done)
216 wait();
218 catch(InterruptedException e)
223 else if(!firstRun)
227 wait(_threadPeriod);
229 catch(InterruptedException e)
233 else
235 firstRun = false;
239 if(_done)
241 cleanupIterator();
242 return;
246 boolean eofEncountered = false;
248 while(!eofEncountered)
250 int maxLines;
251 int maxSize;
252 int maxReadSize;
254 synchronized(this)
256 if(_done || _paused)
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(
281 new Runnable()
283 public void run()
285 stopReading();
288 cleanupIterator();
289 return;
291 catch(Ice.LocalException e)
293 _textArea.appendLines(new String[]
295 "---------------------------",
296 "IceGridAdmin caught: " + e.toString(),
297 "---------------------------"
298 }, maxLines, maxSize);
299 SwingUtilities.invokeLater(
300 new Runnable()
302 public void run()
304 stopReading();
307 return;
310 _textArea.appendLines(linesHolder.value, maxLines, maxSize);
315 private void cleanupIterator()
319 _p.destroy();
321 catch(Ice.LocalException e)
323 // Ignored, maybe should log warning
327 synchronized void pause()
329 if(!_paused)
331 _paused = true;
332 notify();
336 synchronized void terminate()
338 if(!_done)
340 _done = true;
341 notify();
345 synchronized void play()
347 if(_paused)
349 _paused = false;
350 notify();
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
376 private MenuBar()
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();
384 // File menu
386 JMenu fileMenu = new JMenu("File");
387 fileMenu.setMnemonic(java.awt.event.KeyEvent.VK_F);
388 add(fileMenu);
390 ButtonGroup bg = new ButtonGroup();
392 _pauseItem = new JRadioButtonMenuItem(_pause);
393 fileMenu.add(_pauseItem);
394 bg.add(_pauseItem);
395 _playItem = new JRadioButtonMenuItem(_play);
396 fileMenu.add(_playItem);
397 bg.add(_playItem);
398 _stopItem = new JRadioButtonMenuItem(_stop);
399 fileMenu.add(_stopItem);
400 bg.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;
414 while(file == null)
416 int result = fileChooser.showSaveDialog(ShowLogDialog.this);
417 if(result == JFileChooser.APPROVE_OPTION)
419 file = fileChooser.getSelectedFile();
421 if(file != null)
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();
434 if(txt == null)
436 txt = "";
438 os.write(txt, 0, txt.length());
440 catch(java.io.IOException io)
442 JOptionPane.showMessageDialog(
443 ShowLogDialog.this,
444 io.toString(),
445 "Cannot write file",
446 JOptionPane.ERROR_MESSAGE);
448 finally
450 if(os != null)
454 os.close();
456 catch(java.io.IOException io)
463 else
465 break; // while
470 save.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, MENU_MASK));
471 save.putValue(Action.SHORT_DESCRIPTION, "Save As...");
472 fileMenu.add(save);
473 fileMenu.addSeparator();
475 fileMenu.add(new AbstractAction("Close")
477 public void actionPerformed(ActionEvent e)
479 close(true);
482 JMenu editMenu = new JMenu("Edit");
483 editMenu.setMnemonic(java.awt.event.KeyEvent.VK_E);
484 add(editMenu);
486 Action copy = new AbstractAction("Copy")
488 public void actionPerformed(ActionEvent e)
490 _textArea.copy();
494 copy.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, MENU_MASK));
495 copy.putValue(Action.SHORT_DESCRIPTION, "Copy");
496 editMenu.add(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
524 private ToolBar()
526 putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH);
527 putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY, BorderStyle.SEPARATOR);
528 setFloatable(false);
529 putClientProperty("JToolBar.isRollover", Boolean.TRUE);
531 _pauseButton = new JToggleButton(_pause);
532 _pauseButton.setText(null);
533 _pauseButton.setIcon(Utils.getIcon("/icons/16x16/pause.png"));
534 add(_pauseButton);
535 _playButton = new JToggleButton(_play);
536 _playButton.setText(null);
537 _playButton.setIcon(Utils.getIcon("/icons/16x16/start.png"));
538 add(_playButton);
539 _stopButton = new JToggleButton(_stop);
540 _stopButton.setText(null);
541 _stopButton.setIcon(Utils.getIcon("/icons/16x16/stop.png"));
542 add(_stopButton);
544 ButtonGroup bg = new ButtonGroup();
545 bg.add(_pauseButton);
546 bg.add(_playButton);
547 bg.add(_stopButton);
551 ShowLogDialog(Root root, FileIteratorFactory factory, int maxLines, int maxSize, int initialLines, int maxReadSize,
552 int period)
554 super(root.getCoordinator().getMainFrame(), factory.getTitle() + " - IceGrid Admin", false);
556 _maxLines = maxLines;
557 _maxSize = maxSize;
558 _initialLines = initialLines;
559 _maxReadSize = maxReadSize;
560 _period = period;
561 _factory = factory;
562 _root = root;
564 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
565 addWindowListener(new java.awt.event.WindowAdapter()
567 public void windowClosing(java.awt.event.WindowEvent e)
569 close(true);
573 _pause = new AbstractAction("Pause")
575 public void actionPerformed(ActionEvent e)
577 pause();
581 _play = new AbstractAction("Play")
583 public void actionPerformed(ActionEvent e)
585 play();
589 _stop = new AbstractAction("Stop")
591 public void actionPerformed(ActionEvent e)
593 stopReading();
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);
606 pack();
607 setResizable(true);
609 setLocationRelativeTo(root.getCoordinator().getMainFrame());
610 play();
613 void pause()
615 _thread.pause();
616 _pauseItem.setSelected(true);
617 _pauseButton.setSelected(true);
620 void stopReading()
622 if(_thread != null)
624 _thread.terminate();
628 _thread.join();
630 catch(InterruptedException e)
634 _thread = null;
635 _stopItem.setSelected(true);
636 _stopButton.setSelected(true);
637 _pause.setEnabled(false);
641 void play()
643 if(_thread != null)
645 _thread.play();
646 _playItem.setSelected(true);
647 _playButton.setSelected(true);
648 _pause.setEnabled(true);
650 else
652 _thread = new ReaderThread();
653 _thread.start();
657 int getMaxLines()
659 return _maxLines;
662 int getMaxSize()
664 return _maxSize;
667 int getInitialLines()
669 return _initialLines;
672 int getMaxReadSize()
674 return _maxReadSize;
677 int getPeriod()
679 return _period;
682 void setPrefs(int maxLines, int maxSize, int initialLines, int maxReadSize, int period)
684 if(maxLines < 50)
686 maxLines = 50;
688 _maxLines = maxLines;
690 if(maxSize < 1000)
692 maxSize = 1000;
694 _maxSize = maxSize;
696 _initialLines = initialLines;
698 if(maxReadSize < 100)
700 maxReadSize = 100;
702 else if(maxReadSize + 512 > _root.getMessageSizeMax())
704 maxReadSize = _root.getMessageSizeMax() - 512;
706 _maxReadSize = maxReadSize;
708 if(period < 200)
710 period = 200;
712 else if(period > 5000)
714 period = 5000;
716 _period = period;
718 if(_thread != null)
720 _thread.setPrefs();
723 _root.setLogPrefs(_maxLines, _maxSize, _initialLines, _maxReadSize, _period);
726 void close(boolean notifyRoot)
728 stopReading();
730 if(notifyRoot)
732 _root.removeShowLogDialog(_factory.getTitle());
734 dispose();
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;
744 private int _period;
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;