From 9effe1630d97039b3e01cd9b58ed07e75be1d63c Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Mon, 25 Feb 2013 08:40:30 -0500 Subject: [PATCH] Merge pull request #75 from thefake/master --- src/com/gitblit/client/Utils.java | 85 +++++++++++++++++++++++++++++++++++++++--- 1 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/com/gitblit/client/Utils.java b/src/com/gitblit/client/Utils.java index c837579..1e6ab2b 100644 --- a/src/com/gitblit/client/Utils.java +++ b/src/com/gitblit/client/Utils.java @@ -17,11 +17,20 @@ import java.awt.Color; import java.awt.Component; +import java.awt.Desktop; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Insets; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URI; import java.text.MessageFormat; import java.util.Date; import javax.swing.JOptionPane; +import javax.swing.JScrollPane; import javax.swing.JTable; +import javax.swing.JTextArea; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; @@ -31,20 +40,55 @@ public class Utils { - public static JTable newTable(TableModel model) { - JTable table = new JTable(model); + public final static int MARGIN = 5; + + public final static Insets INSETS = new Insets(MARGIN, MARGIN, MARGIN, MARGIN); + + public final static String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm"; + + public final static String DATE_FORMAT = "yyyy-MM-dd"; + + public static JTable newTable(TableModel model, String datePattern) { + return newTable(model, datePattern, null); + } + + public static JTable newTable(TableModel model, String datePattern, final RowRenderer rowRenderer) { + JTable table; + if (rowRenderer == null) { + table = new JTable(model); + } else { + table = new JTable(model) { + + @Override + public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { + Component c = super.prepareRenderer(renderer, row, column); + boolean isSelected = isCellSelected(row, column); + rowRenderer.prepareRow(c, isSelected, row, column); + return c; + } + }; + } + table.setRowHeight(table.getFont().getSize() + 8); table.setCellSelectionEnabled(false); table.setRowSelectionAllowed(true); table.getTableHeader().setReorderingAllowed(false); table.setGridColor(new Color(0xd9d9d9)); table.setBackground(Color.white); - table.setDefaultRenderer(Date.class, new DateCellRenderer(null, Color.orange.darker())); + table.setDefaultRenderer(Date.class, + new DateCellRenderer(datePattern, Color.orange.darker())); return table; + } + + public static void explainNotAllowed(Component c, RpcRequest request) { + String msg = MessageFormat.format("The Gitblit server does not allow the request \"{0}\".", + request.name()); + JOptionPane.showMessageDialog(c, msg, "Not Allowed", JOptionPane.ERROR_MESSAGE); } public static void explainForbidden(Component c, RpcRequest request) { String msg = MessageFormat.format( - "The request \"{0}\" has been forbidden by the Gitblit server.", request.name()); + "The request \"{0}\" has been forbidden for the account by the Gitblit server.", + request.name()); JOptionPane.showMessageDialog(c, msg, "Forbidden", JOptionPane.ERROR_MESSAGE); } @@ -54,9 +98,26 @@ JOptionPane.showMessageDialog(c, msg, "Unauthorized", JOptionPane.ERROR_MESSAGE); } + public static void explainUnknown(Component c, RpcRequest request) { + String msg = MessageFormat.format( + "The request \"{0}\" is not recognized by the Gitblit server.", request.name()); + JOptionPane.showMessageDialog(c, msg, "Unknown Request", JOptionPane.ERROR_MESSAGE); + } + public static void showException(Component c, Throwable t) { - // TODO show the unexpected exception - t.printStackTrace(); + StringWriter writer = new StringWriter(); + t.printStackTrace(new PrintWriter(writer)); + String stacktrace = writer.toString(); + try { + writer.close(); + } catch (Throwable x) { + } + JTextArea textArea = new JTextArea(stacktrace); + textArea.setFont(new Font("monospaced", Font.PLAIN, 11)); + JScrollPane jsp = new JScrollPane(textArea); + jsp.setPreferredSize(new Dimension(800, 400)); + JOptionPane.showMessageDialog(c, jsp, Translation.get("gb.error"), + JOptionPane.ERROR_MESSAGE); } public static void packColumns(JTable table, int margin) { @@ -97,4 +158,16 @@ // Set the width col.setPreferredWidth(width); } + + public static void browse(String url) { + try { + Desktop.getDesktop().browse(new URI(url)); + } catch (Exception x) { + showException(null, x); + } + } + + public static abstract class RowRenderer { + public abstract void prepareRow(Component c, boolean isSelected, int row, int column); + } } -- Gitblit v1.9.1