changeset 1072:047b7869d7e7

Minor cleanup in timeline header and component (part II) review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-April/006433.html reviewed-by: omajid
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 26 Apr 2013 18:10:44 +0200
parents 12894dbd3517
children 3ffe0afe5cdb
files client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineRulerHeader.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineUtils.java thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java
diffstat 4 files changed, 96 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineRulerHeader.java	Fri Mar 22 16:18:06 2013 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineRulerHeader.java	Fri Apr 26 18:10:44 2013 +0200
@@ -56,11 +56,31 @@
 @SuppressWarnings("serial")
 public abstract class TimelineRulerHeader extends GradientPanel {
 
+    /** Default height of this component. Subclasses may use different values */
+    public static final int DEFAULT_HEIGHT = 25;
+    
+    /**
+     * Default increment is 20 pixels per units.
+     * Subclasses may use different values.
+     * 
+     * @see #DEFAULT_INCREMENT_IN_MILLIS
+     */
+    public static final int DEFAULT_INCREMENT_IN_PIXELS = 20;
+
+    /**
+     * Default increments is 1 second (1000 ms) per pixel unit.
+     * Subclasses may use different values.
+     * 
+     * @see #DEFAULT_INCREMENT_IN_PIXELS
+     */
+    public static final long DEFAULT_INCREMENT_IN_MILLIS = 1_000;
+    
     private LongRange range;
     
     public TimelineRulerHeader(LongRange range) {
         
         super(Palette.LIGHT_GRAY.getColor(), Palette.WHITE.getColor());
+        setFont(TimelineUtils.FONT);
         
         this.range = range;
     }
@@ -71,7 +91,7 @@
     
     @Override
     public int getHeight() {
-        return 25;
+        return DEFAULT_HEIGHT;
     }
     
     @Override
@@ -87,6 +107,20 @@
         return getPreferredSize();
     }
     
+    /**
+     * Defines the distance, in pixels, between one tick mark and the other.
+     */
+    public int getUnitIncrementInPixels() {
+        return DEFAULT_INCREMENT_IN_PIXELS;
+    }
+    
+    /**
+     * Defines how many milliseconds pass between two tick marks.
+     */
+    public long getUnitIncrementInMillis() {
+        return DEFAULT_INCREMENT_IN_MILLIS;
+    }
+    
     protected abstract int getCurrentDisplayValue();
     
     @Override
@@ -99,11 +133,11 @@
         int currentValue = getCurrentDisplayValue();
 
         Rectangle bounds = g.getClipBounds();
-        int totalInc = TimelineUtils.drawMarks(range, graphics, bounds, currentValue,
-                                               TimelineUtils.calculateWidth(range),
-                                               getHeight(), true);
+        
+        int unitIncrement = getUnitIncrementInPixels();
         
-        drawTimelineStrings(graphics, currentValue, bounds, totalInc);
+        TimelineUtils.drawMarks(range, graphics, bounds, currentValue, false, unitIncrement);
+        drawTimelineStrings(graphics, currentValue, bounds, unitIncrement);
         
         graphics.setColor(Palette.THERMOSTAT_BLU.getColor());
         graphics.drawLine(bounds.x, bounds.height - 1, bounds.width, bounds.height - 1);
@@ -113,18 +147,19 @@
     
     private void drawTimelineStrings(Graphics2D graphics, int currentValue, Rectangle bounds, int totalInc) {
         
-        Font font = TimelineUtils.FONT;
-        
-        graphics.setFont(font);
-        
+        Font font = graphics.getFont();
+                
         DateFormat df = new SimpleDateFormat("HH:mm:ss");
         
         Paint gradient = new GradientPaint(0, 0, Palette.WHITE.getColor(), 0, getHeight(), Palette.GRAY.getColor());
         
         graphics.setColor(Palette.EARL_GRAY.getColor());
+
+        long incrementInMillis = getUnitIncrementInMillis();
         
-        long round = range.getMin() % (TimelineUtils.STEP * 10);
-        int shift = (int) (round / TimelineUtils.STEP) * totalInc;
+        long round = range.getMin() % (10 * incrementInMillis);
+        
+        int shift = (int) (round / incrementInMillis) * totalInc;
         long currentTime = range.getMin() - round;
         
         int lowerBound = bounds.x - (4 * totalInc);
@@ -148,7 +183,7 @@
                 graphics.setColor(Palette.THERMOSTAT_BLU.getColor());                
                 graphics.drawString(value, i + 1, bounds.y + stringHeight + 5);
             }
-            currentTime += TimelineUtils.STEP;
+            currentTime += incrementInMillis;
             increment++;
         }
     }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineUtils.java	Fri Mar 22 16:18:06 2013 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineUtils.java	Fri Apr 26 18:10:44 2013 +0200
@@ -42,46 +42,28 @@
 
 import com.redhat.thermostat.client.ui.Palette;
 import com.redhat.thermostat.common.model.LongRange;
-import com.redhat.thermostat.common.model.LongRangeNormalizer;
 
 public class TimelineUtils {
-    public static final int INC = 50;
-    public static final int STEP = 1000;
     public static final Font FONT = new Font("SansSerif", Font.PLAIN, 10);
-
-    public static int calculateWidth(LongRange range) {
-        long span = range.getMax() - range.getMin();
-        int width = (int) (span / TimelineUtils.INC);
-        return width;
-    }
-    
-    public static int drawMarks(LongRange range, Graphics2D graphics, Rectangle bounds, int currentValue, int width, int height) {
-        return drawMarks(range, graphics, bounds, currentValue, width, height, false);
-    }
-    
-    public static int drawMarks(LongRange range, Graphics2D graphics, Rectangle bounds,
-                                int currentValue, int width, int height, boolean darkerTop)
+ 
+    public static void drawMarks(LongRange range, Graphics2D graphics, Rectangle bounds,
+                                 int currentValue, boolean darkerTop, int increment)
     {
-        LongRangeNormalizer normalizer = new LongRangeNormalizer(range, 0, width);
-        normalizer.setValue(range.getMin() + TimelineUtils.STEP);
-        int totalInc = (int) normalizer.getValueNormalized();
-
-        int inc = currentValue % totalInc;
+        int inc = currentValue % increment;
         int x = (bounds.x - inc);
         
         graphics.setColor(Palette.GRAY.getColor());
         int upperBound = (bounds.x + bounds.width);
 
-        for (int i = x; i < upperBound; i += totalInc) {
-            graphics.drawLine(i, 0, i, height);
+        for (int i = x; i < upperBound; i += increment) {
+            graphics.drawLine(i, 0, i, bounds.height);
             if (darkerTop) {
                 graphics.setColor(Palette.DARK_GRAY.getColor());
                 graphics.drawLine(i, 0, i, 5);
                 graphics.setColor(Palette.GRAY.getColor());
             }
         }
-        
-        return totalInc;
     }
+
 }
 
--- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java	Fri Mar 22 16:18:06 2013 -0400
+++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java	Fri Apr 26 18:10:44 2013 +0200
@@ -52,9 +52,11 @@
 
 import com.redhat.thermostat.client.swing.ComponentVisibleListener;
 import com.redhat.thermostat.client.swing.SwingComponent;
+
 import com.redhat.thermostat.client.swing.components.timeline.TimelineRulerHeader;
-import com.redhat.thermostat.client.swing.components.timeline.TimelineUtils;
+
 import com.redhat.thermostat.common.model.LongRange;
+
 import com.redhat.thermostat.thread.client.common.Timeline;
 import com.redhat.thermostat.thread.client.common.view.ThreadTimelineView;
 
@@ -113,7 +115,8 @@
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
 
         long now = System.currentTimeMillis();
-        header = new ThreadTimelineHeader(new LongRange(now, now + TimelineUtils.STEP), scrollPane);
+        header = new ThreadTimelineHeader(new LongRange(now, now + TimelineRulerHeader.DEFAULT_INCREMENT_IN_MILLIS),
+                                          scrollPane);
         scrollPane.setColumnHeaderView(header);
 
         ScrollChangeListener listener = new ScrollChangeListener();
@@ -134,7 +137,7 @@
 
                     int extent = scrollBar.getVisibleAmount();
                     int min = scrollBar.getMinimum();
-                    int max = component.getWidth() + (2 * TimelineUtils.INC);
+                    int max = component.getWidth() + (int) (2 * header.getUnitIncrementInMillis());
 
                     scrollBar.setValues(max - extent, extent, min, max);
                 }
@@ -148,10 +151,15 @@
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
-                range.setMax(range.getMax() + (2 * TimelineUtils.STEP));
+                range.setMax(range.getMax() + (int) (2 * header.getUnitIncrementInMillis()));
                 chartModel.removeAllElements();
                 for (Timeline timeline : timelines) {
-                    chartModel.addElement(new TimelineComponent(range, timeline, scrollPane));
+                    
+                    TimelineComponent timelineComp = new TimelineComponent(range, timeline, scrollPane);
+                    timelineComp.setUnitIncrementInMillis(header.getUnitIncrementInMillis());
+                    timelineComp.setUnitIncrementInPixels(header.getUnitIncrementInPixels());
+                    
+                    chartModel.addElement(timelineComp);
                 }
                 header.getRange().setMin(range.getMin());
                 header.getRange().setMax(range.getMax());
--- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java	Fri Mar 22 16:18:06 2013 -0400
+++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java	Fri Apr 26 18:10:44 2013 +0200
@@ -56,6 +56,7 @@
 
 import com.redhat.thermostat.client.swing.GraphicsUtils;
 import com.redhat.thermostat.client.swing.components.GradientPanel;
+
 import com.redhat.thermostat.client.swing.components.timeline.TimelineUtils;
 
 import com.redhat.thermostat.client.ui.Palette;
@@ -74,13 +75,29 @@
     private Timeline timeline;
     private JScrollPane scrollPane;
     private LongRange range;
-    public TimelineComponent(LongRange range, Timeline timeline, JScrollPane scrollPane) {
+    
+    private long millsUnitIncrement;
+    private int pixelUnitIncrement;
+    
+    public TimelineComponent(LongRange range, Timeline timeline, JScrollPane scrollPane)
+    {
         super(Palette.LIGHT_GRAY.getColor(), Palette.WHITE.getColor());
         this.range = range;
         this.scrollPane = scrollPane;
         this.timeline = timeline;
+        
+        millsUnitIncrement = 1_000;
+        pixelUnitIncrement = 20;
     }
 
+    public void setUnitIncrementInPixels(int increment) {
+        this.pixelUnitIncrement = increment;
+    }
+    
+    public void setUnitIncrementInMillis(long increment) {
+        this.millsUnitIncrement = increment;
+    }
+    
     public void setSelected(boolean selected) {
         this.selected = selected;
     }
@@ -98,10 +115,10 @@
             graphics.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
         }
         
-        int height = getHeight();
         int currentValue = scrollPane.getHorizontalScrollBar().getValue();
-        int totalInc = TimelineUtils.drawMarks(range, graphics, bounds, currentValue, getWidth(), height);
-        
+        int totalInc = pixelUnitIncrement;
+        TimelineUtils.drawMarks(range, graphics, bounds, currentValue, false, totalInc);
+
         drawBoldMarks(graphics, currentValue, bounds, totalInc);
         Color lastColor = drawTimeline(graphics, currentValue, bounds);
         
@@ -174,8 +191,8 @@
     
     private void drawBoldMarks(Graphics2D graphics, int currentValue, Rectangle bounds, int totalInc) {
 
-        long round = range.getMin() % 10000;
-        int shift = (int) (round / TimelineUtils.STEP) * totalInc;
+        long round = range.getMin() % (10 * millsUnitIncrement);
+        int shift = (int) (round / millsUnitIncrement) * totalInc;
         
         int lowerBound = bounds.x - (4 * totalInc);
         int x = ((bounds.x - currentValue) - shift);
@@ -204,7 +221,12 @@
     
     @Override
     public int getWidth() {
-        return TimelineUtils.calculateWidth(range);
+         
+        long divisor = millsUnitIncrement / pixelUnitIncrement;
+        
+        long span = range.getMax() - range.getMin();
+        int width = (int) (span / divisor);
+        return width;
     }
     
     @Override
@@ -217,62 +239,5 @@
     public Dimension getPreferredSize() {
         return new Dimension(getWidth(), getHeight());
     }
-        
-    public static void main(String[] args) {
-        SwingUtilities.invokeLater(new Runnable() {
-            @Override
-            public void run() {
-                
-                LongRange range = new LongRange(50000, 2000000); // 31558464000L
-                Timeline timeline = new Timeline("Test", 1000);
-                timeline.add(new TimelineInfo(Palette.THERMOSTAT_BLU, range.getMax() - 1000));
-                timeline.add(new TimelineInfo(Palette.TUNDRA_GREEN, 152000));
-                timeline.add(new TimelineInfo(Palette.DIRTY_CYAN, 63000));
-                timeline.add(new TimelineInfo(Palette.THERMOSTAT_BLU, 62000));
-                timeline.add(new TimelineInfo(Palette.THERMOSTAT_RED, 60210));
-                timeline.add(new TimelineInfo(Palette.THERMOSTAT_BLU, 51299));
-
-                final JFrame frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-                
-                
-                final JScrollPane scrollPane = new JScrollPane();
-                DefaultListModel<TimelineComponent> chartModel = new DefaultListModel<>();
-                for (int i = 0; i < 100; i++) {
-                    chartModel.addElement(new TimelineComponent(range, timeline, scrollPane));
-                }
-                
-                final JList<TimelineComponent> stuff = new JList<>(chartModel);
-                stuff.setCellRenderer(new TimelineCellRenderer());
-                
-                scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
-                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
-                scrollPane.getHorizontalScrollBar().setUnitIncrement(TimelineUtils.INC);
-                
-                final ThreadTimelineHeader header = new ThreadTimelineHeader(range, scrollPane);
-                
-                scrollPane.setColumnHeaderView(header);
-                scrollPane.setViewportView(stuff);
-                scrollPane.getVerticalScrollBar().getModel().addChangeListener(new ChangeListener() {
-                    @Override
-                    public void stateChanged(ChangeEvent e) {
-                        scrollPane.repaint();
-                    }
-                });
-                
-                scrollPane.getHorizontalScrollBar().getModel().addChangeListener(new ChangeListener() {
-                    
-                    @Override
-                    public void stateChanged(ChangeEvent e) {
-                        scrollPane.repaint();
-                    }
-                });
-                
-                frame.add(scrollPane);
-                frame.setSize(300, 300);
-                frame.setVisible(true);
-            }
-        });
-    }     
 }