# HG changeset patch # User Mario Torre # Date 1366713943 -7200 # Node ID 3b90181d422bb7ac11c55398fb24297d86e3b597 # Parent 8cb15af2c32802ab9ac8eabfb89c2696f5d1beeb Move timeline utils and header to swing components review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-April/006411.html reviewed-by: omajid, jerboaa diff -r 8cb15af2c328 -r 3b90181d422b client/swing/pom.xml --- a/client/swing/pom.xml Wed Apr 17 16:09:53 2013 -0400 +++ b/client/swing/pom.xml Tue Apr 23 12:45:43 2013 +0200 @@ -160,6 +160,7 @@ com.redhat.thermostat.client.swing, com.redhat.thermostat.client.swing.components, com.redhat.thermostat.client.swing.components.models, + com.redhat.thermostat.client.swing.components.timeline, com.redhat.thermostat.client.swing.internal, diff -r 8cb15af2c328 -r 3b90181d422b client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineRulerHeader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineRulerHeader.java Tue Apr 23 12:45:43 2013 +0200 @@ -0,0 +1,158 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.components.timeline; + +import java.awt.Dimension; +import java.awt.Font; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Paint; +import java.awt.Rectangle; +import java.beans.Transient; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import javax.swing.JScrollPane; + +import com.redhat.thermostat.client.swing.GraphicsUtils; +import com.redhat.thermostat.client.swing.components.GradientPanel; +import com.redhat.thermostat.client.ui.Palette; +import com.redhat.thermostat.common.model.LongRange; + +@SuppressWarnings("serial") +public abstract class TimelineRulerHeader extends GradientPanel { + + private LongRange range; + + public TimelineRulerHeader(LongRange range) { + + super(Palette.LIGHT_GRAY.getColor(), Palette.WHITE.getColor()); + + this.range = range; + } + + public LongRange getRange() { + return range; + } + + @Override + public int getHeight() { + return 25; + } + + @Override + @Transient + public Dimension getPreferredSize() { + Dimension dim = super.getPreferredSize(); + dim.height = getHeight(); + return dim; + } + + @Override + public Dimension getSize() { + return getPreferredSize(); + } + + protected abstract int getCurrentDisplayValue(); + + @Override + protected void paintComponent(Graphics g) { + + super.paintComponent(g); + + Graphics2D graphics = GraphicsUtils.getInstance().createAAGraphics(g); + + int currentValue = getCurrentDisplayValue(); + + Rectangle bounds = g.getClipBounds(); + int totalInc = TimelineUtils.drawMarks(range, graphics, bounds, currentValue, + TimelineUtils.calculateWidth(range), + getHeight(), true); + + drawTimelineStrings(graphics, currentValue, bounds, totalInc); + + graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + graphics.drawLine(bounds.x, bounds.height - 1, bounds.width, bounds.height - 1); + + graphics.dispose(); + } + + private void drawTimelineStrings(Graphics2D graphics, int currentValue, Rectangle bounds, int totalInc) { + + Font font = TimelineUtils.FONT; + + graphics.setFont(font); + + 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 round = range.getMin() % (TimelineUtils.STEP * 10); + int shift = (int) (round / TimelineUtils.STEP) * totalInc; + long currentTime = range.getMin() - round; + + int lowerBound = bounds.x - (4 * totalInc); + int x = ((bounds.x - currentValue) - shift); + + long increment = 0; + int height = getHeight(); + for (int i = x; i < bounds.width; i += totalInc) { + if (increment % 10 == 0 && i >= lowerBound) { + graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + graphics.drawLine(i, 0, i, height); + + graphics.setPaint(gradient); + + String value = df.format(new Date(currentTime)); + + int stringWidth = (int) font.getStringBounds(value, graphics.getFontRenderContext()).getWidth() - 1; + int stringHeight = (int) font.getStringBounds(value, graphics.getFontRenderContext()).getHeight(); + graphics.fillRect(i + 1, bounds.y + 5, stringWidth + 4, stringHeight + 4); + + graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + graphics.drawString(value, i + 1, bounds.y + stringHeight + 5); + } + currentTime += TimelineUtils.STEP; + increment++; + } + } +} + diff -r 8cb15af2c328 -r 3b90181d422b client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/TimelineUtils.java Tue Apr 23 12:45:43 2013 +0200 @@ -0,0 +1,87 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * . + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.components.timeline; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Rectangle; + +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) + { + LongRangeNormalizer normalizer = new LongRangeNormalizer(range, 0, width); + normalizer.setValue(range.getMin() + TimelineUtils.STEP); + int totalInc = (int) normalizer.getValueNormalized(); + + int inc = currentValue % totalInc; + 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); + if (darkerTop) { + graphics.setColor(Palette.DARK_GRAY.getColor()); + graphics.drawLine(i, 0, i, 5); + graphics.setColor(Palette.GRAY.getColor()); + } + } + + return totalInc; + } +} + diff -r 8cb15af2c328 -r 3b90181d422b thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java --- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java Wed Apr 17 16:09:53 2013 -0400 +++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadTimelineView.java Tue Apr 23 12:45:43 2013 +0200 @@ -52,13 +52,14 @@ import com.redhat.thermostat.client.swing.ComponentVisibleListener; import com.redhat.thermostat.client.swing.SwingComponent; +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; + import com.redhat.thermostat.thread.client.swing.impl.timeline.TimelineCellRenderer; import com.redhat.thermostat.thread.client.swing.impl.timeline.TimelineComponent; import com.redhat.thermostat.thread.client.swing.impl.timeline.TimelineRulerHeader; -import com.redhat.thermostat.thread.client.swing.impl.timeline.TimelineUtils; public class SwingThreadTimelineView extends ThreadTimelineView implements SwingComponent { diff -r 8cb15af2c328 -r 3b90181d422b thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java --- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java Wed Apr 17 16:09:53 2013 -0400 +++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineComponent.java Tue Apr 23 12:45:43 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; import com.redhat.thermostat.common.model.LongRange; import com.redhat.thermostat.common.model.LongRangeNormalizer; diff -r 8cb15af2c328 -r 3b90181d422b thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineRulerHeader.java --- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineRulerHeader.java Wed Apr 17 16:09:53 2013 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,158 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * . - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.client.swing.impl.timeline; - -import java.awt.Dimension; -import java.awt.Font; -import java.awt.GradientPaint; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Paint; -import java.awt.Rectangle; -import java.beans.Transient; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import javax.swing.JScrollPane; - -import com.redhat.thermostat.client.swing.GraphicsUtils; -import com.redhat.thermostat.client.swing.components.GradientPanel; -import com.redhat.thermostat.client.ui.Palette; -import com.redhat.thermostat.common.model.LongRange; - -@SuppressWarnings("serial") -public class TimelineRulerHeader extends GradientPanel { - - private LongRange range; - private JScrollPane scrollPane; - - public TimelineRulerHeader(LongRange range, JScrollPane scrollPane) { - - super(Palette.LIGHT_GRAY.getColor(), Palette.WHITE.getColor()); - - this.range = range; - this.scrollPane = scrollPane; - } - - public LongRange getRange() { - return range; - } - - @Override - public int getHeight() { - return 25; - } - - @Override - @Transient - public Dimension getPreferredSize() { - Dimension dim = super.getPreferredSize(); - dim.height = getHeight(); - return dim; - } - - @Override - public Dimension getSize() { - return getPreferredSize(); - } - - @Override - protected void paintComponent(Graphics g) { - - super.paintComponent(g); - - Graphics2D graphics = GraphicsUtils.getInstance().createAAGraphics(g); - - int currentValue = scrollPane.getHorizontalScrollBar().getValue(); - - Rectangle bounds = g.getClipBounds(); - int totalInc = TimelineUtils.drawMarks(range, graphics, bounds, currentValue, - TimelineUtils.calculateWidth(range), - getHeight(), true); - - drawTimelineStrings(graphics, currentValue, bounds, totalInc); - - graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); - graphics.drawLine(bounds.x, bounds.height - 1, bounds.width, bounds.height - 1); - - graphics.dispose(); - } - - private void drawTimelineStrings(Graphics2D graphics, int currentValue, Rectangle bounds, int totalInc) { - - Font font = TimelineUtils.FONT; - - graphics.setFont(font); - - 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 round = range.getMin() % (TimelineUtils.STEP * 10); - int shift = (int) (round / TimelineUtils.STEP) * totalInc; - long currentTime = range.getMin() - round; - - int lowerBound = bounds.x - (4 * totalInc); - int x = ((bounds.x - currentValue) - shift); - - long increment = 0; - int height = getHeight(); - for (int i = x; i < bounds.width; i += totalInc) { - if (increment % 10 == 0 && i >= lowerBound) { - graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); - graphics.drawLine(i, 0, i, height); - - graphics.setPaint(gradient); - - String value = df.format(new Date(currentTime)); - - int stringWidth = (int) font.getStringBounds(value, graphics.getFontRenderContext()).getWidth() - 1; - int stringHeight = (int) font.getStringBounds(value, graphics.getFontRenderContext()).getHeight(); - graphics.fillRect(i + 1, bounds.y + 5, stringWidth + 4, stringHeight + 4); - - graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); - graphics.drawString(value, i + 1, bounds.y + stringHeight + 5); - } - currentTime += TimelineUtils.STEP; - increment++; - } - } -} - diff -r 8cb15af2c328 -r 3b90181d422b thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineUtils.java --- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/timeline/TimelineUtils.java Wed Apr 17 16:09:53 2013 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -/* - * Copyright 2012, 2013 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * . - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.thread.client.swing.impl.timeline; - -import java.awt.Font; -import java.awt.Graphics2D; -import java.awt.Rectangle; - -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) { - LongRangeNormalizer normalizer = new LongRangeNormalizer(range, 0, width); - normalizer.setValue(range.getMin() + TimelineUtils.STEP); - int totalInc = (int) normalizer.getValueNormalized(); - - int inc = currentValue % totalInc; - 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); - if (darkerTop) { - graphics.setColor(Palette.DARK_GRAY.getColor()); - graphics.drawLine(i, 0, i, 5); - graphics.setColor(Palette.GRAY.getColor()); - } - } - - return totalInc; - } -} -