changeset 1972:4bd90c506cf2

Clean up border-related code in TreeMap PR3059 Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019844.html Original-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/015955.html
author James Aziz <jaziz@redhat.com>
date Wed, 29 Jun 2016 12:22:58 -0400
parents 5b05693ef8de
children 4e64d7dccbcc
files client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponent.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponentTest.java
diffstat 2 files changed, 66 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponent.java	Wed Jun 29 12:22:58 2016 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponent.java	Wed Jun 29 12:22:58 2016 -0400
@@ -120,23 +120,15 @@
      */
     private static final String TITLE = "";
 
-    /**
-     * TreeMap UI Constraint.
+    /*
+     * TreeMap border styles
      */
-    public static final int SIMPLE = 0;
-    public static final int FLAT = 1;
-    public static final int ETCHED_LOWERED = 2;
-    public static final int ETCHED_RAISED = 3;
+    public static final int BORDER_SIMPLE = 0;
+    public static final int BORDER_FLAT = 1;
+    public static final int BORDER_ETCHED_LOWERED = 2;
+    public static final int BORDER_ETCHED_RAISED = 3;
 
-    /**
-     * Stores the chosen UI mode.
-     */
-    private int borderStyle = ETCHED_LOWERED;
-
-    /**
-     * The components' border
-     */
-    private Border defaultBorder;
+    private int borderStyle = BORDER_ETCHED_LOWERED;
 
     /**
      * Font and size for this component's label.
@@ -578,31 +570,35 @@
     }
 
     /**
-     * Switch the component's visualization mode to the one given in input. 
-     * Use static constraints to set correctly a visualization mode.
-     * @param UIMode the UI visualization mode to set.
+     * Switch the component's border style to the one given in input.
+     *
+     * @param borderStyle the border style to use
      */
-    public void setBorderStyle(int UIMode) {
-        this.borderStyle = UIMode;
-        switch (borderStyle) {
-            case 1 : {
-                defaultBorder = new EmptyBorder(0, 0, 0, 0);
+    public void setBorderStyle(int newBorderStyle) {
+        Border border;
+        switch (newBorderStyle) {
+            case BORDER_SIMPLE : {
+                border = new EmptyBorder(0, 0, 0, 0);
                 break;
             }    
-            case 2 : {                
-                defaultBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.white, Color.darkGray);
+            case BORDER_FLAT : {
+                border = new LineBorder(Color.black, 1);
+                break;
+            }
+            case BORDER_ETCHED_LOWERED : {
+                border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.white, Color.darkGray);
                 break;
             }
-            case 3 : {
-                defaultBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.white, Color.darkGray);
-                break;
-            }
+            case BORDER_ETCHED_RAISED : {
+                border = BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.white, Color.darkGray);
+                 break;
+             }
             default : {
-                defaultBorder = new LineBorder(Color.black, 1);
-                break;
+                throw new IllegalArgumentException("Unknown border style: " + newBorderStyle);
             }
         }
-        applyBorderToSubtree(mainComp);
+        this.borderStyle = newBorderStyle;
+        applyBorderToSubtree(mainComp, border);
     }
     
     /**
@@ -610,12 +606,12 @@
      * the default border.
      * @param comp the subtree's root from which apply the border style.
      */
-    private void applyBorderToSubtree(Comp comp) {
-        comp.setBorder(defaultBorder);
+    private void applyBorderToSubtree(Comp comp, Border border) {
+        comp.setBorder(border);
         Component[] children = comp.getComponents();
         for (int i = 0; i < children.length; i++) {
             if (children[i] instanceof Comp) {
-                applyBorderToSubtree((Comp) children[i]);
+                applyBorderToSubtree((Comp) children[i], border);
             }
         }
     }
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponentTest.java	Wed Jun 29 12:22:58 2016 -0400
+++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/TreeMapComponentTest.java	Wed Jun 29 12:22:58 2016 -0400
@@ -36,24 +36,26 @@
 
 package com.redhat.thermostat.client.swing.components.experimental;
 
-import junit.framework.Assert;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.awt.BorderLayout;
 import java.awt.Dimension;
+import java.awt.Font;
 import java.lang.reflect.InvocationTargetException;
 
+import javax.swing.JFrame;
 import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
 
+import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.redhat.thermostat.client.swing.internal.LocaleResources;
 import com.redhat.thermostat.shared.locale.Translate;
 
-
 public class TreeMapComponentTest {
 
     private static final Translate<LocaleResources> t = LocaleResources.createLocalizer();
@@ -427,4 +429,34 @@
         }
         assertTrue(caught);
     }
+
+    public static void main(String[] args) {
+        SwingUtilities.invokeLater(new Runnable() {
+
+            @Override
+            public void run() {
+                JFrame mainWindow = new JFrame();
+                mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+                TreeMapNode node = new TreeMapNode("test1", 1.0);
+
+                // FIXME this hack should not be needed
+                UIManager.put("thermostat-default-font", Font.decode(Font.MONOSPACED));
+
+                TreeMapComponent treeMap = new TreeMapComponent();
+                // FIXME the default renderer should not be null
+                treeMap.setToolTipRenderer(new TreeMapComponent.WeightAsSizeRenderer());
+                treeMap.setModel(node);
+
+                // FIXME no other swing component needs the following:
+                treeMap.processAndDrawTreeMap();
+
+                mainWindow.add(treeMap, BorderLayout.CENTER);
+
+                mainWindow.setSize(400, 200);
+                mainWindow.setVisible(true);
+            }
+        });
+    }
+
 }