changeset 1955:da2c18e537ab

Do not syntax highlight profile results on selection Backport of 28a18a7e419f from HEAD. PR3063 Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019969.html
author Alex Macdonald <almacdon@redhat.com>
date Wed, 29 Jun 2016 09:33:10 -0400
parents 1db217de52a2
children 4c4656f5bab4
files vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java
diffstat 1 files changed, 96 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java	Wed Jun 29 14:09:54 2016 +0200
+++ b/vm-profiler/client-swing/src/main/java/com/redhat/thermostat/vm/profiler/client/swing/internal/SwingVmProfileView.java	Wed Jun 29 09:33:10 2016 -0400
@@ -59,6 +59,7 @@
 import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JSplitPane;
+import javax.swing.JTable;
 import javax.swing.JToggleButton;
 import javax.swing.ListSelectionModel;
 import javax.swing.SwingUtilities;
@@ -78,6 +79,7 @@
 import com.redhat.thermostat.client.swing.components.Icon;
 import com.redhat.thermostat.client.swing.components.ThermostatScrollPane;
 import com.redhat.thermostat.client.swing.components.ThermostatTable;
+import com.redhat.thermostat.client.swing.components.ThermostatTableRenderer;
 import com.redhat.thermostat.client.swing.experimental.ComponentVisibilityNotifier;
 import com.redhat.thermostat.client.ui.Palette;
 import com.redhat.thermostat.common.ActionEvent;
@@ -97,6 +99,10 @@
 
     private static final double SPLIT_PANE_RATIO = 0.3;
 
+    private static final int COLUMN_METHOD_NAME = 0;
+    private static final int COLUMN_METHOD_PERCENTAGE = 1;
+    private static final int COLUMN_METHOD_TIME = 2;
+
     private final CopyOnWriteArrayList<ActionListener<ProfileAction>> listeners = new CopyOnWriteArrayList<>();
 
     private HeaderPanel mainContainer;
@@ -106,6 +112,7 @@
     private DefaultListModel<Profile> listModel;
     private JList<Profile> profileList;
 
+    private ThermostatTable profileTable;
     private DefaultTableModel tableModel;
 
     private JLabel currentStatusLabel;
@@ -220,11 +227,11 @@
             @Override
             public java.lang.Class<?> getColumnClass(int columnIndex) {
                 switch (columnIndex) {
-                case 0:
-                    return String.class;
-                case 1:
+                case COLUMN_METHOD_NAME:
+                    return MethodDeclaration.class;
+                case COLUMN_METHOD_PERCENTAGE:
                     return Double.class;
-                case 2:
+                case COLUMN_METHOD_TIME:
                     return Long.class;
                 default:
                     throw new AssertionError("Unknown column index");
@@ -232,7 +239,21 @@
             }
         };
 
-        ThermostatTable profileTable = new ThermostatTable(tableModel);
+        final PlainTextMethodDeclarationRenderer plainRenderer = new PlainTextMethodDeclarationRenderer();
+        final SyntaxHighlightedMethodDeclarationRenderer colorRenderer = new SyntaxHighlightedMethodDeclarationRenderer();
+
+        profileTable = new ThermostatTable(tableModel) {
+            public javax.swing.table.TableCellRenderer getCellRenderer(int row, int column) {
+                if (column == COLUMN_METHOD_NAME) {
+                    if (profileTable.isCellSelected(row, column)) {
+                        return plainRenderer;
+                    } else {
+                        return colorRenderer;
+                    }
+                }
+                return super.getCellRenderer(row, column);
+            }
+        };
 
         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                 profileListPane, profileTable.wrap());
@@ -380,7 +401,7 @@
 
                 for (MethodInfo methodInfo: results.getMethodInfo()) {
                     Object[] data = new Object[] {
-                            syntaxHighlightMethod(methodInfo.decl),
+                            methodInfo.decl,
                             methodInfo.percentageTime,
                             methodInfo.totalTimeInMillis,
                     };
@@ -390,49 +411,80 @@
         });
     }
 
-    private String syntaxHighlightMethod(MethodDeclaration decl) {
-        final Color METHOD_COLOR = Palette.PALE_RED.getColor();
-        final Color PARAMETER_COLOR = Palette.AZUREUS.getColor();
-        final Color RETURN_TYPE_COLOR = Palette.SKY_BLUE.getColor();
-
-        String highlightedName = htmlColorText(decl.getName(), METHOD_COLOR);
-        String highlightedReturnType = htmlColorText(decl.getReturnType(), RETURN_TYPE_COLOR);
-
-        StringBuilder toReturn = new StringBuilder();
-        toReturn.append("<html>");
-        toReturn.append("<pre>");
-
-        toReturn.append(highlightedReturnType);
-        toReturn.append(" ");
-        toReturn.append("<b>");
-        toReturn.append(highlightedName);
-        toReturn.append("</b>");
-        toReturn.append("(");
-
-        ArrayList<String> parameters = new ArrayList<>();
-        for (String parameter : decl.getParameters()) {
-            parameters.add(htmlColorText(parameter, PARAMETER_COLOR));
-        }
-
-        toReturn.append(StringUtils.join(",", parameters));
-
-        toReturn.append(")");
-        toReturn.append("</pre>");
-        toReturn.append("<html>");
-        return toReturn.toString();
-
-    }
-
-    private String htmlColorText(String unescapedText, Color color) {
-        return "<font color='" + ("#" + Integer.toHexString(color.getRGB() & 0x00ffffff)) + "'>"
-                + StringUtils.htmlEscape(unescapedText) + "</font>";
-    }
-
     @Override
     public Component getUiComponent() {
         return mainContainer;
     }
 
+    static class PlainTextMethodDeclarationRenderer extends ThermostatTableRenderer {
+
+        @Override
+        public Component getTableCellRendererComponent(JTable table, Object value,
+                boolean isSelected, boolean hasFocus, int row, int column) {
+
+            if (!(value instanceof MethodDeclaration)) {
+                throw new AssertionError("Unexpected value");
+            }
+
+            String plainText = ((MethodDeclaration) value).toString();
+            return super.getTableCellRendererComponent(table, plainText, isSelected, hasFocus, row, column);
+        }
+    }
+
+    static class SyntaxHighlightedMethodDeclarationRenderer extends ThermostatTableRenderer {
+
+        @Override
+        public Component getTableCellRendererComponent(JTable table, Object value,
+                boolean isSelected, boolean hasFocus, int row, int column) {
+
+            if (!(value instanceof MethodDeclaration)) {
+                throw new AssertionError("Unexpected value");
+            }
+
+            String syntaxHighlightedText = syntaxHighlightMethod((MethodDeclaration) value);
+            return super.getTableCellRendererComponent(table, syntaxHighlightedText, isSelected, hasFocus, row, column);
+        }
+
+        private String syntaxHighlightMethod(MethodDeclaration decl) {
+            final Color METHOD_COLOR = Palette.PALE_RED.getColor();
+            final Color PARAMETER_COLOR = Palette.VIOLET.getColor();
+            final Color RETURN_TYPE_COLOR = Palette.GRANITA_ORANGE.getColor();
+
+            String highlightedName = htmlColorText(decl.getName(), METHOD_COLOR);
+            String highlightedReturnType = htmlColorText(decl.getReturnType(), RETURN_TYPE_COLOR);
+
+            StringBuilder toReturn = new StringBuilder();
+            toReturn.append("<html>");
+            toReturn.append("<pre>");
+
+            toReturn.append(highlightedReturnType);
+            toReturn.append(" ");
+            toReturn.append("<b>");
+            toReturn.append(highlightedName);
+            toReturn.append("</b>");
+            toReturn.append("(");
+
+            ArrayList<String> parameters = new ArrayList<>();
+            for (String parameter : decl.getParameters()) {
+                parameters.add(htmlColorText(parameter, PARAMETER_COLOR));
+            }
+
+            toReturn.append(StringUtils.join(",", parameters));
+
+            toReturn.append(")");
+            toReturn.append("</pre>");
+            toReturn.append("<html>");
+            return toReturn.toString();
+
+        }
+
+        private String htmlColorText(String unescapedText, Color color) {
+            String hexColorString = "#" + Integer.toHexString(color.getRGB() & 0x00ffffff);
+            return "<font color='" + hexColorString + "'>"
+                    + StringUtils.htmlEscape(unescapedText) + "</font>";
+        }
+    }
+
     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override