/plugins/SVNPlugin/tags/0.5/src/ise/plugin/svn/gui/LogResultsPanel.java

# · Java · 241 lines · 190 code · 25 blank · 26 comment · 14 complexity · 54d3d3ff6d14ee99c5ccec55c85b5fd2 MD5 · raw file

  1. package ise.plugin.svn.gui;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.event.*;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. import javax.swing.*;
  8. import javax.swing.table.*;
  9. import javax.swing.border.EmptyBorder;
  10. import ise.java.awt.LambdaLayout;
  11. import ise.plugin.svn.action.DiffAction;
  12. import ise.plugin.svn.library.GUIUtils;
  13. import org.tmatesoft.svn.core.SVNLogEntry;
  14. import org.gjt.sp.jedit.GUIUtilities;
  15. import org.gjt.sp.jedit.View;
  16. public class LogResultsPanel extends JPanel {
  17. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z", Locale.getDefault() );
  18. private View view = null;
  19. private String username = null;
  20. private String password = null;
  21. /**
  22. * @param map with path/file name as key, a list of associated log entries as the value
  23. * @param showPaths whether or not path information for other files associated with each
  24. * revision are included in the log entries
  25. */
  26. public LogResultsPanel( TreeMap < String, List < SVNLogEntry >> results, boolean showPaths, View view, String username, String password ) {
  27. super( new LambdaLayout() );
  28. this.view = view;
  29. this.username = username;
  30. this.password = password;
  31. setBorder( new EmptyBorder( 3, 3, 3, 3 ) );
  32. boolean top = false;
  33. LambdaLayout.Constraints con = LambdaLayout.createConstraint();
  34. con.a = LambdaLayout.W;
  35. con.s = "w";
  36. con.p = 0;
  37. for ( String path : results.keySet() ) {
  38. JLabel label = new JLabel( "Path: " + path );
  39. // sort the entries
  40. List<SVNLogEntry> entries = results.get( path );
  41. Collections.sort( entries, new EntryComparator() );
  42. // put the results data into an array to pass to a JTable
  43. String[][] data = new String[ entries.size() ][ showPaths ? 5 : 4 ];
  44. Iterator it = entries.iterator();
  45. for ( int i = 0; it.hasNext(); i++ ) {
  46. SVNLogEntry entry = ( SVNLogEntry ) it.next();
  47. String revision = String.valueOf( entry.getRevision() );
  48. String date = DATE_FORMAT.format( entry.getDate() );
  49. String author = entry.getAuthor();
  50. String comment = entry.getMessage();
  51. data[ i ][ 0 ] = revision;
  52. data[ i ][ 1 ] = date;
  53. data[ i ][ 2 ] = author;
  54. data[ i ][ 3 ] = comment;
  55. StringBuffer associated_files;
  56. if ( showPaths && entry.getChangedPaths().size() > 0 ) {
  57. associated_files = new StringBuffer();
  58. String ls = System.getProperty( "line.separator" );
  59. Set changedPaths = entry.getChangedPaths().keySet();
  60. for ( Iterator iter = changedPaths.iterator(); iter.hasNext(); ) {
  61. String cp = ( String ) iter.next();
  62. associated_files.append( cp ).append( ls );
  63. }
  64. data[ i ][ 4 ] = associated_files.toString();
  65. }
  66. }
  67. String[] col_names = showPaths ?
  68. new String[] {"Revision", "Date", "Author", "Comment", "Paths"} :
  69. new String[] {"Revision", "Date", "Author", "Comment"};
  70. LogTable table = new LogTable( data, col_names );
  71. table.setPath( path );
  72. table.addMouseListener(new TableMouseListener(table));
  73. ToolTipManager.sharedInstance().registerComponent( table );
  74. // set column widths and cell renderers
  75. TableColumnModel column_model = table.getColumnModel();
  76. TableColumn column0 = column_model.getColumn( 0 ); // revision
  77. column0.setMaxWidth( 60 );
  78. column0.setPreferredWidth( 60 );
  79. column0.setCellRenderer( new TextCellRenderer() );
  80. TableColumn column1 = column_model.getColumn( 1 ); // date
  81. column1.setMaxWidth( 190 );
  82. column1.setPreferredWidth( 190 );
  83. column1.setCellRenderer( new TextCellRenderer() );
  84. TableColumn column2 = column_model.getColumn( 2 ); // author
  85. column2.setMaxWidth( 100 );
  86. column2.setPreferredWidth( 100 );
  87. column2.setCellRenderer( new TextCellRenderer() );
  88. TableColumn column3 = column_model.getColumn( 3 ); // comment
  89. column3.setCellRenderer( new CommentCellRenderer() );
  90. if ( showPaths ) {
  91. TableColumn column4 = column_model.getColumn( 4 ); // paths
  92. column4.setCellRenderer( new PathCellRenderer() );
  93. }
  94. add( label, con );
  95. ++con.y;
  96. add( GUIUtils.createTablePanel( table ), con );
  97. ++con.y;
  98. add( LambdaLayout.createVerticalStrut( 11, true ), con );
  99. ++con.y;
  100. }
  101. }
  102. public class LogTable extends JTable {
  103. private String path = null;
  104. public LogTable( Object[][] data, Object[] columnNames ) {
  105. super( data, columnNames );
  106. }
  107. public void setPath( String path ) {
  108. LogTable.this.path = path;
  109. }
  110. public String getPath() {
  111. return LogTable.this.path;
  112. }
  113. }
  114. /**
  115. * Non-wrapping text area cell renderer.
  116. */
  117. public class TextCellRenderer implements TableCellRenderer {
  118. private MeasurableTextArea textArea = new MeasurableTextArea();
  119. public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
  120. textArea.setText( value == null ? "" : value.toString() );
  121. table.setRowHeight( row, Math.max( textArea.getBestHeight(), table.getRowHeight() ) );
  122. textArea.setBackground( isSelected ? Color.LIGHT_GRAY : Color.WHITE );
  123. return textArea;
  124. }
  125. }
  126. /**
  127. * Non-wrapping text area cell renderer for the paths column.
  128. */
  129. public class PathCellRenderer implements TableCellRenderer {
  130. private MeasurableTextArea textArea = new MeasurableTextArea();
  131. public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
  132. textArea.setText( value == null ? "" : value.toString() );
  133. textArea.setToolTipText( "<html><b>Other files in this revision:</b><br><pre>" + textArea.getText() );
  134. textArea.setBackground( isSelected ? Color.LIGHT_GRAY : Color.WHITE );
  135. table.setRowHeight( row, Math.max( textArea.getBestHeight(), table.getRowHeight() ) );
  136. return textArea;
  137. }
  138. }
  139. /**
  140. * Wrapping text area cell renderer.
  141. */
  142. public class CommentCellRenderer implements TableCellRenderer {
  143. private MeasurableTextArea textArea = new MeasurableTextArea();
  144. public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
  145. textArea.setText( value == null ? "" : value.toString() );
  146. textArea.setLineWrap( true );
  147. textArea.setWrapStyleWord( true );
  148. textArea.setBackground( isSelected ? Color.LIGHT_GRAY : Color.WHITE );
  149. table.setRowHeight( row, Math.max( textArea.getBestHeight(), table.getRowHeight() ) );
  150. return textArea;
  151. }
  152. }
  153. public class MeasurableTextArea extends JTextArea {
  154. public int getBestHeight() {
  155. int best_height = getMinimumSize().height;
  156. return best_height;
  157. }
  158. }
  159. /**
  160. * for sorting log entries by revision number, latest revision first
  161. */
  162. public class EntryComparator implements Comparator<SVNLogEntry> {
  163. public int compare( SVNLogEntry o1, SVNLogEntry o2 ) {
  164. Long l1 = new Long( o1.getRevision() );
  165. Long l2 = new Long( o2.getRevision() );
  166. return l2.compareTo( l1 );
  167. }
  168. }
  169. /**
  170. * MouseListener to popup context menu on the table.
  171. */
  172. class TableMouseListener extends MouseAdapter {
  173. private LogTable table = null;
  174. public TableMouseListener(LogTable table) {
  175. TableMouseListener.this.table = table;
  176. }
  177. public void mouseReleased( MouseEvent me ) {
  178. handleClick( me );
  179. }
  180. public void mousePressed( MouseEvent me ) {
  181. handleClick( me );
  182. }
  183. private void handleClick( MouseEvent me ) {
  184. if ( me.isPopupTrigger() ) {
  185. JPopupMenu popup = getPopupMenu(table);
  186. if ( popup != null ) {
  187. GUIUtilities.showPopupMenu( popup, table, me.getX(), me.getY() );
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * Create the context menu.
  194. */
  195. private JPopupMenu getPopupMenu( final LogTable table ) {
  196. int[] rows = table.getSelectedRows();
  197. if ( rows.length != 2 ) {
  198. return null;
  199. }
  200. final String path = table.getPath();
  201. JPopupMenu popup = new JPopupMenu();
  202. JMenuItem mi = new JMenuItem( "Diff" );
  203. popup.add( mi );
  204. mi.addActionListener( new ActionListener() {
  205. public void actionPerformed( ActionEvent ae ) {
  206. int[] rows = table.getSelectedRows();
  207. String revision1 = (String)table.getValueAt( rows[ 0 ], 0 );
  208. String revision2 = (String)table.getValueAt( rows[ 1 ], 0 );
  209. DiffAction action = new DiffAction( view, path, revision1, revision2, username, password );
  210. action.actionPerformed( ae );
  211. }
  212. }
  213. );
  214. return popup;
  215. }
  216. }