/plugins/SVNPlugin/tags/0.6.0/trunk/src/ise/plugin/svn/SubversionGUILogHandler.java

#
Java | 330 lines | 165 code | 27 blank | 138 comment | 24 complexity | d0236f4d746e227da547bfeb08562c4f MD5 | raw file

✨ Summary
  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn;
  26. import java.io.Serializable;
  27. import java.util.logging.*;
  28. import java.awt.*;
  29. import java.awt.event.*;
  30. import javax.swing.*;
  31. import javax.swing.event.*;
  32. import javax.swing.text.*;
  33. import ise.plugin.svn.library.*;
  34. import org.gjt.sp.jedit.GUIUtilities;
  35. import org.gjt.sp.jedit.gui.RolloverButton;
  36. /**
  37. * Borrowed from Antelope:
  38. *
  39. * A simple log handler for SVNPlugin that shows the SVN output in a GUI.
  40. * The output can either be in a separate frame, or applications can get
  41. * the textarea to position themselves.<p>
  42. * Level.INFO is logged in blue text.<br>
  43. * Level.WARNING is logged in green text.<br>
  44. * Level.SEVERE is logged in red text.<br>
  45. * All other levels are logged in black text.<br>
  46. *
  47. * @author Dale Anson, danson@germane-software.com
  48. * @created July 22, 2002
  49. */
  50. public class SubversionGUILogHandler extends Handler implements Serializable {
  51. /**
  52. * The output area.
  53. */
  54. private JTextPane _text;
  55. private boolean _tail = true;
  56. private JPanel _content_pane;
  57. /**
  58. * Optional frame
  59. */
  60. private JFrame _frame;
  61. /**
  62. * Green
  63. */
  64. private Color GREEN = new Color( 0, 153, 51 );
  65. /**
  66. * Current font
  67. */
  68. private Font _font = null;
  69. /**
  70. * Constructor for the AntelopeGUILogHandler object
  71. */
  72. public SubversionGUILogHandler() {
  73. this( false );
  74. }
  75. /**
  76. * Constructor for the AntelopeGUILogHandler object
  77. *
  78. * @param use_frame If true, will show the output in a separate frame.
  79. */
  80. public SubversionGUILogHandler( boolean use_frame ) {
  81. _content_pane = new JPanel( new BorderLayout() );
  82. _text = new JTextPane();
  83. _text.setCaretPosition( 0 );
  84. _content_pane.add( new JScrollPane( _text ), BorderLayout.CENTER );
  85. _content_pane.add( getControlPanel(), BorderLayout.SOUTH );
  86. if ( use_frame ) {
  87. _frame = new JFrame( "Subversion Logger" );
  88. _frame.addWindowListener(
  89. new WindowAdapter() {
  90. public void windowClosing( WindowEvent we ) {
  91. _frame.setVisible( false );
  92. }
  93. }
  94. );
  95. _frame.getContentPane().add( _content_pane );
  96. _frame.setSize( 600, 400 );
  97. GUIUtils.centerOnScreen( _frame );
  98. }
  99. setFormatter( new LogFormatter() );
  100. }
  101. /**
  102. * Description of the Class
  103. */
  104. public class LogFormatter extends Formatter {
  105. /**
  106. * Description of the Method
  107. *
  108. * @param record
  109. * @return Description of the Returned Value
  110. */
  111. public String format( LogRecord record ) {
  112. return record.getMessage() + System.getProperty( "line.separator" );
  113. }
  114. }
  115. /**
  116. * Gets the textArea attribute of the AntelopeGUILogHandler object
  117. *
  118. * @return The textArea value
  119. */
  120. public JTextComponent getTextComponent() {
  121. return _text;
  122. }
  123. public Document getDocument() {
  124. return _text.getDocument();
  125. }
  126. public JPanel getPanel() {
  127. return _content_pane;
  128. }
  129. /**
  130. * Sets the font attribute of the AntelopeGUILogHandler object
  131. *
  132. * @param font The new font value
  133. */
  134. public void setFont( Font font ) {
  135. _font = font;
  136. }
  137. /**
  138. * Sets the visible attribute of the optional frame.
  139. *
  140. * @param b The new visible value
  141. */
  142. public void setVisible( boolean b ) {
  143. _frame.setVisible( b );
  144. }
  145. /**
  146. * Disposes of the optional frame.
  147. */
  148. public void dispose() {
  149. _frame.dispose();
  150. }
  151. /**
  152. * Sets the location attribute of the optional frame.
  153. *
  154. * @param x The new location value
  155. * @param y The new location value
  156. */
  157. public void setLocation( int x, int y ) {
  158. _frame.setLocation( x, y );
  159. }
  160. /**
  161. * Sets the bounds attribute of the optional frame.
  162. *
  163. * @param x The new bounds value
  164. * @param y The new bounds value
  165. * @param w The new bounds value
  166. * @param h The new bounds value
  167. */
  168. public void setBounds( int x, int y, int w, int h ) {
  169. _frame.setBounds( x, y, w, h );
  170. }
  171. /**
  172. * Gets the size attribute of the optional frame.
  173. *
  174. * @return The size value
  175. */
  176. public Dimension getSize() {
  177. return _frame.getSize();
  178. }
  179. /**
  180. * Finish out the log.
  181. */
  182. public void close() {
  183. if ( getFormatter() != null ) {
  184. publish( new LogRecord( Level.INFO, getFormatter().getTail( this ) ) );
  185. }
  186. }
  187. /**
  188. * Does nothing.
  189. */
  190. public void flush() {
  191. if ( getFormatter() != null ) {
  192. publish( new LogRecord( Level.INFO, getFormatter().getTail( this ) ) );
  193. }
  194. }
  195. /**
  196. * Starts the log.
  197. */
  198. public void open() {
  199. if ( getFormatter() != null ) {
  200. int index = _text.getDocument().getLength();
  201. try {
  202. _text.getDocument().insertString( index, getFormatter().getHead( SubversionGUILogHandler.this ), null );
  203. }
  204. catch ( Exception e ) {
  205. //Log.log( e );
  206. }
  207. }
  208. }
  209. /**
  210. * Appends the given record to the GUI.
  211. *
  212. * @param lr the LogRecord to write.
  213. */
  214. public void publish( final LogRecord lr ) {
  215. try {
  216. // I removed the invokeLater for svn plugin since the callers _should_
  217. // already be in an invokeLater. Calling invokeLater from within an
  218. // invokeLater causes the logging to be delayed to the UI.
  219. /*
  220. SwingUtilities.invokeLater(
  221. new Runnable() {
  222. public void run() {
  223. */
  224. String msg = lr.getMessage();
  225. if ( msg == null )
  226. return ;
  227. if ( getFormatter() != null )
  228. msg = getFormatter().format( lr );
  229. if ( _text == null ) {
  230. return ;
  231. }
  232. try {
  233. int index = _text.getDocument().getLength();
  234. int caret_position = _text.getCaretPosition();
  235. SimpleAttributeSet set = new SimpleAttributeSet();
  236. if ( _font == null ) {
  237. StyleConstants.setFontFamily( set, "Monospaced" );
  238. }
  239. else {
  240. StyleConstants.setFontFamily( set, _font.getFamily() );
  241. StyleConstants.setBold( set, _font.isBold() );
  242. StyleConstants.setItalic( set, _font.isItalic() );
  243. StyleConstants.setFontSize( set, _font.getSize() );
  244. }
  245. if ( lr.getLevel().equals( Level.WARNING ) ) {
  246. StyleConstants.setForeground( set, GREEN );
  247. }
  248. else if ( lr.getLevel().equals( Level.SEVERE ) ) {
  249. StyleConstants.setForeground( set, Color.RED );
  250. }
  251. else if ( lr.getLevel().equals( Level.INFO ) ) {
  252. StyleConstants.setForeground( set, Color.BLUE );
  253. }
  254. else {
  255. StyleConstants.setForeground( set, Color.BLACK );
  256. }
  257. _text.getDocument().insertString( index, msg, set );
  258. if ( _tail )
  259. _text.setCaretPosition( index + msg.length() );
  260. else
  261. _text.setCaretPosition( caret_position );
  262. }
  263. catch ( Exception e ) {
  264. //Log.log( e );
  265. }
  266. /*
  267. }
  268. }
  269. );
  270. */
  271. }
  272. catch ( Exception ignored ) {
  273. // ignored
  274. //Log.log( ignored );
  275. }
  276. }
  277. private JPanel getControlPanel() {
  278. JPanel panel = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
  279. final JCheckBox tail_cb = new JCheckBox( "Tail" );
  280. tail_cb.setSelected( true );
  281. tail_cb.addActionListener( new ActionListener() {
  282. public void actionPerformed( ActionEvent ae ) {
  283. _tail = tail_cb.isSelected();
  284. if ( _tail )
  285. _text.setCaretPosition( _text.getDocument().getLength() );
  286. }
  287. }
  288. );
  289. RolloverButton clear_btn = new RolloverButton( GUIUtilities.loadIcon( "Clear.png" ) );
  290. clear_btn.addActionListener(new ActionListener(){
  291. public void actionPerformed(ActionEvent ae) {
  292. _text.selectAll();
  293. _text.replaceSelection("");
  294. }
  295. });
  296. panel.add( tail_cb );
  297. panel.add( clear_btn );
  298. return panel;
  299. }
  300. }