You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Utils.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.gitblit.authority;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Insets;
  7. import java.io.PrintWriter;
  8. import java.io.StringWriter;
  9. import java.util.Date;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTable;
  13. import javax.swing.JTextArea;
  14. import javax.swing.table.DefaultTableColumnModel;
  15. import javax.swing.table.TableCellRenderer;
  16. import javax.swing.table.TableColumn;
  17. import javax.swing.table.TableModel;
  18. import com.gitblit.client.DateCellRenderer;
  19. import com.gitblit.client.Translation;
  20. public class Utils {
  21. public final static int MARGIN = 5;
  22. public final static Insets INSETS = new Insets(MARGIN, MARGIN, MARGIN, MARGIN);
  23. public final static String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm";
  24. public final static String DATE_FORMAT = "yyyy-MM-dd";
  25. public static JTable newTable(TableModel model, String datePattern) {
  26. JTable table = new JTable(model);
  27. table.setRowHeight(table.getFont().getSize() + 8);
  28. table.setCellSelectionEnabled(false);
  29. table.setRowSelectionAllowed(true);
  30. table.getTableHeader().setReorderingAllowed(false);
  31. table.setGridColor(new Color(0xd9d9d9));
  32. table.setBackground(Color.white);
  33. table.setDefaultRenderer(Date.class,
  34. new DateCellRenderer(datePattern, Color.orange.darker()));
  35. return table;
  36. }
  37. public static void showException(Component c, Throwable t) {
  38. StringWriter writer = new StringWriter();
  39. t.printStackTrace(new PrintWriter(writer));
  40. String stacktrace = writer.toString();
  41. try {
  42. writer.close();
  43. } catch (Throwable x) {
  44. }
  45. JTextArea textArea = new JTextArea(stacktrace);
  46. textArea.setFont(new Font("monospaced", Font.PLAIN, 11));
  47. JScrollPane jsp = new JScrollPane(textArea);
  48. jsp.setPreferredSize(new Dimension(800, 400));
  49. JOptionPane.showMessageDialog(c, jsp, Translation.get("gb.error"),
  50. JOptionPane.ERROR_MESSAGE);
  51. }
  52. public static void packColumns(JTable table, int margin) {
  53. for (int c = 0; c < table.getColumnCount(); c++) {
  54. packColumn(table, c, 4);
  55. }
  56. }
  57. // Sets the preferred width of the visible column specified by vColIndex.
  58. // The column will be just wide enough to show the column head and the
  59. // widest cell in the column. margin pixels are added to the left and right
  60. // (resulting in an additional width of 2*margin pixels).
  61. private static void packColumn(JTable table, int vColIndex, int margin) {
  62. DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
  63. TableColumn col = colModel.getColumn(vColIndex);
  64. int width = 0;
  65. // Get width of column header
  66. TableCellRenderer renderer = col.getHeaderRenderer();
  67. if (renderer == null) {
  68. renderer = table.getTableHeader().getDefaultRenderer();
  69. }
  70. Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false,
  71. false, 0, 0);
  72. width = comp.getPreferredSize().width;
  73. // Get maximum width of column data
  74. for (int r = 0; r < table.getRowCount(); r++) {
  75. renderer = table.getCellRenderer(r, vColIndex);
  76. comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex),
  77. false, false, r, vColIndex);
  78. width = Math.max(width, comp.getPreferredSize().width);
  79. }
  80. // Add margin
  81. width += 2 * margin;
  82. // Set the width
  83. col.setPreferredWidth(width);
  84. }
  85. }