Mercurial > hg > release > thermostat-1.0
changeset 1296:0ecd20f2a7b7
Tweak TimelineIntervalSelector UI
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-October/008594.html
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Thu, 31 Oct 2013 14:44:53 -0400 |
parents | fa405a7ac9c5 |
children | 0f8feaacab68 |
files | client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelector.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorModel.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorUIBasic.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/Timeline.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorTest.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/timeline/TimelineTest.java |
diffstat | 6 files changed, 216 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelector.java Thu Oct 31 11:55:57 2013 -0400 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelector.java Thu Oct 31 14:44:53 2013 -0400 @@ -32,6 +32,8 @@ package com.redhat.thermostat.client.swing.components; +import java.awt.Paint; + import javax.swing.JComponent; import javax.swing.UIManager; @@ -44,6 +46,10 @@ private TimelineIntervalSelectorModel model; + private Paint linePaint; + + private Paint fillPaint; + public TimelineIntervalSelector() { model = new TimelineIntervalSelectorModel(); @@ -76,4 +82,19 @@ return uiClassID; } + public void setSelectionLinePaint(Paint paint) { + this.linePaint = paint; + } + + public Paint getSelectionLinePaint() { + return this.linePaint; + } + + public void setSelectionFillPaint(Paint paint) { + this.fillPaint = paint; + } + + public Paint getSelectionFillPaint() { + return this.fillPaint; + } }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorModel.java Thu Oct 31 11:55:57 2013 -0400 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorModel.java Thu Oct 31 14:44:53 2013 -0400 @@ -33,6 +33,7 @@ package com.redhat.thermostat.client.swing.components; import java.util.EventListener; +import java.util.concurrent.TimeUnit; import javax.swing.event.EventListenerList; @@ -44,17 +45,22 @@ private final EventListenerList listeners = new EventListenerList(); - private long totalMinimum = 0; - private long totalMaximum = 100; + private final long CREATION_TIME = System.currentTimeMillis(); - private long selectedMinimum = 0; - private long selectedMaximum = 10; + private long totalMinimum = CREATION_TIME; + private long totalMaximum = CREATION_TIME + TimeUnit.HOURS.toMillis(1); + + private long selectedMinimum = CREATION_TIME; + private long selectedMaximum = CREATION_TIME + TimeUnit.MINUTES.toMillis(10); public long getTotalMinimum() { return totalMinimum; } public void setTotalMinimum(long totalMinimum) { + setTotalMinimum(totalMinimum, true); + } + public void setTotalMinimum(long totalMinimum, boolean notify) { if (this.totalMinimum != totalMinimum) { this.totalMinimum = totalMinimum; @@ -70,7 +76,9 @@ this.selectedMinimum = this.totalMinimum; } - fireModelChanged(); + if (notify) { + fireModelChanged(); + } } } @@ -79,6 +87,10 @@ } public void setTotalMaximum(long totalMaximum) { + setTotalMaximum(totalMaximum, true); + } + + public void setTotalMaximum(long totalMaximum, boolean notify) { if (this.totalMaximum != totalMaximum) { this.totalMaximum = totalMaximum; @@ -94,7 +106,9 @@ this.selectedMinimum = this.totalMaximum; } - fireModelChanged(); + if (notify) { + fireModelChanged(); + } } } @@ -103,9 +117,15 @@ } public void setSelectedMinimum(long selectedMinimum) { + setSelectedMinimum(selectedMinimum, true); + } + + public void setSelectedMinimum(long selectedMinimum, boolean notify) { if(this.selectedMinimum != selectedMinimum) { this.selectedMinimum = selectedMinimum; - fireModelChanged(); + if (notify) { + fireModelChanged(); + } } } @@ -114,9 +134,15 @@ } public void setSelectedMaximum(long selectedMaximum) { + setSelectedMaximum(selectedMaximum, true); + } + + public void setSelectedMaximum(long selectedMaximum, boolean notify) { if (this.selectedMaximum != selectedMaximum) { this.selectedMaximum = selectedMaximum; - fireModelChanged(); + if (notify) { + fireModelChanged(); + } } }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorUIBasic.java Thu Oct 31 11:55:57 2013 -0400 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorUIBasic.java Thu Oct 31 14:44:53 2013 -0400 @@ -38,6 +38,9 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Paint; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import javax.swing.Box; import javax.swing.BoxLayout; @@ -77,6 +80,20 @@ timeline.setRange(new Range<>(model.getTotalMinimum(), model.getTotalMaximum())); } }; + private PropertyChangeListener enabledListener = new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (evt.getPropertyName().equals("enabled")) { + boolean enabled = (Boolean) evt.getNewValue(); + timeline.setEnabled(enabled); + if (enabled) { + addUserInputListeners(); + } else { + removeUserInputListeners(); + } + } + } + }; @Override public void installUI(JComponent c) { @@ -93,6 +110,8 @@ protected void installDefaults() { component.setLayout(new BoxLayout(component, BoxLayout.PAGE_AXIS)); component.setBorder(new EmptyBorder(5,5,5,5)); + + component.setSelectionLinePaint(Color.BLACK); } protected void installComponents() { @@ -109,9 +128,9 @@ component.getModel().addChangeListener(timelineSelectionPainter); component.getModel().addChangeListener(timelineRangeUpdater); - component.addMouseListener(mouseListener); - component.addMouseMotionListener(mouseListener); - component.addMouseWheelListener(mouseListener); + component.addPropertyChangeListener(enabledListener); + + addUserInputListeners(); } @Override @@ -133,18 +152,30 @@ } protected void uninstallListeners() { - component.removeMouseWheelListener(mouseListener); - component.removeMouseMotionListener(mouseListener); - component.removeMouseListener(mouseListener); + removeUserInputListeners(); component.getModel().removeChangeListener(timelineRangeUpdater); component.getModel().removeChangeListener(timelineSelectionPainter); } protected void uninstallDefaults() { + component.setSelectionLinePaint(null); + component.setLayout(null); } + private void removeUserInputListeners() { + component.removeMouseWheelListener(mouseListener); + component.removeMouseMotionListener(mouseListener); + component.removeMouseListener(mouseListener); + } + + private void addUserInputListeners() { + component.addMouseListener(mouseListener); + component.addMouseMotionListener(mouseListener); + component.addMouseWheelListener(mouseListener); + } + @Override public int getLeftSelectionPosition() { return domainToX(component.getModel().getSelectedMinimum()); @@ -205,8 +236,22 @@ int startX = domainToX(component.getModel().getSelectedMinimum()); int endX = domainToX(component.getModel().getSelectedMaximum()); - g2.fillRect(startX, 0, (endX - startX), getHeight()); + boolean enabled = component.isEnabled(); + if (enabled) { + g2.setPaint(component.getSelectionLinePaint()); + } else { + g2.setPaint(Color.LIGHT_GRAY); + } + // g2.fillRect(startX, 0, (endX - startX), getHeight()); + + g2.drawLine(0, getHeight(), 0, getHeight()/2); + g2.drawLine(0, getHeight()/2, startX, getHeight()/2); + g2.drawLine(startX, getHeight()/2, startX, 0); + + g2.drawLine(getWidth()-1, getHeight(), getWidth()-1, getHeight()/2); + g2.drawLine(getWidth()-1, getHeight()/2, endX, getHeight()/2); + g2.drawLine(endX, getHeight()/2, endX, 0); g2.dispose(); } }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/Timeline.java Thu Oct 31 11:55:57 2013 -0400 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/timeline/Timeline.java Thu Oct 31 14:44:53 2013 -0400 @@ -134,7 +134,11 @@ drawTicks(graphics, bounds, timeUnitForTicks); - graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + if (isEnabled()) { + graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + } else { + graphics.setColor(Color.GRAY); + } graphics.drawLine(bounds.x, bounds.height - 1, bounds.width, bounds.height - 1); graphics.dispose(); @@ -211,11 +215,15 @@ for (long i = start; i < end; i += deltaInMilliseconds) { int x = (int) normalizer.getValueNormalized(i); - graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + if (isEnabled()) { + graphics.setColor(Palette.THERMOSTAT_BLU.getColor()); + } else { + graphics.setColor(Color.GRAY); + } + graphics.drawLine(x, 0, x, bounds.height); graphics.setPaint(gradient); - String value = df.format(new Date(i)); int stringWidth = (int) font.getStringBounds(value, @@ -225,7 +233,12 @@ graphics.fillRect(x + 1, bounds.y + 5, stringWidth + 4, stringHeight + 4); - graphics.setColor(Color.BLACK /* Palette.THERMOSTAT_BLU.getColor() */); + if (isEnabled()) { + graphics.setColor(Color.BLACK /* Palette.THERMOSTAT_BLU.getColor() */); + } else { + graphics.setColor(Color.GRAY); + } + graphics.drawString(value, x + 1, bounds.y + stringHeight + 5); } }
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorTest.java Thu Oct 31 11:55:57 2013 -0400 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/TimelineIntervalSelectorTest.java Thu Oct 31 14:44:53 2013 -0400 @@ -34,9 +34,12 @@ import java.awt.BorderLayout; import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.concurrent.TimeUnit; import javax.swing.BorderFactory; +import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; @@ -52,7 +55,6 @@ JFrame mainWindow = new JFrame(); final TimelineIntervalSelector intervalSelector = new TimelineIntervalSelector(); - long now = System.currentTimeMillis(); intervalSelector.getModel().addChangeListener(new ChangeListener() { @@ -73,6 +75,16 @@ intervalSelector.getModel().setSelectedMinimum(now); intervalSelector.getModel().setSelectedMaximum(now + TimeUnit.MINUTES.toMillis(10)); + final JCheckBox enable = new JCheckBox("Enabled"); + enable.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + intervalSelector.setEnabled(enable.isSelected()); + } + }); + enable.setSelected(true); + + mainWindow.add(enable, BorderLayout.NORTH); mainWindow.add(intervalSelector, BorderLayout.CENTER); mainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/timeline/TimelineTest.java Thu Oct 31 14:44:53 2013 -0400 @@ -0,0 +1,79 @@ +/* + * 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 + * <http://www.gnu.org/licenses/>. + * + * 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.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.concurrent.TimeUnit; + +import javax.swing.JCheckBox; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; + +import com.redhat.thermostat.common.model.Range; + +public class TimelineTest { + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + JFrame mainWindow = new JFrame(); + + long now = System.currentTimeMillis(); + final Timeline timeline = new Timeline(new Range<Long>(now, now + TimeUnit.MINUTES.toMillis(10))); + + final JCheckBox enable = new JCheckBox("Enabled"); + enable.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + timeline.setEnabled(enable.isSelected()); + } + }); + enable.setSelected(true); + + mainWindow.add(enable, BorderLayout.NORTH); + mainWindow.add(timeline, BorderLayout.CENTER); + mainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + mainWindow.setVisible(true); + + } + }); + } +}